Hello,

I'm the lead programmer on a FIRST robotics team, and this is our first year using C++. I have some experience, but I don't exactly know where to begin in this particular area. For the competition, we have to launch Frisbees through a target mounted approximately 9 feet above the ground. Based on what I've researched, the equations for determining the trajectory of a Frisbee are sketchy at best, not to mention the fact that they could strike terror into even the best programmer. Because of this, I would like to make a data table with the distance from the wall and the angle to hit the target instead of using the formulas. Everything except the angle of the launcher will be constant. Basically, we'll park our launcher x" from the wall and manually figure out the angle to hit the target, then record it in our "data table" (I'm not completely sure if this is the term I'm trying to convey). What I think I would have to do is create a matrix with the distance (in inches) on top and the angle (in degrees) on the bottom. Probably 2x30 or so. This is my first problem. If using a matrix actually sounds like a good method of storing this information, I would like to learn how to make one in C++. If not, could someone suggest another method (I can't store data in a text file because we're communicating with a cRIO)? After that, I need it to print the corresponding angle when given the distance. Ultimately, we'll be acquiring the distance with an ultrasonic rangefinder, and measuring the angle with a gyroscope (a really accurate one), so that the robot can automatically change the angle of the launcher based on the closest integer to the range finder's output value (in real-time). I just need to know how to store and link two corresponding data sets. I already know how to program all the hardware (for the most part). Let it be known that I'm 15 and in no way an expert at programming. I'm not trying to "cheat" at anything, so please don't tear me apart like S**ckOverfl*w did. I know this seems like a lot of information, but It's the only way I know of how to describe my situation. (By the way, this is all just optional for the robot, but I really want to become good at programming, and this is something I would really love to learn how to do). I would really appreciate any help I can get. Thanks!

## 15 Replies - 1437 Views - Last Post: 15 January 2013 - 04:40 PM

### #1

# Using data tables in place of trajectory equations

Posted 09 January 2013 - 07:16 PM

##
**Replies To:** Using data tables in place of trajectory equations

### #2

## Re: Using data tables in place of trajectory equations

Posted 09 January 2013 - 08:47 PM

You could use a two-dimensional array.

The inputs are the distance measured and the angle measured. The output is the angle of the launcher.

The output could be an integer or a real number (here a

Multidimensional arrays

The inputs are the distance measured and the angle measured. The output is the angle of the launcher.

The output could be an integer or a real number (here a

*float*).#define DISTANCE 24 #define ANGLE 30 float data[DISTANCE][ANGLE];

Multidimensional arrays

### #3

## Re: Using data tables in place of trajectory equations

Posted 10 January 2013 - 08:31 AM

I'm still a little confused on how to put information into and get information out of the array... Because the data will be manually collected and estimated, it won't exactly be linear, so I can't use for(;;){} statements to place the data. Once I get the aray created, how would it to return JUST the corresponding number? At this point, I would like to make a small 2x4 matrix with cin>> and cout<< (or some other equivalent) just for study. Based on what I read in the link, my code would vaguely resemble this:

I've been programming in java lately, so some of the syntax could be confused. Thank you for all you help!

#include <iostream> #define WIDTH 4 int data[2][WIDTH]; int main () { float info; cin>>info; //some method of making data for the array //some method of printing only data from the correspondent of "info" printarray ()//I have no idea what I'm doing at this point cout<< //The corresponding number return 0; }

I've been programming in java lately, so some of the syntax could be confused. Thank you for all you help!

### #4

## Re: Using data tables in place of trajectory equations

Posted 10 January 2013 - 10:49 AM

I dont know why you just dont use the equations for trajectory.

link

all you need to know are the angle of fire, the machines (robots) propulsion capability ie v

and the height from the ground the frisbee will be fired from then you dont have to worry about tables.

Snoopy

link

#include<cmath> #include<iostream> int main() { float distance; // distance the frisbee will travel float g, v, theta ,height; g= 9.81f;// gravitational constant v = 15.0f;// initial velocity im mtrs/sec theta = 45.0f;// initial angle of fire in degrees height = 0.0f;// initial height from ground in metres distance = (v*cos(theta))*((v*sin(theta))+sqrt((pow(v*sin(theta),2))+(2*g*height)))/g; //distance = (pow(v,2)*sin(2*theta))/g; //without initial height the equation reduces to this// //you can check both are equivalent by setting height to 0.0 and removing the '//' std::cout << distance; std::cin.ignore(); std::cin.get(); return 0; }

all you need to know are the angle of fire, the machines (robots) propulsion capability ie v

and the height from the ground the frisbee will be fired from then you dont have to worry about tables.

Snoopy

### #5

## Re: Using data tables in place of trajectory equations

Posted 10 January 2013 - 11:50 AM

Frisbees have quite a lot of lift, so a trajectory formula with a reduced g might work. Collecting some data and drawing a graph may help.

If using a data table the data will be hard-coded.

The data could be added at initialization

The array could be initialised in a function, but the indexes need to be specified :

To display the data a function can be created.

If using a data table the data will be hard-coded.

The data could be added at initialization

#define WIDTH 4 int data[2][WIDTH] { {10, 20, 30, 40}, {50, 60, 70, 80} };

The array could be initialised in a function, but the indexes need to be specified :

data[0][0] = 10; data[0][1] = 20; data[0][2] = 30; ... data[1][3] = 80;

To display the data a function can be created.

void print_data(int data_array[][WIDTH]) { int i, j; for(int i; i<ROWS; i++) { for(int j; j<WIDTH; j++) { printf("%d ", data_array[i][j]); } printf("\n"); } } int main() { ... print_data(data); }

### #6

## Re: Using data tables in place of trajectory equations

Posted 10 January 2013 - 12:19 PM

So, something like:

Then just have at. Multiple trials for the same configuration would be ideal; you can take an average from that. Just repeat and record until bored, or until the data you've collected can accurately predict the outcome.

struct DataPoint { float angle float distance; float heightOfImpact; float driftOffCenter; }; typedef std::vector<DataPoint> Data;

Then just have at. Multiple trials for the same configuration would be ideal; you can take an average from that. Just repeat and record until bored, or until the data you've collected can accurately predict the outcome.

### #7

## Re: Using data tables in place of trajectory equations

Posted 10 January 2013 - 12:55 PM

The problem with the typical trajectory equations is that Frisbees don't follow that model very well. The equation for Frisbee trajectory is modeled by a complicated set of equations, dissimilar to that of a ball, which incorporates "unmeasurable" things such as rotational deceleration, drag coefficient, and axial lift among other things. The equations can be found at http://biosport.ucda...DUM_5thMACS.pdf .There's a few pictures in this PDF: web.mit.edu/womens-ult/www/smite/frisbee_physics.pdf which demonstrate the flight path of a Frisbee when the angle is manipulated. I couldn't attach the PDFs because I kept getting upload errors (probably my school router). After reading these and doing additional research, I figured it would be easier to make a data table. If you can make sense of the equations, I would love to hear about it. Thanks!

### #8

## Re: Using data tables in place of trajectory equations

Posted 10 January 2013 - 02:00 PM

Yeah, I found out a lot about frisbees that I didnt know about,

My simplistic approach wont work.... however ...

with a little research and a lot of math conversions of Euler's equations according to Hummel

I put this together.

and it has a good chance of working...

You can test it out and see if it works but I strongly believe it will work

to negligible errors in distance.

Regards

Snoopy.

My simplistic approach wont work.... however ...

with a little research and a lot of math conversions of Euler's equations according to Hummel

I put this together.

and it has a good chance of working...

#include<cmath> #include<iostream> int main() { double alpha =45.0; // alpha is the angle of fire of the frisbee double deltaT = 0.001; //deltaT time steps for Euler's algorithmn in seconds double x; //The x position of the frisbee. double y; //The y position of the frisbee. double vx; //The x velocity of the frisbee. double vy; //The y velocity of the frisbee. double g = -9.81; //The acceleration of gravity (m/s^2). double m = 0.175; //The mass of a standard frisbee in kilograms. double RHO = 1.23; //The density of air in kg/m^3. double AREA = 0.0568; //The area of a standard frisbee in metres double CL0 = 0.1; //The lift coefficient at alpha = 0. of average frisbee double CLA = 1.4; //The lift coefficient dependent on alpha of average frisbee double CD0 = 0.08; //The drag coefficent at alpha = 0 of average frisbee double CDA = 2.72; //The drag coefficient dependent on alpha. double ALPHA0 = -4; //Calculation of the lift coefficient using the relationship given //by S. A. Hummel. double cl = CL0 + CLA*alpha*3.14159/180; //Calculation of the drag coefficient (for Prantl’s relationship) //using the relationship given by S. A. Hummel. double cd = CD0 + CDA*pow((alpha-ALPHA0)*3.14159/180,2); //Initial position x = 0. x = 0; //Initial position y = 1 thrown from a height of 1 metre y = 1; //Initial x velocity vx = 14 metres per second. vx = 14; //Initial y velocity vy = 0. vy = 0; //A loop index to monitor the simulation steps. int k = 0; //A while loop that performs iterations until the y position //reaches zero (i.e. the frisbee hits the ground). while(y>0.0){ //The change in velocity in the y direction obtained setting the //net force equal to the sum of the gravitational force and the //lift force and solving for delta v. double deltavy = (RHO*pow(vx,2)*AREA*cl/2/m+g)*deltaT; //The change in velocity in the x direction, obtained by //solving the force equation for delta v. (The only force //present is the drag force). double deltavx = -RHO*pow(vx,2)*AREA*cd*deltaT; //The new positions and velocities are calculated using //simple introductory mechanics. vx = vx + deltavx; vy = vy + deltavy; x = x + vx*deltaT; y = y + vy*deltaT; //Only the output from every tenth iteration will be sent //to the console so as to decrease the number of data points. if(k%10 == 0) { std::cout<<x << "," << y << "," << vx<<","<<vy<<std::endl; } k++; } std::cin.ignore(); std::cin.get(); return 0; }

You can test it out and see if it works but I strongly believe it will work

to negligible errors in distance.

Regards

Snoopy.

### #9

## Re: Using data tables in place of trajectory equations

Posted 11 January 2013 - 02:08 PM

Snoopy, thanks for translating that! I found the java code for Euler's equation, but when I brought it up on St**ckOverfl*w nobody appreciated it, so I was refraining from posting or mentioning it. It looks pretty good, but I need take the distance as the input and return only the angle as the output, plus I have to solve for the maximum height of 9', which I myself am having a little trouble comprehending. Basically, the launcher will be between 5' and 50' from the wall (with the maximum height of the launcher at a height of 0' off the ground, which I would eventually change depending on the height of the robot, somewhere between 6" and 30". It only has to be accurate to within a foot or two), which has a slot in it that is 9' off of the ground. I'm currently in the process of collaborating with a mathematician to figure that part out, so unless someone just happens to know how to do that (without putting in any effort), I can probably figure that part out. I really liked a lot of the suggestions I was given, and I intend to try out as many as possible, if not all of them. One that I'm looking forward to testing is the typical ball-trajectory equation with reduced gravity (which would require a bunch of testing to make a best-fit equation). At this point, I would still really like to learn how to use multidimensional arrays. I've sort-of decided that I'm going to get this figured out, even if I can't think of an application. I'm not exactly sure if it can be done, but if I could figure out the "location" of a digit in one array, I could print the corresponding digit in another array. This was a suggestion from the mathematician... The two different arrays would look something like this:

I know that a particular character can be singled out in a string based on it's "location", but can it be done with an array? Basically, if the input is the second number in the first array, could I get it to return the second number in the second array? I don't know if this makes a ton of sense, so please let me know if it doesn't. Thank you all for all the help so far!

int firstarray[] = {1, 2, 3, 4, 5}; //not random int secondarray[] = {2, 5, 6, 8, 13}; //random

I know that a particular character can be singled out in a string based on it's "location", but can it be done with an array? Basically, if the input is the second number in the first array, could I get it to return the second number in the second array? I don't know if this makes a ton of sense, so please let me know if it doesn't. Thank you all for all the help so far!

### #10

## Re: Using data tables in place of trajectory equations

Posted 11 January 2013 - 03:48 PM

arrays go in rows and columns

int array[100][100];

for each in array there will be array[rows][columns]

there will be array[0][0]

array[0][1]

array[0][2]

array[0][3]

..........

up to

array[99][99]

so you would access 1 element at array[0][0]

and 1 element at array[0][1]

etc

if you need the height to terminate at y = 2.7432 metres

at distance x = ??

it should be possible to rearrange the equations to do that and output the required angle

given that you know what distance x is and you know what your initial velocity of the robot

frisbee firing device will be.

so do you know what speed the robot will fire the frisbee at ?

try this program it might be what you need

its hard coded to land roughly at 9 feet high with an average initial velocity

of 14 metres/s.

Snoopy.

int array[100][100];

for each in array there will be array[rows][columns]

there will be array[0][0]

array[0][1]

array[0][2]

array[0][3]

..........

up to

array[99][99]

so you would access 1 element at array[0][0]

and 1 element at array[0][1]

etc

if you need the height to terminate at y = 2.7432 metres

at distance x = ??

it should be possible to rearrange the equations to do that and output the required angle

given that you know what distance x is and you know what your initial velocity of the robot

frisbee firing device will be.

so do you know what speed the robot will fire the frisbee at ?

try this program it might be what you need

#include<cmath> #include<iostream> int main() { double x1=0.0; double y1=0.0; int flag =0; std::cout << "Enter distance from target in metres "; std::cin >> x1; std::cout << "Enter height of robot above ground in metres "; std::cin >> y1; double alpha =0.0; // alpha is the angle of fire of the frisbee double deltaT = 0.0001; //deltaT time steps for Euler's algorithmn in seconds double x; //The x position of the frisbee. double y; //The y position of the frisbee. double vx; //The x velocity of the frisbee. double vy; //The y velocity of the frisbee. double g = -9.81; //The acceleration of gravity (m/s^2). double m = 0.175; //The mass of a standard frisbee in kilograms. double RHO = 1.23; //The density of air in kg/m^3. double AREA = 0.0568; //The area of a standard frisbee in metres double CL0 = 0.1; //The lift coefficient at alpha = 0. of average frisbee double CLA = 1.4; //The lift coefficient dependent on alpha of average frisbee double CD0 = 0.08; //The drag coefficent at alpha = 0 of average frisbee double CDA = 2.72; //The drag coefficient dependent on alpha. double ALPHA0 = -4; for(alpha=0.0; alpha <60.0; alpha=alpha+0.05) { //Calculation of the lift coefficient using the relationship given //by S. A. Hummel. double cl = CL0 + CLA*alpha*3.14159/180; //Calculation of the drag coefficient (for Prantl’s relationship) //using the relationship given by S. A. Hummel. double cd = CD0 + CDA*pow((alpha-ALPHA0)*3.14159/180,2); //Initial position x = 0. x = 0; //Initial position y = 1 thrown from a height of 1 metre y = y1; //Initial x velocity vx = 14 metres per second. vx = 14; //Initial y velocity vy = 0. vy = 0; //A loop index to monitor the simulation steps. int k = 0; //A while loop that performs iterations until the y position //reaches zero (i.e. the frisbee hits the ground). while(y>0.0){ //The change in velocity in the y direction obtained setting the //net force equal to the sum of the gravitational force and the //lift force and solving for delta v. double deltavy = (RHO*pow(vx,2)*AREA*cl/2/m+g)*deltaT; //The change in velocity in the x direction, obtained by //solving the force equation for delta v. (The only force //present is the drag force). double deltavx = -RHO*pow(vx,2)*AREA*cd*deltaT; //The new positions and velocities are calculated using //simple introductory mechanics. vx = vx + deltavx; vy = vy + deltavy; x = x + vx*deltaT; y = y + vy*deltaT; if((y >= 2.72&&y<=2.76)&&(x>=x1-0.025&&x<=x1+0.025)) {flag = 1; break;} } if(flag==1) {break;} } if(flag==1) std::cout <<"The Optimal angle is " << alpha <<" Degrees, landing with a height of "<<y<<" metres."; else std::cout <<"Failed"; std::cin.ignore(); std::cin.get(); return 0; }

its hard coded to land roughly at 9 feet high with an average initial velocity

of 14 metres/s.

Snoopy.

This post has been edited by **snoopy11**: 11 January 2013 - 05:26 PM

### #11

## Re: Using data tables in place of trajectory equations

Posted 12 January 2013 - 05:17 PM

Snoopy, I can't even tell you how much I appreciate that! Thank you so much! I can't wait to test it out with our robot! I'll write back with my results after we get the launcher built. Thanks!

### #12

## Re: Using data tables in place of trajectory equations

Posted 12 January 2013 - 07:00 PM

Thats ok,

I understand your not really a programmer and more a robotics type guy

you may have to adjust this bit

if((y >= 2.72&&y<=2.76)&&(x>=x1-0.025&&x<=x1+0.025))

to this

if((y >= 2.74&&y<=2.75)&&(x>=x1-0.025&&x<=x1+0.025))

for more accurate results

and adjust vx = 14 to whatever speed your robot fires a frisbee at.

Regards

Snoopy.

I understand your not really a programmer and more a robotics type guy

you may have to adjust this bit

if((y >= 2.72&&y<=2.76)&&(x>=x1-0.025&&x<=x1+0.025))

to this

if((y >= 2.74&&y<=2.75)&&(x>=x1-0.025&&x<=x1+0.025))

for more accurate results

and adjust vx = 14 to whatever speed your robot fires a frisbee at.

Regards

Snoopy.

This post has been edited by **snoopy11**: 12 January 2013 - 07:11 PM

### #13

## Re: Using data tables in place of trajectory equations

Posted 12 January 2013 - 09:04 PM

Thanks! I would very much like to consider myself a programmer, and you've probably been programming for a few more years than me. I'm pretty sure you're not 15 (such as myself) :P It's totally fine. I really appreciate the help!

### #14

## Re: Using data tables in place of trajectory equations

Posted 13 January 2013 - 05:38 AM

Oh ok my mistake,

I am older twice your age but I have

only been programming in C and C++

for the last three years.

If I had known I would have given you less

help :-P

Snoopy.

I am older twice your age but I have

only been programming in C and C++

for the last three years.

If I had known I would have given you less

help :-P

Snoopy.

This post has been edited by **snoopy11**: 13 January 2013 - 05:39 AM

### #15

## Re: Using data tables in place of trajectory equations

Posted 15 January 2013 - 03:17 PM

I was looking through this code, and noticed that the variable "k" has no purpose at all. Any reason for that?