|
Re: math.h library
A Taylor Series expansion solution for sine and cosine would look like this (if you are doing floating point arithmetic) with angle in radians:
angle_pow_2 = angle * angle;
sin_angle = -0.0001984127 + (angle_pow_2 * 0.0000027557);
sin_angle = 0.0083333333 + (angle_pow_2 * sin_angle);
sin_angle = -0.1666666667 + (angle_pow_2 * sin_angle);
sin_angle = 1.0 + (angle_pow_2 * sin_angle);
sin_angle = angle * sin_angle;
cos_angle = -0.0013888889 + (angle_pow_2 * 0.0000248016);
cos_angle = 0.0416666667 + (angle_pow_2 * cos_angle);
cos_angle = -0.5000000000 + (angle_pow_2 * cos_angle);
cos_angle = 1.0 + (angle_pow_2 * cos_angle);
Since our team will be using integer arithmetic exclusively on the PIC, we generated a table using Excel (saved as a CSV file) and imported into the PIC code as a table of constants existing in ROM space.
Note that only sine from 0 to 90 degrees is required as the function is symmetrical. Both cosine and tangent can be mathematically generated from the sine.
The problem with math.h is that it returns a double and you really do not want to do floating point on your 'bot. The nice thing about a table lookup is that you control the format of the stored (returned) variable. In our case, an integer in 2FX14 format.
Hope this helps.
__________________
Mike Betts
Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013
As easy as 355/113...
|