Society of Robots - Robot Forum

Software => Software => Topic started by: Liana on June 13, 2007, 05:10:48 PM

Title: Arduino programming
Post by: Liana on June 13, 2007, 05:10:48 PM
I will be using an IR sensor and am told that the readings naturally come back in polar coordinates.  I would like to be able to change these polar coordinates into cartesian coordinates.  Does anyone know how I would write this using the Arduino software?  (The Arduino language is based on C/C++)

http://www.arduino.cc/
Title: Re: Arduino programming
Post by: rgcustodio on June 13, 2007, 06:12:57 PM
Conversion is easy, see here:
http://www.mathsisfun.com/polar-cartesian-coordinates.html (http://www.mathsisfun.com/polar-cartesian-coordinates.html)

Converting the mathematical terms to C is also easy because the math functins (sine, cosine, atan) are available in avr-libc. Its almost a direct replacement for the terms and functions.

Do note however that linking with the math library would probably bloat your code, to a point where it won't fit in the AVR's flash. Don't expect it to be fast either. If these becomes a problem you can implement a LUT for approximating the trigonometric values:
http://www.societyofrobots.com/programming_trigtable.shtml (http://www.societyofrobots.com/programming_trigtable.shtml)
Title: Re: Arduino programming
Post by: Admin on June 15, 2007, 08:53:51 AM
Liana, that trig lookup code I wrote has up to a 10 degree error in it (acceptable but could be better).

I should be writing up a better one soon for AVR so I can do highly accurate super fast positioning for the robot arm on my newest robot (http://www.societyofrobots.com/robotforum/index.php?topic=1218.0).

Ill post the trig code when its done.
Title: Re: Arduino programming
Post by: Liana on June 18, 2007, 11:58:18 AM
Thanks to both of you.  Do you know when you'll be posting the new trig code?
Title: Re: Arduino programming
Post by: Admin on June 18, 2007, 07:17:35 PM
hmmm it may be awhile before I get my robot working enough that I can verify my code, but Im fairly sure I didnt make any mistakes. I listed it below if you are willing to beta test it.

To use it, just call cos_SoR(20) or sin_SoR(172) and it will return the answer multiplied by 100 (to avoid floating point math).

For example, if you wanted cos_SoR(20), it will return 94 (its really .94)

You can also do tan_SoR(degrees) which will give an answer multiplied by 10.

Let me know how it goes.


Code: [Select]
//***********TRIG LOOKUP TABLES**********
//returns a trig sin or cos calculation value multiplied by 100 (to avoid floating point math)
//returns a trig tan calculation value multiplied by 10 (to avoid floating point math)
//only allows for angles between 0 and 360 degrees

//multiplied by 100 so no floating point math
signed int angtable[73]={100,100,98,97,94,91,87,82,77,71,64,57,50,42,34,26,17,9,0,-9,-17,-26,-34,-42,-50,-57,-64,-71,-77,-82,-87,-91,-94,-97,-98,-100,
-100,-100,-98,-97,-94,-91,-87,-82,-77,-71,-64,-57,-50,-42,-34,-26,-17,-9,0,9,17,26,34,42,50,57,64,71,77,82,87,91,94,97,98,100,100};

signed int cos_SoR(long signed int degrees)//returns cos*100
{
if (degrees >= 0)//positive angles
return angtable[degrees/5];
else
return -angtable[72-(-degrees)/5];
}

signed int sin_SoR(long signed int degrees)//returns sin*100
{
degrees=degrees - 90;//phase shift 90 degrees

if (degrees >= 0)//positive angles
return angtable[degrees/5];
else
return -angtable[72-(-degrees)/5];
}

signed int tan_SoR(long signed int degrees)//returns tan * 10
{
//tan(x) = sin(x)/cos(x)
if (degrees == 90 || degrees == -90 || degrees == 270 || degrees == -270)//blows up
return 0;//what else should I return?!?!?
return sin_SoR(degrees)/cos_SoR(degrees)*10;
}

//***************************************