Society of Robots - Robot Forum

Software => Software => Topic started by: BANE on October 16, 2010, 09:27:38 AM

Title: arc cos function for avr studio 4
Post by: BANE on October 16, 2010, 09:27:38 AM
hello,
I just recently ordered an axon II and just finished downloading all the software will waiting for it to ship.  I need to create a inverse kinematics program that uses arc cosine, but after looking through webbotlib i can't find acos in it.  is there something additional that i need to download?

I searched acos and found this thread
http://www.societyofrobots.com/robotforum/index.php?topic=7516.0 (http://www.societyofrobots.com/robotforum/index.php?topic=7516.0)

being that i got the most recent avr studio does that mean i already have that function and perhaps the webbotlib hasn't been updated?
Title: Re: arc cos function for avr studio 4
Post by: knossos on October 16, 2010, 10:09:48 AM
Webbotlib is a library for control of devices attached to your microcontroller.  It is not a general C library.  You would need to include the math library as well.
Title: Re: arc cos function for avr studio 4
Post by: knossos on October 16, 2010, 10:13:35 AM
Oh and if you weren't sure how to include the math library, add the following line
Code: [Select]
#include "math.h"to your code with all the other #includes.
Title: Re: arc cos function for avr studio 4
Post by: KurtEck on October 16, 2010, 11:53:19 AM
FYI:
With the current versions of Lynxmotion phoenix (Hex) code which are written in basic for the Basic Atom Pros, I know that for performance reasons the code was converted to use fixed point math for the IK/FK calculations.  To do that the code uses a look up table to compute the arc cosine.  Also uses tables for Cos/Sin as well...

I Used these same tables when I ported the phoenix code to run on an Arduino Pro...  More about that in the thread: http://www.lynxmotion.net/viewtopic.php?f=20&t=6730 (http://www.lynxmotion.net/viewtopic.php?f=20&t=6730)

Kurt
Title: Re: arc cos function for avr studio 4
Post by: macdad- on October 16, 2010, 12:49:12 PM
Look through the WinAVR library, it should have the math.h file.
Title: Re: arc cos function for avr studio 4
Post by: knossos on October 16, 2010, 05:39:39 PM
Current versions of WinAVR do include the math library.
Title: Re: arc cos function for avr studio 4
Post by: BANE on October 16, 2010, 10:17:09 PM
Thanks all,
i won't be getting my axon II till next month so i can't actually program or try any code in AVR studio:(.  After reading the comments you guy posted i realize that was a stupid question because i have used acos in ms visual basic before and didn't dawn on me that you could just include the math.h library in avr studio........duh......noob fail! thnx though 

@Kurt
i'm assuming you've seen my post at lynxmotion only i'm asking if how to do it in basic

Well, after doing some research on "tables" and looking at the phoenix code i created this code that spits out the table so i could graph it in excel to see how actuate it is.
i graphed a perfect acos and then what the table will give.  it looks close enough but i will try to get the shape a little better.
After reading some of the comments in the phoenix code i don't understand the math behind getting more accuracy:(

Code: [Select]
acos var word
counter var word
counter = 0
GetACos bytetable 255,254,252,251,250,249,247,246,245,243,242,241,240,238,237,236,234,233,232,231,229,228,227,225, |
224,223,221,220,219,217,216,215,214,212,211,210,208,207,206,204,203,201,200,199,197,196,195,193, |
192,190,189,188,186,185,183,182,181,179,178,176,175,173,172,170,169,167,166,164,163,161,160,158, |
157,155,154,152,150,149,147,146,144,142,141,139,137,135,134,132,130,128,127,125,123,121,119,117, |
115,113,111,109,107,105,103,101,98,96,94,92,89,87,84,81,79,76,73,73,73,72,72,72,71,71,71,70,70, |
70,70,69,69,69,68,68,68,67,67,67,66,66,66,65,65,65,64,64,64,63,63,63,62,62,62,61,61,61,60,60,59, |
59,59,58,58,58,57,57,57,56,56,55,55,55,54,54,53,53,53,52,52,51,51,51,50,50,49,49,48,48,47,47,47, |
46,46,45,45,44,44,43,43,42,42,41,41,40,40,39,39,38,37,37,36,36,35,34,34,33,33,32,31,31,30,29,28, |
28,27,26,25,24,23,23,23,23,22,22,22,22,21,21,21,21,20,20,20,19,19,19,19,18,18,18,17,17,17,17,16, |
16,16,15,15,15,14,14,13,13,13,12,12,11,11,10,10,9,9,8,7,6,6,5,3,0


do
Acos = GetACos(counter)
counter = counter + 1
serout s_out,i9600,[dec Acos,13]
while counter < 278
Title: Re: arc cos function for avr studio 4
Post by: KurtEck on October 17, 2010, 07:53:29 AM
Hi Bane,

I will not take the credit for writing the phoenix code.  I have done some of the things like, speed up code with assembly language, the DIY remote control work, XBee...  Please note: my math is a bit rusty...

I am pretty sure the arccos function is more accurate than what you are showing.  The actual code using the bytetable breaks the usage up into three different ranges and does some different scaling within those ranges (0-.9), (.9-.99), (.99-1).

I will answer more under your topic on Lynxmotion
Kurt

Edit: I put more up on the Lynxmotion site. I put up an edited version of your program that uses the actual basic function and instead of simply printing the table it instead does a for loop increment the value passed in to the arccos function by 20(easy to change the increment).  This should better smooth out your curve.

How to get more accuracy.  assuming this turns out to not be enough, you can do several things, depending on what you wish to trade off, speed or memory.  You could for example increase the number of table entries.  Or you could use some linear interpolation for values between entries. 
Title: Re: arc cos function for avr studio 4
Post by: airuno2l on November 13, 2010, 08:36:14 AM
Lookup table? Why not use a Taylor series expansion? It doesn't take up memory, and is probably more accurate. Wiki has the expansion for acos...http://en.wikipedia.org/wiki/Inverse_trigonometric_function#Infinite_series. (http://en.wikipedia.org/wiki/Inverse_trigonometric_function#Infinite_series.) I plotted the exact acos in black and the approximate in red. For the approximate, I only used the first three terms, but the more you add the more accurate it will get. (http://)