Sie sind auf Seite 1von 7

PID CONTROL

PID without a PhD


By Tim Wescott
FLIR Systems

At work, I am one of three desig-


nated “servo guys,” and the only
one who implements control
loops in software. As a result, I of-
ten have occasion to design digi-
tal control loops for various proj-
ects. I have found that while there
certainly are control problems
that require all the expertise I can
bring to bear, a great number of
Figure 1
control problems can be solved
with simple controllers, without
resorting to any control theory
at all. This article will tell you how
to implement and tune a simple
controller without getting into
heavy mathematics and without
requiring you to learn any control
theory. The technique used to
tune the controller is a tried and
true method that can be applied
to almost any control problem
with success.

PID control
The PID controller has been in
use for over a century in various
forms. It has enjoyed popularity
as a purely mechanical device,
Figure 2
as a pneumatic device, and as an
electronic device. The digital PID
controller using a microprocessor is being driven only from plant throughout this article, and carriage on a printer, or a throttle
has recently come into its own feedback. The plant feedback is show the effects of apply- mechanism in an automobile
in industry. As you will see, it is a subtracted from the command ing the various controllers to cruise control system, or almost
straightforward task to embed a signal to generate an error. This them: any other moderately precise po-
PID controller into your code. error signal drives the propor- • A motor driving a gear train sition controller. Figure 2 shows
PID stands for “proportional, tional and integral elements. • A precision positioning sys- a diagram of such a system. The
integral, derivative.” These three The resulting signals are added tem motor is driven by a voltage that
terms describe the basic ele- together and used to drive the • A thermal system is commanded by software. The
ments of a PID controller. Each of plant. I haven’t described what motor output is geared down to
these elements performs a differ- these elements do yet-we’ll get Each of these systems has dif- drive the actual mechanism. The
ent task and has a different effect to that later. I’ve included an al- ferent characteristics and each position of this final drive is mea-
on the functioning of a system. ternate placement for the propor- one requires a different control sured by the potentiometer.
In a typical PID controller these tional element (dotted lines)-this strategy to get the best perfor- A DC motor driven by a volt-
elements are driven by a combi- can be a better location for the mance. age wants to go at a constant
nation of the system command proportional element, depending speed that is proportional to the
and the feedback signal from the on how you want the system to Motor and gear applied voltage. Usually the mo-
object that is being controlled respond to commands. The first example plant is a mo- tor armature has some resistance
(usually referred to as the “plant”). tor driving a gear train, with the that limits its ability to accelerate,
Their outputs are added together Sample plants output position of the gear train so the motor will have some de-
to form the system output. In order to discuss this subject being monitored by a potenti- lay between the change in input
Figure 1 shows a block dia- with any sense of reality we ometer or some other position voltage and the resulting change
gram of a basic PID controller. In need some example systems. reading device. You might see in speed. The gear train takes
this case the derivative element I’ll use three example plants this kind of mechanism driving a the movement of the motor and

 eetindia.com | October 2000 | EE Times-India


multiplies it by a constant. Finally, The equations of motion for
the potentiometer measures the this system are fairly simple. The
position of the output shaft. force on the stage is proportional
Figure 3 shows the step to the drive command alone, so
response of the motor and gear the acceleration of the system is
combination. I’m using a time exactly proportional to the drive.
constant value of t0 = 0.2s. The The step response of this system
step response of a system is just by itself is a parabola, as shown
the behaviour of the output in in Figure 5. As we will see later
response to an input that goes this makes the control problem
from zero to some constant value more challenging because of
at time t = 0. Since we’re dealing the sluggishness with which
with fairly generic examples here the stage starts moving, and its
I’ve shown the step response as a enthusiasm to keep moving once
fraction of full scale, so it goes to it gets going.
1. Figure 3 shows the step input
and the motor response. The Temperature control
response of the motor starts out The third example plant I’ll use is Figure 3
slowly due to the time constant, a heater. Figure 6 shows a dia-
but once that is out of the way gram of an example system. The
the motor position ramps at a vessel is heated by an electric
constant velocity. heater, and the temperature of its
contents is sensed by a tempera-
Precision actuator ture-sensing device.
It is sometimes necessary to con- Thermal systems tend to have
trol the position of something very complex responses. I’m go-
very precisely. A precise position- ing to ignore quite a bit of detail
ing system can be built using a and give a very approximate
Figure 4
freely moving mechanical stage, model. Unless your performance
a speaker coil (a coil and magnet requirements are severe, an ac-
arrangement), and a non-con- curate model isn’t necessary.
tact position transducer. Figure 7 shows the step
You might expect to see this response of the system to a
sort of mechanism stabilising an change in Vd. I’ve used time
element of an optical system, constants of t1 = 0.1s and t2 =
or locating some other piece of 0.3s. The response tends to settle
equipment or sensor. Figure 4 out to a constant temperature
shows such a system. Software for a given drive, but it can take
commands the current in the coil. a great deal of time doing it.
This current sets up a magnetic Also, without lots of insulation,
field that exerts a force on the thermal systems tend to be very
magnet. The magnet is attached sensitive to outside effects. This
to the stage, which moves with effect is not shown in the figure,
an acceleration proportional to but we’ll be investigating it later
the coil current. Finally, the stage in the article.
position is monitored by a non-
Figure 5
contact position transducer. Controllers
With this arrangement, the The elements of a PID controller
force on the magnet is indepen- presented here either take their
dent of the stage motion. Fortu- input from the measured plant
nately this isolates the stage from output or from the error signal,
external effects. Unfortunately which is the difference between
the resulting system is very “slip- the plant output and the system
pery,” and can be a challenge to command. I’m going to write the
control. In addition, the electrical control code using floating point
requirements to build a good cur- to keep implementation details
rent-output amplifier and non- out of the discussion. It’s up to
contact transducer interface can you to adapt this if you are go-
be challenging. You can expect ing to implement your controller
that if you are doing a project like with integer or other fixed-point
this you are a member of a fairly arithmetic.
talented team (or you’re working I’m going to assume a func-
on a short-lived project). tion call as shown below. As Figure 6

 eetindia.com | October 2000 | EE Times-India


the discussion evolves, you’ll up the response to a point. Be-
see how the data structure and yond that point (kp = 5, kp = 10)
the internals of the function the motor starts out faster, but it
shape up. overshoots the target. In the end
the system doesn’t settle out any
double UpdatePID(SPid * pid, quicker than it would have with
double error, double lower gain, but there is more
position) overshoot. If we kept increasing
{ the gain we would eventually
. reach a point where the system
. just oscillated around the target
. and never settled out-the system
} would be unstable.
The motor and gear start
The reason I pass the error to to overshoot with high gains
the PID update routine instead because of the delay in the mo-
of passing the command is that tor response. If you look back at
sometimes you want to play tricks Figure 2, you can see that the Figure 7
with the error. Leaving out the er- motor position doesn’t start
ror calculation in the main code ramping up immediately. This
makes the application of the PID delay, plus high feedback gain, is
more universal. This function will what causes the overshoot seen
get used like this: in Figure 8. Figure 9 shows the
response of the precision actua-
. tor with proportional feedback
. only. Proportional control alone
position = ReadPlantADC(); obviously doesn’t help this sys-
drive = UpdatePID(&plantPID, tem. There is so much delay in
plantCommand - position, the plant that no matter how
position); low the gain is, the system will
DrivePlantDAC(drive); oscillate. As the gain is increased,
. the frequency of the output will
. increase but the system just
won’t settle.
Proportional Figure 10 shows what hap-
Proportional control is the easiest pens when you use pure pro-
feedback control to implement, portional feedback with the Figure 8
and simple proportional control is temperature controller. I’m show-
probably the most common kind ing the system response with a
of control loop. A proportional disturbance due to a change in
controller is just the error signal ambient temperature at t = 2s.
multiplied by a constant and fed Even without the disturbance you
out to the drive. The proportional can see that proportional control
term gets calculated with the fol- doesn’t get the temperature to
lowing code: the desired setting. Increasing
the gain helps, but even with
double pTerm; kp = 10 the output is still below
. target, and you are starting to see
. a strong overshoot that continues
. to travel back and forth (this is
pTerm = pid->pGain * error; called ringing).
. As the previous examples
. show, a proportional controller
. alone can be useful for some
return pTerm; things, but it doesn’t always help.
Plants that have too much delay,
Figure 9
Figure 8 shows what hap- like the precision actuator, can’t
pens when you add proportional be stabilised with proportional
feedback to the motor and gear control. Some plants, like the may need to be driven faster than Integral
system. For small gains (kp = 1) temperature controller, cannot be is possible with proportional con- Integral control is used to add long-
the motor goes to the correct tar- brought to the desired set point. trol alone. To solve these control term precision to a control loop. It is
get, but it does so quite slowly. In- Plants like the motor and gear problems you need to add integral almost always used in conjunction
creasing the gain (kp = 2) speeds combination may work, but they or differential control or both. with proportional control.

 eetindia.com | October 2000 | EE Times-India


The code to implement an late, after the plant has gotten up
integrator is shown below. The speed. To stabilise the two previ-
integrator state, iState is the sum ous systems, you need a little bit
of all the preceding inputs. The of their present value, which you
parameters iMin and iMax are the get from a proportional term.
minimum and maximum allow- Figure 13 shows the motor
able integrator state values. and gear with proportional and
integral (PI) control. Compare
double iTerm; this with Figures 8 and 11. The
. position takes longer to settle
. out than the system with pure
. proportional control, but it will
// calculate the integral state not settle to the wrong spot.
// with appropriate limiting Figure 14 shows what hap-
pid->iState += error; pens when you use PI control
if (pid->iState > pid->iMax) on the heater system. The heater
pid->iState = still settles out to the exact target
Figure 10
pid->iMax; temperature, as with pure inte-
else if (pid->iState gral control (see Figure 12), but
< with PI control, it settles out two
pid-> to three times faster. This figure
iMin) shows operation pretty close to
pid->iState = pid->iMin; the limit of the speed attainable
iTerm = pid->iGain * iState; using PI control with this plant.
// calculate the integral term Before we leave the discussion
. of integrators, there are two more
. things I need to point out. First,
. since you are adding up the er-
ror over time, the sampling time
Integral control by itself usu- that you are running becomes
ally decreases stability, or de- important. Second, you need to
stroys it altogether. Figure 11 pay attention to the range of your
shows the motor and gear with integrator to avoid windup.
pure integral control (pGain = The rate that the integrator
0). The system doesn’t settle. state changes is equal to the
Like the precision actuator with average error multiplied by the
Figure 11
proportional control, the motor integrator gain multiplied by the
and gear system with integral sampling rate. Because the inte-
control alone will oscillate with grator tends to smooth things out
bigger and bigger swings until over the long term you can get
something hits a limit. (Hopefully away with a somewhat uneven
the limit isn’t breakable.) sampling rate, but it needs to av-
Figure 12 shows the tem- erage out to a constant value. At
perature control system with worst, your sampling rate should
pure integral control. This system vary by no more than ý20% over
takes a lot longer to settle out any 10-sample interval. You can
than the same plant with propor- even get away with missing a few
tional control (see Figure 10), but samples as long as your average
notice that when it does settle sample rate stays within bounds.
out, it settles out to the target Nonetheless, for a PI controller
value-even with the disturbance I prefer to have a system where
added in. If your problem at hand each sample falls within ý1% to
doesn’t require fast settling, this ý5% of the correct sample time,
might be a workable system. and a long-term average rate that
Figure 12 shows why we use is right on the button. Figure 12
an integral term. The integrator If you have a controller that
state “remembers” all that has needs to push the plant hard, all the time spent in saturation grator unwinds and the process
gone on before, which is what your controller output will spend can cause the integrator state reverses. This situation can get so
allows the controller to cancel significant amounts of time out- to grow (wind up) to very large bad that the system never settles
out any long term errors in the side the bounds of what your values. When the plant reaches out, but just slowly oscillates
output. This same memory also drive can actually accept. This the target, the integrator value is around the target position.
contributes to instability-the con- condition is called saturation. still very large, so the plant drives Figure 15 illustrates the effect
troller is always responding too If you use a PI controller, then beyond the target while the inte- of integrator windup. I used the

 eetindia.com | October 2000 | EE Times-India


motor/controller of Figure 13, and the command value changes.
limited the motor drive to ý0.2. The differential term itself is
Not only is controller output much the last value of the position
greater than the drive available to minus the current value of the
the motor, but the motor shows position. This gives you a rough
severe overshoot. The motor ac- estimate of the velocity (delta
tually reaches its target at around position/sample time), which
five seconds, but it doesn’t reverse predicts where the position will
direction until eight seconds, and be in a while.
doesn’t settle out until 15 seconds
have gone by. double dTerm;
The easiest and most direct .
way to deal with integrator wind- .
up is to limit the integrator state, .
as I showed in my previous code dTerm = pid->dGain * (posi-
example. Figure 16 shows what tion - pid->dState);
happens when you take the pid->dState = position;
system in Figure 15 and limit .
the integrator term to the avail- .
Figure 13
able drive output. The controller .
output is still large (because of
the proportional term), but the With differential control you
integrator doesn’t wind up very can stabilise the precision actua-
far and the system starts settling tor system. Figure 17 shows the
out at five seconds, and finishes response of the precision ac-
at around six seconds. tuator system with proportional
Note that with the code ex- and derivative (PD) control. This
ample above you must scale iMin system settles in less than 1/2 of
and iMax whenever you change a second, compared to multiple
the integrator gain. Usually you seconds for the other systems.
can just set the integrator mini- Figure 18 shows the heating
mum and maximum so that the system with PID control. You can
integrator output matches the see the performance improve-
drive minimum and maximum. If ment to be had by using full PID
you know your disturbances will control with this plant.
be small and you want quicker Differential control is very
settling, you can limit the integra- powerful, but it is also the most
tor further. problematic of the control types
Figure 14
presented here. The three prob-
Differential lems that you are most likely go-
I didn’t even show the precision ing to experience are sampling
actuator in the previous section. irregularities, noise, and high
This is because the precision ac- frequency oscillations. When
tuator cannot be stabilised with I presented the code for a dif-
PI control. In general, if you can’t ferential element I mentioned
stabilise a plant with proportional that the output is proportional
control, you can’t stabilise it with to the position change divided
PI control. We know that propor- by the sample time. If the posi-
tional control deals with the pres- tion is changing at a constant
ent behaviour of the plant, and rate but your sample time varies
that integral control deals with from sample to sample, you will
the past behaviour of the plant. If get noise on your differential
we had some element that pre- term. Since the differential gain
dicts the plant behaviour then is usually high, this noise will be
this might be used to stabilise amplified a great deal.
the plant. A differentiator will do When you use differential
the trick. control you need to pay close Figure 15
The code below shows the attention to even sampling. I’d
differential term of a PID con- say that you want the sampling sampling interval, design your sure the actual ADC conversion
troller. I prefer to use the actual interval to be consistent to within software to sample with very high happens at the right time. It may
plant position rather than the 1% of the total at all times-the priority. You don’t have to actu- be best to put all your sampling
error because this makes for closer the better. If you can’t set ally execute the controller with in an ISR or very high-priority task,
smoother transitions when the hardware up to enforce the such rigid precision-just make then execute the control code in

 eetindia.com | October 2000 | EE Times-India


Listing 1: PID controller code

typedef struct
{
double dState; // Last position input
double iState; // Integrator state
double iMax, iMin;

// Maximum and minimum allowable integrator state


double iGain, // integral gain
pGain, // proportional gain
dGain; // derivative gain
} SPid;
double UpdatePID(SPid * pid, double error, double position)
{
double pTerm, Figure 16
dTerm, iTerm;
pTerm = pid->pGain * error;
// calculate the proportional term
// calculate the integral state with appropriate limiting
pid->iState += error;
if (pid->iState > pid->iMax) pid->iState = pid->iMax;
else if (pid->iState
<
pid->iMin) pid->iState = pid->iMin;
iTerm = pid->iGain * iState; // calculate the integral term
dTerm = pid->dGain * (position - pid->dState);
pid->dState = position;
return pTerm + iTerm - dTerm;
}

a more relaxed manner. beyond the scope of this article.


Differential control suffers Probably the best that you can
from noise problems because do about this problem is to look Figure 17
noise is usually spread relatively at how likely you are to see any
evenly across the frequency noise, how much it will cost to get If you can, hook your system Otherwise start by adjusting your
spectrum. Control commands quiet inputs, and how badly you up to some test equipment, or differential gain.
and plant outputs, however, usu- need the high performance that write in some debug code to The way the controller is coded
ally have most of their content at you get from differential control. allow you to look at the appro- you cannot use differential con-
lower frequencies. Proportional Once you’ve worked this out, priate variables. If your system trol alone. Set your proportional
control passes noise through you can avoid differential control is slow enough you can spit gain to some small value (one
unmolested. Integral control altogether, talk your hardware the appropriate variables out or less). Check to see how the
averages its input signal, which folks into getting you a lower on a serial port and graph them system works. If it oscillates with
tends to kill noise. Differential noise input, or look for a control with a spreadsheet. You want proportional gain you should
control enhances high frequen- systems expert. to be able to look at the drive be able to cure it with differen-
cy signals, so it enhances noise. The full text of the PID control- output and the plant output. In tial gain. Start with about 100
Look at the differential gains ler code is shown in Listing 1 and addition, you want to be able times more differential gain than
that I’ve set on the plants above, is available at www.embedded. to apply some sort of a square- proportional gain. Watch your
and think of what will happen if com/code.html. wave signal to the command drive signal. Now start increas-
you have noise that makes each input of your system. It is fairly ing the differential gain until you
sample a little bit different. Mul- Tuning easy to write some test code see oscillation, excessive noise,
tiply that little bit by a differential The nice thing about tuning a PID that will generate a suitable test or excessive (more than 50%)
gain of 2,000 and think of what controller is that you don’t need command. Once you get the overshoot on the drive or plant
it means. to have a good understanding setup ready, set all gains to zero. output. Note that the oscillation
You can low-pass filter your of formal control theory to do a If you suspect that you will not from too much differential gain is
differential output to reduce fairly good job of it. About 90% need differential control (like the much faster than the oscillation
the noise, but this can severely of the closed-loop controller ap- motor and gear example or the from not enough. I like to push
affect its usefulness. The theory plications in the world do very thermal system) then skip down the gain up until the system is
behind how to do this and how well indeed with a controller that to the section that discusses on the verge of oscillation then
to determine if it will work is is only tuned fairly well. tuning the proportional gain. back the gain off by a factor of

 eetindia.com | October 2000 | EE Times-India


two or four. Make sure the drive
signal still looks good. At this
point your system will probably
be responding very sluggishly, so
it’s time to tune the proportional
and integral gains.
If it isn’t set already, set the
proportional gain to a starting
value between 1 and 100. Your
system will probably either show
terribly slow performance or it
will oscillate. If you see oscilla-
tion, drop the proportional gain
by factors of eight or 10 until the
oscillation stops. If you don’t see
oscillation, increase the propor-
tional gain by factors of eight or
10 until you start seeing oscilla-
tion or excessive overshoot. As
with the differential controller, I
usually tune right up to the point
of too much overshoot then re-
duce the gain by a factor of two
or four. Once you are close, fine
tune the proportional gain by
factors of two until you like what
Figure 18
you see.
Once you have your propor- Sampling rate of saturation until the control away with lowering the sam-
tional gain set, start increasing So far I’ve only talked about system has effectively settled out. pling rate somewhat (I would
integral gain. Your starting values sample rates in terms of how If you look at Figure 16, the con- hesitate to lengthen the sample
will probably be from 0.0001 to consistent they need to be, but troller comes out of saturation at time to more than one-fifth of
0.01. Here again, you want to I haven’t told you how to decide about 5.2s, and has settled out at the desired settling time). If
find the range of integral gain ahead of time what the sample around 6.2s. If you can live with you aren’t using a differentiator
that gives you reasonably fast rate needs to be. If your sampling the one second settling time you and you are careful about using
performance without too much rate is too low you may not be could get away with a sampling enough bits in your integrator
overshoot and without being too able to achieve the performance rate as low as 10Hz. you can get away with sampling
close to oscillation. you want, because of the added You should treat the sam- rates 1,000 times faster than the
delay of the sampling. If your pling rate as a flexible quantity. intended settling time.
Other issues sampling rate is too high you will Anything that might make the
Unless you are working on a create problems with noise in control problem more difficult Exert control
project with very critical per- your differentiator and overflow would indicate that you should This covers the basics of imple-
formance parameters you can in your integrator. raise the sampling rate. Fac- menting and tuning PID control-
often get by with control gains The rule of thumb for digital tors such as having a difficult lers. With this information, you
that are within a factor of two of control systems is that the sample plant to control, or needing should be able to attack the next
the “correct” value. This means time should be between 1/10th differential control, or needing control problem that comes your
that you can do all your “multi- and 1/100th of the desired sys- very precise control would all way and get it under control.
plies” with shifts. This can be very tem settling time. System settling indicate raising the sampling
handy when you’re working with time is the amount of time from rate. If you have a very easy
a slow processor. the moment the drive comes out control problem you could get Email   Send inquiry

 eetindia.com | October 2000 | EE Times-India

Das könnte Ihnen auch gefallen