0% found this document useful (0 votes)
66 views7 pages

F Gaddy

Embedded Systems Programming, May 1997 ESP Magazine Garth Gaddy, "How to Write a PID Algorithm" (May)

Uploaded by

spyeagle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views7 pages

F Gaddy

Embedded Systems Programming, May 1997 ESP Magazine Garth Gaddy, "How to Write a PID Algorithm" (May)

Uploaded by

spyeagle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

RETURN

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,

W people you know


having
angiogram or stents
put in a collapsed
artery, you realize these procedures
can involve a whole variety of medical
catheters of differing sizes. These
an
C++, or other languages.
Up until writing this algorithm, I
had always depended on PIDs supplied
with the control hardware/software
package I was using. Whether I was
working on a PLC, a motion controller,
or a PC-based control system, I was
catheters are manufactured by the only required to insert the PID into the
process of polymer extrusion. Quality control logic, run the program, and
control of the catheters demand a pre- tune the loop. As I said earlier, this is
cise outside diameter with tolerances an extruder pressure control loop
of +/-0.0005in. One method of main- whose output is cascaded into the set-
taining an accurate outside diameter is point of a screw servo loop. One option
to maintain accurate polymer pressure I had was to employ an unused axis of
inside the extruder. The need for pre- the 8-axis motion controller for this
cise pressure control is what brought pressure loop and cascade its output
me to the task of writing my own PID into the screw loop. This option would
(proportional-integral-differential) have wasted the motion control axis
algorithm. Those of you who have which might be needed later, so I
worked with PID controllers know that decided to write my own loop and
a wide variety of algorithms exist in embed it in the screw servo motion
the industry. The intent of this article is program. Each time the servo motion
not to cover all PID algorithms but to program executed, the pressure loop
show one implementation of a “home- would also execute and deliver a new
made” PID and how I managed some screw loop setpoint.
of the trickier aspects of PIDs, such as I started researching the subject and
bumpless transfer and setpoint found many books on the subject of
changes. PIDs. Most were heavy on theory and
The extrusion systems I work on use either light or weightless on practical
a Delta Tau Data Systems PMAC (pro- “show-me-the-code” kind of informa-
grammable multi-axis controller) to tion I was looking for. Control system
control a servo motor that drives the integrators I talked to knew how to
extrusion screw that in turn provides tune one but not how to write one. It
the polymer pressure. In order to con- was black magic. I then found a book
trol pressure, the PMAC A/D convert- on the basics called Real-Time
er reads the melt pressure and then Computer Control: An Introduction
passes data to my pressure PID whose (Stuart Bennett, Prentice Hall
output is then cascaded into the set- International, Hertfordshire, UK,
Skeleton Stoodios

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

62 EMBEDDED SYSTEMS PROGRAMMING MAY 1997


difference between the setpoint and the
Until writing this process variable. This can be done by
either:

algorithm, I had Error = SP - PV

always depended or:

on PIDs supplied Error = PV - SP

The method you select will deter-


with the control mine whether your control loop is a for-
ward or reverse acting PID (some call
hardware/ this either inc/inc or inc/dec). In other
words, as the output of the loop increas-
software package es, does the process variable go up or
go down? How your process responds
will determine which method you
I was using. choose. For example, I used the first
method above of calculating error in
folks at Delta Tau check my algorithm. my polymer pressure loop PID. An
They gave it a thumbs up but men- increase in the output of that loop
tioned that I might have difficulty (servo velocity command) turns the
going from manual to auto and back screw faster, which in turn yields a
without further code. I was so excited higher pressure. Another way to look at
to have something that was close to it is if there is positive error in Equation
implementation that I said I would fig- 2, the pressure is lower than the set-
ure it out and headed for the extrusion point. Positive error will always yield
lab. As soon as I ran some test numbers
through the algorithm I knew that they
ABBREVIATIONS
weren’t kidding. More code was need-
ed. I didn’t like the bumpless transfer
methods in the book, and after banging ■ Setpoint (SP)
■ Process Variable(PV)
my head against the extruder for a few ■ Proportional Gain (KP)
hours, I came up with the method ■ Integral Gain (KI)
described in the examples. ■ Derivative Gain (KD)
Many differing PID algorithms are ■ PV High Limit – PV Low Limit
used in industry. Each PLC manufac- (PV_Span)
■ CV High Limit – CV Low Limit
turer that offers a PID adds a custom (CV_Span)
twist to the algorithm. Each PC-based ■ PID or Manual Flag (Auto_Mode)
control system that has a PID also adds ■ Error (Error)
a custom twist. The only exception is ■ Sum of the Error (Sum_Error)
the classic ISA PID algorithm that ■ Change in Process Variable Since Last
PID Execution (d_PV)
conforms to a standard. For the pur- ■ Change in Error Since Last PID
poses of my examples below, I will be Execution (d_Error)
referring to the independent or parallel ■ Proportional Term (PTerm)
PID algorithm (which I used). See the ■ Integral Term (ITerm)
sidebar “Abbreviations” for a few def- ■ Derivative Term (DTerm)
■ Process Variable on Last PID Execution
initions of the abbreviations I use in the (Last_PV)
following text and code fragments. ■ Error on Last PID Execution (Last_Error)
■ Error when manual to auto
ERROR (Initial_Error)
■ Last Setpoint when setpoint changes

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)

MAY 1997 EMBEDDED SYSTEMS PROGRAMMING 63


PID Algorithm

an increase in the loop output (we’ll see FIGURE 1


Error over time.
that later), which, in this case, will turn
the screw faster and increase the pres-
sure, thus reducing the error. If I had
chosen the second method and there
had been any error, the loop would
have sent the screw velocity in the error
wrong direction (faster for high pres-
sure or slower for low pressure) and the
pressure would have “run away” in the time
direction of the error.
An example of using the method in
Equation 2 is a tank level control using which would then reduce the controller that is calculated on that one execution
a proportional valve to let liquid out as output, closing the valve, and bring the of the PID. If you had a P-only con-
an amount of liquid is being pumped level back up. If Equation 1 were cho- troller, with a Kp greater than zero, the
in. In this case, the PV is the actual sen, a low tank would cause positive controller’s output will increase as the
level and the SP is the desired level. error, open the valve more, and empty error increases and decrease as error
The output of the loop controls the per- the tank. decreases. The typical (simplified)
cent the discharge valve is open, from code fragment for a P-only controller
0% (fully closed) to 100% (fully open). PROPORTIONAL TERM is:
After a bit of thought it is apparent that

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

64 EMBEDDED SYSTEMS PROGRAMMING MAY 1997


PID Algorithm
As you can also see, the higher the ;Then Error = (PV - SP) * (CV_Span / PV_Span)
Kp the higher the output for a given Pterm = KP * Error ;Then
amount of error. That’s why a loop CV = Pterm Sum_Error = Sum_Error + Error
with a high proportional gain is more Iterm = KI * Sum_Error
responsive to error than one with a INTEGRAL TERM CV = Iterm
respectively lower gain.

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:

Error = (PV - SP) * (CV_Span / PV_Span) or: ;PV Method

66 EMBEDDED SYSTEMS PROGRAMMING MAY 1997


PID Algorithm

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.

68 EMBEDDED SYSTEMS PROGRAMMING MAY 1997


PID Algorithm

BUMPLESS TRANSFER AND SETPOINT ■ The Auto subsection 1 is the bump-


CHANGES less transfer section from Line 04 to

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

70 EMBEDDED SYSTEMS PROGRAMMING MAY 1997


PID Algorithm
by either Lines 14 to 17 or 18 to 21
depending on the sign of the
Initial_Error and the controller is then
shifted into PID mode from Lines 25 to
31. Before we leave the bumpless
transfer code explanation, note Lines
16 and 20—Last_PV is set equal to PV.
This is necessary to assure that on first
calculation of the Dterm, its output is
stable. Otherwise a severe “bump”
may result on transition.
Lines 25 to 31 should be self-
explanatory by now for they are the
standard, no-frills PID. If the operator
should select a new pressure setpoint
(SP) code from Lines 33 to 45 executes
until the PV passes the new SP. You’ll
notice that this code looks similar to
the bumpless code mentioned above.
The code uses the same technique of
turning the PID into a straight I-con-
troller, which is more gentle about
seeking a large setpoint change than a
full PID. This section of code is exe-
cuted until either Line 38 or 42
(depending on the relationship
between SP and Last_SP) see that the
SP has been passed by the PV. It is
then shifted back to standard PID.

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.

Garth Gaddy has been working in the


embedded control field for five years,
and his company is G2 Data and
Control. He is a licensed pilot and
A&P mechanic, and he’s currently
working on a computerized flight con-
trol system and auto pilot for home-
built aircraft. Garth can be reached at
grg01@alumni.csufresno.edu.

72 EMBEDDED SYSTEMS PROGRAMMING MAY 1997

You might also like