Professional Documents
Culture Documents
PID Tutorial PDF
PID Tutorial PDF
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:
• 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
1
takes this new error signal and computes its derivative and its integral again. This process
goes on and on.
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.
2
Example (1)
Solution to the Cruise Control Problem Using
PID control
The transfer function for this cruise control problem is the following,
𝑌(𝑠) 1
=
𝑈(𝑠) 𝑚𝑠 + 𝑏
• m = 1000
• b = 50
• R(s) = 10
• Y(s) = velocity output
and the block diagram of an typical unity feedback system is shown below.
Recall from the PID tutorial page, the transfer function of a PID controller is
Proportional control
3
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.
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;
R=10;
num=[kp];
den=[m b+kp];
t=0:0.1:20;
step(R*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;
R=10;
4
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:
𝑌(𝑠) 𝐾𝑝 𝑠 + 𝐾𝑖
=
𝑅(𝑠) 𝑚𝑠 + (𝑏 + 𝐾𝑝 )𝑠 + 𝐾𝑖
2
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;
5
ki = 1;
m=1000;
b=50;
R=10;
num=[kp ki];
den=[m b+kp ki];
t=0:0.1:20;
step(R*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;
R=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(R*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.
6
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.
𝑌(𝑠) 𝐾𝐷 𝑠 2 + 𝐾𝑝 𝑠 + 𝐾𝑖
=
𝑅(𝑠) (𝑚 + 𝐾𝐷 )𝑠 2 + (𝑏 + 𝐾𝑝 )𝑠 + 𝐾𝑖
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;
R=10;
num=[kd kp ki];
den=[m+kd b+kp ki];
t=0:0.1:20;
step(R*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.
7
Example (2)
Suppose we have a simple mass, spring, and damper problem.
8
(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
The goal of this problem is to show you how each of Kp, Ki and Kd contributes to obtain
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)
9
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];
10
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.
11
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
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)
12
Now, we have obtained the system with no overshoot, fast rise time, and no
steady-state error.
Example (3)
13
PID Design Method for DC Motor Speed
Control
DC Motor Speed Modeling
Physical setup and system equations
A common actuator in control systems is the DC motor. It directly provides rotary motion
and, coupled with wheels or drums and cables, can provide transitional motion. The electric
circuit of the armature and the free body diagram of the rotor are shown in the following
figure:
For this example, we will assume the following values for the physical parameters. These
values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate
controls lab.
The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back
emf, e, is related to the rotational velocity by the following equations:
From the figure above we can write the following equations based on Newton's law
combined with Kirchhoff's law:
14
Transfer Function
Using Laplace Transforms, the above modeling equations can be expressed in terms of s.
By eliminating I(s) we can get the following open-loop transfer function, where the rotational
speed is the output and the voltage is the input.
Design requirements
First, our uncompensated motor can only rotate at 0.1 rad/sec with an input voltage of 1 Volt
(this will be demonstrated later when the open-loop response is simulated). Since the most
basic requirement of a motor is that it should rotate at the desired speed, the steady-state error
of the motor speed should be less than 1%. The other performance requirement is that the
motor must accelerate to its steady-state speed as soon as it turns on. In this case, we want it
to have a settling time of 2 seconds. Since a speed faster than the reference may damage the
equipment, we want to have an overshoot of less than 5%.
If we simulate the reference input (r) by an unit step input, then the motor speed output
should have:
We can represent the above transfer function into Matlab by defining the numerator and
denominator matrices as follows:
15
Create a new m-file and enter the following 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)];
Now let's see how the original open-loop system performs. Add the following commands
onto the end of the m-file and run it in the Matlab command window:
step(num,den,0:0.1:3)
title('Step Response for the Open Loop System')
You should get the following plot:
From the plot we see that when 1 volt is applied to the system, the motor can only achieve a
maximum speed of 0.1 rad/sec, ten times smaller than our desired speed. Also, it takes the
motor 3 seconds to reach its steady-state speed; this does not satisfy our 2 seconds settling
time criterion.
16
and the system schematic looks like:
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:
17
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;
18
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')
19
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:
Kp=100,
Ki=200,
Kd=10,
20