Skip to content

Instantly share code, notes, and snippets.

@brooksware2000
Created August 7, 2012 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brooksware2000/3280748 to your computer and use it in GitHub Desktop.
Save brooksware2000/3280748 to your computer and use it in GitHub Desktop.
Routines to compute PID value and regulate heater controls
'-------------------------------------------------------------------------------
' Regulates heater controls based on phase temperature parameters
'-------------------------------------------------------------------------------
PROCESS_PHASE:
GOSUB READ_TEMPERATURE
GOSUB COMPUTE_PID
WHILE (elapsedTime <> 0)
IF (SecondsChanged = 1) THEN
elapsedTime = elapsedTime - 1
GOSUB READ_TEMPERATURE
GOSUB COMPUTE_PID
TimeOn = (cycleTime * OutVal)/100
Tvar = Tvar + 1
IF (Tvar >= outMax) THEN Tvar = outMin
IF (Tvar < TimeOn AND curTemp < setpoint) THEN
PORTPIN = 1 : PINSTATE = 1 : GOSUB SET_PIN
ELSE
PORTPIN = 1 : PINSTATE = 0 : GOSUB SET_PIN
ENDIF
GOSUB DISPLAY_STATUS
SecondsChanged = 0
ENDIF
WEND
RETURN
'-------------------------------------------------------------------------------
' Computes the output drive from the individual PID components.
'-------------------------------------------------------------------------------
COMPUTE_PID:
errorCurrent = setpoint - curTemp
pTerm = Kp * errorCurrent
iTerm = iTerm + (Ki * errorCurrent)
IF (iterm > outMax) THEN iTerm = outMax
IF (iTerm < outMin) THEN iTerm = outMin
dTerm = (curTemp - lastMeasured)
OutVal = (pTerm + (iTerm - (Kd * dTerm)))
lastMeasured = curTemp
RETURN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment