Shop OBEX P1 Docs P2 Docs Learn Events
Floating point conversion, How I do — Parallax Forums

Floating point conversion, How I do

micman2micman2 Posts: 18
edited 2009-05-14 13:20 in Propeller 1
Hi all,

i've a question:
I've a PID algorithmic ,



  encL := Enco.ReadEncoderLeft
  cur_posL := encL -  prevVL    
  prevVL := encL  
  
  cur_errorL := set_posL - cur_posL    

  PL := f.FMul(f.FFloat(cur_errorL),constKp)                            
  IL := f.FAdd(f.FFloat(IL),(f.FMul(f.FMul(f.FFloat(cur_errorL),cstdt),constKi)))
  eL := cur_errorL - pre_errorL
  DL := f.FDiv(f.FMul(f.FFloat(eL),constKd),cstdt)

  outputL := f.FAdd(f.FAdd(PL,IL),DL)





the result "outputL " is floating point algorithmic , The result is floating point ? how to convert in long ?


The next code limit -100 < outputL < 100 and send in PWM



  if outputL > 100
     outputL := 100

  if outputL < -100
     outputL := -100

  PWMg.setpwmDuty1(outputL,frequenzaPWM)






The constant is:

    constKp = 1.0
    constKi = 0.0
    constKd = 0.0        




work but When I change in :

    constKp = 1.2     <------
    constKi = 0.0
    constKd = 0.0        




1.2 (.2) (.1;.3;.4 ecc ecc ) send in over limit the "outputL"

Why?

Comments

  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2009-05-13 11:40
    To turn a floating point number into an integer you need to use either f.FTrunc or f.FRound. Their both in the float32 object. You would use them like this
     outputL := f.FRound(f.FAdd(f.FAdd(PL,IL),DL))
    
    


    and outputL will now be a normal integer smile.gif.
  • micman2micman2 Posts: 18
    edited 2009-05-13 11:53
    Thanks you!


         cur_errorL := 5
         PL := f.FMul(f.FFloat(cur_errorL),f.FFloat(2))                             
         debugLout := f.FRound(PL)
    
    



    debugLout = 10

    The result is Ok ! ( I see with viewPort v4.1.504)

    But when I change constKp with 1.8

    constKp = 1.8

         cur_errorL := 5
         PL := f.FMul(f.FFloat(cur_errorL),f.FFloat(1.8))                             
         debugLout := f.FRound(PL)
    
    






    The result is wrong
    debugLout = 1 !!!!!! Why???

    Post Edited (micman2) : 5/13/2009 12:01:48 PM GMT
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2009-05-13 12:10
    Could you please post the code thats producing the debugLout=1? It's a little hard to follow whats happening without it.
  • micman2micman2 Posts: 18
    edited 2009-05-13 13:04
    Send source code
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2009-05-14 07:59
    micman2 said...
         cur_errorL := 5
         PL := f.FMul(f.FFloat(cur_errorL),f.FFloat(1.8))                             
         debugLout := f.FRound(PL)
    
    


    The answer is quite simple, in fact, so simple that it took me awhile to see it smile.gif. You have entered cur_errorL as a floating point constant. However, you then try and convert it to a floating point as if it were an integer. Change the code above to this
         cur_errorL := 5
         PL := f.FMul(cur_errorL,1.8)                             
         debugLout := f.FRound(PL)
    
    


    and you should have more luck.
  • micman2micman2 Posts: 18
    edited 2009-05-14 11:21
    thanks you very mach!!! smile.gifsmile.gif
  • Mike GreenMike Green Posts: 23,101
    edited 2009-05-14 13:20
    You have to remember the ".0" otherwise the constant is an integer, not floating point:
    cur_errorL := 5.0
    PL := f.FMul(cur_errorL,1.8)
    debugLout := f.FRound(PL)
    
Sign In or Register to comment.