Math.h sin syntax error

Im trying to use the math.h sin function but i keep getting a syntax error. Im trying to use trig to figure out how far away our bot is from the target light. Heres what’s not working:

int targheight;
float dist;
targheight = 3;
dist = ((int)targheight * float sin(((3.14159 / 2) - ((((((int)TILT_SERVO - 190) * (65 / 124)) * (3.14159 / 180)))))) / float sin(float((((((int)TILT_SERVO - 190) * (65 / 124)) * (3.14159 / 180)));

What line is geting the syntax error, and what is the error(if your compiler gives you some knid of error report.)

You seem to be missing some closing parentheses for those bolded open parentheses. Try using a text editor that has parentheses highlighting like notepad2. Also it might be easier to read if you split it up over multiple lines (it’s not really more efficient code if you write it all on one line.)


dist = **(** (int)targheight * float sin(((3.14159 / 2) - ((((((int)TILT_SERVO - 190) * (65 / 124)) * (3.14159 / 180)))))) / float sin **(** float **(** (((((int)TILT_SERVO - 190) * (65 / 124)) * (3.14159 / 180)));

The line with the error is:
dist = ( (int)targheight * float sin(((3.14159 / 2) - ((((((int)TILT_SERVO - 190) * (65 / 124)) * (3.14159 / 180)))))) / float sin ( float ( (((((int)TILT_SERVO - 190) * (65 / 124)) * (3.14159 / 180)));

The error is:

C:\frc_camera_2\frc_camera\user_routines.c:360:Error: syntax error
Halting build on first failure as requested.
BUILD FAILED: Sat Jan 20 19:11:20 2007

The parentheses don’t match, that’s for sure. I’m no expert in C syntax, but the “float sin” part looks wrong too.

I’d create a lookup table in advance and use it instead of doing all that complicated and time-consuming calculation on the fly.

Radicalnerd told you at least part of your problem. You have a different number of open parenthesis than you have close parenthesis. You also seem to be attempting a type cast of float without the required parenthesis.

If you’re trying to get the horizontal distance between you and the rack, and you have the angle measure from the camera, and you have the height of the light, wouldn’t you use tan to find the distance?

I would suggest dividing those numbers and rounding at whatever number of digits you feel comfortable at. That will eliminate 5 pairs of parentheses. Also, you could do some of that math in earlier steps and use a final, easy to read line for the distance.

Assuming you want the horizontal distance to the rack from your robot, maybe…this (I’m assuming TILT_SERVO is an angle measure):

int targheight;
float dist;
targheight = 3;

dist = targheight / tan(TILT_SERVO);

This is what I was trying to say:

http://kevin.org/frc/2007_frc_range_calc.pdf

dist = ((int)targheight * float sin(((3.14159 / 2) ...

should be


dist = ((int)targheight * (float) sin(((3.14159 / 2) ...
                            ^^^^ 

to recap:

  1. make sure parenthesis are properly matched up
  2. type casts such as int and float need to be in their own parenthesis
    like you did with (int) already