|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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:
|
|
#2
|
||||
|
||||
|
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);
}
|
|
#3
|
|||
|
|||
|
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:
|
|
#4
|
|||||
|
|||||
|
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; 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 be careful about your floating-point expressions, the RC doesn't like them much. |
|
#5
|
|||
|
|||
|
Re: Storing functions to variables
Thanks for the help, but nothing has worked so far. We still have the same problem.
|
|
#6
|
|||||
|
|||||
|
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.
|
|
#7
|
|||
|
|||
|
Re: Storing functions to variables
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.
|
|
#8
|
|||
|
|||
|
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));
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; Code:
tiltD = ((tiltS - 124) * 65) / 124.0; Last edited by Salik Syed : 11-02-2007 at 17:18. |
|
#9
|
|||
|
|||
|
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.
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Storing the balls | Max Brin | Technical Discussion | 4 | 18-02-2006 20:34 |
| Math.h and Functions and Variables | amateurrobotguy | Programming | 1 | 26-02-2005 03:19 |
| PDF of all functions/variables! | mikesown | Programming | 1 | 01-02-2005 18:56 |
| Storing Tetras on Robot | MisterX | General Forum | 22 | 15-01-2005 19:26 |
| SCORing vs. STORing | Chris Fultz | General Forum | 3 | 15-01-2004 13:36 |