Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Trig problems (http://www.chiefdelphi.com/forums/showthread.php?t=44215)

revolution1737 17-02-2006 21:50

Trig problems
 
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?
Code:

float meh;
angle = sin(3.14);
printf("the angle is = %d\r\n",meh );


Jake M 17-02-2006 21:53

Re: Trig problems
 
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

Re: Trig problems
 
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

Re: Trig problems
 
Quote:

Originally Posted by revolution1737
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?
Code:

float meh;
angle = sin(3.14);
printf("the angle is = %d\r\n",meh );



Stupid me

Code:

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

Re: Trig problems
 
You're trying to print a float value as an integer (%d)

steven114 18-02-2006 00:07

Re: Trig problems
 
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

Re: Trig problems
 
Yea, you can always manipulate the value to print it.
E.g.,

Code:

#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

Re: Trig problems
 
Quote:

Originally Posted by Mark McLeod
Yea, you can always manipulate the value to print it.
E.g.,

Code:

#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

Re: Trig problems
 
Quote:

Originally Posted by revolution1737
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:

Code:

#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



All times are GMT -5. The time now is 01:37.

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