### Author Topic: Trig Approximations  (Read 5771 times)

0 Members and 1 Guest are viewing this topic.

#### Tony R

• Regular Contributor
• Posts: 117
• Country:
##### Trig Approximations
« on: November 21, 2011, 02:54:47 pm »
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?

Tony R.
Computer Engineering Student
Focus: Embedded Assembly Programming, Realtime Systems,  IEEE Student Member

#### ejeffrey

• Super Contributor
• Posts: 2219
• Country:
##### Re: Trig Approximations
« Reply #1 on: November 21, 2011, 03:17:42 pm »
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?

#### Mechatrommer

• Super Contributor
• Posts: 9987
• Country:
• reassessing directives...
##### Re: Trig Approximations
« Reply #2 on: November 21, 2011, 03:23:20 pm »
...guided by a compass and GPS
i dont see the need for heavy trig there.
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)

#### ejeffrey

• Super Contributor
• Posts: 2219
• Country:
##### Re: Trig Approximations
« Reply #3 on: November 21, 2011, 04:13:07 pm »
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.

#### Tony R

• Regular Contributor
• Posts: 117
• Country:
##### Re: Trig Approximations
« Reply #4 on: November 21, 2011, 06:22:47 pm »
i dont see the need for heavy trig there.

http://www.movable-type.co.uk/scripts/latlong.html

Just the first 2 topics distance and heading, I have defiantly done more trig, but I am worried about the speed.
Tony R.
Computer Engineering Student
Focus: Embedded Assembly Programming, Realtime Systems,  IEEE Student Member

#### Tony R

• Regular Contributor
• Posts: 117
• Country:
##### Re: Trig Approximations
« Reply #5 on: November 21, 2011, 06:25:19 pm »
Are you doing fixed or floating point math?

floating point
Tony R.
Computer Engineering Student
Focus: Embedded Assembly Programming, Realtime Systems,  IEEE Student Member

#### ejeffrey

• Super Contributor
• Posts: 2219
• Country:
##### Re: Trig Approximations
« Reply #6 on: November 21, 2011, 06:44:46 pm »
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.html

the 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

#### Mechatrommer

• Super Contributor
• Posts: 9987
• Country:
• reassessing directives...
##### Re: Trig Approximations
« Reply #7 on: November 21, 2011, 09:24:45 pm »
Just the first 2 topics distance and heading, I have defiantly done more trig, but I am worried about the speed.
you 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.
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)

#### RCMR

• Frequent Contributor
• Posts: 405
##### Re: Trig Approximations
« Reply #8 on: November 21, 2011, 10:28:50 pm »
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".

#### Mechatrommer

• Super Contributor
• Posts: 9987
• Country:
• reassessing directives...
##### Re: Trig Approximations
« Reply #9 on: November 21, 2011, 10:59:34 pm »
If you're really concerned there are a number of FPUs available for microcontroller systems...
some school suggested, get 10x more powerful mcu than you really intended.
It's extremely difficult to start life.. one features of nature.. physical laws are mathematical theory of great beauty... You may wonder Why? our knowledge shows that nature is so constructed. We simply have to accept it. One could describe the situation by saying that... (Paul Dirac)

#### RCMR

• Frequent Contributor
• Posts: 405
##### Re: Trig Approximations
« Reply #10 on: November 22, 2011, 07:38:11 pm »
This crowd has some interesting FPUs

http://www.micromegacorp.com/

I've even got a few laying around here somewnere.

#### Flavour Flave

• Regular Contributor
• !
• Posts: 76
• Country:
• Never knowingly unoffensive.
##### Re: Trig Approximations
« Reply #11 on: November 22, 2011, 07:44:47 pm »
What about COordinate Rotation DIgital Computer or CORDIC

http://en.wikipedia.org/wiki/CORDIC

#### NiHaoMike

• Super Contributor
• Posts: 6615
• Country:
• "Don't turn it on - Take it apart!"
##### Re: Trig Approximations
« Reply #12 on: December 04, 2011, 11:58:54 pm »
This crowd has some interesting FPUs

http://www.micromegacorp.com/

I've even got a few laying around here somewnere.
The pinout suggests that they're just dsPICs with custom firmware.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.

#### RCMR

• Frequent Contributor
• Posts: 405
##### Re: Trig Approximations
« Reply #13 on: December 05, 2011, 02:12:05 am »
The pinout suggests that they're just dsPICs with custom firmware.
They are.

Smf