|
Re: Floats
Steven,
To expand for the benefit of other interested viewers, I thought I'd chime in...
I am entirely in agreement with Dave, Dave and Tristan on this issue. Never use floating point...
Let’s look at a sin function. According to Microchip application note AN660, the math library “sin” function has a performance of 4030 (minimum) to 6121 (maximum) machine cycles. Note: This data is for the PIC16 family. I could not find PIC18 data but I expect it is similar.
As my fixed point integer representation, I choose 0x0400 = 360 degrees for my integer variable “theta”. This is a granularity of about 1/3 of a degree (I dare you to argue that you need more. A table of integers (0x4000 = 1.000) is generated in Excel for the 1024 data points required and included into the code as a rom const int array named sine_table (2K of program memory used).
The statement
sine = sine_table [theta];
executes in 15 machine cycles. Note that you get cos just as easy from the same array:
cosine = sine_table [(theta + 0x0100) % 0x0400];
If using 2K of program space bothers you, you can use various math techniques such as a Taylor series expansion (see this thread for a discussion).
Regards,
Mike
Post Script: I never saw Seth's last question in the above link until just now...
Code:
// sine_table is an array of 1024 2FX14 integers where 0x4000 = 1.0 and 0xC000 = -1.0
// sine.csv is a slightly modified "comma separated variable" file created by Excel.
// The declaration "rom const" causes this array to exist in the PIC program space.
rom const int sine_table[DEG_360] =
{
#include "sine.csv"
};
Sorry for not responding sooner Seth... - Mike
__________________
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...
|