Log in

View Full Version : Trig problems


revolution1737
17-02-2006, 21:50
I have tried Kevin Watson's trig code with the eeprom, a cordic approach (team 226's code i think), and the default sin function in math.h and every time I come out with a wrong value when I print. I usually get 16512 or 0. I have tried switching from radians to degrees. Am I just getting unlucky or is there something I'm missing?
float meh;
angle = sin(3.14);
printf("the angle is = %d\r\n",meh );

Jake M
17-02-2006, 21:53
Well, it might do some good to actually assign meh a value. Either that or printf angle, instead of meh. [/sarcasm]

steven114
17-02-2006, 21:54
I can't vouch for the specific code you're using, but make sure that your sin() function actually wants an angle in floating point radians. Many take integers, as floating point is many times slower...

If this isn't the problem, posting the code you're using will help greatly :)

revolution1737
17-02-2006, 21:57
I have tried Kevin Watson's trig code with the eeprom, a cordic approach (team 226's code i think), and the default sin function in math.h and every time I come out with a wrong value when I print. I usually get 16512 or 0. I have tried switching from radians to degrees. Am I just getting unlucky or is there something I'm missing?
float meh;
angle = sin(3.14);
printf("the angle is = %d\r\n",meh );


Stupid me

float angle;
angle = sin(3.14);
printf("the angle is = %d\r\n",angle );[/QUOTE]

There, I think...

Mark McLeod
18-02-2006, 00:01
You're trying to print a float value as an integer (%d)

steven114
18-02-2006, 00:07
Speaking of that, the printf doesn't even support floating point (%f). You'll have to do something like multiply your result by 1000, cast to int, and print that.

Mark McLeod
18-02-2006, 00:31
Yea, you can always manipulate the value to print it.
E.g.,


#define ACCURACY 1000 //How many decimal places are important to you
float f;
int i, i2; // Might need to be longs if you have a lot of significant digits

f = 245.56;

/* Print float value */
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%03d \r", i, i2); //e.g., f = 245.559

revolution1737
18-02-2006, 01:23
Yea, you can always manipulate the value to print it.
E.g.,


#define ACCURACY 1000 //How many decimal places are important to you
float f;
int i, i2; // Might need to be longs if you have a lot of significant digits

f = 245.56;

/* Print float value */
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%03d \r", i, i2); //e.g., f = 245.559


I wish I had taken a class so I knew C. Could you write it so it takes the value in degrees from get_gyro_angle() and uses it in a sin function. I have spen 9 hours (seriously) and I still cannot get the right value. Any help is greatly appreciated.

teh_r4v3
19-02-2006, 01:09
I wish I had taken a class so I knew C. Could you write it so it takes the value in degrees from get_gyro_angle() and uses it in a sin function. I have spen 9 hours (seriously) and I still cannot get the right value. Any help is greatly appreciated.

First off, remember that the sin function is not part of the standard library, so you'll have to add it in yourself. Also, it takes radians, not degrees. On top of that, the PIC processor the IFI RC uses doesn't have a FPU (floating point unit), so it has to emulate one in software. (In other words, doing anything with decimals is reeealy slow.) So what I'm going to write isn't exactly the most optimized code in the world.

Going off of Mark's code, this should work:

#define ACCURACY 1000 //How many decimal places are important to you
#define PI 3.14 //Pi to however many digits you want
float f;
int i, i2; // Might need to be longs if you have a lot of significant digits
f = sin((float) get_gyro_angle() / (2.0 * PI));
/* Print float value *
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%03d \r", i, i2); //e.g., f = 245.559