Você está na página 1de 87

http://en.wikipedia.

org/wiki/PID_controller

PID controller
From Wikipedia, the free encyclopedia

A block diagram of a PID controller

A proportional–integral–derivative controller (PID controller) is a genericcontrol loop feedback


mechanism (controller) widely used in industrial control systems – a PID is the most commonly used feedback
controller. A PID controller calculates an "error" value as the difference between a measured process
variableand a desired setpoint. The controller attempts to minimize the error by adjusting the process control
inputs.

The PID controller calculation (algorithm) involves three separate constant parameters, and is accordingly
sometimes called three-term control: theproportional, the integral and derivative values,
denoted P, I, and D. Heuristically, these values can be interpreted in terms of time: P depends on
the present error, Ion the accumulation of past errors, and D is a prediction of future errors, based on current
rate of change.[1] The weighted sum of these three actions is used to adjust the process via a control element
such as the position of a control valve or the power supply of a heating element.

In the absence of knowledge of the underlying process, a PID controller is the best controller.[2] By tuning the
three parameters in the PID controller algorithm, the controller can provide control action designed for specific
process requirements. The response of the controller can be described in terms of the responsiveness of the
controller to an error, the degree to which the controller overshoots the setpoint and the degree of system
oscillation. Note that the use of the PID algorithm for control does not guarantee optimal control of the system
or system stability.

Some applications may require using only one or two actions to provide the appropriate system control. This
is achieved by setting the other parameters to zero. A PID controller will be called a PI, PD, P or I controller in
the absence of the respective control actions. PI controllers are fairly common, since derivative action is
sensitive to measurement noise, whereas the absence of an integral term may prevent the system from
reaching its target value due to the control action.

Contents
[hide]

• 1 Control loop basics

• 2 PID controller theory

○ 2.1 Proportional term

 2.1.1 Droop

○ 2.2 Integral term

○ 2.3 Derivative term

○ 2.4 Summary

• 3 Loop tuning

○ 3.1 Stability

○ 3.2 Optimum behavior

○ 3.3 Overview of methods

○ 3.4 Manual tuning

○ 3.5 Ziegler–Nichols method

○ 3.6 PID tuning software

• 4 Modifications to the PID algorithm

• 5 History

• 6 Limitations of PID control

○ 6.1 Linearity

○ 6.2 Noise in derivative


• 7 Improvements

○ 7.1 Feed-forward

○ 7.2 Other improvements

• 8 Cascade control

• 9 Physical implementation of PID control

• 10 Alternative nomenclature and PID forms

○ 10.1 Ideal versus standard PID form

○ 10.2 Basing derivative action on PV

○ 10.3 Basing proportional action on

PV

○ 10.4 Laplace form of the PID

controller

○ 10.5 PID Pole Zero Cancellation

○ 10.6 Series/interacting form

○ 10.7 Discrete implementation

○ 10.8 Pseudocode

• 11 PI controller

• 12 See also

• 13 References

• 14 External links

○ 14.1 PID tutorials

○ 14.2 Special topics and PID control

applications

[edit]Control loop basics


Further information: Control loop

A familiar example of a control loop is the action taken when adjusting hot and cold faucet valves to maintain
the faucet water at the desired temperature. This typically involves the mixing of two process streams, the hot
and cold water. The person touches the water to sense or measure its temperature. Based on this feedback
they perform a control action to adjust the hot and cold water valves until the process temperature stabilizes
at the desired value.

Sensing water temperature is analogous to taking a measurement of the process value or process variable
(PV). The desired temperature is called the setpoint (SP). The input to the process (the water valve position)
is called the manipulated variable (MV). The difference between the temperature measurement and the
setpoint is the error (e) and quantifies whether the water is too hot or too cold and by how much.

After measuring the temperature (PV), and then calculating the error, the controller decides when to change
the tap position (MV) and by how much. When the controller first turns the valve on, it may turn the hot valve
only slightly if warm water is desired, or it may open the valve all the way if very hot water is desired. This is
an example of a simple proportional control. In the event that hot water does not arrive quickly, the controller
may try to speed-up the process by opening up the hot water valve more-and-more as time goes by. This is
an example of anintegral control.

Making a change that is too large when the error is small is equivalent to a high gain controller and will lead to
overshoot. If the controller were to repeatedly make changes that were too large and repeatedly overshoot
the target, the output would oscillate around the setpoint in either a constant, growing, or decaying sinusoid. If
the oscillations increase with time then the system is unstable, whereas if they decrease the system is stable.
If the oscillations remain at a constant magnitude the system is marginally stable.

In the interest of achieving a gradual convergence at the desired temperature (SP), the controller may wish
to damp the anticipated future oscillations. So in order to compensate for this effect, the controller may elect
to temper their adjustments. This can be thought of as aderivative control method.

If a controller starts from a stable state at zero error (PV = SP), then further changes by the controller will be
in response to changes in other measured or unmeasured inputs to the process that impact on the process,
and hence on the PV. Variables that impact on the process other than the MV are known as disturbances.
Generally controllers are used to reject disturbances and/or implement setpoint changes. Changes in
feedwater temperature constitute a disturbance to the faucet temperature control process.

In theory, a controller can be used to control any process which has a measurable output (PV), a known ideal
value for that output (SP) and an input to the process (MV) that will affect the relevant PV. Controllers are
used in industry to regulate temperature, pressure, flow rate,chemical composition, speed and practically
every other variable for which a measurement exists.

[edit]PID controller theory


This section describes the parallel or non-interacting form of the PID controller. For other forms please see
the section "Alternative nomenclature and PID forms".
The PID control scheme is named after its three correcting terms, whose sum constitutes the manipulated
variable (MV). Hence:

where

Pout, Iout, and Dout are the contributions to the output from the PID controller from each of the three
terms, as defined below.
[edit]Proportional term

Plot of PV vs time, for three values of Kp (Ki and Kd held constant)

The proportional term (sometimes called gain) makes a change to the output that is proportional to
the current error value. The proportional response can be adjusted by multiplying the error by a
constant Kp, called the proportional gain.

The proportional term is given by:

where

Pout: Proportional term of output


Kp: Proportional gain, a tuning parameter
SP: Setpoint, the desired value
PV: Process value (or process variable), the measured value
e: Error = SP − PV
t: Time or instantaneous time (the present)
A high proportional gain results in a large change in the
output for a given change in the error. If the proportional gain
is too high, the system can become unstable (see the
section on loop tuning). In contrast, a small gain results in a
small output response to a large input error, and a less
responsive (or sensitive) controller. If the proportional gain is
too low, the control action may be too small when
responding to system disturbances.

[edit]Droop

A pure proportional controller will not always settle at its


target value, but may retain a steady-state error. Specifically,
the process gain - drift in the absence of control, such as
cooling of a furnace towards room temperature, biases a
pure proportional controller. If the process gain is down, as
in cooling, then the bias will be below the set point, hence
the term "droop".

Droop is proportional to process gain and inversely


proportional to proportional gain. Specifically the steady-
state error is given by:

e = G / Kp
Droop is an inherent defect of purely proportional
control. Droop may be mitigated by adding a
compensating bias term (setting the setpoint above the
true desired value), or corrected by adding an
integration term (in a PI or PID controller), which
effectively computes a bias adaptively.

Despite droop, both tuning theory and industrial


practice indicate that it is the proportional term that
should contribute the bulk of the output change.

[edit]Integral term
Plot of PV vs time, for three values of Ki (Kp and Kd held
constant)

The contribution from the integral term (sometimes


called reset) is proportional to both the magnitude of
the error and the duration of the error. Summing the
instantaneous error over time (integrating the error)
gives the accumulated offset that should have been
corrected previously. The accumulated error is then
multiplied by the integral gain and added to the
controller output. The magnitude of the contribution of
the integral term to the overall control action is
determined by the integral gain, Ki.

The integral term is given by:

where

Iout: Integral term of output


Ki: Integral gain, a tuning parameter
SP: Setpoint, the desired value
PV: Process value (or process variable), the measured value
e: Error = SP − PV
t: Time or instantaneous time (the present)
τ: a dummy integration variable
The integral term
(when added to
the proportional
term) accelerates
the movement of
the process
towards setpoint
and eliminates
the residual
steady-state
error that occurs
with a
proportional only
controller.
However, since
the integral term
is responding to
accumulated
errors from the
past, it can cause
the present value
to overshoot the
setpoint value
(cross over the
setpoint and then
create a
deviation in the
other direction).
For further notes
regarding integral
gain tuning and
controller
stability, see the
section on loop
tuning.

[edit]Derivati
ve term

Plot of PV vs
time, for three
values of
Kd (Kp and
Ki held
constant)

The rate of
change of the
process error is
calculated by
determining the
slope of the error
over time (i.e., its
first derivative
with respect to
time) and
multiplying this
rate of change by
the derivative
gain Kd. The
magnitude of the
contribution of
the derivative
term (sometimes
called rate) to the
overall control
action is termed
the derivative
gain, Kd.

The derivative
term is given by:

where

Dout: Derivative term of output


Kd: Derivative gain, a tuning parameter
SP: Setpoint, the desired value
PV: Process value (or process variable), the measured value
e: Error = SP − PV
t: Time or instantaneous time (the present)
The derivativ
slows the rat
change of the
controller out
this effect is
noticeable cl
the controller
setpoint. Hen
derivative co
used to redu
magnitude of
overshoot pr
by the integra
component a
improve the
combined co
process stab
However,
differentiation
signal amplif
noise and thu
term in the co
is highly sens
noise in the e
term, and ca
a process to
become unst
the noise and
derivative ga
sufficiently la
Hence an
approximatio
differentiator
limited bandw
more commo
used. Such a
is known as a
Phase-Lead
compensator

[edit]Summ

The proportio
integral, and
derivative ter
summed to c
the output of
controller.
Defining u(t)
controller out
final form of t
algorithm is:

where the tun


parameters a

Proportiona
Larger values typically mean faster response since the larger the error, the larger the proportional
term compensation. An excessively large proportional gain will lead to process instability and
oscillation.
Integral gain
Larger values imply steady state errors are eliminated more quickly. The trade-off is larger overshoot:
any negative error integrated during transient response must be integrated away by positive error
before reaching steady state.
Derivative g
Larger values decrease overshoot, but slow down transient response and may lead to instability due
to signal noise amplification in the differentiation of the error
[edit]Loop

Tuning a con
its control pa
band, integra
gain/rate) to
desired contr
(bounded os
requirement,
systems hav
applications
and requirem
another.

Some proces
linearity and
at full-load co
process is st
can be corre
scheduling (u
different ope
controllers of
control using
performance
by careful tun
be unaccepta

PID tuning is
though there
and in princip
because it m
within the lim
are according
tuning, and m
are the subje
describes so
methods for

[edit]Stabil

If the PID con


of the propor
terms) are ch
controlled pro
i.e. its output
without oscill
saturation or
Instability is c
particularly in
lag.

Generally, st
reverse of ins
process mus
combination
setpoints, tho
stability (bou
acceptable o

[edit]Optim

The optimum
change or se
depending on

Two basic re
are regulatio
staying at a g
tracking (imp
– these refer
variable track
criteria for co
timeand settl
must not allo
process varia
example, this
processes m
expended in

[edit]Overv

There are se
PID loop. Th
generally inv
some form o
choosing P, I
dynamic mod
tuning metho
inefficient, pa
response tim
longer.

The choice o
on whether o
"offline" for tu
of the system
offline, the be
involves subj
change in inp
a function of
to determine
[edit]Manu

If the system
tuning metho
set Ki and K
the Kp until th
oscillates, the
approximatel
"quarter amp
Then increas
in sufficient t
However, too
instability. Fin
until the loop
its reference
However, too
excessive re
PID loop tuni
to reach the
however, som
overshoot, in
damped clos
which will req
less than hal
causing oscil
[edit]Ziegle

For more det


see Ziegler–N

Another heur
formally know
method, intro
Ziegler and N
1940s. As in
the Ki and K
The P gain is
the ultimate g
of the loop st
oscillation pe
gains as sho
These gains
form of the P
to the standa
derivative tim
only depende
period Pu. Pl
"Alternative n

[edit]PID tu
Most modern
tune loops us
methods sho
tuning and lo
used to ensu
software pac
develop proc
optimal tunin
can even dev
data from ref

Mathematica
impulse in th
controlled sy
design the P
response tim
mathematica
recommende
can literally t
set of loop va
harder to find
controllers of
which very sm
sent to the pr
itself to calcu

Other formul
loop accordin
criteria. Many
embedded w
hardware mo

Advances in
software also
PID Loops in
State (NSS)
model the dy
a disturbance
parameters i

[edit]Modif
algorithm
The basic PI
challenges in
have been ad
modifications

Integral win
For more det
windup.

One common
ideal PID imp
windup, whe
occurs (say a
integral term
than the max
regulation va
system overs
increase as t
unwound. Th
by:

 Initializin
desired v
 Increasin
ramp

 Disabling
PV has e

 Limiting
the integ

 Preventin
accumul
determin
Overshootin
disturbance
For example, a PID loop is used to control the temperature of an electric resistance furnace, the
system has stabilized. Now the door is opened and something cold is put into the furnace the
temperature drops below the setpoint. The integral function of the controller tends to compensate this
error by introducing another error in the positive direction. This overshoot can be avoided by freezing
of the integral function after the opening of the door for the time the control loop typically needs to
reheat the furnace.
Replacing th
based part
Often the time-response of the system is approximately known. Then it is an advantage to simulate
this time-response with a model and to calculate some unknown parameter from the actual response
of the system. If for instance the system is an electrical furnace the response of the difference
between furnace temperature and ambient temperature to changes of the electrical power will be
similar to that of a simple RC low-pass filter multiplied by an unknown proportional coefficient. The
actual electrical power supplied to the furnace is delayed by a low-pass filter to simulate the response
of the temperature of the furnace and then the actual temperature minus the ambient temperature is
divided by this low-pass filtered electrical power. Then, the result is stabilized by another low-pass
filter leading to an estimation of the proportional coefficient. With this estimation, it is possible to
calculate the required electrical power by dividing the set-point of the temperature minus the ambient
temperature by this coefficient. The result can then be used instead of the integral function. This also
achieves a control error of zero in the steady-state, but avoids integral windup and can give a
significantly improved control action compared to an optimized PID controller. This type of controller
does work properly in an open loop situation which causes integral windup with an integral function.
This is an advantage if, for example, the heating of a furnace has to be reduced for some time
because of the failure of a heating element, or if the controller is used as an advisory system to a
human operator who may not switch it to closed-loop operation. It may also be useful if the controller
is inside a branch of a complex control system that may be temporarily inactive.

Many PID loo


example, a v
major cost an
the form of e
mechanical r
mechanical w
device is acti
a significant
outputdeadb
of the output
modifying the
change woul
range). The c
deadband be

The proportio
excessive mo
subjected to
error, such a
the derivative
of the error, w
instantaneou
algorithms in

Derivative o
In this case the PID controller measures the derivative of the output quantity, rather than the
derivative of the error. The output is always continuous (i.e., never has a step change). For this to be
effective, the derivative of the output must have the same sign as the derivative of the error.
Setpoint ram
In this modification, the setpoint is gradually moved from its old value to a newly specified value using
a linear or first order differential ramp function. This avoids the discontinuity present in a simple step
change.
Setpoint we
Setpoint weighting uses different multipliers for the error depending on which element of the controller
it is used in. The error in the integral term must be the true control error to avoid steady-state control
errors. This affects the controller's setpoint response. These parameters do not affect the response to
load disturbances and measurement noise.
[edit]Histo

PID theory dev

PID controlle
were subseq
earliest exam
Sperry in 191
controller wa
(Minorsky 19
for the US Na
ahelmsman,
based on the
change;[7] thi
was stability,
problem. Wh
disturbances
notably a stif
term. Finally,

Trials were c
controlling th
yielded susta
yaw of ±1/6°,

The Navy ult


personnel[why
others in the

[edit]Limita

While PID co
often perform
they can perf
provide optim
that it is a fee
knowledge o
and a compr
model of the
incorporating

The most sig


control with k
control error.
such as by c
different use
performance
precision, an
cascading m

PID controlle
the PID loop
not overshoo
also have dif
regulation ve
behavior (say
have lag in re

[edit]Linea

Another prob
in particular s
linear system
temperature
heating elem
overshoot ca
downward. In
to prevent or
increases se

[edit]Noise

A problem w
measuremen
in the output.
pass filter in
However, low
other out, so
better choice
which improv
some case, t
with little loss
as a PI contr

[edit]Impro

[edit]Feed-

The control s
the feedback
forward (or o
the desired a
combined wi
performance
major portion
primarily to re
the setpoint (
Since the fee
feedback, it c
improving the
For example
mechanical l
the prime mo
being used to
or torque bei
take the insta
value approp
controller. Th
decelerated,
prime mover
situation use
output to red
setpoint and
open-loop fe
provide a mo

[edit]Other

In addition to
through meth
in different op
logic [10] [11] . F
instrumentati
rate, measur
required to a

[edit]Casc

One distinctiv
controllers ca
This is called
PIDs arrange
PID controlle
physical para
controller act
outer loop co
changing par
mathematica
controller is i
by using cas

[edit]Phys

In the early h
was impleme
controllers us
by compress
industry stan

Electronic an
state or tube
analog PID c
electronic sy
the power co
detection circ
controllers ha
implemented

Most modern
in programm
digital contro
they are rela
implementati

Variable volta
of Pulse-widt
variation is a
cycle that the
system the p
second within
percentage in
for high enou

[edit]Altern

[edit]Ideal
The form of t
and the one
In this form th

where

Ti is the integral time


Td is the derivative time
In this standa
inner summa
and past erro
effectively pr
that the loop
value to com
eliminating th
value is scale

In the ideal p

the gain para

through
treated as sim
where the pa
theoretical tre
complex mat

[edit]Basin

In most comm
This is becau
when the SP
changes in e
responds to p

[edit]Basin

Most comme
This means t
seem to adve
controller ma
modification
disturbances
of the disturb
process distu
algorithm can

Tuning meth
algorithm. Ki

[edit]Lapla

Sometimes it

Having the P
makes it eas

[edit]PID P
The PID equ

When this fo
If

Then

This can be v

[edit]Series

Another repr

where the pa

, , and

with

This form ess


digital, many

[edit]Discr

The analysis
controller to b
sampling tim
The derivativ

Thus, a veloc
second deriv

[edit]Pseud

Here is a sim

previous_e
integral =
start:
error =
integral
derivati
output =
previous
wait(dt)
goto sta

[edit]PI co

Basic block of a

A PI Control

The controlle
where Δ is th

Δ = SP - PV.
A PI controlle

where

G = KP = proportional gain
G / τ = KI = integral gain
Setting a valu

The lack of d

Without deriv
perturbations

PID Controller
CIRCUIT

PID1.CIR Download the SPICE file


Tuning the PID controller can be like learning to roller blade, ski or maybe riding a bull.
Until you've done it a few times, the literature you've read really doesn't hit home. But
after after few attempts (and falls), you find it wasn't so bad after all - in fact it was kind
of fun!
The PID controller is every where - temperature, motion, flow controllers - and its
available in analog and digital forms. Why use it? It helps get your output (velocity,
temperature, position) where you want it, in a short time, with minimal overshoot, and
with little error. In many applications the PID controller can do the job - but as usual,
with compromises. After a short intro to the PID terms and an example control system,
you'll get a chance tune a PID controller.

THE PID CONTROLLER


You've probably seen the terms defined before: P -Proportional, I - Integral, D -
Derivative. These terms describe three basic mathematical functions applied to the error
signal , Verror = Vset - Vsensor. This error represents the difference between where you
want to go (Vset), and where you're actually at (Vsensor). The controller performs the
PID mathematical functions on the error and applies the their sum to a process (motor,
heater, etc.) So simple, yet so powerful! If tuned correctly, the signal Vsensor should
move closer to Vset.
Tuning a system means adjusting three multipliers Kp, Ki and Kd adding in various
amounts of these functions to get the system to behave the way you want. The table
below summarizes the PID terms and their effect on a control system.
Term Math Function Effect on Control System
P Typically the main drive in a control loop, KP reduces a
KP x Verror
Proportional large part of the overall error.
Reduces the final error in a system. Summing even a
I
Integral KI x ∫ Verror dt small error over time produces a drive signal large enough
to move the system toward a smaller error.
Counteracts the KP and KI terms when the output
D
KD x dVerror / dt changes quickly. This helps reduce overshoot and ringing.
Derivative
It has no effect on final error.

THE CONTROL SYSTEM


The SPICE circuit for the Control System looks pretty much like the block diagram.
PID CONTROLLER. How do we create the PID terms? To get the Proportional term,
EP multiples Verror at V(2) by a fixed gain of 1 - easy enough! To get the Integral term,
current source GI converts V(2) to a current and integrates it on C1=1F. Finally,
the Derivative term is created by GD converting V(2) to a current and forcing it through
L1. The resulting voltage becomes V(5) = L1 di / dt. A quick substitution of L1 = 1 H
and i = Verror gets you V(5) = d Verror / dt.
OUTPUT PROCESS. EOUT represents a very simplified model of a process to be
controlled like motor velocity or heater temperature. The gain of 100 could represent an
output transfer function of 100 RPM / V or 100 ° C / V. To include the effects of the
motor's inertia or heater's thermal mass, we've added some time delay into the output
using two cascaded RC filters. Although Vout is simulated in volts, we know it really
represents other variables like velocity in RPM or temperature in °C.
SENSOR. The sensor tells you, typically by a voltage, what's happening at the control
system output. For motor velocity, a tachometer could generate 1 V / 100 RPM; for
temperature, a thermistor circuit could produce 0.01 V / deg C. ESENSOR models this
feedback device. Because a sensor does not respond instantly, an RC filter is also added
here to model its finite response time.

TUNING THE PID CONTROLLER


Although you'll find many methods and theories on tuning a PID, here's a straight
forward approach to get you up and soloing quickly.
1. SET KP. Starting with KP=0, KI=0 and KD=0, increase KP until the output starts
overshooting and ringing significantly.
2. SET KD. Increase KD until the overshoot is reduced to an acceptable level.
3. SET KI. Increase KI until the final error is equal to zero.

HANDS-ON DESIGN Run a simulation of the circuit file PID1.CIR. VSET generates a
10V step input voltage to the control system. You can adjust the PID terms at the EPID
source that adds the P, I and D terms at V(3), V(4) and V(5). Initially, the PID multipliers
are set to KP=1, KI=0 and KD=0.
EPID 6 0 POLY(3) (3,0) (4,0) (5,0) 0 1 0 0

SET KP. Plot the system input V(1) and the sensor output (12). Although the response
looks smooth, what is the sensor voltage compared to the desired 10V? The output falls
short by 5V! To reduce this error, increase KP to 10 (Change EPID to look like ... 10 0
0 ). Wow, the output now reaches 9V, reducing the error to 1V. But as you can see, the
output is getting wild with overshoot and ringing. Push KP up higher to 20 or 30. Yes, the
error reduces, but the overshoot gets worse. Eventually, your system will become
unstable and break out into song (oscillate). Back off KP to 20 or so.
SET KD. The derivative term can rescue the response by counteracting the KP drive
when the output is changing. Start with a small value like KD=0.2 and rerun the
simulation (Change EPID to look like ...
20 0 0.2 ). Now you're wrestling control back into the system - the ringing and
overshoot are reduced! Crank up KD some more. Improvement should continue to a point
where the system becomes less stable and overshoot increases again. Return KD to
around 0.5.
SET KI. With KP=20, KI=0 and KD=0.5 the response looks respectable, but the final
error is a disappointing 0.5V (or 5%)! Now, try the KI term. This will integrate the
remaining error into a drive signal big enough to reduce the error further. Start with KI =
10 ( EPID should look like ... 20 10 0.5 ). Check out the last half of the V(12) - the
sensor output moves slowly toward 10V! You might want to put up a cursor on the plot
to monitor the exact value of V(12). The bigger you make KI, the faster it will move
toward 10V. Like the other terms, a value is reached where the KI does more harm than
good as the system becomes less stable.

DIVING DEEPER
Diving a little deeper you can get a clearer view of the PID components. Before we go
beneath the surface, set KP=10, KI=0 and KD=0.
INSIDE P Run a couple of simulations with KP=10 and KP 20. Plot V(1) and V(12).
What is the final error for each case? You may have noticed the errors of 1 and 0.5V are
proportional to the gains of 10 and 20. You can estimate the error by
Verror ≈ Vset / KP
for large KP. What kind of gain do you need for a 1% error? You can easily calculate it
as a gain of 100. However, we've already seen how large gain cause overshoot, ringing
and oscillations! KP can't do it alone.
INSIDE D KD counteracts KP - let's see how. Set KP back to 10 and run a simulation.
Plot the P function V(3) which is really Verror. Now plot the D term V(5) which we
know is dVerror / dt. To get a good view of D, set the Y-Axis limits to +100 /-200V.
Notice how dVerror / dt swings negative when Verror is initially positive. How does this
help? Essentially D counteracts the P term potentially reducing ringing and oscillations.
The nice thing about the D term is that it goes to zero as the output settles. This makes
sense - no change in output, no derivative term. Add in some of the D function by setting
the multipliers to KP=10, KI=0 and KD=0.5. The initial overshoot should be significantly
reduced.
INSIDE I We've seen how a large gain produces a small error. However, big KP gets
you into big trouble with overshoot and ringing. Alternatively, an integrator can also give
you a big gain by accumulating even a small error over time. Run a simulation with
KP=10, KI=0 and KD=0.5 and plot the I term at V(4). What does it look like? Notice, a
ramp function, representing Verror integrated over over time, builds up to a significant
drive voltage. Add in the I function by setting KP=10, KI=40 and KD=0.5. The I term
completes the controller's job by moving the output toward an error of zero.

SIMULATION NOTE
Need a handy way to combine multiple signals such as the PID sum? A controlled source
can be a function of multiple inputs described by a polynomial. What is this polynomial?
The equation is defined by a coefficient list at the end of the statement. The example
below
EPID 6 0 POLY(3) (3,0) (4,0) (5,0) 0 10 2 1

creates a polynomial of the form


V(6,0) = 0 + 10 ∙ V(3,0) + 2 ∙ V(4,0) + 1 ∙ V(5,0)
or more generally,
VO = k0 + k1∙V1 + k2∙V2 + k3∙V3
In fact, you can get higher order terms and cross terms by extending the coefficient list
VO = ...+ k4∙V1∙V1 + k5∙V1∙V2 + k6∙V1∙V3 +
+ k7∙V2∙V2 + k8∙V2∙V3 +
+ k9∙V3∙V3
If you're not using a term, you need to put a 0 in its place; you can't just leave it out. This
feature is great for simpler polynomials. Keeping track of the coefficient list for large
polynomials can get crazy fast.

SPICE FILE
Download the file or copy this netlist into a text file with the *.cir extention.
PID1.CIR - THE PID CONTROLLER
*
* SET POINT
VSET 1 0 PWL(0MS 0MV 1MS 10V 2000MS 10V)
RSET 1 0 1MEG
*
* CALCULATE ERROR
EERROR 2 0 1 12 1
RERROR 2 0 1MEG
*
* P - PROPORTIONAL TERM
EP 3 0 2 0 1
RP 3 0 1MEG
*
* I - INTEGRAL TERM
GI 0 4 2 0 1
C1 4 0 1
R1 4 0 1MEG
*
* D - DERIVATIVE TERM
GD 0 5 2 0 1
L1 5 0 1
*
* ADD PID TERMS, ADJUST PID MULTIPLIERS
EPID 6 0 POLY(3) (3,0) (4,0) (5,0) 0 1 0 0
RPID 6 0 1MEG
*
* AMPLIFIER
EAMP 7 0 6 0 1
RAMP 7 0 1MEG
*
* PROCESS BLOCK WITH TIME LAG (PHASE SHIFT)
EOUT 8 0 7 0 100
RP1 8 9 100K
CP1 9 0 1UF
RP2 9 10 100K
CP2 10 0 1UF
*
* SENSOR BLOCK WITH TIME LAG
ESENSOR 11 0 10 0 0.01
RP3 11 12 10K
CP3 12 0 1UF
*
* ANALYSIS
.TRAN 10MS 2000MS
*
* VIEW RESULTS
.PRINT TRAN V(1) V(12)
.PROBE
.END

What Is PID—Tutorial Overview


Get Tuning Tips Newsletter
PID stands for Proportional, Integral, Derivative. Controllers are designed to eliminate the need for
continuous operator attention. Cruise control in a car and a house thermostat are common examples of
how controllers are used to automatically adjust some variable to hold the measurement (or process
variable) at the set-point. The set-point is where you would like the measurement to be. Error is defined as
the difference between set-point and measurement.
(error) = (set-point) - (measurement) The variable being adjusted is called the manipulated variable which
usually is equal to the output of the controller. The output of PID controllers will change in response to
a change in measurement or set-point. Manufacturers of PID controllers use different names to identify
the three modes. These equations show the relationships:
P Proportional Band = 100/gain
I Integral = 1/reset (units of time)
D Derivative = rate = pre-act (units of time)
Depending on the manufacturer, integral or reset action is set in either time/repeat or repeat/time. One is
just the reciprocal of the other. Note that manufacturers are not consistent and often use reset in units of
time/repeat or integral in units of repeats/time. Derivative and rate are the same.
Choosing the proper values for P, I, and D is called "PID Tuning". Find out about PID Tuning Software
Proportional Band
With proportional band, the controller output is proportional to the error or a change in measurement
(depending on the controller).
(controller output) = (error)*100/(proportional band)
With a proportional controller offset (deviation from set-point) is present. Increasing the controller gain will
make the loop go unstable. Integral action was included in controllers to eliminate this offset.
Integral
With integral action, the controller output is proportional to the amount of time the error is present.
Integral action eliminates offset.
CONTROLLER OUTPUT = (1/INTEGRAL) (Integral of) e(t) d(t)
Notice that the offset (deviation from set-point) in the time response plots is now gone. Integral action has
eliminated the offset. The response is somewhat oscillatory and can be stabilized some by adding
derivative action. (Graphic courtesy of ExperTune Loop Simulator.)
Integral action gives the controller a large gain at low frequencies that results in eliminating offset and
"beating down" load disturbances. The controller phase starts out at –90 degrees and increases to near 0
degrees at the break frequency. This additional phase lag is what you give up by adding integral action.
Derivative action adds phase lead and is used to compensate for the lag introduced by integral action.
Derivative
With derivative action, the controller output is proportional to the rate of change of the measurement or
error. The controller output is calculated by the rate of change of the measurement with time.
dm
CONTROLLER OUTPUT = DERIVATIVE ----
dt
Where m is the measurement at time t.
Some manufacturers use the term rate or pre-act instead of derivative. Derivative, rate, and pre-act are
the same thing.
DERIVATIVE = RATE = PRE ACT
Derivative action can compensate for a changing measurement. Thus derivative takes action to inhibit
more rapid changes of the measurement than proportional action. When a load or set-point change occurs,
the derivative action causes the controller gain to move the "wrong" way when the measurement gets
near the set-point. Derivative is often used to avoid overshoot.
Derivative action can stabilize loops since it adds phase lead. Generally, if you use derivative action, more
controller gain and reset can be used.
With a PID controller the amplitude ratio now has a dip near the center of the frequency response. Integral
action gives the controller high gain at low frequencies, and derivative action causes the gain to start
rising after the "dip". At higher frequencies the filter on derivative action limits the derivative action. At
very high frequencies (above 314 radians/time; the Nyquist frequency) the controller phase and amplitude
ratio increase and decrease quite a bit because of discrete sampling. If the controller had no filter the
controller amplitude ratio would steadily increase at high frequencies up to the Nyquist frequency (1/2 the
sampling frequency). The controller phase now has a hump due to the derivative lead action and filtering.
(Graphic courtesy of ExperTune Loop Simulator.)
The time response is less oscillatory than with the PI controller. Derivative action has helped stabilize the
loop.
Control Loop Tuning
It is important to keep in mind that understanding the process is fundamental to getting a well designed
control loop. Sensors must be in appropriate locations and valves must be sized correctly with appropriate
trim.
In general, for the tightest loop control, the dynamic controller gain should be as high as possible without
causing the loop to be unstable. Choosing a controller gain is accomplished easily with PID Tuning
Software
PID Optimization Articles
Fine Tuning "Rules"
This picture (from the Loop Simulator) shows the effects of a PI controller with too much or too little P or I
action. The process is typical with a dead time of 4 and lag time of 10. Optimal is red.
You can use the picture to recognize the shape of an optimally tuned loop. Also see the response shape of
loops with I or P too high or low. To get your process response to compare, put the controller in manual
change the output 5 or 10%, then put the controller back in auto.
P is in units of proportional band. I is in units of time/repeat. So increasing P or I, decreases their action in
the picture.
View graphic in
hi-resolution

Starting
PID
Settings
For
Common
Control
Loops

PB
Loop Type
%
Flow 50 to 50

Liquid Pressure 50 to 50

Gas Pressure 1 to 50

Liquid Level 1 to 50

Temperature 2 to 100

Chromatograph 100 to 2

These settings
are rough,
assume proper
control loop
design, ideal or
series algorithm and do not apply to all controllers. Use ExperTune's PID Loop Optimizer to find the proper
PID settings for your process and controller. (From Process Control Systems (Shinskey) p.99 and Tuning
and Control Loop Performance (McMillan) p 39)

Get Tuning Tips Newsletter

PID Tutorial
Introduction
The three-term controller
The characteristics of P, I, and D controllers
Example Problem
Open-loop step response
Proportional control
Proportional-Derivative control
Proportional-Integral control
Proportional-Integral-Derivative control
General tips for designing a PID controller
Key Matlab Commands used in this tutorial are: step cloop
Note: Matlab commands from the control system toolbox are highlighted in red.

Introduction
This tutorial will show you the characteristics of the each of proportional (P), the integral
(I), and the derivative (D) controls, and how to use them to obtain a desired response. In
this tutorial, we will consider the following unity feedback system:

Plant: A system to be controlled


Controller: Provides the excitation for the plant; Designed to control the overall
system behavior
The three-term controller
The transfer function of the PID controller looks like the following:

• Kp = Proportional gain
• KI = Integral gain
• Kd = Derivative gain
First, let's take a look at how the PID controller works in a closed-loop system using the
schematic shown above. The variable (e) represents the tracking error, the difference
between the desired input value (R) and the actual output (Y). This error signal (e) will be
sent to the PID controller, and the controller computes both the derivative and the integral
of this error signal. The signal (u) just past the controller is now equal to the proportional
gain (Kp) times the magnitude of the error plus the integral gain (Ki) times the integral of
the error plus the derivative gain (Kd) times the derivative of the error.
This signal (u) will be sent to the plant, and the new output (Y) will be obtained. This
new output (Y) will be sent back to the sensor again to find the new error signal (e). The
controller takes this new error signal and computes its derivative and its integral again.
This process goes on and on.
The characteristics of P, I, and D controllers
A proportional controller (Kp) will have the effect of reducing the rise time and will
reduce ,but never eliminate, the steady-state error. An integral control (Ki) will have the
effect of eliminating the steady-state error, but it may make the transient response worse.
A derivative control (Kd) will have the effect of increasing the stability of the system,
reducing the overshoot, and improving the transient response. Effects of each of
controllers Kp, Kd, and Ki on a closed-loop system are summarized in the table shown
below.
CL RESPONSE RISE TIME OVERSHOOT SETTLING TIME S-S ERROR
Kp Decrease Increase Small Change Decrease
Ki Decrease Increase Increase Eliminate
Kd Small Change Decrease Decrease Small Change
Note that these correlations may not be exactly accurate, because Kp, Ki, and Kd are
dependent of each other. In fact, changing one of these variables can change the effect of
the other two. For this reason, the table should only be used as a reference when you are
determining the values for Ki, Kp and Kd.
Example Problem
Suppose we have a simple mass, spring, and damper problem.

The modeling equation of this system is

(1)
Taking the Laplace transform of the modeling equation (1)
The transfer function between the displacement X(s) and the input F(s) then becomes

Let
• M = 1kg
• b = 10 N.s/m
• k = 20 N/m
• F(s) = 1
Plug these values into the above transfer function

The goal of this problem is to show you how each of Kp, Ki and Kd contributes to obtain
• Fast rise time
• Minimum overshoot
• No steady-state error
Open-loop step response
Let's first view the open-loop step response. Create a new m-file and add in the following
code:

num=1;
den=[1 10 20];
step(num,den)
Running this m-file in the Matlab command window should give you the plot shown
below.
The DC gain of the plant transfer function is 1/20, so 0.05 is the final value of the output
to an unit step input. This corresponds to the steady-state error of 0.95, quite large indeed.
Furthermore, the rise time is about one second, and the settling time is about 1.5 seconds.
Let's design a controller that will reduce the rise time, reduce the settling time, and
eliminates the steady-state error.
Proportional control
From the table shown above, we see that the proportional controller (Kp) reduces the rise
time, increases the overshoot, and reduces the steady-state error. The closed-loop transfer
function of the above system with a proportional controller is:

Let the proportional gain (Kp) equals 300 and change the m-file to the following:

Kp=300;
num=[Kp];
den=[1 10 20+Kp];

t=0:0.01:2;
step(num,den,t)
Running this m-file in the Matlab command window should gives you the following plot.
Note: The Matlab function called cloop can be used to obtain a closed-loop transfer
function directly from the open-loop transfer function (instead of obtaining closed-loop
transfer function by hand). The following m-file uses the cloop command that should
give you the identical plot as the one shown above.
num=1;
den=[1 10 20];
Kp=300;

[numCL,denCL]=cloop(Kp*num,den);
t=0:0.01:2;
step(numCL, denCL,t)

The above plot shows that the proportional controller reduced both the rise time
and the steady-state error, increased the overshoot, and decreased the settling
time by small amount.

Proportional-Derivative control
Now, let's take a look at a PD control. From the table shown above, we see that
the derivative controller (Kd) reduces both the overshoot and the settling time.
The closed-loop transfer function of the given system with a PD controller is:

Let Kp equals to 300 as before and let Kd equals 10. Enter the following
commands into an m-file and run it in the Matlab command window.

Kp=300;
Kd=10;
num=[Kd Kp];
den=[1 10+Kd 20+Kp];
t=0:0.01:2;
step(num,den,t)

This plot shows that the derivative controller reduced both the overshoot and
the settling time, and had small effect on the rise time and the steady-state
error.

Proportional-Integral control
Before going into a PID control, let's take a look at a PI control. From the
table, we see that an integral controller (Ki) decreases the rise time,
increases both the overshoot and the settling time, and eliminates the steady-
state error. For the given system, the closed-loop transfer function with a PI
control is:

Let's reduce the Kp to 30, and let Ki equals to 70. Create an new m-file and
enter the following commands.

Kp=30;
Ki=70;
num=[Kp Ki];
den=[1 10 20+Kp Ki];

t=0:0.01:2;
step(num,den,t)
Run this m-file in the Matlab command window, and you should get the following
plot.
We have reduced the proportional gain (Kp) because the integral controller also
reduces the rise time and increases the overshoot as the proportional controller
does (double effect). The above response shows that the integral controller
eliminated the steady-state error.

Proportional-Integral-Derivative control
Now, let's take a look at a PID controller. The closed-loop transfer function of
the given system with a PID controller is:

After several trial and error runs, the gains Kp=350, Ki=300, and Kd=50 provided
the desired response. To confirm, enter the following commands to an m-file and
run it in the command window. You should get the following step response.
Kp=350;
Ki=300;
Kd=50;

num=[Kd Kp Ki];
den=[1 10+Kd 20+Kp Ki];

t=0:0.01:2;
step(num,den,t)
Now, we have obtained the system with no overshoot, fast rise time, and no
steady-state error.

General tips for designing a PID controller


When you are designing a PID controller for a given system, follow the steps
shown below to obtain a desired response.
1. Obtain an open-loop response and determine what needs to be improved
2. Add a proportional control to improve the rise time
3. Add a derivative control to improve the overshoot
4. Add an integral control to eliminate the steady-state error
5. Adjust each of Kp, Ki, and Kd until you obtain a desired overall response.
You can always refer to the table shown in this "PID Tutorial" page to
find out which controller controls what characteristics.
Lastly, please keep in mind that you do not need to implement all three
controllers (proportional, derivative, and integral) into a single system, if
not necessary. For example, if a PI controller gives a good enough response
(like the above example), then you don't need to implement derivative controller
to the system. Keep the controller as simple as possible.

User Feedback
Top of Form
We would like to hear about difficulties you had with the tutorials, suggestions
you have for improvement, errors that you found, or any other comments that you
have. This feedback is anonymous; include your email address if you want a
reply.

Submit Feedback Reset

PID Examples
Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted
Pendulum | Pitch Controller | Ball and Beam
Tutorials
Basics | Modeling | PID | Root Locus | Frequency Response | State
Space | Digital Control | Examples
Bottom of Form

Example: Solution to the Cruise Control Problem


Using PID control
Proportional control
PI control
PID control
The transfer function for this cruise control problem is the following,

• m = 1000
• b = 50
• U(s) = 10
• Y(s) = velocity output
and the block diagram of an typical unity feedback system is shown below.

The design criteria for this problem are:


Rise time < 5 sec
Overshoot < 10%
Steady state error < 2%
To see the original problem setup, see Cruise Control Modeling page.
Recall from the PID tutorial page, the transfer function of a PID controller is

Let's first take a look at the proportional control.


Proportional control
The first thing to do in this problem is to find a closed-loop transfer function with a
proportional control (Kp) added. By reducing the block diagram, the closed-loop transfer
function with a proportional controller becomes:

Recall from the PID tutorial page, a proportional controller (Kp) decreases the rise time.
This is what we need, if you refer to the Cruise Control Modeling page.
For now, let Kp equals 100 and see what happens to the response. Create an new m-
file and enter the following commands.
kp=100;
m=1000;
b=50;
u=10;
num=[kp];
den=[m b+kp];
t=0:0.1:20;
step(u*num,den,t)
axis([0 20 0 10])
Running this m-file in the Matlab command window should give you the following step
response.
Note: You can use the Matlab command cloop to find the closed-loop response directly
from the open-loop transfer function. If you choose to do so, change the m-file to the
following and run it in the command window. You should get the same plot as the one
shown above.
kp=100;
m=1000;
b=50;
u=10;
num=[1];
den=[m b];
[numc,denc]=cloop(kp*num,den,-1);
t = 0:0.1:20;
step (u*numc,denc,t)
axis([0 20 0 10])

As you can see from the plot, both the steady-state error and the rise time do not satisfy
our design criteria. You can increase the proportional gain (Kp) to improve the system
output. Change the existing m-file so that Kp equal 10000 and rerun it in the Matlab
command window. You should see the following plot.
The steady-state error has dropped to near zero and the rise time has decreased to less
than 0.5 second. However, this response is unrealistic because a real cruise control
system generally can not change the speed of the vehicle from 0 to 10 m/s in less than 0.5
second.
The solution to this problem is to choose a proportional gain (Kp) that will give a
reasonable rise time, and add an integral controller to eliminate the steady-state error.
PI control
The closed-loop transfer function of this cruise control system with a PI controller is:

Recall from the PID tutrial page, an addition of an integral controller to the system
eliminates the steady-state error. For now, let Kp equals 600 and Ki equals 1 and see
what happens to the response. Change your m-file to the following.

kp = 600;
ki = 1;
m=1000;
b=50;
u=10;
num=[kp ki];
den=[m b+kp ki];
t=0:0.1:20;
step(u*num,den,t)
axis([0 20 0 10])

Note: If you choose to obtain the closed-loop response directly from the open-loop
transfer function, enter the following commands instead of the ones shown above:
kp=600;
ki=1;
m=1000;
b=50;
u=10;
num=[1];
den=[m b];
num1=[kp ki];
den1=[1 0];
num2=conv(num,num1);
den2=conv(den,den1);
[numc,denc]=cloop(num2,den2,-1);
t=0:0.1:20;
step(u*numc,denc,t)
axis([0 20 0 10])

Whichever the m-file you run, you should get the following output:

Now adjust both the proportional gain (Kp) and the integral gain (Ki) to obtain the
desired response. When you adjust the integral gain (Ki), we suggest you to start with a
small value since large (Ki) most likely unstabilize the response.
With Kp equals 800 and Ki equals 40, the step response will look like the following:
As you can see, this step response meets all design criteria.
PID control
For this particular example, no implementation of a derivative controller was needed to
obtain a required output. However, you might want to see how to work with a PID
control for the future reference. The closed-loop transfer function for this cruise control
system with a PID controller is.

Let Kp equals 1, Ki equals 1, and Kd equals 1 and enter the following commands into an
new m-file.

kp=1;
ki=1;
kd=1;
m=1000;
b=50;
u=10;
num=[kd kp ki];
den=[m+kd b+kp ki];
t=0:0.1:20;
step(u*num,den,t)
axis([0 20 0 10])

Running this m-file should give you the step response of the system with PID controller.
Adjust all of Kp, Kd, and Ki until you obtain satisfactory results. We will leave this as an
exercise for you to work on.
Suggestion: Usually choosing appropriate gains require trial and error processes. The best
way to attack this tedious process is to adjust one variable (Kp, Kd, or Ki) at a time and
observe how changing one variable influences the system output. The characteristics of
Kp, Kd, and Ki are summarized in the PID Tutorial page.

Example: PID Design Method for DC Motor


Speed Control
Proportional control
PID control
Tuning the gains
From the main problem, the dynamic equations and the open-loop transfer function of the
DC Motor are:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to
the Modeling a DC Motor page.
With a 1 rad/sec step input, the design criteria are:
• Settling time less than 2 seconds
• Overshoot less than 5%
• Steady-stage error less than 1%
Now let's design a PID controller and add it into the system. First create a new m-file and
type in the following commands (refer to the Modeling page for the details of getting
these commands).
J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
num=K;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];
Recall that the transfer function for a PID controller is:

Proportional control
Let's first try using a proportional controller with a gain of 100. Add the following code
to the end of your m-file:
Kp=100;
numa=Kp*num;
dena=den;
To determine the closed-loop transfer function, we use the cloop command. Add the
following line to your m-file:
[numac,denac]=cloop(numa,dena);
Note that numac and denac are the numerator and the denominator of the overall closed-
loop transfer function.
Now let's see how the step response looks, add the following to the end of your m-file,
and run it in the command window:
t=0:0.01:5;
step(numac,denac,t)
title('Step response with Proportion Control')
You should get the following plot:

PID control
From the plot above we see that both the steady-state error and the overshoot are too
large. Recall from the PID tutorial page that adding an integral term will eliminate the
steady-state error and a derivative term will reduce the overshoot. Let's try a PID
controller with small Ki and Kd. Change your m-file so it looks like the following.
Running this new m-file gives you the following plot.

J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
num=K;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];

Kp=100;
Ki=1;
Kd=1;
numc=[Kd, Kp, Ki];
denc=[1 0];
numa=conv(num,numc);
dena=conv(den,denc);
[numac,denac]=cloop(numa,dena);
step(numac,denac)
title('PID Control with small Ki and Kd')

Tuning the gains


Now the settling time is too long. Let's increase Ki to reduce the settling time. Go back to
your m-file and change Ki to 200. Rerun the file and you should get the plot like this:
Now we see that the response is much faster than before, but the large Ki has worsened
the transient response (big overshoot). Let's increase Kd to reduce the overshoot. Go back
to the m-file and change Kd to 10. Rerun it and you should get this plot:

So now we know that if we use a PID controller with


Kp=100,
Ki=200,
Kd=10,
all of our design requirements will be satisfied.

Example: PID Design Method for the DC Motor


Position
Proportional control
PID control
Tuning the gains
From the main problem, the dynamic equations in transfer function form are the
following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to
the Modeling a DC Motor page.
With a 1 rad/sec step reference, the design criteria are:
• Settling time less than 0.04 seconds
• Overshoot less than 16%
• No steady-state error
• No steady-state error due to a disturbance
Now let's design a PID controller and add it into the system. First create a new m-file and
type in the following commands(refer to main problem for the details of getting those
commands).
J=3.2284E-6;
b=3.5077E-6;
K=0.0274;
R=4;
L=2.75E-6;
num=K;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];
Recall that the transfer function for a PID controller is:

Proportional control
Let's first try using a proportional controller with a gain of 1.7. Add the following code to
the end of your m-file:
Kp=1.7;
numcf=[Kp];
dencf=[1];
numf=conv(numcf,num);
denf=conv(dencf,den);
To determine the closed-loop transfer function, we use the cloop command. Add the
following line to your m-file:
[numc,denc]=cloop(numf,denf);
Note that numc and denc are the numerator and the denominator of the overall closed-loop
transfer function.
Now let's see how the step response looks. Add the following to the end of your m-file,
and run it in the command window:
t=0:0.001:0.2;
step(numc,denc,t)
You should get the following plot:

Now lets take a look at the step disturbance response. Add the following to the end of
your m-file, and run it in the command window:
numdcl=conv(numc,1);
dendcl=conv(denc,Kp);
step(numdcl,dendcl,t);
You should get the following plot:

PID control
From the plots above we see that although the steady-state error looks good the settling
time is too large, as is the overshoot. We also see that the steady-state error to a
disturbance is large. Recall from PID tutorial page that adding an integral term will
eliminate the steady-state error and a derivative term will reduce the overshoot. Let's first
try a PI controller to get rid of the disturbance steady state error. Change your m-file so it
looks like:

J=3.2284E-6;
b=3.5077E-6;
K=0.0274;
R=4;
L=2.75E-6;
num=K;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2) 0];

Kp=1.7;
Ki=20;
numcf=[Kp Ki];
dencf=[1 0];
numf=conv(numcf,num);
denf=conv(dencf,den);
[numc,denc]=cloop(numf,denf,-1);
t=0:0.001:0.4;
step(numc,denc,t)
You should get the following step response:
Lets see what happened to the step disturbance response, add the following to your m-
file:
figure
numdcl=conv(numc,dencf);
dendcl=conv(denc,numcf);
step(numdcl,dendcl,t);
You should get the following plot:

Tuning the gains


The settling time is still too long. Let's increase the gains in order to speed up the
response. Go back to your m-file and change Ki to 200 and Kp to 17. Rerun the file and
you should get plots like these:
Now we see that the response is faster than before, but the large Ki has worsened the
transient response (big overshoot). Let's now try a PID controller to reduce the overshoot.
Go back to the m-file and make the following changes to look at the step response.
Kp=17;
Ki=200;
Kd=0.15;
numcf=[Kd Kp Ki];
dencf=[1 0];
numf=conv(numcf,num);
denf=conv(dencf,den);
[numc,denc]=cloop(numf,denf,-1);
t=0:0.001:0.1;
step(numc,denc,t)
Rerun it and you should get this plot:

Your step disturbance plot should look like this:

We now see that our step response looks really good, it has less than 16% overshoot and
the settling time is roughly 40ms, and there is no steady-state error. However the step
disturbance response is now really slow. Lets increase Ki to speed up the disturbance
response. Change Ki to 600 in your m-file and rerun the file. You should get the
following plots:
We now can see that the step response has a settling time of roughly 40ms, it has less
than 16% overshoot, and it has no steady state error. The step disturbance response also
has no steady state error. So now we know that if we use a PID controller with
Kp=17,
Ki=600,
Kd=.15,
all of our design requirements will be satisfied.
Example:PID Design Method for the Bus
Suspension System
Adding a PID controller
Plotting the closed-loop response
Choosing the gains for the PID controller
From the main problem, the dynamic equations in transfer function form are the
following:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations and schematic,
please refer to the bus modeling page.
We want to design a feedback controller so that when the road disturbance (W) is
simulated by a unit step input, the output (X1-X2) has a settling time less than 5 seconds
and an overshoot less than 5%. For example, when the bus runs onto a 10 cm high step,
the bus body will oscillate within a range of +/- 5 mm and will stop oscillating within 5
seconds.
The system model can be represented in Matlab by creating a new m-file and entering the
following commands (refer to main problem for the details of getting those commands).
m1=2500;
m2=320;
k1 = 80000;
k2 = 500000;
b1 = 350;
b2 = 15020;

nump=[(m1+m2) b2 k2]
denp=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+
(b2*k1) k1*k2]

num1=[-(m1*b2) -(m1*k2) 0 0]
den1=[(m1*m2) (m1*(b1+b2))+(m2*b1) (m1*(k1+k2))+(m2*k1)+(b1*b2) (b1*k2)+
(b2*k1) k1*k2]

numf=num1;
denf=nump;

Adding a PID controller


Recall that the transfer function for a PID controller is:

where KP is the proportional gain, KI is the integral gain, and KD is the derivative gain.
Let's assume that we will need all three of these gains in our controller. To begin, we
might start with guessing a gain for each: KP=208025, KI=832100 and KD=624075.
This can be implemented into Matlab by adding the following code into your m-file:
KD=208025;
KP=832100;
KI=624075;
numc=[KD,KP,KI];
denc=[1 0];
Now let's simulate the response of the system (the distance X1-X2) to a step disturbance on the road.
From the schematic above we can find the transfer function from the road disturbance W to the
output(X1-X2):

This transfer function can be modeled in Matlab by adding the following code into your m-file:
numa=conv(conv(numf,nump),denc);
dena=conv(denf,polyadd(conv(denp,denc),conv(nump,numc)));
Note that the function "polyadd" is not a standard function in Matlab; you will need to copy it to a
new m-file to use it. Click here for more information on defining new functions in Matlab.
Refer to the bus modeling page, nump = denf as we can see in the matlab command
above. Thus we can simplified this transfer function to be the following:
numa=conv(numf,denc);
dena=polyadd(conv(denp,denc),conv(nump,numc));

Plotting the closed-loop response


Now we have created the closed-loop transfer function in Matlab that will represent the
plant, the disturbance, as well as the controller. Let's see what the closed-loop step
response for this system looks like before we begin the control process. Keep in mind that
we are going to use a 0.1 m high step as our disturbance, to simulate this, all we need to
do is to multiply numa by 0.1. Add the following code into your m-file:
t=0:0.05:5;
step(0.1*numa,dena,t)
title('closed-loop response to 0.1m high step w/ pid controller')

you should see the response (X1-X2) to a step W like this:

From the graph, the percent overshoot = 9%, which is 4% larger than the requirement, but the
settling time is satisfied, less than 5 seconds. To choose the proper gain that yields reasonable output
from the beginning, we start with choosing a pole and two zeros for PID controller. A pole of this
controller must be at zero and one of the zeros has to be very close to the pole at the origin, at 1. The
other zero, we will put further from the first zero, at 3, actually we can adjust the second-zero's
position to get the system to fulfill the requirement. Add the following command in the m-file, so
you can adjust the second-zero's location and choose the gain to have a rough idea what gain you
should use for KD,KP, and KI.
z1=1;
z2=3;
p1=0;
numc=conv([1 z1],[1 z2])
denc=[1 p1]
num2=conv(nump,numc);
den2=conv(denp,denc);
rlocus(num2,den2)
title('root locus with PID controller')
[K,p]=rlocfind(num2,den2)
you should see the closed-loop poles and zeros on the s-plane like this and you can choose the gain
and dominant poles on the graph by yourself:

We will explain root locus method in more detail in the "Root Locus" page.
Choosing the gains for the PID controller
Now that we have the closed-loop transfer function, controlling the system is simply a
matter of changing the KD,KP,and KI variables. From the figure above, we can see that
the system has larger damping than required, but the settling time is very short. This
response still doesn't satisfy the 5% overshoot requirement. As mentioned before, this can
be rectified by adjusting the KD, KP and KI variables to find better response. Let's
increase KP,KI,KD by 2 times to see what will happen. Go back to your m-file and
multiply KP,KI,KD by 2 and then rerun the program, you should get the following plot:
To compare this graph with the graph of low-gain PID controller, you can change the
axis:
axis([0 5 -.01 .01])

Now we see that the percent overshoot and settling time meet the requirements of the system. The
percent overshoot is about 5% of the input's amplitude and settling time is 2 seconds less than 5
seconds from requirement.
For this problem, it turns out that the PID design method adequately controls the system.
This can been seen by looking at the root locus plot. Such a task can be achieved by
simply changing only the gains of a PID controller. Feel free to play around with all three
of the parameters,KD,KP and KI, as we suggested, but you will most likely get the
response to have either large percent overshoot or very long settling time. But if you do
find a good PID design, please email us with your results! We are always interested in
different ways to solve our examples; we may include your solution in a future version of
these tutorials.

Example: Solution to the Inverted Pendulum


Problem Using PID Control
Open-loop Representation
Closed-loop transfer function
Adding the PID controller
What happens to the cart's position?
The transfer function of the plant for this problem is given below:

where,

The design criteria (with the pendulum receiving a 1N impulse force from the cart) are:
• Settling time of less than 5 seconds.
• Pendulum should not move more than 0.05 radians away from the vertical.
To see how this problem was originally set up, consult the inverted pendulum
modeling page.
Open-loop Representation
The first thing to do when using PID control in Matlab is to find the transfer function of
the system and to check to see if it makes sense. The transfer function found from the
Laplace transforms for the output Phi (the pendulum's angle) can be set up using Matlab
by inputting the numerator and denominator as vectors. Create anm-file (or a '.m' file
located in the same directory as Matlab) and copy the following text to model the transfer
function:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num = [m*l/q 0]
den = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q]
Your output should be:

num =
4.5455 0

den =
1.0000 0.1818 -31.1818 -4.4545

Closed-loop transfer function


The control of this problem is a little different than the standard control problems you
may be used to. Since we are trying to control the pendulum's position, which should
return to the vertical after the initial disturbance, the reference signal we are tracking
should be zero. The force applied to the cart can be added as an impulse disturbance. The
schematic for this problem should look like the following.

It will be easier to determine the appropriate transfer function to enter into Matlab if we
first rearrange the schematic as follows:
Now, we can find the closed-loop transfer function.

Adding the PID controller


This closed-loop transfer function can be modeled in Matlab by copying the following
code to the end of your m-file (whether your using the transfer function from the Laplace
transforms or from the state-space representation):

kd = 1;
k = 1;
ki = 1;
numPID = [kd k ki];
denPID = [1 0];
numc = conv(num,denPID)
denc = polyadd(conv(denPID,den),conv(numPID,num))

Note: Non-standard Matlab commands used in this example are highlighted in green.
The function polyadd is not in the Matlab toolbox. You will have to copy it to a new m-
file to use it. This transfer function assumes that both derivative and integral control will
be needed along with proportional control. This does not have to be the case. If you
wanted to start with PI control, just remove the kd term fromnumPID. If you wanted to start
with PD control, just remove the ki term from numPID and change denPID to equal [1].
Assuming you do not change the PID control, you should get the following closed-loop
numerator and denominator in the Matlab command window:
numc =
4.5455 0 0

denc =
1.0000 4.7273 -26.6363 0.0910 0
Now we can begin the actual control of this system. First let's see what the impulse
response looks like with the numbers we already have. Enter the following code to the
end of your m-file:
t=0:0.01:5;
impulse(numc,denc,t)
axis([0 1.5 0 40])
You should get the following velocity response plot from the impulse disturbance:

This response is still not stable. Let's start by increasing the proportional control to the
system. Increase the k variable to see what effect it has on the response. If you set k=100,
and set the axis to axis([0, 2.5, -0.2, 0.2]), you should get the following velocity
response plot:
The settling time is acceptable at about 2 seconds. Since the steady-state error has already
been reduced to zero, no more integral control is needed. You can remove the integral
gain constant to see for yourself that the small integral control is needed. The overshoot is
too high, so that must be fixed. To alleviate this problem, increase the kd variable.
With kd=20, you should get a satisfactory result. You should now see the following
velocity response plot:
As you can see, the overshoot has been reduced so that the pendulum does not move
more than 0.05 radians away from the vertical. All of the design criteria have been met,
so no further iteration is needed.
What happens to the cart's position?
At the beginning on this solution page, the block diagram for this problem was given.
The diagram was not entirely complete. The block representing the the position was left
out because that variable was not being controlled. It is interesting though, to see what is
happening to the cart's position when the controller for the pendulum's angle is in place.
To see this we need to consider the actual system block diagram:

Rearranging a little bit, you get the following block diagram:

The feedback loop represents the controller we have designed for the pendulum's. The
transfer function from the cart's position to the impulse force, with the PID feedback
controller which we designed, is given as follows:

Recall that den1=den2 if the pole/zero at the origin that was cancelled is added back in.
So the transfer function from X to F can be simplified to:
Now that we have the transfer function for the entire system, let's take a look at the
response. First we need the transfer function for the cart's position. To get this we need to
go back to the laplace transforms of the system equations and find the transfer function
from X(s) to U(s). Below is this transfer function:

where,

For more about the Laplace transform please refer to the inverted pendulum
modeling page.
The pole/zero at the origin cancelled out of the transfer function for Phi, has been put
back in. So that now den1 = den2, making calculations easier. Now, create a new m-file
and run it in the command window:

M = .5;
m = 0.2;
b = 0.1;
i = 0.006;
g = 9.8;
l = 0.3;

q = (M+m)*(i+m*l^2)-(m*l)^2; %simplifies input

num1 = [m*l/q 0 0];


den1 = [1 b*(i+m*l^2)/q -(M+m)*m*g*l/q -b*m*g*l/q 0];

num2 = [(i+m*l^2)/q 0 -m*g*l/q];


den2 = den1

kd = 20;
k = 100;
ki = 1;
numPID = [kd k ki];
denPID = [1 0];

numc = conv(num2,denPID);
denc = polyadd(conv(denPID,den2),conv(numPID,num1));
t=0:0.01:5;
impulse(numc,denc,t)

As you can see, the cart moves in the negative direction with a constant velocity. So
although the PID controller stabilizes the angle of the pendulum, this design would not be
feasible to implement on an actual physical system.

Example: PID Design method for the Pitch


Controller
Proportional control
Proportional-Derivative control
Proportional-Integral-Derivative control
In the Pitch Controller Modeling page, the transfer function was derived as

The input (elevator deflection angle, delta e) will be 0.2 rad (11 degrees), and the output
is the pitch angle (theta).
The design requirements are
• Overshoot: Less than 10%
• Rise time: Less than 2 seconds
• Settling time: Less than 10 seconds
• Steady-state error: Less than 2%
To see the original problem setup, please refer to the Pitch Controller Modeling page.
Recall from the PID Tutorial page, the transfer function of a PID controller is:

We will implement combinations of proportional (Kp), integral (Ki), and derivative (Kd)
controllers in an unity feedback system shown below to study the system output.

Let's first take a look at a proportional control.,


Proportional control
The first thing in solving this problem using PID control is to find a closed-loop transfer
function with a proportional control (Kp). A closed-loop transfer function can be
obtained either by hand or using the Matlab function called cloop. Either way, you should
obtain the closed-loop transfer function as:

Note: Matlab cannot manipulate symbolic variables. To use the function cloop, enter the
following commands to an m-file and run it in the Matlab command window. You should
obtain the numerical coefficients of numerator and denominator of the closed-loop
transfer function.
Kp=[1]; %Enter any numerical value for the proportional gain
num=[1.151 0.1774];
num1=conv(Kp,num);
den1=[1 0.739 0.921 0];

[numc,denc]=cloop (num1,den1)

For now, let the proportional gain (Kp) equal 2 and observe the system behavior. We will
use the closed-loop transfer function derived by hand. Enter the following commands into
a new m-file and run it in the Matlab command window. You should obtain the step
response similar to the one shown below:
de=0.2;
Kp=2;

numc=Kp*[1.151 0.1774];
denc=[1 0.739 1.151*Kp+0.921 0.1774*Kp];

t=0:0.01:30;
step (de*numc,denc,t)

As you see, both the overshoot and the settling time need some improvement.
PD control
Recall from the PID Tutorial page, the derivative controller will reduce both the
overshoot and the settling time. Let's try a PD controller. The closed-loop transfer
function of the system with a PD controller is:

Using the commands shown below and with several trial-and-error runs, a proportional
gain (Kp) of 9 and a derivative gain (Kd) of 4 provided the reasonable response. To
comfirm this, change your m-file to the following and run it in the Matlab command
window. You should obtain the step response similar to the one shown below:

de=0.2;
Kp=9;
Kd=4;

numc=[1.151*Kd 1.151*Kp+0.1774*Kd 0.1774*Kp];


denc=[1 0.739+1.151*Kd 0.921+1.151*Kp+0.1774*Kd 0.1774*Kp];

t=0:0.01:10;
step (de*numc,denc,t)

This step response shows the rise time of less than 2 seconds, the overshoot of less than
10%, the settling time of less than 10 seconds, and the steady-state error of less than 2%.
All design requirements are satisfied.
PID Control
Even though all design requirements were satisfied with the PD controller, the integral
controller (Ki) can be added to reduce the sharp peak and obtain smoother response.
After several trial-and-error runs, the proportional gain (Kp) of 2, the integral gain (Ki) of
4, and the derivative gain (Kd) of 3 provided smoother step response that still satisfies all
design requirements. To cofirm this, enter the following commands to an m-file and run it
in the command window. You should obtain the step response shown below:

Note: This time we are going to use the function cloop to find the closed-loop transfer
function, and then obtain the step response.
de=0.2;
Kp=2;
Kd=3;
Ki=4;

numo=[1.151 0.1774];
deno=[1 0.739 0.921 0];
numpid=[Kd Kp Ki];
denpid=[1 0];

num1=conv(numo,numpid);
den1=conv(deno,denpid);

[numc,denc] = cloop(num1,den1);
t=0:0.01:10;
step (de*numc,denc,t)
Comments
1. To find appropriate gains (Kp, Kd, and Ki), you can use the table shown in PID
Tutorial page; however, please keep in your mind that changing one gain might
change the effect of the other two. As a result, you may need to change other two
gains as you change one gain.
2. Our system with a PI controller do not provide the desired response; thus, a PI
Control section was omitted from this page. You may confirm this by using the
m-file shown in PID Control section, and set Kd equals to zero.

Example: Solution to the Ball & Beam Problem


Using PID Control
Closed-loop Representation
Proportional Control
Proportional-Derivative Control
The open-loop transfer function of the plant for the ball and beam experiment is given
below:

The design criteria for this problem are:


• Settling time less than 3 seconds
• Overshoot less than 5%
To see the derivation of the equations for this problem refer to the ball and beam
modeling page.
Closed-loop Representation
The block diagram for this example with a controller and unity feedback of the ball's
position is shown below:

First, we will study the response of the system shown above when a proportional
controller is used. Then, derivative and/or integral control will be added if necessary.
Recall, that the transfer function for a PID controller is:

Proportional Control
The closed-loop transfer function for proportional control with a proportional gain (kp)
equal to 100, can be modeled by copying the following lines of Matlab code into an m-
file (or a '.m' file located in the same directory as Matlab)
m = 0.111;
R = 0.015;
g = -9.8;
L = 1.0;
d = 0.03;
J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];
den = [1 0 0];

kp = 1;
numP = kp*num;

[numc, denc] = cloop(numP, den)

NOTE: Matlab commands from the control system toolbox are highlighted in red.

You numerator and denominator should be:


numc =

0 0 0.2100

denc =

1.0000 0 0.2100
Now, we can model the system's response to a step input of 0.25 m. Add the following
line of code to your m-file and run it:
step(0.25*numc,denc)

You should get the following output:

As, you can see the addition of proportional gain does not make the system stable. Try
changing the value of kp and note that the system remains unstable.
Proportional-Derivative Control
Now, we will add a derivative term to the controller. Copy the following lines of code to
an m-file and run it to view the system's response to this control method.
m = 0.111;
R = 0.015;
g = -9.8;
L = 1.0;
d = 0.03;
J = 9.99e-6;

K = (m*g*d)/(L*(J/R^2+m)); %simplifies input

num = [-K];
den = [1 0 0];
kp = 10;
kd = 10;
numPD = [kd kp];

numh = conv(num, numPD);


[numc, denc] = cloop(numh, den);
t=0:0.01:5;
step(0.25*numc,denc,t)
Your plot should be similar to the following:

Now the system is stable but the overshoot is much too high and the settling time needs
to go down a bit. From the PID tutorial page in the section on characteristics of P, I, and
D controllers, we see that by increasing kd we can lower overshoot and decrease the
settling time slightly. Therefore, make kd = 20 in your m-file and run it again. Your
output should be:
The overshoot criterion is met but the settling time needs to come down a bit. To
decrease the settling time we may try increasing the kp slightly to increase the rise time.
The derivative gain (kd) can also be increased to take off some of the overshoot that
increasing kp will cause. After playing with the gains a bit, the following step response
plot can be achieved with kp = 15 and kd = 40:

As you can see from the above plot all the control objectives have been met without the
use of an integral controller (settling time for this example is considered achieved when
the response is less than 2% of it's final value). Remember, that for a control problem
there is more than one solution for the problem.
For other methods of controlling the ball and beam example, see the root locus, frequency
response, and state-space links below.

Você também pode gostar