15 Replies  982 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
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 realtime). 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!
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
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
#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
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
If using a data table the data will be hardcoded.
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
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
#8
Re: Using data tables in place of trajectory equations
Posted 10 January 2013  02:00 PM
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((alphaALPHA0)*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
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
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((alphaALPHA0)*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>=x10.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
#12
Re: Using data tables in place of trajectory equations
Posted 12 January 2013  07:00 PM
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>=x10.025&&x<=x1+0.025))
to this
if((y >= 2.74&&y<=2.75)&&(x>=x10.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
#14
Re: Using data tables in place of trajectory equations
Posted 13 January 2013  05:38 AM
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
