ATan2 in SPIN
JoeCreate
Posts: 36
I have been trying to impliment an atan2 function to determine the heading from my current x,y to a target x,y position.· It works using DynamicMathLib.CoordsToDegs(xcoord, ycoord, txcoord, tycoord) but I can't get it to work with Mike Green's ATAN2 function in Spin.
I'm attaching my TEST app which just outputs the results from the DynamicMathLib function and the Spin ATAN2 function.· I'm wondering if someone might look at it to see why the SPIN version of ATAN2 doesn't give the same results.
I don't want to use DynamicMathLib because of Memory.· The ONLY thing I need it for right now is Atan2.· I'm hoping to get this ATAN2 function working.· Orrrrr, maybe there is another way?
This is also a good Demo app of how to calculate new coordinates based on distance and angle travelled, and calculating the distance and angle from current position to a target coordinate.
Thanks for any info anyone can give.
Post Edited (JoeCreate) : 12/31/2007 6:19:41 AM GMT
I'm attaching my TEST app which just outputs the results from the DynamicMathLib function and the Spin ATAN2 function.· I'm wondering if someone might look at it to see why the SPIN version of ATAN2 doesn't give the same results.
I don't want to use DynamicMathLib because of Memory.· The ONLY thing I need it for right now is Atan2.· I'm hoping to get this ATAN2 function working.· Orrrrr, maybe there is another way?
This is also a good Demo app of how to calculate new coordinates based on distance and angle travelled, and calculating the distance and angle from current position to a target coordinate.
Thanks for any info anyone can give.
Post Edited (JoeCreate) : 12/31/2007 6:19:41 AM GMT
Comments
I think you should call ATAN2D - D for degrees! - in Dynamic Math Lib to do the same what Mike does...
I see your arguments with COGs (it needs 2, doesn't it?), but doing it with FLOATMATH is TERRIBLY slow...
Cog's isn't a problem, the problem is just RAM. I went from approximately 2200 free Longs to 600 after including DynamicMathLib. It's not worth the ram just for an ATAN2 function. Speed isn't really an issue either so it doesn't need to be in Assembly.
I just want a "simple" ATAN2 in Spin or Assembly that returns the same results as the standard C library atan2 function without having to include a whole object worth of functions that I won't use (if they got optimized out by the compiler, then it wouldn't be an issue). An ATAN2 function that doesn't use any COGS or DynamicMathLib functions would be ideal.
Is THIS...
result := d.CoordsToDegs(x1,y1,x2,y2)/10
if result<0
· result+=360
the same as THIS?...
result := atan2(x1-x2, y1-y2)
The first example returns the correct results, the heading in degrees (0..359) in which I should go to reach x2,y2 if I am at x1,y1.
How do I need to manipulate the results of this ATAN2 function (in the Spin source posted above) to get the same results as the first example above (which uses the DynamicMathLib CoordsToDegs function)?·
Post Edited (JoeCreate) : 12/31/2007 6:13:25 AM GMT
http://www.acroname.com/examples/10098/10098.html#e5
there are sin, cos and atan2 functions that use 16bits integer math.
The results for sin and cos are scaled up by 100 (so they return -100 to +100 to represent -1.0 to +1.0).
Since the propeller uses 32bit integers you can scale them for some more decimal digits.
regards peter
More or less, yes! But I do not know why you devide by 10?
Also, I should compute
atan2(x2-x1, y2-y1)
But that would be a sign change only.
I said "modify Mike's atan2" as you saifd you need radians (".. as in C...")