Storing functions to variables

I am trying to set a variable, type double, to a function that calculates distance. This is the code I have.

dist = 80.0/tan(tilt_angR);

If I use a printf statement to calculate and display “dist,” the value caclulates and prints properly. However, I need to set this value to a variable. Any help is appreciated.

If I read this correctly, you’re trying to make a function that returns a distance.



double dist(double tilt_angR)
{
  return 80.0/tan(tilt_angR);
}


That code will do what I think you want to do.

Thanks for the help, but I have another problem. The variable tilt_angR is set to another function, and I am having the same problem as before. Here is my code.

tilt_angD = ((((int)TILT_SERVO - 124) * 65)/124));
tilt_angR = ((tilt_angD *3.14159265)/180.0);
dist = (80.0/tan(tilt_angR));

Thanks for the help.

Remember that in C, those aren’t functions or equations. They are statements, executed once.

To assign a function to a variable would be something like:


(type omitted) myfunc = tan;

So you need to declare all three variables, like so:


int tilt_angD = ((((int)TILT_SERVO - 124) * 65)/124));
double tilt_angR = ((tilt_angD *3.14159265)/180.0);
double dist = (80.0/tan(tilt_angR));

Just watch out for integer math.

Just be careful about your floating-point expressions, the RC doesn’t like them much.

Thanks for the help, but nothing has worked so far. We still have the same problem.

I think we’re having difficulties divining what you’re attempting. If you could post some larger chunks of your code, such as where you’re calling these statements and an example of the code where the printfs are working. Don’t be shy about cutting and pasting a vast majority of your user_routines.c or zipping up some of your source files and attaching them to a post.

Ok, I have everything setup properly now, I think. The only thing I need to do is assign the variable ‘TILT_SERVO’ to the variable I pass into my function, ‘tiltS’. I am not sure where to declare my variable and how or where to set its value to ‘TILT_SERVO’. I have attached the three files I have edited for my code.

calcs.c (788 Bytes)
calcs.h (421 Bytes)
user_routines.c (17.6 KB)


calcs.c (788 Bytes)
calcs.h (421 Bytes)
user_routines.c (17.6 KB)

when you call the function simply put TILT_SERVO in the parameter

i.e :


double range

void someFunction ( blah blah)
{
    range = DistanceCalc(TILT_SERVO);
}

or:
printf("The Distance is: %lf inches
", DistanceCalc(TILT_SERVO));

if you want to assign your variable the value TILT_SERVO
just declare it (outside of the main loop, towards the beginning of the file under the #defines)
like this: double tiltS;
then insidde your main loop put this code:
tiltS = TILT_SERVO;
this is probably redundant and un-necessary since you already have TILT_SERVO holding the value of the camera tilt

also you may want to be wary of this line


tiltD = ((tiltS - 124) * 65) / 124;

you should change it to this:


tiltD = ((tiltS - 124) * 65) / 124.0;

Just to clarify that you do not in any case want to do integer division

I do not want to do integer division, but I have to. The robot controller does not support floats or doubles. I have the function printing an int and dividing it by a number in order to get a proper answer in inches. If I use a float and type cast it to an int and divide by 100, the function prints a blank space.