![]() |
Syntax Error!
I'm getting a syntax error on this line:
int Distance_Function(float tilt_angle_rad) { if (tracking_initialized == 1) { distance_from_wall = (int)(3.162 / tan(tilt_angle_rad)); return(distance_from_wall); } } I'm not really sure I declaired everything right, but can someone help me out? I added math.h and included it in the tracking.c(where this code is from). float tilt_angle_rad; tilt_angle_rad = (0.01745) * (((TILT_SERVO - 144) * 25)/50); Distance_Function(tilt_angle_rad); I also have that somewhere else in the code. And I declaired my function aswell. Help? Thanks in advance! |
Re: Syntax Error!
Where and how is distance_from_wall declared?
|
Re: Syntax Error!
Declaired in tracking.h
// The distance from the robot to the wall. Initial value is not actually zero; calculated when camera is turned on. #define distance_from_wall 0 and also prototyped the Distance_Function in tracking.h // function prototypes void Servo_Track(void); void Initialize_Tracking(void); unsigned char Get_Tracking_Configuration(unsigned int, unsigned char); int Distance_Function(float); |
Re: Syntax Error!
#define is for CONSTANTS - you want to use a variable such as int or float.
Constants get their value once - you cannot assign them a new one. Since you seem to be using this data in multiple C files, here is what you need to do: in tracking.h: extern int distance_from_wall; in tracking.c, in the global space (top of file, not inside a function): int distance_from_wall = 0; Now your code will work the way you expect. |
Re: Syntax Error!
Abwehr, you rock, it compiled, we're testing now!
Goten, I don't know what you are saying... |
Re: Syntax Error!
Anyone know why it's giving me a really wrong number for distance? I made sure the angle is given in radiens. And the height is correct aswell (it is in meters). Thanks guys!
|
Re: Syntax Error!
Quote:
Assume the angle is pi/4 (45 degrees)... tan(theta) = 1 and your "answer" is pi rather than the expected value of 8.5 feet... |
Re: Syntax Error!
lol, no that is the difference between the camera height and green light height in meters, not pi.
Distance = (Green Light Height - Camera Height) / tan theta just one question, do I have to do my angle in radians? because I believe I converted it corectly .01745 radians = 1 degree. |
Re: Syntax Error!
The math.h trig functions only take radians.
If you want, you can do a macro to do degrees, i.e.: #define PI 3.14159265f #define dtan(x) tan(x*PI/180.0f) Then, put this in your code where appropriate: dtan(tilt_angle_rad); The only thing that I can think of that would make your equation give bogus results is if the servo angle conversion function is off. Make sure that whatever Kevin's code defines as the servo's zero corresponds with 0 degrees of tilt - you may have to either rotate the servo horn or redefine the zero point (in tracking.h) to find a good zero. |
Re: Syntax Error!
The conversion factor is roughly 0.01745 I'll use that instead of defining it, it works, it's easier, unless there's some reason that I shouldn't. I'm pretty new to programming, and I'm always glad to learn.
tilt_angle_rad = (0.01745) * (((TILT_SERVO - 144) * 25)/50); but anyways, we already checked and the angle is correct. Yet I'm getting like -166.25 meters for the light that is like 2.5 meters away.... My friend said it maybe that it may be overflow, so I changed everything from floating points to integers, I'll test, see how it goes. Thanks alot of the help! What does the f after the 180.0 and the pi mean? |
Re: Syntax Error!
That indicates that the numbers are floats.
I suppose the reason why it's needed is that the computer always needs to know what data type everything is. So in a normal variable declaration, you say something like float distance; This tells the computer or FRC controller that you are declaring a variable of type float, and the processor allocates a specific number of bytes to that variable. It's usually 4 bytes to a float. Since the memory allocation must be done before doing anything with the variable, variables must be declared before they are used, and they need to be declared as being a specific type because different types are allocated different amounts of memory. In a macro, there is no keyword float. Thus, you need some sort of indicator showing of what type the constant or whatever is. Whoever made C decided that in a macro "f" would suffice for this purpose. #define PI 3.14159265f |
Re: Syntax Error!
So, I guess that might be my problem. I never declared those numbers as floats. It's probably rounding them to integers or something. I will try it out! Thanks!
|
Re: Syntax Error!
Yes, it is always very important to make sure you declare things with the right type. However, when you write something like 3.142, the computer knows you want a float. In fact, the computer even treats 15.0 as a float. Why else would you have put the decimal point if not to mean that you want a float?
|
Re: Syntax Error!
I would start by checking your angle measurement. See if that number makes sense, then worry about your distance formula (which looks right to me).
|
Re: Syntax Error!
Quote:
|
| All times are GMT -5. The time now is 02:14. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi