Trig Approximations  - Page 1
Forum section: electronics / general

author: Tony R
author_rank: Regular Contributor
timestamp: November 21, 2011, 02:54:47 pm
content: Hello, for my current project i am working on a car that will be guided by a compass and GPS. for this i need to guide the car based on the heading and position. the formulas that can do this for me are very heavy on the trig functions.My question is are the trig functions that are included in the standard math.h file efficient? or should i write my own approximations for trig functions? or should I use a table?I would like up to 1 +/ .5 degree of accuracy so about 0.0175 rad of resolution or so. What is the best way of doing this? will the standard math.h work, or should i create my own functions?

author: ejeffrey
author_rank: Super Contributor
timestamp: November 21, 2011, 03:17:42 pm
content: The math.h functions are going to be designed for correctness before speed. I think they should be accurate to a couple of LSBs.A table with linear interpolation will be super-fast and relatively compact. If you have the ROM space available that is probably the best option. If you need or want better accuracy the table size will start getting too big, in that case you want a more sophisticated approximation -- a quadratic approximation will probably be enough for all intents and purposes.Are you doing fixed or floating point math?

author: Mechatrommer
author_rank: Super Contributor
timestamp: November 21, 2011, 03:23:20 pm
content: Quote from: Tony R on November 21, 2011, 02:54:47 pmi dont see the need for heavy trig there.

author: ejeffrey
author_rank: Super Contributor
timestamp: November 21, 2011, 04:13:07 pm
content: I guess I should also suggest: use the default math.h methods unless you see a problem with performance. If performance is a problem, add drop-in replacements that can be changed with a #define to call the originals in case you ever suspect that a bug in the approximations is causing trouble.

author: Tony R
author_rank: Regular Contributor
timestamp: November 21, 2011, 06:22:47 pm
content: Quote from: Mechatrommer on November 21, 2011, 03:23:20 pmhttp://www.movable-type.co.uk/scripts/latlong.htmlJust the first 2 topics distance and heading, I have defiantly done more trig, but I am worried about the speed.

author: Tony R
author_rank: Regular Contributor
timestamp: November 21, 2011, 06:25:19 pm
content: Quote from: ejeffrey on November 21, 2011, 03:17:42 pmfloating point

author: ejeffrey
author_rank: Super Contributor
timestamp: November 21, 2011, 06:44:46 pm
content: It should only take a few minutes to measure how long those formulas take to calculate on your target platform.sin and cos are trivial with lookup tables. atan2 is a bit more complicated. A quick google search turns up this link:http://lists.apple.com/archives/perfoptimization-dev/2005/Jan/msg00051.htmlthe code at the bottom claims to be accurate to .005 radian.There is also this stack overflow question about approximations for other functions:http://stackoverflow.com/questions/345085/how-do-trigonometric-functions-work/394512#394512

author: Mechatrommer
author_rank: Super Contributor
timestamp: November 21, 2011, 09:24:45 pm
content: Quote from: Tony R on November 21, 2011, 06:22:47 pmyou are not going to ask the car head straight half the globe are'nt you? i look your problem more into "finding the shortest path" given the country/city map/road from GPS. but well, i think math.h is good enough.

author: RCMR
author_rank: Frequent Contributor
timestamp: November 21, 2011, 10:28:50 pm
content: If you're really concerned there are a number of FPUs available for microcontroller systems. You can offload all that stuff to the FPU and have your MCU do something else until the interrupt says "I'm done".

author: Mechatrommer
author_rank: Super Contributor
timestamp: November 21, 2011, 10:59:34 pm
content: Quote from: RCMR on November 21, 2011, 10:28:50 pmsome school suggested, get 10x more powerful mcu than you really intended.

author: RCMR
author_rank: Frequent Contributor
timestamp: November 22, 2011, 07:38:11 pm
content: This crowd has some interesting FPUshttp://www.micromegacorp.com/I've even got a few laying around here somewnere.

author: Flavour Flave
author_rank: Regular Contributor
timestamp: November 22, 2011, 07:44:47 pm
content: What about COordinate Rotation DIgital Computer or CORDIChttp://en.wikipedia.org/wiki/CORDIC

author: NiHaoMike
author_rank: Super Contributor
timestamp: December 04, 2011, 11:58:54 pm
content: Quote from: RCMR on November 22, 2011, 07:38:11 pmThe pinout suggests that they're just dsPICs with custom firmware.

author: RCMR
author_rank: Frequent Contributor
timestamp: December 05, 2011, 02:12:05 am
content: Quote from: NiHaoMike on December 04, 2011, 11:58:54 pmThey are.