![]() |
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 |
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 |
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.
|
Re: math.h library
whats a linear table lookup?
|
Re: math.h library
Quote:
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 |
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. |
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? |
Re: math.h library
Quote:
|
Re: math.h library
Quote:
-Kevin |
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. |
Re: math.h library
Quote:
Quote:
-Kevin |
Re: math.h library
Quote:
-Kevin |
Re: math.h library
Quote:
Quote:
Quote:
|
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. |
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