Software > Software

Arduino programming

(1/1)

Liana:
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/

rgcustodio:
Conversion is easy, see here:
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

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.

Ill post the trig code when its done.

Liana:
Thanks to both of you.  Do you know when you'll be posting the new trig code?

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: ---//***********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;
}

//***************************************
--- End code ---