Sinus Function

For Formula Flowcode Micromouse users to discuss projects, flowcharts, and any other issues related to the Formula Flowcode Micromouse.

Moderator: Benj

Post Reply
dgruffaz
Posts: 1
Joined: Fri Nov 13, 2009 5:33 pm
Contact:

Sinus Function

Post by dgruffaz »

Hi,

Please, do someone know if it's possible to use the sinus function with flowcode?

Thanks
Denis

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Sinus Function

Post by Jan Lichtenbelt »


User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Sinus and Cosinus, accuracy about 1E5

Post by Jan Lichtenbelt »

With a rather simple algoritm I manage to calculate sinus and cosinus with a high accuracy. I found a relative error in sinus and cosinus below 2E-5. The algorithm used is a simple recurrency formula (Singleton, 1967) as given in http://en.wikipedia.org/wiki/Trigonometric_tables.

The start angle is 64 degrees and converges to the wanted angle by steps of 32, 16, 8, 4, 2, 1, 1/2, 1/4 etc. In this flowcode up to 21 steps. The maximum number of steps is determined by the RAM size of 368 bytes availabe in the PIC16F886. (each additional step will take 8 bytes extra).

It is nice to see, that it is posisble to calculate sinus and cosinus in a pure Flowcode way, but due to the floating variables it take a lot of ROM and RAM.

Much pleasure with it.

Jan Lichtenbelt

PS.
1) Pay attention, I did simulate it only in PIC16F886, but did not test it in a microcontroller
2) The accuracy can be better for microcontrollers, due to accuracy loss in the simulation mode (see elsewhere). I will try to find the microcontroller accuracy.
Attachments
Sinus and Cosinus with high accuracy.fcf
(18.39 KiB) Downloaded 923 times

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Calculation of accuracy sinus and cosinus

Post by Jan Lichtenbelt »

With a rather simple algorithm I manage to calculate sinus and cosinus with a high accuracy. I calculated the relative errors in sinus and cosinus in an excel file. Two algorithms are used. The first one is a simple recurrency formula (Singleton, 1967) as given in http://en.wikipedia.org/wiki/Trigonometric_tables.

This first algorithm will give errors increasing with decreasing angles, with larger errors in the neighbourhood of 0 degree. An other, second, algorithm for small angles is the well known approximation: sinus(angle)=angle (in radians) with decreasing relative errors with decreasing angles. There will be an angle where both algorithms will have to same relative error. Above that angle the first algorithm must be used, below this angle the second algorithm is advised.

The start angle used, is 64 degrees and this angle converges to the wanted angle by steps of 32, 16, 8, 4, 2, 1, 1/2, 1/4 etc. The maximum number of steps is determined by the RAM size of the available microcontroller (each additional step will take an additional 8 bytes for the 2 floating points arrays used).

For three numbers of iteration steps (determining by the RAM available) I looked more in detail to N=7, N= 16 and N=22 steps, equal to an angle difference of respectively 1, 2^-9 and 2^-15 degree. These last two are about 0.002 degree respectively 0.00003 degree.

N=7 iteration steps (angle accuracy of 1 degree; integer angles):
I used 64 as starting angle instead of 45 degrees, because with starting at 64 degrees the sinus values for whole (integer) angles will result in exact sinus values. In this case only N=7 iteration steps are needed. Remark: Exact means the same accuracy of floating point numbers as in Excel.

N=16 iteration steps (angle accuracy of 0.002 degree):
At 3 to 4 degrees both algorithms show the same relative error of 0.0005 (5E-4). This is also the maximum relative error, which will decrease rapidly a factor 10 to 5E-5 for an angle of 1 degree and to zero for an angle=0. For larger angles, the relative error decreases slowly. Pay attention the relative error strongly fluctuates when changing the angle, even down to 0 for integer angles.

N=22 iteration steps (angle accuracy of 0.00003 degree):
The maximum relative error of about 2E-5 is found for both algorithms at an angle of 1 degree.

As soon as I have the PIC16F886 I will check if the theory fits the practice.
Last edited by Jan Lichtenbelt on Thu Oct 21, 2010 11:13 am, edited 2 times in total.

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Sinus Function

Post by Jan Lichtenbelt »

Please find my overview Word document of using sinus and cosinus functions in Flowcode.

In next window you will find the Flowcodes mentioned in this document.

Jan Lichtenbelt
Attachments
Flowcode Sinus and Cosinus.doc
(64.5 KiB) Downloaded 1043 times
Last edited by Jan Lichtenbelt on Sat Oct 16, 2010 2:33 pm, edited 2 times in total.

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Sinus Function

Post by Jan Lichtenbelt »

The following flowcode sinus and cosinus are attached (see details word document here above):
2a) Sinus and Cosinus with array of bytes
2b) Sinus and Cosinus with array of integers
3a) Sinus and Cosinus with 7 iterations

These three flowcodes contain a macro to show the relative errors. This macro use the sin(), cos() and atan() function which are only available in the simulation mode of the flowcode.

To use these 3 flowcodes in microcontrollers, remove first the last "Error-Calculation" macro call in MAIN and then the macro call "Error-Calculation" itself (Macro > Remove).

Pay attention, for the last flowcode 3a) a large RAM, like eg. in 16PIC887, is necessary.

All 3 flowcodes have the possiblity to interpolate or not. The variable InterAnglesOnly can be (re)set in the Init macro.

If you do not need the interpolation, delete de "Sinus_Cosinus" macro. The "Sin_Cos" will be left as essential macro to calculate the sinus and cosinus value.

Good Luck

Jan Lichtenbelt
Attachments
Sinus and Cosinus with 7 iterations.fcf
(23.95 KiB) Downloaded 861 times
Sinus and Cosinus with array of integers.fcf
(20.79 KiB) Downloaded 857 times
Sinus and Cosinus with array of bytes.fcf
(20.75 KiB) Downloaded 832 times

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

atan function

Post by benp »

I used the cordic algorithm to calculate atan.
See:
http://www.matrixmultimedia.com/mmforum ... =31&t=7447
It is possible to calculate sin, cos and square root with the cordic algorithm but I don't know if the accurency and speed is better or not than Singleton.
Regards
INSA 1er cycle GCP projects with or without eblocks:
http://www.youtube.com/user/INSAgcp

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Sinus Function

Post by Jan Lichtenbelt »

Dear Benp

The Cordic method for calculation of sinus and cosinus is also a simple method. The Cordic 1 method can not be used, while it needs the square root. But you mentioned the Cordic2 method. This needs 15 real, floating point variables. The accuracy is rather poor 5E-3. It can be improved a factor 5 if the 15 variables with more accuracy are used.
Up to now the Singleton method with 7 iterations seems favorite to me, while it gives exact sinus and cosinus values for integer angles and also high accuracy with interpolation for real angles.

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Sinus Function

Post by Jan Lichtenbelt »

Writing is someting else as talking. Everybody would say "Of coarse". But I found that that is not as trivial as it sounds.

I thought the sinus and cosinus problem was solved with the different possibilities as given above. But after a time I told a friend about this and at the same time I told him, I realised I forgot a possibility. I did not found this solution in all the time I was writing in this FORUM.

The most easy way to calculate the sinus and cosines in an exact way (within the limits of the accuracy of floating points) is to start calculating the sinus and cosines at steps of 1 degree. The number of floating point variables decrease from 2 x 7 to just 2 (alpha and beta for 1 degree). The advantage is exact values for sinus and cosines for integer (whole) angles. The disadvantage is that the number of calculation increase to maximum 45 compared to 7 in the 7 iteration program. That means a small time increase.

For real angles, here again an interpolation is used between integer (whole) angles.

Find the simulation program attached. If you will use it in the 16PIC877, delete first the error macro as given above.

I found the use of the RAM to be lowered from 64% to 48% and the ROM from 75% to 61% compared to the 7 iteration program. Important lack of memory is an issue.

These exact values for sinus and cosinus can be changed from 1-degree angles into half degree, quart-degree etc, by changing the alpha and beta values in agreement [alpha=sin^2(pi/N) and beta=sin(2pi/N), with N=360, 720 and 1440 respectively]. But the calculation time will increase by the same factor too.

Good luck

Jan Lichtenbelt
Attachments
Sinus and Cosinus in steps of 1 degree.fcf
(23.39 KiB) Downloaded 862 times

Post Reply