F Gaddy
F Gaddy
by GARTH GADDY
How to Write a
PID Algorithm
The tricky part is trying to build
a PID that operates smoothly in
all modes of operation.
hen you hear of examples can be easily converted to C,
point of the extruder screw servo 1994). The book covered writing a
motor control loop. My PID is written generic PID loop and also included
in the PMAC motion control language, some methods of bumpless transfer. I
which is similar to Basic, and the code put together a basic PID and had the
A
s its name implies, a PID con-
(Last_SP)
sists of three components or ■ Controlled Variable (CV)
terms: proportional, integral, ■ Manual RPM Setpoint
and derivative. The proportional term (Manual_Command)
is a product of error and proportional ■ PID Execution Stage
gain. Error is calculated by finding the (First_PID_Execution)
T
he proportional term is so
Equation 2 is the best choice. Let’s do named because its output is Pterm = Kp * Error
a what-if to make sure. If the tank were proportional to error. It
too low, it would cause negative error, responds to instantaneous error, error CV = Pterm
T
he integral term is so named
Those of you with a perceptive eye because its output is the integral If an error persisted that was not cor-
might have noticed something that did- of the error with respect to time. rected by the proportional term and the
n’t seem like it would work: units. Its job is to remove long term or steady KI is greater than zero, the integral
How do you handle dissimilar input state error that is not removed by the term would begin to grow larger and
and output units? My pressure PID SP Proportional Term. As you may larger eventually correcting or “reset-
and PV units are in psi, while the CV is remember from calculus, the first inte- ting” the long term error.
in RPM. The process of converting the gral of an equation of a line is equal to
units is called normalization and is the area under the line. In Figure 1, the DERIVATIVE TERM
handled by setting up a ratio of the line shows error over time and the area
T
he derivative term is meant to
input and output spans, and multiply- under the line is the integral of the slow down changes in the
ing the result by the variable you want error. We calculate the integral by process being controlled. This
to convert (error in this case). The final adding the error to itself each time the slowing is done by calculating the
Pterm code fragment is as follows: PID executes. Integral term code frag- changes in the process or the “deriva-
ment in an integral-only controller tive” of the process with respect to
Error = (SP - PV) * (CV_Span / PV_Span) looks like this: time. Two indicators of process change
are available, PV and error. Derivative
or: Error = (SP - PV) * (CV_Span / PV_Span) Term code fragment is as follows:
LISTING 1
Screw servo motion program.
Error = (SP - PV) * (CV_Span/PV_Span)
01 While (True) d_PV = (Last_PV - PV) * (CV_Span/PV_Span)
02 If (Auto_Mode = 1 ) Dterm = KD * d_PV
03 If (First_PID_Execution = 0 OR First_PID_Execution = 1) Last_PV = PV
04 Error = (SP - PV) * (CV_Span / PV_Span) CV = Dterm
05 Sum_Error = Sum_Error + Error
06 If (First_PID_Execution = 0)
;Error Method
07 Sum_Error = Manual_Command / KI
08 First_PID_Execution = 1
Error = (SP - PV) * (CV_Span/PV_Span)
09 Initial_Error = Error d_Error = Last_Error - Error
10 Endif Dterm = KD * d_Error
11 Pterm = 0 Last_Error = Error
12 Iterm = KI * Sum_Error CV = Dterm
13 Dterm = 0
14 If (Initial_Error > 0 and Error < 0) As you can see, if the PV method is
15 First_PID_Execution = 2 used, the normalization code must be
16 Last_PV = PV added in. If the error method is used,
17 Endif normalization has already been done
18 If (Initial_Error < 0 and Error > 0)
in the error calculation. The output of
19 First_PID_Execution = 2
20 Last_PV = PV
the derivative term will be large if KD
21 Endif is greater than zero and there is a large
22 Last_SP = SP change in either PV or error. Changing
23 Else the derivative gain will change the
24 If (SP = Last_SP) effect that the Dterm has on the final
25 Error = (SP - PV) * (CV_Span / PV_Span) output.
26 Pterm = KP * Error
27 Sum_Error = Sum_Error + Error PUTTING IT ALL TOGETHER
28 Iterm = KI * Sum_Error
I
n a complete PID algorithm, all
29 d_PV = (Last_PV - PV) * (CV_Span / PV_Span) three terms are put together. The
30 Last_PV = PV
code fragment that follows is a for-
31 Dterm = KD * d_PV
32 Else
ward acting, PV based Dterm PID:
33 Error = (SP - PV) * (CV_Span / PV_Span)
34 Sum_Error = Sum_Error + Error Error = (SP - PV) * (CV_Span / PV_Span)
35 Pterm = 0 Pterm = KP * Error
36 Iterm = KI * Sum_Error Sum_Error = Sum_Error + Error
37 Dterm = 0 Iterm = KI * Sum_Error
38 If (SP > Last_SP and PV > SP) d_PV = (Last_PV - PV) * (CV_Span / PV_Span)
39 Last_SP = SP Last_PV = PV
40 Last_PV = PV Dterm = KD * d_PV
41 Endif CV = Pterm + Iterm + Dterm
42 If (SP < Last_SP and PV < SP)
43 Last_SP = SP
44 Last_PV = PV
So that’s it. How to write a PID. Go
45 Endif to it and good luck. But wait, will this
46 Endif thing really work? You know, in the
47 Endif real world? Well, yes and no. This PID
48 CV = Pterm + Iterm + Dterm would work but not transition from a
49 Else manually controlled process to a PID-
50 CV = Manual_Command controlled process very gracefully. It
51 First_PID_Execution = 0 would also not handle setpoint changes
52 Endif very well. So let’s take a look at those
53 EndWhile two issues. Because the remaining
code will have more features, refer to
Listing 1 in order to follow along in the
text.
B
umpless transfer is the act of Line 22
taking a process from manual- ■ The Auto subsection 2 is the normal
ly-controlled to PID-controlled operation section from Line 24 to
without any noticeable upset or Line 46
“bump” in the process. I will use my ■ Within the normal operation sec-
extruder pressure control PID for the tion, there is one more set of sub-
next examples in order to drive home sections
this concept. ■ Normal operation subsection 1 is
Let’s say my extruder screw is run- PID control from Line 25 to Line 31
ning manually controlled at 15 RPM. If ■ Normal operation subsection 2 is
I were to use the above code alone and for setpoint changes from Line 33 to
put the screw in auto (PID loop on), the Line 45
output of the loop on the first execution
would go from 15 RPM in manual to In order to understand how the
something barely above zero, based on Listing 1 PID works, let’s walk
the pressure error at the instant the loop through a typical scenario of startup
executed. The output would then head and transition into Auto or PID mode.
off towards the setpoint at a rate depen- Prior to start, the operator makes sure
dent upon gain values. Not very bump- the machine is set in manual mode and
less. In order to make the transition then enables the screw servo loop. This
bumpless, I need to somehow have the action executes the Listing 1 code
output of the PID loop equal the man- within the infinite loop (Lines 01 to
ual output at the instant of manual to 53). The operator then enters a
auto switchover. I do this by “pre-load- Manual_Command (RPM) on the operator
ing” the Iterm with the manual output interface and the screw begins turning
command on the first execution of the at that RPM. The only statements that
PID. The loop is then kept in an inte- are performing any servo control at
gral-only mode until the PV meets the this point are Lines 50 and 51. These
SP, and then it is switched into PID lines keep the servo at its manual set-
mode. point and reset the First_PID_Execution
Smoothing out setpoint changes will flag. When polymer pressure is up to
allow the operator to make large set- desired level, the operator places the
point changes without upsetting the screw in Auto (PID mode). From here
process. This outcome is often on the only control code executed will
achieved with a “ramped setpoint.” I be between Lines 02 and 49. On first
chose a method which resulted in less scan in PID mode, Line 07 sets up the
code by switching the controller to an Iterm “pre-load”, Line 08 sets the
integral-only mode until the process First_PID_Execution flag so these three
reaches the setpoint. The controller is lines of code will only run once, and
then switched back into PID mode. Line 09 sets an Initial_Error variable
Let me say a few words about to the error. This will be used in con-
Listing 1: junction with Lines 14 to 22 to gently
bring the extruder pressure up to set-
■ The PID is set in an infinite loop point. The next three lines of code set
between Line 01 and Line 53 the three PID terms and you’ll notice
■ It is then broken down into two that the Pterm and Dterm are set to
modes, auto and manual, which are zero. This leaves an I-only controller
determined by the If-Then-Else whose Iterm will slowly (gently,
statement starting on Line 02, Else bumplessly) get larger or smaller
at Line 49, and ending at Line 52 depending on the error until it forces
■ The Auto section is from Line 03 to the output to a point where the PV
Line 48 and has two subsections passes the SP. This event is picked up
SMOOTH OPERATOR
A
s you can see, PID code alone
is fairly simple. The trickier
part comes in when trying to
build a PID that operates smoothly in
all modes of operation. Many ways
exist to accomplish PID control, bump-
less transfer, and handle setpoint
changes. Other methods that could and
probably should be tried are variable
gains and ramped setpoint. Each
method must be weighed against the
type of PID algorithm used. What
worked on a parallel algorithm may
not work well on an ISA PID.