|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
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 );
|
|
#2
|
|||
|
|||
|
Re: Trig problems
Well, it might do some good to actually assign meh a value. Either that or printf angle, instead of meh. [/sarcasm]
|
|
#3
|
|||
|
|||
|
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 ![]() |
|
#4
|
|||
|
|||
|
Re: Trig problems
Quote:
Stupid me Code:
float angle;
angle = sin(3.14);
printf("the angle is = %d\r\n",angle );
There, I think... |
|
#5
|
|||||
|
|||||
|
Re: Trig problems
You're trying to print a float value as an integer (%d)
|
|
#6
|
|||
|
|||
|
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.
|
|
#7
|
|||||
|
|||||
|
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
|
|
#8
|
|||
|
|||
|
Re: Trig problems
Quote:
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. |
|
#9
|
|||
|
|||
|
Re: Trig problems
Quote:
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
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| speed of math.h trig functions? | Jared Russell | Programming | 4 | 07-02-2006 07:13 |
| Anyone Else Having Problems with Q&A on FIRST? | Windwarrior | General Forum | 6 | 25-01-2006 10:54 |
| Do you all have problems with.... | Munkaboo | Website Design/Showcase | 19 | 03-03-2003 19:51 |
| Radio problems -- not | archiver | 2000 | 1 | 23-06-2002 22:23 |