Control Pid
Control Pid
Control Pid
Appendix part 6
Simulink is a graphical extension to MATLAB for modeling and simulation of systems. One of
the main advantages of Simulink is the ability to model a nonlinear system, which a transfer
function is unable to do. Another advantage of Simulink is the ability to take on initial conditions.
When a transfer function is built, the initial conditions are assumed to be zero.
In Simulink, systems are drawn on screen as block diagrams. Many elements of block diagrams
are available, such as transfer functions, summing junctions, etc., as well as virtual input and output
devices such as function generators and oscilloscopes. Simulink is integrated with MATLAB and
data can be easily transferred between the programs. In these tutorials, we will apply Simulink to
the examples from the MATLAB tutorials to model the systems, build controllers, and simulate
the systems. Simulink is supported on Unix, Macintosh, and Windows environments; and is
included in the student version of MATLAB for personal computers. For more information on
Simulink, please visit the MathWorks link at the top of the page.
The idea behind these tutorials is that you can view them in one window while running Simulink
in another window. System model files can be downloaded from the tutorials and opened in
Simulink. You will modify and extend these system while learning to use Simulink for system
modeling, control, and simulation. Do not confuse the windows, icons, and menus in the tutorials
for your actual Simulink windows. Most images in these tutorials are not live - they simply display
what you should see in your own Simulink windows. All Simulink operations should be done in
your Simulink windows.
Contents
6.1. Starting Simulink .......................................................................................................................... 2/6
Simulink is started from the MATLAB command prompt by entering the following command:
simulink
Alternatively, you can hit the Simulink button at the top of the MATLAB window as shown here:
When it starts, Simulink brings up a single window, entitled Simulink Library Browser which
can be seen here.
simple.mdl
Open this file in Simulink by entering the following command in the MATLAB command window.
(Alternatively, you can load this file using the Open option in the File menu in Simulink, or by
hitting Ctrl-O in Simulink).
simple
A new model can be created by selecting New from the File menu in any Simulink window (or by
hitting Ctrl-N).
There are two major classes of items in Simulink: blocks and lines. Blocks are used to generate,
modify, combine, output, and display signals. Lines are used to transfer signals from one block to
another.
Blocks
There are several general classes of blocks within the Simulink library:
Blocks have zero to several input terminals and zero to several output terminals. Unused input
terminals are indicated by a small open triangle. Unused output terminals are indicated by a small
triangular point. The block shown below has an unused input terminal on the left and an unused
output terminal on the right.
Lines
Lines transmit signals in the direction indicated by the arrow. Lines must always transmit signals
from the output terminal of one block to the input terminal of another block. On exception to this
is a line can tap off of another line, splitting the signal to each of two destination blocks, as shown
below (split.mdl).
Lines can never inject a signal into another line; lines must be combined through the use of a block
such as a summing junction.
A signal can be either a scalar signal or a vector signal. For Single-Input, Single-Output (SISO)
systems, scalar signals are generally used. For Multi-Input, Multi-Output (MIMO) systems, vector
signals are often used, consisting of two or more scalar signals. The lines used to transmit scalar
and vector signals are identical. The type of signal carried by a line is determined by the blocks on
either end of the line.
The simple model consists of three blocks: Step, Transfer Function, and Scope. The Step is a
Source block from which a step input signal originates. This signal is transferred through the line
in the direction indicated by the arrow to the Transfer Function Continuous block. The Transfer
Function block modifies its input signal and outputs a new signal on a line to the Scope. The Scope
is a Sink block used to display a signal much like an oscilloscope.
There are many more types of blocks available in Simulink, some of which will be discussed later.
Right now, we will examine just the three we have used in the simple model.
A block can be modified by double-clicking on it. For example, if you double-click on the Transfer
Function block in the Simple model, you will see the following dialog box.
This dialog box contains fields for the numerator and the denominator of the block's transfer
function. By entering a vector containing the coefficients of the desired numerator or denominator
polynomial, the desired transfer function can be entered. For example, to change the denominator
to
(1)
[1 2 4]
and hit the close button, the model window will change to the following,
The Step block can also be double-clicked, bringing up the following dialog box.
The default parameters in this dialog box generate a step function occurring at time = 1 sec, from
an initial level of zero to a level of 1 (in other words, a unit step at t = 1). Each of these parameters
can be changed. Close this dialog before continuing.
The most complicated of these three blocks in the Scope block. Double-clicking on this brings up
a blank oscilloscope screen.
When a simulation is performed, the signal which feeds into the scope will be displayed in this
window. Detailed operation of the scope will not be covered in this tutorial. The only function
we will use is the autoscale button, which appears as a pair of binoculars in the upper portion of
the window.
simple2.mdl
Open this file in Simulink following the previous instructions for this file. You should see the
following model window.
Before running a simulation of this system, first open the scope window by double-clicking on the
scope block. Then, to start the simulation, either select Start from the Simulation menu, click the
Play button at the top of the screen, or hit Ctrl-T.
The simulation should run very quickly and the scope window will appear as shown below.
Note that the simulation output (shown in yellow) is at a very low level relative to the axes of the
scope. To fix this, hit the autoscale button (binoculars), which will rescale the axes as shown
below.
Note that the step response does not begin until t = 1. This can be changed by double-clicking on
the step block. Now, we will change the parameters of the system and simulate the system again.
Double-click on the Transfer Function block in the model window and change the denominator
to:
[1 20 400]
Re-run the simulation (hit Ctrl-T) and you should see what appears as a flat line in the scope
window. Hit the autoscale button, and you should see the following in the scope window.
Notice that the autoscale button only changes the vertical axis. Since the new transfer function has
a very fast response, it compressed into a very narrow part of the scope window. This is not really
a problem with the scope, but with the simulation itself. Simulink simulated the system for a full
ten seconds even though the system had reached steady state shortly after one second.
To correct this, you need to change the parameters of the simulation itself. In the model window,
select Configuration Parameters from the Simulation menu. You will see the following dialog
box.
There are many simulation parameter options; we will only be concerned with the start and stop
times, which tell Simulink over what time period to perform the simulation. Chnage Start time
from 0.0 to 0.8 (since the step doesn't occur until t = 1.0). Change Stop time from 10.0 to 2.0,
which should be only shortly after the system settles. Close the dialog box and rerun the simulation.
After hitting the autoscale button, the scope window should provide a much better display of the
step response as shown below.
In this section, you will learn how to build systems in Simulink using the building blocks in
Simulink's Block Libraries. You will build the following system.
First, you will gather all of the necessary blocks from the block libraries. Then you will modify
the blocks so they correspond to the blocks in the desired model. Finally, you will connect the
blocks with lines to form the complete system. After this, you will simulate the complete system
to verify that it works.
Gathering Blocks
• Create a new model (New from the File menu or hit Ctrl-N). You will get a blank model
window.
• Click on the Sources listing in the main Simulink window.
• This will bring up the Sources block library. Sources are used to generate signals.
• Drag the Step block from the Sources window into the left side of your model window.
Modify Blocks
• Double-click on the Sum block. Since you will want the second input to be subtracted, enter
+- into the list of signs field. Close the dialog box.
• Double-click the Gain block. Change the gain to 2.5 and close the dialog box.
• Double-click the PID Controller block and change the Proportional gain to 1 and the
Integral gain to 2. Close the dialog box.
• Double-click the Transfer Function block. Leave the numerator [1], but change the
denominator to [1 2 4]. Close the dialog box. The model should appear as:
• Change the name of the PID Controller block to PI Controller by double-clicking on the
word PID Controller.
• Similarly, change the name of the Transfer Function block to Plant. Now, all the blocks
are entered properly. Your model should appear as:
Now that the blocks are properly laid out, you will now connect them together. Follow these steps.
• Drag the mouse from the output terminal of the Step block to the positive input of the
Sum input. Another option is to click on the Step block and then Ctrl-Click on the Sum
block to connect the two togther. You should see the following.
• The resulting line should have a filled arrowhead. If the arrowhead is open and red, as
shown below, it means it is not connected to anything.
• You can continue the partial line you just drew by treating the open arrowhead as an output
terminal and drawing just as before. Alternatively, if you want to redraw the line, or if the
line connected to the wrong terminal, you should delete the line and redraw it. To delete a
line (or any other object), simply click on it to select it, and hit the delete key.
• Draw a line connecting the Sum block output to the Gain input. Also draw a line from the
Gain to the PI Controller, a line from the PI Controller to the Plant, and a line from the
Plant to the Scope. You should now have the following.
• The line remaining to be drawn is the feedback signal connecting the output of the Plant
to the negative input of the Sum block. This line is different in two ways. First, since this
line loops around and does not simply follow the shortest (right-angled) route so it needs
to be drawn in several stages. Second, there is no output terminal to start from, so the line
has to tap off of an existing line.
• Drag a line off the negative portion of the Sum block straight down and release the mouse
so the line is incomplete. From the endpoint of this line, click and drag to the line between
the Plant and the Scope. The model should now appear as follows.
• Finally, labels will be placed in the model to identify the signals. To place a label anywhere
in the model, double-click at the point you want the label to be. Start by double-clicking
above the line leading from the Step block. You will get a blank text box with an editing
cursor as shown below.
• Type an r in this box, labeling the reference signal and click outside it to end editing.
• Label the error (e) signal, the control (u) signal, and the output (y) signal in the same
manner. Your final model should appear as:
To save your model, select Save As in the File menu and type in any desired model name. The
completed model can be found here.
Simulation
Now that the model is complete, you can simulate the model. Select Start from the Simulation
menu to run the simulation. Double-click on the _Scope_block to view its output. Hit the autoscale
button (binoculars) and you should see the following:
In some cases, parameters, such as gain, may be calculated in MATLAB to be used in a Simulink
model. If this is the case, it is not necessary to enter the result of the MATLAB calculation directly
into Simulink. For example, suppose we calculated the gain in MATLAB in the variable K.
Emulate this by entering the following command at the MATLAB command prompt.
K = 2.5
This variable can now be used in the Simulink Gain block. In your Simulink model, double-click
on the Gain block and enter the following the Gain field.
Close this dialog box. Notice now that the Gain block in the Simulink model shows the variable K
rather than a number.
Now, you can re-run the simulation and view the output on the Scope. The result should be the
same as before.
Now, if any calculations are done in MATLAB to change any of the variables used in the Simulink
model, the simulation will use the new values the next time it is run. To try this, in MATLAB,
change the gain, K, by entering the following at the command prompt.
K = 5
Start the Simulink simulation again, bring up the Scope window, and hit the autoscale button. You
will see the following output which reflects the new, higher gain.
Appendix part 7
PID Control
Contents
7.1 Introduction: PID Controller Design ........................................................................................ 2/7
In this tutorial we will introduce a simple yet versatile feedback compensator structure, the
Proportional-Integral-Derivative (PID) controller. We will discuss the effect of each of the pid
parameters on the closed-loop dynamics and demonstrate how to use a PID controller to improve
the system performance.
Key MATLAB commands used in this tutorial are: tf , step , pid , feedback , pidtool ,
pidtune
The output of a PID controller, equal to the control input to the plant, in the time-domain is as
follows:
de
u (t ) = K p e(t ) + Ki e(t )dt + K d (1)
dt
First, let's take a look at how the PID controller works in a closed-loop system using the schematic
shown above. The variable ( ) represents the tracking error, the difference between the desired
input value ( ) and the actual output ( ). This error signal ( ) will be sent to the PID controller, and
the controller computes both the derivative and the integral of this error signal. The control signal
( ) to the plant is equal to the proportional gain ( K p ) (times the magnitude of the error plus the
integral gain ( K i ) times the integral of the error plus the derivative gain ( K d ) times the derivative
of the error.
This control signal ( ) is sent to the plant, and the new output ( y ) () is obtained. The new output (
) is then fed back and compared to the reference to find the new error signal ( e ) . The controller
takes this new error signal and computes its derivative and its integral again, ad infinitum.
The transfer function of a PID controller is found by taking the Laplace transform of Eq.(1).
Ki K s 2 + K p s + Ki
Kp + + Kd s = d (2)
s s
We can define a PID controller in MATLAB using the transfer function directly, for example:
Kp = 1;
Ki = 1;
Kd = 1;
s = tf('s');
C = Kp + Ki/s + Kd*s
C =
s^2 + s + 1
-----------
s
C = pid(Kp,Ki,Kd)
C =
1
Kp + Ki * --- + Kd * s
s
with Kp = 1, Ki = 1, Kd = 1
Let's convert the pid object to a transfer function to see that it yields the same result as above:
tf(C)
ans =
s^2 + s + 1
-----------
s
A proportional controller ( K p ) will have the effect of reducing the rise time and will reduce but
never eliminate the steady-state error. An integral control ( K i ) will have the effect of eliminating
the steady-state error for a constant or step input, but it may take the transient response slower. A
derivative control ( K d ) will have the effect of increasing the stability of the system, reducing the
overshoot, and improving the transient response.
Note that these correlations may not be exactly accurate, because K p , K i and K d are dependent on
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
K p , K d , Ki .
Mx = F (t ) − bx − kx (3)
Ms 2 X ( s ) + bsX ( s ) + kX ( s ) = F ( s ) (4)
The transfer function between the displacement X ( s ) and the input F ( s ) then becomes
X ( s) 1
= (5)
F ( s) Ms + bs + k
2
Let
M = 1 kg
b = 10 N s/m
k = 20 N/m
F = 1 N
X ( s) 1
= 2 (6)
F ( s) s + 10s + 20
The goal of this problem is to show you how each of K p , K i , K d contributes to obtain
Let's first view the open-loop step response. Create a new m-file and run the following code:
s = tf('s');
P = 1/(s^2 + 10*s + 20);
step(P)
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 eliminate the steady-state error.
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:
X ( s) Kp
= 2 (7)
F( s) s + 10s + ( 20 + Kp )
Let the proportional gain (Kp) equal 300 and change the m-file to the following:
Kp = 300;
C = pid(Kp)
T = feedback(C*P,1)
t = 0:0.01:2;
step(T,t)
C =
Kp = 300
P-only controller.
T =
300
----------------
s^2 + 10 s + 320
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.
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:
X ( s) Kd s + K p
= 2 (8)
F( s) s + (10 + K d ) s + ( 20 + Kp )
Let equal 300 as before and let equal 10. Enter the following commands into an m-file and
run it in the MATLAB command window.
Kp = 300;
Kd = 10;
C = pid(Kp,0,Kd)
T = feedback(C*P,1)
t = 0:0.01:2;
step(T,t)
C =
Kp + Kd * s
with Kp = 300, Kd = 10
T =
10 s + 300
----------------
s^2 + 20 s + 320
This plot shows that the derivative controller reduced both the overshoot and the settling time, and
had a small effect on the rise time and the steady-state error.
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:
X ( s) K p s + Ki
= 3 (9)
F( s) s + 10s + ( ( 20 + Kp ) s + Ki )
2
Let's reduce the Kp to 30, and let Kp equal 70. Create an new m-file and enter the following
commands.
Kp = 30;
Ki = 70;
C = pid(Kp,Ki)
T = feedback(C*P,1)
t = 0:0.01:2;
step(T,t)
C =
1
Kp + Ki * ---
s
with Kp = 30, Ki = 70
T =
30 s + 70
------------------------
s^3 + 10 s^2 + 50 s + 70
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.
Now, let's take a look at a PID controller. The closed-loop transfer function of the given system
with a PID controller is:
X (s) K d s 2 + K p s + Ki
= (10)
F( s) s3 + (10 + K d ) s 2 + ( 20 + Kp ) s + Ki
After several trial and error runs, the gains = 350, = 300, and = 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;
C = pid(Kp,Ki,Kd)
T = feedback(C*P,1);
t = 0:0.01:2;
step(T,t)
C =
1
Kp + Ki * --- + Kd * s
s
Now, we have obtained a closed-loop system with no overshoot, fast rise time, and no steady-
state error.
When you are designing a PID controller for a given system, follow the steps shown below to
obtain a desired response.
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 a derivative
controller on the system. Keep the controller as simple as possible.
MATLAB provides tools for automatically choosing optimal PID gains which makes the trial and
error process described above unnecessary. You can access the tuning algorithm directly using
pidtune or through a nice graphical user interface (GUI) using pidtool.
The MATLAB automated tuning algorithm chooses PID gains to balance performance (response
time, bandwidth) and robustness (stability margins). By default the algorthm designs for a 60
degree phase margin.
Let's explore these automated tools by first generating a proportional controller for the mass-
spring-damper system by entering the following commands:
pidtool(P,'p')
The pidtool GUI window, like that shown below, should appear.
Notice that the step response shown is slower than the proportional controller we designed by
hand. Now click on the Show Parameters button on the top right. As expected the proportional
gain constant, Kp, is lower than the one we used, Kp = 94.85 < 300.
We can now interactively tune the controller parameters and immediately see the resulting
response int he GUI window. Try dragging the resposne time slider to the right to 0.14s, as shown
in the figure below. The response does indeeed speed up, and we can see Kp is now closer to the
manual value. We can also see all the other performance and robustness parameters for the system.
Note that the phase margin is 60 degrees, the default for pidtool and generally a good balance of
robustness and performance.
Now let's try designing a PID controller for our system. By specifying the previously designed or
(baseline) controller, C, as the second parameter, pidtool will design another PID controller
(instead of P or PI) and will compare the response of the system with the automated controller with
that of the baseline.
pidtool(P,C)
We see in the output window that the automated controller responds slower and exhibits more
overshoot than the baseline. Now choose the Design Mode: Extended option at the top, which
reveals more tuning parameters.
Now type in Bandwidth: 32 rad/s and Phase Margin: 90 deg to generate a controller similar in
performance to the baseline. Keep in mind that a higher bandwidth (0 dB crossover of the open-
loop) results in a faster rise time, and a higher phase margin reduces the overshoot and improves
the system stability.
Finally we note that we can generate the same controller using the command line tool pidtune
instead of the pidtool GUI
opts = pidtuneOptions('CrossoverFrequency',32,'PhaseMargin',90);
[C, info] = pidtune(P, 'pid', opts)
C =
1
Kp + Ki * --- + Kd * s
s
info =
Stable: 1
CrossoverFrequency: 32
PhaseMargin: 90
The transfer function model for the cruise control problem is given below. Please see the Cruise
Control: System Modeling page for the derivation.
………………………………………………………………………………………………………
(11)
Recall from the Introduction: PID Controller Design page, the transfer function of a PID
controller is
………………………………………………………………………………………………………
(12)
We can define a PID controller in MATLAB using the transfer function directly:
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
The first thing to do in this problem is to find a closed-loop transfer function with a proportional
control (C = Kp) added.
By reducing the unity feedback block diagram, the closed-loop transfer function with a
proportional controller becomes:
………………………………………………………………………………………………………
(13)
Recall from the Introduction: PID Controller Design page, a proportional controller, Kp, decreases
the rise time, which is desirable in this case.
For now, use Kp equal 100 and a reference speed of 10 m/s. Create a new m-file and enter the
following commands.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Note that we have used the MATLAB feedback command to simplify the block diagram reduction
of the closed-loop system. Please verify for yourself that the result agrees with the closed-loop
transfer function, T, derived above.
Running the m-file in MATLAB should give you the step response above. As you can see from
the plot, neither the steady-state error nor the rise time satisfy our design criteria.
You can increase the proportional gain, Kp, to reduce the rise time and the steady-state error.
Change the existing m-file so that Kp equals 5000 and rerun it in the MATLAB command window.
You should see the following plot.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
The steady-state error is now essentially zero, and the rise time has been reduced substantially.
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 seconds due to power limitations of the
engine and drivetrain.
Actuator limitations are very frequently encountered in practice in control systems engineering,
and consequently, the required control action must always be considered when proposing a new
controller. We will discuss this issue much more in subsequent tutorials.
The solution to this problem in this case is to choose a lower proportional gain, Kp, that will give
a reasonable rise time, and add an integral controller to eliminate the steady-state error.
4.2.5. PI control
The closed-loop transfer function of this cruise control system with a PI controller (C = Kp + Ki/s)
is:
………………………………………………………………………………………………………
(14)
Recall from the Introduction: PID Controller Design page, an addition of an integral controller to
the system eliminates the steady-state error. For now, let Kp equal 600 and Ki equal 1 and see
what happens to the response. Change your m-file to the following.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
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 a large Ki
can destabilize the response. When Kp equals 800 and Ki equals 40, the step response will look
like the following:
………………………………………………………………………………………………………
………………………………………………………………………………………………………
For this particular example, no implementation of a derivative controller was needed to obtain the
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 (C
= Kp + Ki/s + Kd*s) is:
………………………………………………………………………………………………………
(15)
Let Kp equal 1, Ki equal 1, and Kd equal 1 and enter the following commands into an new m-
file.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Plot the step response and 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 requires a trial and error process. The best way to
attack this tedious process is to adjust one variable (Kp, Ki, or Kd) at a time and observe how
changing one variable influences the system output. The characteristics of Kp, Ki, and Kd are
summarized in the Introduction: PID Controller Design page.
From the main problem, the dynamic equations in the Laplace domain and the open-loop transfer
function of the DC Motor are the following.
………………………………………………………………………………………………………
(16)
………………………………………………………………………………………………………
(17)
………………………………………………………………………………………………………
(18)
The structure of the control system has the form shown in the figure below.
For the original problem setup and the derivation of the above equations, please refer to the DC
Motor Speed: System Modeling page.
For a 1-rad/sec step reference, the design criteria are the following.
Now let's design a controller using the methods introduced in the Introduction: PID Controller
Design page. Create a new m-file and type in the following commands.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
………………………………………………………………………………………………………
(19)
Let's first try employing a proportional controller with a gain of 100, that is, C(s) = 100. To
determine the closed-loop transfer function, we use the feedback command. Add the following
code to the end of your m-file.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Now let's examine the closed-loop step response. Add the following commands to the end of your
m-file and run it in the command window. You should generate the plot shown below. You can
view some of the system's characteristics by right-clicking on the figure and choosing
Characteristics from the resulting menu. In the figure below, annotations have specifically been
added for Settling Time, Peak Response, and Steady State.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
From the plot above we see that both the steady-state error and the overshoot are too large. Recall
from the Introduction: PID Controller Design page that increasing the proportional gain Kp will
reduce the steady-state error. However, also recall that increasing Kp often results in increased
overshoot, therefore, it appears that not all of the design requirements can be met with a simple
proportional controller.
This fact can be verified by experimenting with different values of Kp. Specifically, you can
employ the SISO Design Tool by entering the command sisotool(P_motor) then opening a
closed-loop step response plot from the Analysis Plots tab of the Control and Estimation Tools
Manager window. With the Real-Time Update box checked, you can then vary the control gain
in the Compensator Editor tab and see the resulting effect on the closed-loop step response. A
little experimentation verifies what we anticipated, a proportional controller is insufficient for
meeting the given design requirements; derivative and/or integral terms must be added to the
controller.
Recall from the Introduction: PID Controller Design page adding an integral term will eliminate
the steady-state error to a step reference and a derivative term will often reduce the overshoot. Let's
try a PID controller with small Ki and Kd. Modify your m-file so that the lines defining your
control are as follows. Running this new m-file gives you the plot shown below.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Inspection of the above indicates that the steady-state error does indeed go to zero for a step
input. However, the time it takes to reach steady-state is far larger than the required settling time
of 2 seconds.
In this case, the long tail on the step response graph is due to the fact that the integral gain is small
and, therefore, it takes a long time for the integral action to build up and eliminate the steady-state
error. This process can be sped up by increasing the value of Ki. Go back to your m-file and change
Ki to 200 as in the following. Rerun the file and you should get the plot shown below. Again the
annotations are added by right-clicking on the figure and choosing Characteristics from the
resulting menu.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
As expected, the steady-state error is now eliminated much more quickly than before. However,
the large Ki has greatly increased the overshoot. Let's increase Kd in an attempt to reduce the
overshoot. Go back to the m-file and change Kd to 10 as shown in the following. Rerun your m-
file and the plot shown below should be generated.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
As we had hoped, the increased Kd reduced the resulting overshoot. Now we know that if we use
a PID controller with
The structure of the control system has the form shown in the figure below.
For the original problem setup and the derivation of the above equations, please refer to the DC
Motor Position: System Modeling page.
For a 1-radian step reference, the design criteria are the following.
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 these commands).
J = 3.2284E-6;
b = 3.5077E-6;
K = 0.0274;
R = 4;
L = 2.75E-6;
s = tf('s');
P_motor = K/(s*((J*s+b)*(L*s+R)+K^2));
Recall that the transfer function for a PID controller has the following form.
………………………………………………………………………………………………………
(20)
Let's first try using a proportional controller with gain ranging from 1 to 21. An array of LTI
models, each with a different proportional gain, can be built using a for loop. The closed-loop
transfer functions can be generated using the feedback command. Add the following code to the
end of your m-file and run it in the MATLAB command window:
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Let's also consider the system's response to a step disturbance. In this case, we will assume a
reference of zero and look at the how the system responds to the disturbance by itself. The
feedback command can still be employed for generating the closed-loop transfer function where
there is still negative feedback, however, now only the plant transfer function P(s) is in the forward
path and the controller C(s) is considered to be in the feedback path. Refer back to the block
diagram at the top of this page to see the structure of the system. Add the following to the end of
your m-file and run it in the command window.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
The above plots show that the system has no steady-state error in response to the step reference by
itself, no matter the choice of proportional gain Kp. This is due to the fact that the plant has an
integrator, that is, the system is type 1. However, the system has significant steady-state error when
the disturbance is added. Specifically, the response due to the reference and disturbance applied
simultaneously is equal to the sum of the two graphs shown above. This follows from the property
of superposition that holds for linear systems. Therefore, to have zero steady-state error in the
presence of a disturbance, we need the disturbance response to decay to zero. The larger the value
of Kp the smaller the steady-state error is due to the disturbance, but it never reaches zero.
Furthermore, employing increasingly larger values of Kp has the adverse effect of increasing the
overshoot and settle time as can be seen from the step reference plot. Recall from the DC Motor
Position: System Modeling page that adding an integral term will eliminate the steady-state error
and a derivative term can reduce the overshoot and settling time.
4.4.2. PI control
Let's first try a PI controller to get rid of the steady-state error due to the disturbance. We will set
Kp = 21 and test integral gains Ki ranging from 100 to 500. Change your m-file to the following
and run in the command window.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Now let's see what happened to the step disturbance response. Change the following commands in
your m-file and re-run in the command window. You should generate a plot like the one shown in
the figure below.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
The integral control has reduced the steady-state error to zero, even when a step disturbance is
present; that was the goal for adding the integral term. For the response to the step reference, all
of the reponses look similar with the amount of oscillation increasing slightly as Ki is made larger.
However, the response due to the disturbance changes significantly as the integral gain Ki is
changed. Specifically, the larger the value of Ki employed, the faster the error decays to zero. We
will choose Ki = 500 because the error due to the disturbance decays to zero quickly, even though
the response to the reference has a longer settling time and more overshoot. We will attempt to
reduce the settling time and overshoot by adding a derivative term to the controller.
Adding a derivative term to the controller means that we now have all three terms of the PID
controller. We will investigate derivative gains Kd ranging from 0.05 to 0.25. Go back to the m-
file and make the following changes. Running the altered m-file will generate a graph like the one
shown below.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
Let's see what happened to the step disturbance response, change the following commands in your
m-file and re-run at the command line.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
It looks like when Kd = 0.15, we can meet our design requirements. To determine the precise
characteristics of the step response you can use the right-click menu of the step response plot, or
you can use the MATLAB command stepinfo as shown below.
………………………………………………………………………………………………………
………………………………………………………………………………………………………
From the above, we see that the response to a step reference has a settling time of roughly 34ms
(< 40 ms), overshoot of 12% (< 16%), and no steady-state error. Additionally, the step disturbance
response also has no steady-state error. So now we know that if we use a PID controller with
Phục lục 8
Hướng dẫn sử dụng phần mềm TIA portal và áp dụng điều khiển mức
8.1 Sơ đồ khối của hệ thống
Cảm biến
trình
mv TUYẾN u
Modular
PQWx
OUT
HIỆU
ANALOG
IN mv
0-100 % TÍNH
4-20 mA AO 0-27648 OUT
0.0-100.0
Modular
đọc, xuất
tín hiệu
Analog analog
Analog Output
Input
0…10 Vdc
I/V 4...20 mA
0…100 cm 0…100%
T
4...20 mA Quá trình và thiết bị
4…20 mA
I
L
(b)
Hình 8.3. Lưu đồ thiết bị và điều khiển hệ thống
Hình 8.4. Vị trí của các công tắc khi làm thí nghiệm phần này
Bước1: Cấu hình phần cứng cho bộ điều khiển
- Sử dụng phần mềm TIA Portal cấu hình phần cứng với các modular và mã
thiết bị được thể hiện ở hình 1.12
Hình 8.5. Cấu hình bộ điều khiển hệ thống điều khiển mức
1 2
3
4
5
6
11
10 8
13
14
15 12
16
19
17 18
20
±10Vdc
21
23
22
24 27
30
25 28 31
26 29 32
1 2
3
4
6
7
5
9
10
11 12
13
Q0.2 Van V1
Bước2: Viết chương trình xác định giá trị hiện tại của biến quá trình (cv)
- Xác định mối quan hệ In-Out của hàm xử lý tín hiệu analog in
Mục tiêu của phần này là dựa vào lưu đồ thiết bị và điều khiển hệ thống (hình 1.14)
ta tính ra được giá trị hiện tại của biến quá trình (cv, pv). Trong sơ đồ hình 1.14 x là
biến quá trình (x=cv), mối quan hệ giữa x và các thông số khác như sau:
float(PIWx) - PIWmin
x= (x max - x min ) + x min
PIWmax - PIWmin (1)
Để có được giá trị hiện tại của biến quá trình thì phương trình (1) được viết trong
PLC. Chúng ta có thể sử dụng hàm thư viện Scale (FC105) có trong TIA portal để
có được kết quả x (là cv). Tuy nhiên, để làm quen với các hàm toán học trong PLC
thì phương trình (1) được triển khai thành chương trình con theo trình tự sau:
+ Phân tích các kiểu dữ liệu có trong phương trình (1). Chúng ta cần lưu ý, trong
ngôn ngữ lập trình, để kết quả các phép toán nhân, chia, cộng, trừ được thực hiện
đúng thì các biến trong phép toán phải cùng kiểu dữ liệu. Trong PLC có hai cách để
chuyển đổi kiểu dữ liệu, đó là, ép kiểu nếu là hằng số và dùng lệnh chuyển đổi nếu
là tham số. Trong phương trình (1), biến PIWx có giá trị chứa trong nó là kiểu số
nguyên 16 bit (int16, do là kết quả chuyển đổi từ ADC của modular analog input),
Practice of automatic control and process control 8/8
Mechatronics Department Process control and SCADA lab
mà các biến có trong phương trình (1) đều là số thực (real) nên chúng ta cần dùng
lệnh để chuyển đổi
+ Phân tích các biến phụ và các phép toán số học cần thiết khi viết chương trình
con cho phương trình (1)
1 2
Vị trí
khai báo 3
biến
In
PIW_min
Out
x
PIWmax
x_max
x_min
Biến vào Biến ra
Biến vào
Biến ra
Biến phụ
• Viết hàm FC1, gọi hàm và áp dụng xác định mức lỏng trong bình
Lệnh
Convert Lệnh
Toán
học
2. Vào chương
trình chính 3. Gọi hàm và truyền
(OB1) 3giá trị cho hàm. Kết
quả MD3 chứa giá
trị là mức trong bình
(là biến quá trình,
cv)
Hình 8.9. Chương trình xác định mức lỏng trong bình
Practice of automatic control and process control 11/8
Mechatronics Department Process control and SCADA lab
Bước 3: Viết chương trình tạo ra tín hiệu điều khiển (mv)
- Xác định mối quan hệ In-Out của hàm xử lý tín hiệu analog out
Mục tiêu của phần này là dựa vào lưu đồ thiết bị và điều khiển hệ thống (hình 1.14)
ta tính ra được giá trị là số nguyên cần đưa vào ô nhớ của kênh analog output
(PQWx) để xuất tín hiệu điều khiển cho thiết bị là van tuyến tính để từ đó thay đổi
lưu lượng của chất lỏng đổ vào bình. Mối quan hệ giữa PQWx và các thông số
khác như sau:
(2)
Phương trình toán PID được sử dụng trong kỹ thuật điều khiển vòng kín như sau:
(3)
Luật cập nhật giá trị mv tại thời điểm k cho bộ điều khiển PID số như sau
+ (4)
update_time Begin
Parameter input:
K_c, tau_i, tau_d, delta_t
+ e(k-2) = e(k-1)
+ e(k-1) = e(k)
+ e(k) = sp(k) - pv(k)
+MV_pid(k-1)=MV_pid(k)
K_c
temp1=delta_t/tau_i
temp2=tau_d/delta_t
tau_i temp3=1+temp1
temp4=temp3+temp2
tau_d temp5=K_c*temp4
temp6=temp5*e(k)
temp7=temp2*2
MV_pid(k)
delta_t temp8=1+temp7
temp9=K_c*temp8
temp10=temp9*e(k-1)
MV_pid(k-1) temp11=K_c*temp2
temp12=temp11*e(k-2)
e(k)
temp13=temp6-temp10
e(k-1) temp14=temp13+temp12
MV_pid(k)= MV_pid(k-1)+temp14
e(k-2)
Stop
End
Thuật toán PID số được triển khai trong khối FC3 như sau:
Bước 5: Tạo khối DB1 chứa các thông số liên quan đến thuật toán PID và bit
đảo trạng thái m0.1 cứ sau 0.5 s để làm thời gian cập nhật delta_t (chương
trình trong OB35)
1
Khai
báo
thông
số
2
4 5
6
1
Khai
báo
thông
số
2
4 5
6
Vùng nhớ
PQW của
PLC, ô nhớ,
tùy theo khi
khai báo phần
Hình 8.16. Chương trình điều khiển mức dùng PLC S7-300
Bước 6: Tạo bảng để giám sát và nhập giá trị trong phần mềm TIA Portal
3
Nhập các
biến cần
1 giám sát và
2 ghi số liệu
của PLC
Nhập giá trị xuống ô nhớ của PLC: ví dụ sp=10;Kc=2.5; tau_i=15.3; tau_d=0.8;
delta_t=0.5;
6 5
Bước 2: Bật (mở) nguồn cho trạm bằng công tắc nguồn (19)
Bước 3: Kiểm tra SW điều khiển chế độ vận hành của CPU 315-2PN/DP phải
ở vị trí “RUN”
Bước 4: Kiểm tra van tuyến tính (08) và bộ đo lưu lượng (05) có hoạt động hay
không? Chuyển công tắc điều khiển chế độ vận hành cho van tuyến
tính (21) sang vị trí “MAN”. Công tắc điều chế độ vận hành cho bơm
li tâm (20) sang vị trí “MAN”. Chỉnh bộ chỉnh dòng (17), quan sát
nước có chảy vào thùng chứa (15) hay không? Nếu nước có chảy vào
thùng chứa (15) và bộ đo lưu lượng (05) có chỉ thị lưu lượng nước
chảy qua thì trạm ở chế độ sẵn sàng hoạt động phục vụ cho việc thí
nghiệm và thực hành, nếu không có nước chảy vào thùng chứa (15)
hoặc bộ đo lưu lượng (07) không chỉ thị giá trị thì báo cho giảng viên
hướng dẫn
Bước 5: Bật nguồn cho CPU của máy tính
Bước 7: Mở phần mềm hỗ trợ cho việc thí nghiệm/thực hành
Bước 8: Kiểm tra kết nối máy tính với bộ điều khiển (CPU 315-2PN/DP) của
trạm điều khiển mức. Nếu không kết nối thì báo cho giảng viên hướng
dẫn
Bước 9: Tiến hành thí nghiệm/thực hành theo đúng hướng dẫn trong tài liệu
học tập.
Trong quá trình thí nghiệm/thực hành có sự cố về thiết bị thì báo cho
giảng viên hướng dẫn
Bước 10: Hoàn tất thí nghiệm/thực hành. Sau khi đã hoàn tất các phần thí
nghiệm/thực hành trong bài thì:
Phụ lục 9
THIẾT KẾ GIAO DIỆN GIÁM SÁT DÙNG WINCC
A. PHẦN THAM KHẢO
9. Hướng dẫn sử dụng WinCC
Để tạo mới một project với WinCC theo các bước sau:
➢ Tạo mới một project
➢ Tạo driver kết nối giữa WinCC và PLC
➢ Tạo tag group kết nối với PLC
➢ Tạo picture bằng Graphic Designer
➢ Tạo môi trường Runtime và chạy Runtime.
9.1 Tạo mới một Project
- Từ Dekstop chọn biểu tượng
4
1
2
6
Hình 9.6. Chọn phương thức giao tiếp giữa WINCC và PLC
- Trong hộp thoại Connection Properties, đặt tên kết nối và click vào nút
Properties, trong hộp thoại Connection Parameter ta thiết lập các thông số. Chú ý
các thông số này phải đúng với phần cứng PLC S7 300, CPU_2PN ta thiết lập các
thông số như sau:
▪ Internal tag: là loại tags được cung cấp bởi các data manager trong WinCC.
Những tag này không được kết nối vào các ô nhớ của PLC. Internal tag có thể dùng
để tính toán và hiệu chỉnh trực tiếp trên WinCC hoặc lưu trữ những thông tin chung
như thời gian.
▪ External tag: còn gọi là “ Process Tags” hay “Power Tags”. Tags này được kết
nối trực tiếp vào địa chỉ các ô nhớ của PLC hay thiết bị tương tự. Trong các
“External Tags” có một dạng tag đặc biệt là “ Raw Data Tags”
❖ Các kiểu dữ liệu của tag:
- Binary tag: 1 bit dữ liệu nhị phân.
- Unsigned 8 bit: 8 bit (1 byte) dữ liệu nhị phân không dấu, biểu diễn số nguyên
dương 0 ÷ 255
- Signed 8 bit: 8 bit (1 byte) dữ liệu nhị phân có dấu, biểu diễn số nguyên -128 ÷
127.
- Signed 16 bit: 16 bit (2 byte) dữ liệu nhị phân có dấu, biểu diễn số nguyên -
32768 ÷ +32767.
- Unsigned 32bit: 32 bit (4 byte) dữ liệu nhị phân không dấu.
- Signed 32 bit: 32bit (4 byte) dữ liệu nhị phân có dấu, biểu diễn số nguyên
- Floating – point number 32 bit IEEE 754: 32 bit (4 byte) số thực dấu chấm
động.
- Floating – point number 64 bit IEEE 754: 64 bit (8 byte) số thực dấu chấm
động.
- Text 8 - bit character.
- Text 16 - bit character.
- Raw Data type: kiểu tag quản lí dữ liệu.
- Text reference: kiểu dữ liệu tham chiếu vào bảng “ text library” (thông qua cột
“text ID”).
- Structure tag: dùng để tạo ra kiểu dữ liệu cấu trúc riêng. Có thể kết hợp với các
kiểu dữ liệu khác để tạo ra dữ liệu cần thiết.
❖ Các kiểu địa chỉ tag trong WinCC:
- Khối dữ liệu (DB).
Practice of automatic control and process control 6/9
Mechatronics Department Process control and SCADA lab
- Trong cửa sổ bên phải sẽ hiện lên một Picture mới với tên mặc định. Để đổi tên
Picture, ta phải Right_click vào Picture và chọn Rename Picture.
Global Scripts,…
▪ Picture Window
▪ Control: được sử dụng để bổ sung vào những thành phần của window ( như nút
nhấn). Đối tượng này có những thuộc tính được hiển thị trong hộp thoại “Object
- Double click vào I/O Field cửa sổ I/O Field Configuration xuất hiện và trình tự
thực hiện như sau:
Ví dụ: I/O field nhập giá trị setpoint
- Graphic Object: dùng để chèn những File hình ảnh có đuôi “.bmp” vào trong
Picture WinCC
2 3
1
Đặt tên cho button
2
Đặt font cho tên button 3
Gán thuộc tính cho button, Ví dụ 1: Thuộc tính của button là nhấn nút RUN thì
tag “RUN” lên 1.
4
4
5
5 6
6
7
7
Theo như ví dụ nhấn nút RUN thì tag “Run_Stop_Button” có mức logic là “1”
10
9
11
12
Hình 9.18. Thiết lập nút nhấn có thuộc tính khi nhấn tạo cạnh lên
Ví dụ 3: Thuộc tính của button là nhấn nút EXIT thì thoát chế độ chạy Runtime
3
2
4
Hình 9.19. Cách tạo thuộc tính của button là
nhấn nút EXIT thì thoát chế độ chạy Runtime
Ví dụ 4: Thuộc tính khi nhấn nút thì chuyển sang giao diện khác (ví dụ Screen03).
Practice of automatic control and process control 15/9
Mechatronics Department Process control and SCADA lab
Hình 9.20. Cách tạo thuộc tính của button là nhấn nút chuyển sang giao diện khác
❖ Tạo WinCC Online Trend Control : Đồ thị giám sát quá trình
Practice of automatic control and process control 16/9
Mechatronics Department Process control and SCADA lab
5 4
Vùng
chọn các
3 đối tượng
8
9
Chọn Yes để co
giãn hình tùy ý
Bồn không có Bar. Muốn có Bar thì tạo riêng sau đó chồng hình. Muốn có các co
nối thì vào thư viện pipes lấy. Kết quả như sau
Hình 9.22. Cách lấy bồn và bar chỉ thị mức trong bồn
9.5 Chạy và dừng Runtime
Nút chạy
Nút dừng runtime
runtime