Shop OBEX P1 Docs P2 Docs Learn Events
Can somebody help me make this Float! — Parallax Forums

Can somebody help me make this Float!

Sniper KingSniper King Posts: 221
edited 2008-08-20 22:24 in Propeller 1
This is an enhanced PID loop for autonomous control stuff.· It would be nice if the whole thing worked in the realm of floating point math to make it much more accurate.· I need to see how to make my variables into floating math and then use them in the code I have supplied.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··


Michael King
Application Engineer
R&D
Digital Technology Group

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-08-19 23:35
    Have you read the documentation that comes with the floating point library object?· That has all sorts of examples of how to do floating point.· Keep in mind that the Spin compiler supports floating point constant expressions, but will not compile code to call the floating point library.· You have to do that yourself as shown in the floating point library documentation.
  • DavidGregDavidGreg Posts: 38
    edited 2008-08-20 00:05
    You can see my PID object for an example of how to floating point math

    http://forums.parallax.com/forums/default.aspx?f=25&m=277320&g=277621#m277621
  • Sniper KingSniper King Posts: 221
    edited 2008-08-20 15:31
    My problem is not knowing how to assign a variable. For instance. I would like to assign a:=3.14159265 but this is not directly possible. Now I know I can do a string2float on this. But what if this is a value coming out of a sensor like an ADC? Then I would have a long value tha I need to calculate. How then do I change that to a Float in order to do some math on it?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group
  • Harrison.Harrison. Posts: 484
    edited 2008-08-20 16:44
    How do you represent your floating point numbers? The float math stuff uses IEEE 754 floating point format. So you can either convert the number yourself, or use some simple math to convert your integer number to floating point.

    Let's say your number is '1234'. You could do:
    OBJ
      fp : "FloatMath"
    
    PUB main | myint, myfloat
      myint := 1234 ' integer
      myfloat := fp.FFloat(myint) ' converts your integer into a IEEE 754 fp number
    
    



    Now let's say your stuff was in a fixed point format. Something like '12.34' represented as '1234'.
    OBJ
      fp : "FloatMath"
    
    PUB main | myint, myfloat
      myint := 1234 ' 12.34 fixed point number
      myfloat := fp.FDiv(fp.FFloat(myint), 100.0) ' converts your fixed point into a IEEE 754 fp number
    
    



    Remember that the spin compiler will automatically convert floating point constants to IEEE 754 format.
  • Sniper KingSniper King Posts: 221
    edited 2008-08-20 16:59
    That is cool and thank you.· that does help.· What if I start with a floating point value, How can I make if float?

    Can I say...
    OBJ
      fp : "FloatMath"
    
    PUB main | myint, myfloat
      myint := 1234 ' 12.34 fixed point number
      myfloat := fp.FFloat(32.5) ' converts your fixed point into a IEEE 754 fp number
    
     
    


    fp.FFloat(32.5)·is the changed value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D
    Digital Technology Group

    Post Edited (Sniper King) : 8/20/2008 5:05:11 PM GMT
  • M. K. BorriM. K. Borri Posts: 279
    edited 2008-08-20 19:06
    I did this if you have a use for it [noparse]:)[/noparse] This was run on a tank, airplane and 3 boats so far. Especially note the dynamic math library, it does geodesy for you and other things. You're welcome to recycle any part of my code just let me know what you're doing with it because if it's fun I'd like to know! [noparse]:D[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • Harrison.Harrison. Posts: 484
    edited 2008-08-20 21:16
    Sniper King said...
    That is cool and thank you. that does help. What if I start with a floating point value, How can I make if float?

    Just do 'myfloat := 23.5'. The spin compiler will automatically convert all constant floats to the correct format. If you want to force the compiler to do some constant float evaluation you can use float().

    OBJ
      fp : "FloatMath"
    
    PUB main | myfloat1, myfloat2
      myfloat1 := 12.34   ' the compiler will automatically convert 12.34 to a IEEE 754 float representation
      myfloat2 := float(12.34 * 1.23 / 4.23)   ' force the compiler to do compile time float evaluation.  you cannot use variables inside of float(...)
    
    
  • M. K. BorriM. K. Borri Posts: 279
    edited 2008-08-20 21:33
    My bad, thought he wanted a math function not a constant... sorry for the spam

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://forums.parallax.com/showthread.php?p=650217

    meow, i have my own topic now? (sorta)
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-08-20 22:24
    Sniper King said...
    I would like to assign a:=3.14159265 but this is not directly possible.
    It is possible: a := pi. PI is a reserved word which assigns the floating point equivalent of 3.14...

    Theres no way of getting around it, other than assigning floating point constants, you must include some form of floating point library. The Propeller does not have the capability of natively handling floating point mathmatical operations.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 8/20/2008 10:29:52 PM GMT
Sign In or Register to comment.