Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   math.h library (http://www.chiefdelphi.com/forums/showthread.php?t=23172)

mightywombat 18-12-2003 19:59

math.h library
 
I need to use some trig functions in some code that I'm writing for our kiwi drive train. Does anyone know where to find C libraries (specifically the math.h)? I've found the GNU C library but didn't think it would work because it said that it only ran on GNU systems. All I need is simple sin cos and tan funcs.
peace.
bill

EricS-Team180 18-12-2003 22:12

Re: math.h library
 
Hi.
I recall that Mike Betts and Dr. Joe had some good comments on trig functions in this thread:
http://www.chiefdelphi.com/forums/sh...highlight=trig

(named "Edurobot and arrays" from November, in this forum)

For just sin, cos and tan, you might want to make a linear table lookup.

I've downloaded the cmath library from www.netlib.org/cephes, but I haven't tried it out on the edu rc

Eric

mightywombat 18-12-2003 22:14

Re: math.h library
 
thanks alot. i'll check it out. although i did find that math.h does in fact come with the compiler after doing a search on my comp.

mightywombat 19-12-2003 00:37

Re: math.h library
 
whats a linear table lookup?

seanwitte 19-12-2003 08:18

Re: math.h library
 
Quote:

Originally Posted by mightywombat
whats a linear table lookup?

The only issue with a table lookup is that you're confined to the values you put in your table.

You can use an approximation called the Maclaurin series for Sine, Cosine, and Tangent. The formulas are listed below. The angle, x, is in radians. There are 2*Pi radians in a circle, so 360 degrees = 2*Pi radians. These formulas match the actual Sine, Cosine, and Tangent to three decimal points.

Sine(x) = x - x^3/3! + x^5/5!
Cosine(x) = 1 - x^2/2! + x^4/4!
Tangent(x) = x + 2x^3/3! + 16x^5/5!

This is a special case of the Taylor series, which I'll leave to you look up. The Taylor series would allow you to reduce the number of terms you're calculating by using know values of Sin/Cos/Tan near the angle you're looking for.

The ! is the factorial operator and just means to take the number and multiple it by every number between it and 0.

3! = 3*2*1 = 6
4! = 4*3*2*1 = 24

Mike Betts 19-12-2003 08:45

Re: math.h library
 
A Taylor Series expansion solution for sine and cosine would look like this (if you are doing floating point arithmetic) with angle in radians:

angle_pow_2 = angle * angle;

sin_angle = -0.0001984127 + (angle_pow_2 * 0.0000027557);
sin_angle = 0.0083333333 + (angle_pow_2 * sin_angle);
sin_angle = -0.1666666667 + (angle_pow_2 * sin_angle);
sin_angle = 1.0 + (angle_pow_2 * sin_angle);
sin_angle = angle * sin_angle;

cos_angle = -0.0013888889 + (angle_pow_2 * 0.0000248016);
cos_angle = 0.0416666667 + (angle_pow_2 * cos_angle);
cos_angle = -0.5000000000 + (angle_pow_2 * cos_angle);
cos_angle = 1.0 + (angle_pow_2 * cos_angle);


Since our team will be using integer arithmetic exclusively on the PIC, we generated a table using Excel (saved as a CSV file) and imported into the PIC code as a table of constants existing in ROM space.

Note that only sine from 0 to 90 degrees is required as the function is symmetrical. Both cosine and tangent can be mathematically generated from the sine.

The problem with math.h is that it returns a double and you really do not want to do floating point on your 'bot. The nice thing about a table lookup is that you control the format of the stored (returned) variable. In our case, an integer in 2FX14 format.

Hope this helps.

mightywombat 19-12-2003 13:49

Re: math.h library
 
sweeeeetness. this rocks. i've got a few questions.
do you suggest reading all of the values from 0-90 (in fact you'd only need 0-45 because sin (46-90) = 1 - sin (0-45)) into a matrix or actually calculating them?
I'm also confused as to the difference between ROM and RAM space. I know what the acronyms stand for but thats about it.
why no floating points? is it too much for it to handle? are there any other things i should stay away from calculation/variable wise?

Kevin Watson 19-12-2003 14:01

Re: math.h library
 
Quote:

Originally Posted by seanwitte
You can use an approximation called the Maclaurin series for Sine, Cosine, and Tangent.

For us math geeks, there is also the most wonderful CORDIC algorithm. I suspect that it'll be faster than the Taylor series. I found a pretty good FAQ (with code) here.

Kevin Watson 19-12-2003 14:04

Re: math.h library
 
Quote:

Originally Posted by mightywombat
are there any other things i should stay away from calculation/variable wise?

Division is very painful without hardware support.

-Kevin

mightywombat 19-12-2003 14:22

Re: math.h library
 
whoa.. that CORDIC is rather confusing stuff. I'm going to play with it and see if i can figure it out a little more.
so you are saying that I should stay away from dividing two numbers?
(the question sounds dumb but i'm very surprised that it would be so tough)
I remember reading that it can handle addition VERY well but doesn't do much else as quick. thanks for the quick reply.

Kevin Watson 19-12-2003 14:33

Re: math.h library
 
Quote:

Originally Posted by mightywombat
whoa.. that CORDIC is rather confusing stuff. I'm going to play with it and see if i can figure it out a little more.

Yeah, you gotta be a math geek to like the CORDIC :D.


Quote:

Originally Posted by mightywombat
so you are saying that I should stay away from dividing two numbers? (the question sounds dumb but i'm very surprised that it would be so tough) I remember reading that it can handle addition VERY well but doesn't do much else as quick. thanks for the quick reply.

Well, I shouldn't have made such a generalization. Integer division by powers of two is very fast (if the compiler is designed for such an optimization) because you only need to do a binary right shift to divide a number by two (multiplication works by shifting left). Division of two eighty-bit IEEE floating-point numbers on an eight-bit microcontroller is very painful.

-Kevin

Kevin Watson 19-12-2003 14:44

Re: math.h library
 
Quote:

Originally Posted by Kevin Watson
Well, I shouldn't have made such a generalization. Integer division by powers of two is very fast (if the compiler is designed for such an optimization) because you only need to do a binary right shift to divide a number by two (multiplication works by shifting left). Division of two eighty-bit IEEE floating-point numbers on an eight-bit microcontroller is very painful.

-Kevin

I forgot to point out that with timer support, it would be very easy to profile the different algorithms to see which is most efficient on the PIC18F8520. I just happened to have written some example timer code that might help.

-Kevin

Mike Betts 19-12-2003 15:22

Re: math.h library
 
Quote:

Originally Posted by mightywombat
do you suggest reading all of the values from 0-90 (in fact you'd only need 0-45 because sin (46-90) = 1 - sin (0-45)) into a matrix or actually calculating them?

Check your math... I think that 0-90 degrees is the minimum you want to try.


Quote:

Originally Posted by mightywombat
I'm also confused as to the difference between ROM and RAM space. I know what the acronyms stand for but thats about it.

RAM is cleared to zero (at best) or random noise after a power up. ROM is non-volatile.


Quote:

Originally Posted by mightywombat
why no floating points? is it too much for it to handle? are there any other things i should stay away from calculation/variable wise?

I would suggest you do a search of these fori (I still like that word, Dr. J!) for other discussions on floating point. Bottom line: The PIC18C is not a floating point processor...

Joshua Lynn 26-12-2003 14:41

Re: math.h library
 
I am currently working on a math library for trig functions and a few other floating point operations like floor, ceiling, and sqrt. I hope to have them available by Jan 10.

These functions are based on other open source numerical methods. The trick is the need to be converted from their double data type operations to float data type in the format used by the PIC which is not IEEE 754.

The solutions are all based on various forms of approximation.

For those who want to try it themselves here is an iterative way to calculate the sqrt:

y=sqrt(x)

y0 = x/2
y1 = (y0 + x/y0)/2
yn = (yn + x/yn-1)/2

Iterate through the solution n times until you have the desired level of precision. For low values of x the convergence is good for n=10. For larger values the first guess should be adjusted to reflect the order of magnitude of x. If x =10^6 start with an initial value 10^3 or (Order of Magnitude)/2. The closer the first division is to the answer the sooner the number will converge.

So far I have worked out
SQRT
SIN
COS
TAN
SEC
CSC
COT
Floor
Ceiling
ABS

I will have at least one of the following trig functions
ATAN
ASIN
ACOS

I may be able to get them all but I have a bit more research to do.

Additionally, I have found other methods to calculate
ln
Log
MOD
x^y --Used for larger values of y it is more efficient

Stay tuned for more.

Joe Johnson 03-01-2004 01:12

Re: math.h library
 
I did a quick Excel spreadsheet to see whether a linear or parabolic interpolation was better (I expected the parabolic to be better).

I used 128 values 16 bit values in my table for a 90 degree section of the sine function (I realize this is a prettty thick mesh, but it is only .25Kbytes for my entire sine and cosine approximation which seems small enough). Anyway, I was only off at most 2 counts of a 16 bit output with a linear interpolation. Surprisingly (to me at least) the parabolic interpolation was worse! It was sometime off by 3 counts and also had a higher average error. I suppose that this could be due to the fact that sine is a x + x^3+ x^5 ... function (note no even powers) while I was adding a x^2 term to my approximation.

Bottom line: a max 2 counts out of a 16 bit number is going to be fine for me and my robot. I plan out writing a simple lookup table with linear interpolation (at least for sine and cosine -- we will see about tangent, arcsine, arccosine and the all important arctangent).

Joe J.


All times are GMT -5. The time now is 14:59.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi