Since the standard PID control algorithm is far from ideal for many types of con- trols, software engineers have produced an almost endless array of variations on the basic theme. In most cases, these modifications amount to replacing the gain con- stants we have been discussing with gain tables or simple functions.
Amp.
Amp.
Output Command +
Amp.
P Gain
- +
Reading
Rabbit
Amp. Integ. I Gain
Set-Point Gain
+
Error Signal
++
Differ.
Rabbit D Gain
Figure 5.4. Hybrid predictive and reactive control
Asymmetric gains
One of the most common problems encountered is that many systems are asymmet- ric in their response to a power command. For example, a heater may be adequately powerful to quickly increase the temperature of a mass with say 30% power, but when the same amount of power is removed, the temperature may not drop nearly as fast as it had increased. This asymmetric response is obviously due to the fact that cooling is not merely the result of removing power, but rather the result of air con- vection.
The drive power requirement for a heavy robot is also very asymmetric. While a good deal of forward power is usually required to accelerate the robot, only a small amount of reverse power is required to decelerate it at the same rate. If a symmetric PID is used to control such a motor, it may be impossible to obtain adequate forward responsiveness without causing the robot to summersault when deceleration occurs.
Things get even more exciting when a robot goes over the top of a ramp and starts down the back side.
For asymmetric loads, it is often useful to provide two gains for each term. One gain is used for positive term inputs, while the other is used for negative inputs.
Error band limits
Another useful modification to reactive PID terms is to limit the error range over which they respond proportionally. If the error is within this band, then the error is multiplied by the gain, otherwise the appropriate error limit is used. This is particu- larly useful with the integral term to prevent wind-up. The example below is coded in Visual Basic, and utilizes many of the techniques discussed thus far.
In all these cases, you will be governed by the principal of enlightened instinct. To become a Zen master of such techniques, you must understand the causes of prob- lems, and the basics of physics and dynamics, but in the end the solution is often half theory and half instinct. If the choice is made to be rigidly mathematical in the solution, you may still end up guessing at a lot of parameters, and producing an algorithm that sucks every available cycle out of your control computer.
My personal preference for creating such controls is to design the software so that virtually every factor can be changed on the fly during operation. You can then run the system and observe its response to changes in various parameters. For example, the robot can be set to drive backward and forward between two points while vari-
Figure 5.5 shows typical code for a thermal PID control using many of the terms and tricks just discussed.
‘Calculate the PID for a single control using error proportional,
‘error integral, rabbit, and rabbit derivative terms. Error and
‘rabbit derivative gains are non-symmetric.
‘The process is controlled by an array of singles called
‘“ControlSingles”. Each term also has a limit band on its error.
‘If the error is greater than the band, then the band value is
‘substituted for the limit.
‘Routine returns the power command as an integer between 0 and 9999.
Public Static Function DoPIDs(TempRabbit As Single, Temp As Single) As Integer Dim Error As Single ‘Raw error
Dim LimError As Single ‘Error or limit, whichever is smaller.
Dim OutputAccum As Long
Dim PGain As Long ‘Proportional command Gain (0 to 9999) Dim DGain As Long ‘Derivative command Gain
Dim IGain As Long ‘Integral command Gain
Dim RGain As Long ‘Setpoint rabbit command Gain Dim PTerm As Single
Dim ITerm As Single Dim DTerm As Single Dim RTerm As Single Dim RabbitDeriv As Single Dim LastTempRabbit As Single
Dim IntegralHold(MaxZones) As Integer ‘0=No Hold, 1=allow pos. only, -1=allow neg.
On Error Resume Next
‘Calculate the error and rabbit derivative.
Error = TempRabbit - Temp
RabbitDeriv = TempRabbit - LastTempRabbit LastTempRabbit = TempRabbit
‘Get the Rabbit and error gains
RGain = ControlSingles(RabbitGain) ‘Rabbit gain is always positive.
‘Some gains depend on error polarity
If Error >= 0 Then ‘For positive errors use positive gains PGain = ControlSingles(PposGain)
IGain = ControlSingles(IposGain) Else
PGain = ControlSingles(PnegGain) IGain = ControlSingles(InegGain) End If
‘
‘Since there is no error derivative term, we will use
‘Dgain to mean the rabbit derivative gain. Its gain
‘depends on the polarity of the rabbit derivative.
If RabbitDeriv > 0 Then
DGain = ControlSingles(DposGain) Else
DGain = ControlSingles(DnegGain) End If
‘Now do the calculation for each term.
‘First limit the error to the band for each gain If Error > ControlSingles(PBand) Then
LimError = ControlSingles(PBand) ElseIf Error < -ControlSingles(PBand) Then
LimError = -ControlSingles(PBand) Else
LimError = Error End If
PTerm = CDbl((PGain * LimError) / 100) If Error > ControlSingles(IBand) Then
LimError = ControlSingles(IBand) ElseIf Error < -ControlSingles(IBand) Then
LimError = -ControlSingles(IBand) Else
LimError = Error End If
‘The I term is cumulative, so it’s gain range is 1/100th that of P.
‘Integral is bled off while rabbit is moving, or if the output
‘accumulator has gone below zero with a negative integral or over
‘full range with a positive integral.
If Abs(RabbitDeriv) < ControlSingles(AttackRate) / 30 Then If (LimError > 0 And IntegralHold >= 0) Or _
(LimError < 0 And IntegralHold <= 0) Then
ITerm = LimitTerm(ITerm + ((IGain * LimError) / 10000)) End If
Else ‘Bleed off the i term.
ITerm = 0.99 * ITerm End If
DTerm = LimitTerm(RabbitDeriv * DGain * 10) RTerm = LimitTerm(TempRabbit * (RGain) / 500) OutputAccum = PTerm + ITerm + DTerm + RTerm
‘Limit the output accumulator and flag the integrator
‘if the output goes out of range. (In a properly tuned
‘control, this should never happen).
If OutputAccum > 9999 Then OutputAccum = 9999
IntegralHold = -1 ‘Allow only down integration ElseIf OutputAccum < 0 Then
OutputAccum = 0
IntegralHold = 1 ‘Allow only upward integration Else
IntegralHold = 0 ‘Allow both directions.
End If
DoPIDs = CInt(OutputAccum) End Function
‘---
‘Prevent Terms from exceeding integer range for logging.
Private Function LimitTerm(Term As Single) If Term > 9999 Then
LimitTerm = 9999 ElseIf Term < -9999 Then
LimitTerm = -9999 Else
LimitTerm = Term End If
End Function
Figure 5.5. A band-limited hybrid control with integral hold-off