![]() |
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!
|
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 |
Re: Trig. Functions in EasyC
Quote:
Brad |
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.
|
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) |
Re: Trig. Functions in EasyC
This is good stuff! Excellent suggestions! :D
Quote:
|
| All times are GMT -5. The time now is 04:45. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi