Go to Post An FRC team's success should not be measured in how many robots it can build or how many blue banners it can collect. It should be measured by how many engineers (and other professionals) it 'builds'. - jvriezen [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 18-12-2003, 19:59
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
Question 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
  #2   Spotlight this post!  
Unread 18-12-2003, 22:12
EricS-Team180's Avatar
EricS-Team180 EricS-Team180 is offline
SPAM, the lunchmeat of superheroes!
AKA: Eric Schreffler
FRC #0180 (SPAM)
Team Role: Engineer
 
Join Date: Apr 2002
Rookie Year: 2001
Location: Stuart, Florida
Posts: 561
EricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond reputeEricS-Team180 has a reputation beyond repute
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
__________________

Don't PANIC!
S. P. A. M.
  #3   Spotlight this post!  
Unread 18-12-2003, 22:14
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
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.
  #4   Spotlight this post!  
Unread 19-12-2003, 00:37
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
Re: math.h library

whats a linear table lookup?
  #5   Spotlight this post!  
Unread 19-12-2003, 08:18
seanwitte seanwitte is offline
Registered User
None #0116
Team Role: Engineer
 
Join Date: Nov 2002
Location: Herndon, VA
Posts: 378
seanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant futureseanwitte has a brilliant future
Send a message via AIM to seanwitte
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
  #6   Spotlight this post!  
Unread 19-12-2003, 08:45
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
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.
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #7   Spotlight this post!  
Unread 19-12-2003, 13:49
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
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?
  #8   Spotlight this post!  
Unread 19-12-2003, 14:01
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
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
Engineer at stealth-mode startup
http://kevin.org
  #9   Spotlight this post!  
Unread 19-12-2003, 14:04
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
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
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #10   Spotlight this post!  
Unread 19-12-2003, 14:22
mightywombat's Avatar
mightywombat mightywombat is offline
bins? stacking?? NOOOOO!
AKA: Bill Rosemurgy
#0857 (Superior Roboworks)
Team Role: Programmer
 
Join Date: Dec 2003
Location: Houghton, MI
Posts: 93
mightywombat is on a distinguished road
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.
  #11   Spotlight this post!  
Unread 19-12-2003, 14:33
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
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 .


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
Engineer at stealth-mode startup
http://kevin.org
  #12   Spotlight this post!  
Unread 19-12-2003, 14:44
Kevin Watson's Avatar
Kevin Watson Kevin Watson is offline
La Caņada High School
FRC #2429
Team Role: Mentor
 
Join Date: Jan 2002
Rookie Year: 2001
Location: La Caņada, California
Posts: 1,335
Kevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond reputeKevin Watson has a reputation beyond repute
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
__________________
Kevin Watson
Engineer at stealth-mode startup
http://kevin.org
  #13   Spotlight this post!  
Unread 19-12-2003, 15:22
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
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...
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #14   Spotlight this post!  
Unread 26-12-2003, 14:41
Joshua Lynn Joshua Lynn is offline
Registered User
FRC #0423 (SEC Mechanical Mayhem)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Elkins Park, PA
Posts: 16
Joshua Lynn is on a distinguished road
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.
  #15   Spotlight this post!  
Unread 03-01-2004, 01:12
Joe Johnson's Avatar Unsung FIRST Hero
Joe Johnson Joe Johnson is offline
Engineer at Medrobotics
AKA: Dr. Joe
FRC #0088 (TJ2)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Raynham, MA
Posts: 2,648
Joe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond reputeJoe Johnson has a reputation beyond repute
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.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
The New and Improved FIRST CAD Library Ed Sparks General Forum 22 03-02-2007 18:03
Pneumatic Valves Posed in First Cad Library Ed Sparks Inventor 0 19-01-2003 17:33
Gear Box Parts Posted on First Cad Library Ed Sparks Inventor 1 10-01-2003 01:54
CAD Library Updated ..... Ed Sparks General Forum 0 21-01-2002 18:19
HELP NEEDED using CAD library ahecht Inventor 9 21-01-2002 11:50


All times are GMT -5. The time now is 18:01.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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