Shop OBEX P1 Docs P2 Docs Learn Events
PID returns integer? — Parallax Forums

PID returns integer?

TJHJTJHJ Posts: 243
edited 2008-10-24 17:33 in Propeller 1
So I keep looking at this code from obex, for a pid loop.
{{
                PID Controller
                  Code by David C. Gregory ([url=mailto:david.c.gregory@gmail.com]david.c.gregory@gmail.com[/url])
                  Inspiration by "PID without a PhD" by Tim Wescott, 2000
                  This is an elegant PID controller object.  It has only two methods
                                set - used to set the gain characteristics of the controller
                                update - used to give the controller new information and get a new output
                  Basic controller tuning is described by Wescott, [url=http://www.embedded.com/2000/0010/0010feat3.htm]http://www.embedded.com/2000/0010/0010feat3.htm[/url]
}}
obj
  fMath         : "FloatMath"  
var
  long pGain          'Proportional Gain
  long iGain          'Integral Gain
  long dGain          'Differential Gain    
  long iMin           'Minimum Integral Windup
  long iMax           'Maximum Integral Windup
  long iState         'Integral State
  long dState         'Previous position, used for calculating rate of change of position 
pub set(_pGain,_iGain,_dGain,_iMin,_iMax)
{{ Initiallizes PID controller.  Also usefull for modifying
    controller characteristics during runtime. }}
  pGain := _pGain
  iGain := _iGain
  dGain := _dGain
  iMin := _iMin
  iMax := _iMax
  iState := 0.00
  dState := 0.00
pub update(setpoint, position) | pTerm, iTerm, dTerm, error
'' Updates the controller with new information.
''  Returns the new output setting as an integer.
  error := fMath.Fsub(setpoint,position)
  pTerm  := fMath.Fmul(error, pGain)
  iState := iMin #> (fMath.Fadd(iState,error)) <# iMax
  iTerm := fMath.Fmul(iState,iGain)
  dTerm  := fMath.Fmul(dGain,fMath.Fsub(position,dState))
  dState := position  
  return(fMath.Fsub(fMath.Fadd(pTerm,iTerm),dTerm))

In the comments it says returns an integer. But in my mind it would seem to return a float number. Am I missing something, is it using a different version of floatmath than I have? Along with this doesnt setpoint and position need to be floats in order for the code to work correctly?

I hope it is not inappropiate to post the whole code like this, if so please remove it or tell me and I will.

Thanks for the time.
TJ

Post Edited (TJHJ) : 10/24/2008 5:30:26 PM GMT

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-10-24 17:33
    It appears that you are correct, it returns a floating point number from the formula P + I - D

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

    Parallax, Inc.
Sign In or Register to comment.