trigonometric function - a solution for calculation

Moderator: Benj

Post Reply
benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

trigonometric function - a solution for calculation

Post by benp »

With floating point, it is no so difficult to implement trigonométric and scientific function with CORDIC Algorithm.
This could be easily done in flowcode.
The most useful functions should be sin, cos and square root.

Here are a links to do this:
http://cdeval.free.fr/IMG/pdf/cordic.pdf
It is french but there is a code (in english) ready to us.
Here is for example the code for sin and cosinus for ti 92 calculator:

Code: Select all

Cordic2(z)
Func
Local angle,x,y,temp,s,i
{.7854,.46365,.24498,.12435,.06242,.03124,.01562,.00781,.00391,.00195,9.8e-4,4.9e–4,2.4e–4,1.2e–4,6.e-5}»angle
.60725»x
0»y
For i,0,14
when(z=0,1,signe(z))»s
x-s*y/2^i»temp
y+s*x/2^i»y
temp»x
z-s*angle[i+1]»z
EndFor
Return {x,y}
EndFunc
You give the value z to the function and it returns x=cosinus and y=sinus with 2^-14=6e-5 précision.
It is possible to find the other scientific function with similar cordic function.

Here are other links for CORDIC:
University of texas
http://www.math.utep.edu/Faculty/helmut ... ordic.html
Jacques Laporte - worked on HP-35 (hard to find ready to use functions)
http://www.jacques-laporte.org/index.html

Another solution is maybe to use HI TECH but it is more expensive...
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: trigonometric function - a solution for calculation

Post by Benj »

Hello,

There is a free version of the Hi Tech compiler that will compile up to 30 days and then from then on without optimisations.

Otherwise maybe AVR or ARM would be a better option for you as these devices will handle the floating point calculations nativly without making you pay thousands of pounds for a compiler with optimisations.

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: trigonometric function - a solution for calculation

Post by Steve »

The CORDIC algorithm is an interesting approach for a PICmicro and I think we'll look into it to see if it would be useful to implement it.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: trigonometric function - a solution for calculation

Post by benp »

Hello,

I did a test of the cordic for atan (y/x) in the attached excel file:
cordic-atan2.xls
(39 KiB) Downloaded 473 times
It can be useful to know the direction of a vector x,y (my application is gps).
I can code this in c code for flowcode or in flowcode macro. Which one is the most useful for you if you want to implement it in flowcode?
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: trigonometric function - a solution for calculation

Post by Steve »

That's great. C code is usually more useful for us.

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Fast Atan calculation with flowcode

Post by benp »

Sorry for the delay.

This is a "fast" Atan calculation example.
The calculations are done with integers only.
It return atan (y/x) in deg with a precision better than 1 deg.
This is the angle of the point (x,y)
The simulation is working.
It is possible to improve it(static variable, C code, negative value...).
The program can be adapted for a real calculation and more precision.
2680-atan01.fcf
(10 KiB) Downloaded 524 times
The same method can be used to calculate sinus, cosinus, tang, square root ect...
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

sin and cos - fast integer macro

Post by benp »

I did this with the cordic method.
Sin and cos calculation with angle as integer and result as integer.
Precision 1 degree and 0.02 with 7 iterations.
The macro is ready to use and can be exported.
2680-sin-cos-macro2.fcf
(13.1 KiB) Downloaded 496 times
Real calculations can be performed with the same method (but slover).

I don't know if this method is quicker or slower than:
http://www.matrixmultimedia.com/mmforum ... =13&t=7635

Can the sin, cos and atan be included in the next flowcode release?
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: trigonometric function - a solution for calculation

Post by medelec35 »

Hiya benp. Thats certianly some clever maths there!. Way beyond my level.
Hope you don't mind me mentioning there is an issue with one of the calculations which means the simulation will be fine, but on real hardware results will be wrong.
Calculation that will cause a problem is: z = z*360/1023
I follow a rule that if not using float (so applies to byte and int.) any result of a calculation must not be greater than 32767. If it is you will be exceeding the allocated memory for 16bit calculations so register will roll over, producing incorrect results. (because 32767 + 1 = -32768)

E.g if z starts of at 153. Then 153*360=55080.
exceeding 326767 by 22313
After rollover, 153*360/1023 = -10 and not 53 as the simulator will show.
This will catch a lot of people out (myself included, that's why I had to work out what was going on lol).
Martin

benp
Posts: 155
Joined: Sat Mar 28, 2009 5:44 pm
Location: LYON FRANCE
Has thanked: 3 times
Been thanked: 41 times
Contact:

Re: trigonometric function - a solution for calculation

Post by benp »

Thank you medelc35, I didn't noticed this.
I didn't test the main program on harware but just the macro: I draw a GPS "course over ground"(=direction angle) as a direction line on a graphic screen.
I extracted the working macro just and write the main program to have a very simple example.
I corrected the formula for potentiometer to angle transformation with a z=z/2 and a MOD 360 in the macro.
This new main program should work on harware with any z>0 value:
2680-sin-cos-macro3.fcf
(16 KiB) Downloaded 510 times
The macro is optimized for fast opération and I tried to make it easy to read.
Just follow the straigth line in the macro and you will understand that the calculation is very simple.
I just did a copy of the cordic2 algorithm showed in my first post (optimized for integer).

Here is a useful application of sin-cos calculation: draw a circle on a graphic screen:
2680-sin-cos-macro-graphic3.fcf
(17 KiB) Downloaded 487 times
I didn't test this on hardware because I don't have a graphic screen.
The simulation is very slow but it should be faster on hardware.
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

Post Reply