Page 1 of 1

trigonometric function - a solution for calculation

Posted: Mon Jun 07, 2010 12:11 pm
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...

Re: trigonometric function - a solution for calculation

Posted: Mon Jun 07, 2010 4:30 pm
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.

Re: trigonometric function - a solution for calculation

Posted: Tue Jun 08, 2010 8:21 am
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.

Re: trigonometric function - a solution for calculation

Posted: Sun Jul 11, 2010 8:10 am
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 488 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?

Re: trigonometric function - a solution for calculation

Posted: Mon Jul 12, 2010 7:08 am
by Steve
That's great. C code is usually more useful for us.

Fast Atan calculation with flowcode

Posted: Sat Oct 23, 2010 1:00 pm
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 541 times
The same method can be used to calculate sinus, cosinus, tang, square root ect...

sin and cos - fast integer macro

Posted: Fri Jan 21, 2011 9:01 pm
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 514 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?

Re: trigonometric function - a solution for calculation

Posted: Fri Jan 21, 2011 11:29 pm
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).

Re: trigonometric function - a solution for calculation

Posted: Sat Jan 22, 2011 12:23 pm
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 523 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 512 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.