# passing variables to and from functions..

ok, this is probably really simple, but i’m pretty new to this, and i’m not super comfortable with local vs global variables and pointers. i’m trying to use these functions that were published here: Trossen Robotics Community Database Error

i’m just using the inverse kinematics portion:

``````// inverse kinematics
// helper functions, calculates angle theta1 (for YZ-pane)
int delta_calcAngleYZ(float x0, float y0, float z0, float &theta) {
float y1 = -0.5 * 0.57735 * f; // f/2 * tg 30
y0 -= 0.5 * 0.57735    * e;    // shift center to edge
// z = a + b*y
float a = (x0*x0 + y0*y0 + z0*z0 +rf*rf - re*re - y1*y1)/(2*z0);
float b = (y1-y0)/z0;
// discriminant
float d = -(a+b*y1)*(a+b*y1)+rf*(b*b*rf+rf);
if (d < 0) return -1; // non-existing point
float yj = (y1 - a*b - sqrt(d))/(b*b + 1); // choosing outer point
float zj = a + b*yj;
theta = 180.0*atan(-zj/(y1 - yj))/pi + ((yj>y1)?180.0:0.0);
return 0;
}

// inverse kinematics: (x0, y0, z0) -> (theta1, theta2, theta3)
// returned status: 0=OK, -1=non-existing position
int delta_calcInverse(float x0, float y0, float z0, float &theta1, float &theta2, float &theta3) {
theta1 = theta2 = theta3 = 0;
int status = delta_calcAngleYZ(x0, y0, z0, theta1);
if (status == 0) status = delta_calcAngleYZ(x0*cos120 + y0*sin120, y0*cos120-x0*sin120, z0, theta2);  // rotate coords to +120 deg
if (status == 0) status = delta_calcAngleYZ(x0*cos120 - y0*sin120, y0*cos120+x0*sin120, z0, theta3);  // rotate coords to -120 deg
return status;
}
``````

I want to send xyz values, and get back three theta variables. here’s what I tried (but didn’t work):

``````if (delta_calcInverse(vector[0],vector[1],vector[2],Dvector[0],Dvector[1],Dvector[2])){}
else return;
``````

I was thinking that it would update the values of Dvector in the function, then i could use them in other functions, but they stay zero.

any help would be much appreciated! thanks.

Nevermind! Apparently I did it right, but was passing numbers that don't work.. a little confusing, but at least i'm getting the function to work.