|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
Trig. Functions in EasyC
I haven't been able to find an answer to this on any of the Vex forums so I figured I'd throw it here. The students on our Vex team are working on a concept with their autonomous and need to be able to use trig functions (primarily tangent) but they can do everything else they are working with in EasyC if they can get the value of a calculation using these trig. functions. Unlike MPLAB it seems that you can't just plug in a math header file and use it like in FRC, but perhaps I am wrong. Has anyone does this and/or any suggestions on how to go about doing it? Thanks!
|
|
#2
|
||||
|
||||
|
Re: Trig. Functions in EasyC
You can approximate sin very well between -PI/2 and PI/2 with the function f(theta) = theta - theta3/6 + theta5/120.
Once you've got that approximation, you can basically compute any sin value by using the periodic properties of sin. sin(x) = sin(PI - x) between PI/2 and 3PI/2 (this recursively uses the bit we can accurately approximate) sin(x) = sin(x - 2PI) between 3PI/2 and 2PI. Here's the code for sin: Code:
#define PI 3.14159
float ApproxSin(float theta)
{
while(theta > (3*PI)/2)
{
theta -= 2*PI;
}
while(theta < -PI/2)
{
theta += 2*PI;
}
// theta is now between -PI/2 and 3*PI/2
if(theta > -PI/2 && theta <= PI/2) // basic approximation
{
return theta - theta*theta*theta/6 + theta*theta*theta*theta*theta/120;
}
else if(theta > PI/2 && theta < 3*PI/2)
{
return ApproxSin(PI - theta);
}
}
|
|
#3
|
|||
|
|||
|
Re: Trig. Functions in EasyC
Quote:
Brad |
|
#4
|
|||
|
|||
|
Re: Trig. Functions in EasyC
I would like to suggest, without actually knowing your specific application, that you might want to utilize a lookup table. If it is a situation where you need to do the calculations many, many times, then a lookup table will save you computation time as trig functions do have some overhead. Precalculate your trig function for, say, 0-359 degrees and plug the values into an array (indexed 0-359 of course!). Then when needed you just index the array for the answer. If this is a navigation application 0-359 degrees should really be enough resolution, given the error inherent in the sensors. The only drawback is, of course, you have to use up memory to store the array of floats.
|
|
#5
|
||||
|
||||
|
Re: Trig. Functions in EasyC
Quote:
![]() I'm not sure how much code space the VEX controller has, but 360 floats in a table is 1440 bytes, which is quite a bit. Another idea in that vein would be to have a smaller table (45 entries) and just interpolate between them. Code:
float GetSin(int degrees)
{
int subDegrees = degrees & 7; // gets the last 3 bits
int tableIndex = degrees / 8; // finds out the table entry we want
return ((8-subDegrees)*table[tableIndex] + subDegrees*table[tableIndex+1])/8;
}
|
|
#6
|
|||
|
|||
|
Re: Trig. Functions in EasyC
This is good stuff! Excellent suggestions!
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Organize EasyC functions in a fashion we like? | Chris_Elston | Programming | 3 | 20-02-2006 18:53 |
| speed of math.h trig functions? | Jared Russell | Programming | 4 | 07-02-2006 07:13 |
| Calling C functions from EasyC | koenig3456 | Programming | 4 | 27-01-2006 16:03 |
| Trig functions and type conversion | kaszeta | Programming | 6 | 14-01-2006 23:34 |
| Return of the Inverse Trig Functions | Leo M | Programming | 3 | 24-01-2002 08:12 |