Shop OBEX P1 Docs P2 Docs Learn Events
Best way to us atan or atan2 — Parallax Forums

Best way to us atan or atan2

electric550electric550 Posts: 122
edited 2009-07-31 04:53 in Propeller 1
Is there a math library that allows for use of integers for the atan2 math function. The only math library that I can find that supports atan2 uses floating point. The problem is that I am getting data then I want to use the atan2 method and send out the value in degrees from the serial port but the value is in floating point so when I send the data out the serial port using the decimal format the result is a large number because of the formating. Thanks!

Comments

  • electric550electric550 Posts: 122
    edited 2009-07-31 03:20
    I was looking for a short function with lookup table of some sort perhaps, to speed things up. I could write one but I just wanted to see if there was a quick way to do this all ready written.
  • TimmooreTimmoore Posts: 1,031
    edited 2009-07-31 03:34
    This thread talks about an int math lib http://forums.parallax.com/forums/default.aspx?f=25&m=371538 but it doesn't loko like its ready.
    There are a couple of other options - multiple the float by 100 and convert to int and send that, it gives you a 2 deciaml place number - you can do it to other no of places as well.
    Look at FloatString.spin it allows to to convert a float to a string and control the format - no of places etc.
  • electric550electric550 Posts: 122
    edited 2009-07-31 03:37
    I tried to do the following but it is returning a zero
    when the code runs an example of the two variables would be X = 315 Y = -375

    On my calculator this returns -0.6986 in radians so multiply by 180/pi = -40.030 but here it is just outputing zero.
    Is it posible to use these two libraries together or is this a problem?


    math : "DynamicMathLib"
    floatm : "FloatMath"


    Z := math.ATan2(X,Y)
    Z := Z*180/3.14159
    Z := floatm.FRound(Z)
    uarts.dec(0,Z)
  • electric550electric550 Posts: 122
    edited 2009-07-31 03:38
    I will take a look at that
  • electric550electric550 Posts: 122
    edited 2009-07-31 04:05
    Well that did not work maybe I am doing something wrong I tried

    FloatString : "FloatString"
    math : "DynamicMathLib"

    Z := X/Y
    Z := math.ATan(Z)
    Z := Z*180/3.14159
    Z := FloatString.FloatToString(Z)
    uarts.str(0,Z)
    uarts.tx(0,10)
    uarts.tx(0,13)

    here is some data that si returned

    Mag XYZ: 47 -525 -673
    1.401298e-45
    Mag XYZ: 48 -528 -671
    1.401298e-45
    Mag XYZ: 52 -527 -669
    1.401298e-45
    Mag XYZ: 54 -535 -672
    1.401298e-45
    Mag XYZ: 58 -531 -669
    1.401298e-45

    It always returns either the small number shown above or 0 depending on orientation. So I am not sure what is going on at this point. I will look for something silly.
  • TimmooreTimmoore Posts: 1,031
    edited 2009-07-31 04:15
    Are X and Y floats? THe float libs will only take floats. Also the Z := Z*180/3.14159 is incorrect, you need to use float routines for * and /
    e.g. Z:= math.FMul(math.FDiv(180,3.14159))
    This also works (from some of my code - looks like its doing similar)
         a2tan_angle[noparse][[/noparse]Pitch] := fMath.FMul(t.ATan2(tiltReadings[noparse][[/noparse]tyAxis], tiltReadings[noparse][[/noparse]tzAxis]), constant(180.0 / pi)) 'use accelerometer to calculate pitch tilt    
    
    
  • electric550electric550 Posts: 122
    edited 2009-07-31 04:53
    Yea you are right that fixed it...Thanks A bunch! i did not realize that the float and ints were like that...Thanks again.
Sign In or Register to comment.