Shop OBEX P1 Docs P2 Docs Learn Events
ATan2 in SPIN — Parallax Forums

ATan2 in SPIN

JoeCreateJoeCreate Posts: 36
edited 2007-12-31 21:18 in Propeller 1
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

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-12-31 03:33
    Both use the same Taylor series...

    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...
  • JoeCreateJoeCreate Posts: 36
    edited 2007-12-31 04:39
    I don't want to use DynamicMathLib at all. Just including it uses up all of my remaining memory (even had to remove some other stuff to make it fit).

    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.
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-31 04:57
    Then you have to modify Mike's routine to return the radians rather than the degrees.
  • JoeCreateJoeCreate Posts: 36
    edited 2007-12-31 06:08
    I did notice he was returning degrees in his routine, which is what I wanted, which was convenient. I was converting the C atan2 results into degrees.

    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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2007-12-31 06:59
    On this page
    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
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-31 20:20
    JoeCreate said...
    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)

    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...")
  • deSilvadeSilva Posts: 2,967
    edited 2007-12-31 20:25
    @Peter: They use a VERY unprecise approximation. One could do it MUCH better on a 32 bit machine...
  • JoeCreateJoeCreate Posts: 36
    edited 2007-12-31 21:18
    I do need Degrees, not radian, so I shouldn't need to modify Mikes atan2. I divide by 10 since the CoordsToDegs function returns 0.5 degrees as 5 so I divide by 10 to make it a 0..359 value instead of 0..3599.
Sign In or Register to comment.