Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Storing functions to variables (http://www.chiefdelphi.com/forums/showthread.php?t=53432)

Tz0m 04-02-2007 14:21

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.
Quote:

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.

Bongle 04-02-2007 14:45

Re: Storing functions to variables
 
If I read this correctly, you're trying to make a function that returns a distance.

Code:


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

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

Tz0m 04-02-2007 15:14

Re: Storing functions to variables
 
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.
Quote:

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.

Astronouth7303 04-02-2007 19:07

Re: Storing functions to variables
 
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:
Code:

(type omitted) myfunc = tan;
So you need to declare all three variables, like so:
Code:

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.

Tz0m 09-02-2007 16:06

Re: Storing functions to variables
 
Thanks for the help, but nothing has worked so far. We still have the same problem.

Kevin Sevcik 09-02-2007 22:25

Re: Storing functions to variables
 
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.

Tz0m 10-02-2007 14:07

Re: Storing functions to variables
 
3 Attachment(s)
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.

Salik Syed 11-02-2007 17:14

Re: Storing functions to variables
 
when you call the function simply put TILT_SERVO in the parameter

i.e :
Code:

double range

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

or:
printf("The Distance is: %lf inches\n", 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
Code:

tiltD = ((tiltS - 124) * 65) / 124;
you should change it to this:
Code:

tiltD = ((tiltS - 124) * 65) / 124.0;
Just to clarify that you do not in any case want to do integer division

Tz0m 18-02-2007 11:36

Re: Storing functions to variables
 
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.


All times are GMT -5. The time now is 04:12.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi