NYC
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« on: February 14, 2012, 04:41:34 pm » |
I'm breaking up some messy code into separate functions, and can't seem to figure out how to pass two parameters into a function, and get two back. The passing part is easy - I can just call get_angles(somenumber, somenumber), but how do I make theta1 and theta2 available within the loop() or wherever I call the function? If anyone has insight or a good link to a reference that would be great! Thanks Oh, and in case you're interested, this function is supposed to pass an x,y position and return two joint angles, so I can control this: http://youtu.be/IHGXqud2m9Qdouble get_angles(double Px, double Py) { // first find theta2 where c2 = cos(theta2) and s2 = sin(theta2) double c2 = (pow(Px,2) + pow(Py,2) - pow(a1,2) - pow(a2,2))/(2*a1*a2); // is btwn -1 and 1 double s2 = sqrt(1 - pow(c2,2)); double theta2 = degrees(atan2(s2,c2)); // solves for the angle in degrees and places in correct quadrant // now find theta1 where c1 = cos(theta1) and s1 = sin(theta1) double theta1 = degrees(atan2(-a2*s2*Px + (a1 + a2*c2)*Py, (a1 + a2*c2)*Px + a2*s2*Py)); //return?????? }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19056
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: February 14, 2012, 04:44:33 pm » |
You could pass by reference, and not return anything. Or you could return a struct. Watch out for the function prototypes.
|
|
|
|
« Last Edit: February 14, 2012, 04:49:32 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 277
Posts: 25500
Solder is electric glue
|
 |
« Reply #2 on: February 14, 2012, 04:46:41 pm » |
and get two back. You can't get two values back from a function, that is not the way C is built. The closest you can get is to return a pointer to an array, or let it operate on global variables. Alternatively you can pass an array pointer in and get the function to modify what it points to.
|
|
|
|
|
Logged
|
|
|
|
|
NYC
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #3 on: February 14, 2012, 05:08:58 pm » |
return a pointer to an array - how? You could pass by reference, and not return anything - how? Or you could return a struct - how? Watch out for the function prototypes - what do you mean?
or let it operate on global variables - okay so if I declare theta1 and theta2 as global, and the get_angles function does something to them, the new values of theta1 and theta2 will be available anywhere, correct?
thanks!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19056
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: February 14, 2012, 05:10:52 pm » |
Just make sure you don't return a pointer to a local automatic variable.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
God Member
Karma: 0
Posts: 596
Arduino rocks
|
 |
« Reply #5 on: February 14, 2012, 05:11:06 pm » |
Something along these lines: struct angles { double a; double b; };
struct angles f(double x, double y) { struct angles ang;
ang.a = .... ang.b = ....
return ang; }
void loop() { struct angles theAngles;
x = ... y = ...
theAngles = f(x, y); }
// inputs: x, y // outputs: a, b void f(double x, double y, double* a, double* b) { *a = .... *b = ....
return; // no return value }
void loop() { double theta1; double theta2;
x = ... y = ...
f(x, y, &theta1, &theta2); }
|
|
|
|
|
Logged
|
|
|
|
|
|
|
NYC
Offline
Newbie
Karma: 0
Posts: 24
|
 |
« Reply #7 on: February 14, 2012, 05:21:00 pm » |
thanks! will work in implementing that now
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15316
Measurement changes behavior
|
 |
« Reply #8 on: February 14, 2012, 05:31:24 pm » |
While frowned upon by the grey beards, I would just tend to use global variables is such situations. As my sketches tend to be on the small size, and I'm the only one writing and using the code, I don't have too much difficulty tracking of what functions will have access to changing such variables and have not had a bug yet where the reason was the use of global variables Vs if I had used local scope variables and passed the return values.
Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Pittsburgh, PA, USA
Offline
Faraday Member
Karma: 30
Posts: 2923
I only know some basic electricity....
|
 |
« Reply #9 on: February 14, 2012, 07:15:46 pm » |
Well my beard is white and I use globals. For one thing, they stay allocated.
|
|
|
|
|
Logged
|
Examples can be found at Learning in the Main Site and at the Playground
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15316
Measurement changes behavior
|
 |
« Reply #10 on: February 14, 2012, 07:27:00 pm » |
Well my beard is white and I use globals. For one thing, they stay allocated.
Well then both you and Santa Claus have white beards and therefore friendly by definition. While grey beards are always rather stern and abrupt with us less experienced 'Arduino programmers'. 
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Online
Brattain Member
Karma: 277
Posts: 25500
Solder is electric glue
|
 |
« Reply #11 on: February 14, 2012, 10:39:45 pm » |
Well I am with you on the use of global variables, so what colour does it make my beard?
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15316
Measurement changes behavior
|
 |
« Reply #12 on: February 14, 2012, 10:53:16 pm » |
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #13 on: February 15, 2012, 03:33:22 am » |
You could pass by reference, and not return anything - how?
That is probably the simplest. double a1 = 1, a2 = 2;
void get_angles(const double Px, const double Py, double & theta1, double & theta2) { // first find theta2 where c2 = cos(theta2) and s2 = sin(theta2) double c2 = (pow(Px,2) + pow(Py,2) - pow(a1,2) - pow(a2,2))/(2*a1*a2); // is btwn -1 and 1 double s2 = sqrt(1 - pow(c2,2)); theta2 = degrees(atan2(s2,c2)); // solves for the angle in degrees and places in correct quadrant
// now find theta1 where c1 = cos(theta1) and s1 = sin(theta1) theta1 = degrees(atan2(-a2*s2*Px + (a1 + a2*c2)*Py, (a1 + a2*c2)*Px + a2*s2*Py)); } // end of get_angles
void setup () { double t1, t2; get_angles (20, 30, t1, t2); }
void loop () {} I noticed when I was doing this that you have a1 and a2 as variables which presumably are global. So you need to make up your mind if the function is going to be a "black box" (and thus pass everything to it). If not, passing some stuff and not others seems to be hedging your bets a bit. In my example get_angles returns its results by reference (the last two arguments) so it needs "return" nothing.
|
|
|
|
|
Logged
|
|
|
|
|
|