Download as pdf or txt
Download as pdf or txt
You are on page 1of 99

Mechatronics Department Process control and SCADA lab

Appendix part 6

Simulink Basics Tutorial

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

6.2. Model Files ................................................................................................................................... 2/6

6.3. Basic Elements .............................................................................................................................. 3/6

6.4. Simple Example ............................................................................................................................ 5/6

6.5. Modifying Blocks ......................................................................................................................... 5/6

6.6. Running Simulations ..................................................................................................................... 8/6

6.7. Building Systems ........................................................................................................................ 13/6

Practice of automatic control and process control 1/6


Mechatronics Department Process control and SCADA lab

6.1 Starting Simulink

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.

6.2 Model Files


In Simulink, a model is a collection of blocks which, in general, represents a system. In addition,
to drawing a model into a blank model window, previously saved model files can be loaded either
from the File menu or from the MATLAB command prompt. As an example, the file in the
directory you are running MATLAB from.

simple.mdl

Practice of automatic control and process control 2/6


Mechatronics Department Process control and SCADA lab

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

The following model window should appear.

A new model can be created by selecting New from the File menu in any Simulink window (or by
hitting Ctrl-N).

6.3 Basic Elements

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:

• Sources: used to generate various signals


• Sinks: used to output or display signals

Practice of automatic control and process control 3/6


Mechatronics Department Process control and SCADA lab

• Continuous: continuous-time system elements (transfer functions, state-space models, PID


controllers, etc.)
• Discrete: linear, discrete-time system elements (discrete transfer functions, discrete state-
space models, etc.)
• Math Operations: contains many common math operations (gain, sum, product, absolute
value, etc.)
• Ports & Subsystems: contains useful blocks to build a system

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).

Practice of automatic control and process control 4/6


Mechatronics Department Process control and SCADA lab

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.

6.4 Simple Example

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.

6.5 Modifying Blocks

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.

Practice of automatic control and process control 5/6


Mechatronics Department Process control and SCADA lab

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)

enter the following into the denominator field

[1 2 4]

and hit the close button, the model window will change to the following,

Practice of automatic control and process control 6/6


Mechatronics Department Process control and SCADA lab

which reflects the change in the denominator of the transfer function.

The Step block can also be double-clicked, bringing up the following dialog box.

Practice of automatic control and process control 7/6


Mechatronics Department Process control and SCADA lab

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.

6.6 Running Simulations

To run a simulation, we will work with the following model file:

simple2.mdl

Open this file in Simulink following the previous instructions for this file. You should see the
following model window.

Practice of automatic control and process control 8/6


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 9/6


Mechatronics Department Process control and SCADA lab

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]

Practice of automatic control and process control 10/6


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 11/6


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 12/6


Mechatronics Department Process control and SCADA lab

6.7 Building Systems

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.

If you would like to download the completed model, click here.

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

Follow the steps below to collect the necessary 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.

Practice of automatic control and process control 13/6


Mechatronics Department Process control and SCADA lab

• This will bring up the Sources block library. Sources are used to generate signals.

Practice of automatic control and process control 14/6


Mechatronics Department Process control and SCADA lab

• Drag the Step block from the Sources window into the left side of your model window.

Practice of automatic control and process control 15/6


Mechatronics Department Process control and SCADA lab

• Click on the Math Operations listing in the main Simulink window.


• From this library, drag a Sum and Gain block into the model window and place them to the
right of the Step block in that order.
• Click on the Continuous listing in the main Simulink window.
• First, from this library, drag a PID Controller block into the model window and place it to
the right of the Gain block.
• From the same library, drag a Transfer Function block into the model window and place it
to the right of the PID Controller block.

• Click on the Sinks listing in the main Simulink window.


• Drag the Scope block into the right side of the model window.

Practice of automatic control and process control 16/6


Mechatronics Department Process control and SCADA lab

Modify Blocks

Follow these steps to properly modify the blocks in your model.

• 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:

Practice of automatic control and process control 17/6


Mechatronics Department Process control and SCADA lab

• 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:

Practice of automatic control and process control 18/6


Mechatronics Department Process control and SCADA lab

Connecting Blocks with Lines

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.

Practice of automatic control and process control 19/6


Mechatronics Department Process control and SCADA lab

• 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.

Practice of automatic control and process control 20/6


Mechatronics Department Process control and SCADA lab

• 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.

Practice of automatic control and process control 21/6


Mechatronics Department Process control and SCADA lab

• 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.

Practice of automatic control and process control 22/6


Mechatronics Department Process control and SCADA lab

• 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:

Practice of automatic control and process control 23/6


Mechatronics Department Process control and SCADA lab

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:

Taking Variables from MATLAB

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.

Practice of automatic control and process control 24/6


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 25/6


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 26/6


Mechatronics Department Process control and SCADA lab

Appendix part 7

PID Control
Contents
7.1 Introduction: PID Controller Design ........................................................................................ 2/7

7.1.1 PID Overview ...................................................................................................................... 2/7


7.1.2 The Characteristics of P, I, and D Controllers ................................................................. 3/7
7.1.3 Example Problem ................................................................................................................ 4/7
7.1.4 Open-Loop Step Response.................................................................................................. 5/7
7.1.5 Proportional Control .......................................................................................................... 6/7
7.1.6 Proportional-Derivative Control ....................................................................................... 7/7
7.1.7 Proportional-Integral Control ........................................................................................... 8/7
7.1.8 Proportional-Integral-Derivative Control ...................................................................... 10/7
7.1.9 General Tips for Designing a PID Controller................................................................. 11/7
7.1.10 Automatic PID Tuning ..................................................................................................... 11/7
7.2. Cruise Control: PID Controller Design .................................................................................. 15/7

7.2.1. System model and parameters ......................................................................................... 15/7


7.2.2. Performance specifications............................................................................................... 15/7
7.2.3. PID overview ..................................................................................................................... 15/7
7.2.4. Proportional control ......................................................................................................... 14/7
7.2.5. PI control ........................................................................................................................... 18/6
7.2.6. PID control ........................................................................................................................ 20/7
7.3. DC Motor Speed: PID Controller Design ............................................................................... 21/7

7.3.1. Proportional control ......................................................................................................... 22/7


7.3.2. PID control ........................................................................................................................ 23/7
7.3.3. Tuning the gains ................................................................................................................ 24/7
7.4. DC Motor Position: PID Controller Design............................................................................ 26/7

7.4.1. Proportional control ......................................................................................................... 27/7


7.4.2. PI control ........................................................................................................................... 29/7
7.4.3. PID control ........................................................................................................................ 31/7

Practice of automatic control and process control 1/7


Mechatronics Department Process control and SCADA lab

7.1 Introduction: PID Controller Design

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

7.1.1 PID Overview

In this tutorial, we will consider the following unity feedback system:

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

Practice of automatic control and process control 2/7


Mechatronics Department Process control and SCADA lab

+ K p = Proportional gain; K i = Integral gain K d = Derivative gain

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

Continuous-time transfer function.

Alternatively, we may use MATLAB's PID controller object to generate an equivalent


continuous-time controller as follows:

C = pid(Kp,Ki,Kd)
C =

1
Kp + Ki * --- + Kd * s
s

with Kp = 1, Ki = 1, Kd = 1

Continuous-time PID controller in parallel form.

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

Continuous-time transfer function.

7.1.2 The Characteristics of P, I, and D Controllers

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

Practice of automatic control and process control 3/7


Mechatronics Department Process control and SCADA lab

derivative control ( K d ) will have the effect of increasing the stability of the system, reducing the
overshoot, and improving the transient response.

The effects of each of controller parameters, K p , K i and K d on a closed-loop system are


summarized in the table below.

PARAMETERS RISE TIME OVERSHOOT SETTLING TIME S-S ERROR


Kp Decrease Increase Small Change Decrease
Ki Decrease Increase Increase Eliminate
Kd Small Change Decrease Decrease No Change

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 .

7.1.3 Example Problem

Suppose we have a simple mass, spring, and damper problem.

The modeling equation of this system is

Mx = F (t ) − bx − kx (3)

Taking the Laplace transform of the modeling equation, we get

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

Practice of automatic control and process control 4/7


Mechatronics Department Process control and SCADA lab

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

Plug these values into the above transfer function

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

Fast rise time


Minimum overshoot
No steady-state error

7.1.4 Open-Loop Step Response

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)

Practice of automatic control and process control 5/7


Mechatronics Department Process control and SCADA lab

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.

7.1.5 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:

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

Continuous-time transfer function.

Practice of automatic control and process control 6/7


Mechatronics Department Process control and SCADA lab

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.

7.1.6 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:

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

Practice of automatic control and process control 7/7


Mechatronics Department Process control and SCADA lab

with Kp = 300, Kd = 10

Continuous-time PD controller in parallel form.

T =

10 s + 300
----------------
s^2 + 20 s + 320

Continuous-time transfer function.

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.

7.1.7 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:

X ( s) K p s + Ki
= 3 (9)
F( s) s + 10s + ( ( 20 + Kp ) s + Ki )
2

Practice of automatic control and process control 8/7


Mechatronics Department Process control and SCADA lab

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

Continuous-time PI controller in parallel form.

T =

30 s + 70
------------------------
s^3 + 10 s^2 + 50 s + 70

Continuous-time transfer function.

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.

Practice of automatic control and process control 9/7


Mechatronics Department Process control and SCADA lab

7.1.8 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:

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

with Kp = 350, Ki = 300, Kd = 50

Continuous-time PID controller in parallel form.

Practice of automatic control and process control 10/7


Mechatronics Department Process control and SCADA lab

Now, we have obtained a closed-loop system with no overshoot, fast rise time, and no steady-
state error.

7.1.9 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 a derivative
controller on the system. Keep the controller as simple as possible.

7.1.10 Automatic PID Tuning

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.

Practice of automatic control and process control 11/7


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 12/7


Mechatronics Department Process control and SCADA lab

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.

Practice of automatic control and process control 13/7


Mechatronics Department Process control and SCADA lab

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

with Kp = 320, Ki = 169, Kd = 31.5

Continuous-time PID controller in parallel form.

info =
Stable: 1
CrossoverFrequency: 32
PhaseMargin: 90

Practice of automatic control and process control 14/7


Mechatronics Department Process control and SCADA lab

4.1. Cruise Control: PID Controller Design


Key MATLAB commands used in this tutorial are: tf , step , feedback

4.2.1. System model and parameters

The transfer function model for the cruise control problem is given below. Please see the Cruise
Control: System Modeling page for the derivation.

………………………………………………………………………………………………………

(11)

The parameters used in this example are as follows:

(m) vehicle mass 1000 kg


(b) damping coefficient 50 N.s/m
(r) reference speed 10 m/s

4.2.2. Performance specifications

• Rise time < 5 s


• Overshoot < 10%
• Steady-state error < 2%

4.2.3. PID overview

The block diagram of a typical unity feedback system is shown below.

Recall from the Introduction: PID Controller Design page, the transfer function of a PID
controller is

………………………………………………………………………………………………………

(12)

Practice of automatic control and process control 15/7


Mechatronics Department Process control and SCADA lab

We can define a PID controller in MATLAB using the transfer function directly:

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Alternatively, we may use MATLAB's pid controller object to generate an equivalent


continuous time controller as follows:

………………………………………………………………………………………………………
………………………………………………………………………………………………………

4.2.4. Proportional control

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 16/7


Mechatronics Department Process control and SCADA lab

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 17/7


Mechatronics Department Process control and SCADA lab

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)

Practice of automatic control and process control 18/7


Mechatronics Department Process control and SCADA lab

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:

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 19/7


Mechatronics Department Process control and SCADA lab

4.2.6. PID control

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.

Practice of automatic control and process control 20/7


Mechatronics Department Process control and SCADA lab

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.

4.2. DC Motor Speed: PID Controller Design


Key MATLAB commands used in this tutorial are: tf , step , feedback

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.

• Settling time less than 2 seconds


• Overshoot less than 5%

Practice of automatic control and process control 21/7


Mechatronics Department Process control and SCADA lab

• Steady-state error less than 1%

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Recall that the transfer function for a PID controller is:

………………………………………………………………………………………………………

(19)

4.3.1. Proportional control

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 22/7


Mechatronics Department Process control and SCADA lab

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.

4.3.2. PID control

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.

Practice of automatic control and process control 23/7


Mechatronics Department Process control and SCADA lab

………………………………………………………………………………………………………
………………………………………………………………………………………………………

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.

4.3.3. Tuning the gains

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 24/7


Mechatronics Department Process control and SCADA lab

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 25/7


Mechatronics Department Process control and SCADA lab

As we had hoped, the increased Kd reduced the resulting overshoot. Now we know that if we use
a PID controller with

Kp = 100, Ki = 200, and Kd = 10,

all of our design requirements will be satisfied.

4.3. DC Motor Position: PID Controller Design

Key MATLAB commands used in this tutorial are: tf , step , feedback

The structure of the control system has the form shown in the figure below.

Practice of automatic control and process control 26/7


Mechatronics Department Process control and SCADA lab

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.

• Settling time less than 0.040 seconds


• Overshoot less than 16%
• No steady-state error, even in the presence of a step disturbance input

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)

4.4.1. Proportional control

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:

………………………………………………………………………………………………………

Practice of automatic control and process control 27/7


Mechatronics Department Process control and SCADA lab

………………………………………………………………………………………………………

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

You should generate the plot shown in the figure below.

Practice of automatic control and process control 28/7


Mechatronics Department Process control and SCADA lab

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 29/7


Mechatronics Department Process control and SCADA lab

You should generate a figure like the one shown below.

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 30/7


Mechatronics Department Process control and SCADA lab

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.

4.4.3. PID control

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 31/7


Mechatronics Department Process control and SCADA lab

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.

………………………………………………………………………………………………………
………………………………………………………………………………………………………

Practice of automatic control and process control 32/7


Mechatronics Department Process control and SCADA lab

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

Kp = 21, Ki = 500, and Kd = 0.15,

all of our design requirements will be satisfied.

Practice of automatic control and process control 33/7


Mechatronics Department Process control and SCADA lab

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

Hình 8.1. Sơ đồ nguyên lý điều khiển mức nước. DV


PV
SP PID Gp
-

Cảm biến

Hình 8.2. Sơ đồ khối điều khiển vòng kín


Trong đó : SP, là giá trị đặt được nhập từ giao diện điều khiển (HMI hoặc từ máy
tính giám sát). PV, là giá trị hiện tại của mức lỏng. ess = SP – PV: Giá trị sai số

Practice of automatic control and process control 1/8


Mechatronics Department Process control and SCADA lab

Lưu đồ thiết bị và điều khiển hệ thống


CẢM BIẾN AI Số thực
x ĐO u PIWx IN OUT PV (giá trị hiện tại của
0.0-100.0 cm MỨC 0-10 Vdc A/D 0-27648 HÀM XỬ 0.0->100.0 mức nước)
& BỘ Modular LÝ TÍN
CHUYỂN AI HIỆU THUẬT
ĐỔI DÒNG ANALOG
SANG ÁP IN TOÁN
u = umin ÷ umax PID
Giá trị đặt SP(giá trị đặt
x = xmin ÷ xmax PIWx = PIWmin÷PIWmax
của mức nước)
AO HÀM XỬ
Quá VAN D/A LÝ TÍ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

mv = mvmin ÷ mvmax u = umin ÷ umax PQWx = PQWmin÷PQWmax


(a)

IN_A1 CTRL_PID OUT_A1


.. .. .. Các
.. .. .. chương
PV_I trình
N
SP_I
N

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

Practice of automatic control and process control 2/8


Mechatronics Department Process control and SCADA lab

8.2 PHẦN THỰC HÀNH

Lưu ý: Công tắc


điều khiển VTT
vị trí AUTO

Lưu ý: Công tắc


điều khiển bơm ở
vị trí AUTO

Vị trí của van


tuyến tính trong
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

Practice of automatic control and process control 3/8


Mechatronics Department Process control and SCADA lab

1 2

3
4

5
6

Practice of automatic control and process control 4/8


Mechatronics Department Process control and SCADA lab

11
10 8

13
14

15 12

Practice of automatic control and process control 5/8


Mechatronics Department Process control and SCADA lab

16

19
17 18

20
±10Vdc

21

23
22

24 27
30

25 28 31
26 29 32

Hình 1.17. Trình tự thực 33


hiện cấu hình phần cứng bộ 34
điều khiển 35

Practice of automatic control and process control 6/8


Mechatronics Department Process control and SCADA lab

- Nạp phần cứng vào PLC

1 2

3
4

6
7
5

9
10

11 12

13

Hình 8.6. Trình tự nạp phần cứng vào PLC

Practice of automatic control and process control 7/8


Mechatronics Department Process control and SCADA lab

Địa Chỉ Tên thiết bị

I0.0 S1 Nút Start/Stop

I0.1 LIS1 Cảm biến phát hiện ngưỡng trên

Q0.0 Van V3 và Bơm

Q0.1 Đèn báo trạng thái Start/ Stop

Q0.2 Van V1

AI 0 (PIW272) LT Thiết bị đo đo mức điện áp, 0…10 Vdc.

AI 1 (PIW274) FT Cảm biến đo lưu lượng 4…20 Ma

AO 1 (PQW290) V2 Van tuyến tính, dòng 2 dây 4…20 mA

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)

- Viết chương trình xác định mức lỏng

Để 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)

• Temp1(int32) = convert (PIWx) sang int32


•Temp2 (real32) = convert (Temp1)
•Temp3 (real32) = Temp2 – PIWmin
•Temp4 (real32) = PIWmax – PIWmin
•Temp5 (real32) = Temp3/Temp4
•Temp6 (real32) = xmax – xmin
•Temp7 (real32) = Temp5*Temp6
•x = Temp7 + xmin

+ Viết chương trình con


Chúng ta viết chương trình con thực hiện phương trình (1) trong khối FC theo trình
tự sau:
• Tạo lập khối FC

1 2

Vị trí
khai báo 3
biến

Hình 8.7. Khởi tạo khối FC

• Khai báo biến cho hàm và các biến phụ


Practice of automatic control and process control 9/8
Mechatronics Department Process control and SCADA lab

In

PIW_min
Out
x
PIWmax

x_max

x_min
Biến vào Biến ra

Hình 1.19: Các biến vào ra của hàm FC

Biến vào

Biến ra

Biến phụ

Hình 8.8. Khai báo biến hàm FC

Practice of automatic control and process control 10/8


Mechatronics Department Process control and SCADA lab

• 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

1. Chương trình con


cho phương trình
(1)

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)

- Viết chương trình xác định giá trị của PQWx


Cách viết chương trình cho phương trình (2) cũng tương tự như phương trình (1)
với các hàm toán học như sau:

•Temp3 (real32) = mv – mvmin

•Temp4 (real32) = mvmax – mvmin

•Temp5 (real32) = Temp3/Temp4

•Temp6 (real32) = PQWmax – PQWmin

•Temp7 (real32) = Temp5*Temp6

•Temp8 (real32)= Temp7 + PQWmin

•Temp9 (Dint32) = Round(Temp8)

•PWQx (int16) = Mov (in (Dint32), out(int16))

Practice of automatic control and process control 12/8


Mechatronics Department Process control and SCADA lab

Chương trình cho


phương trình (2)
được triển khai
trong FC2

Gọi hàm với MD7


chứa giá trị mv

Hình 8.10. Chương trình tạo ra tín hiệu điều khiển mv

Bước 4: Viết chương trình PID số

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)

Practice of automatic control and process control 13/8


Mechatronics Department Process control and SCADA lab

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

Hình 8.11. Lưu đồ giải thuật viết chương trình PID số

Practice of automatic control and process control 14/8


Mechatronics Department Process control and SCADA lab

Thuật toán PID số được triển khai trong khối FC3 như sau:

Hình 1.24: Chương trình PID số

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)

Practice of automatic control and process control 15/8


Mechatronics Department Process control and SCADA lab

1
Khai
báo
thông
số
2

4 5
6

Chương trình bù bit (OB35)

Hình 8.12. Tạo DB1 và chương trình ngắt OB35

Practice of automatic control and process control 16/8


Mechatronics Department Process control and SCADA lab

1
Khai
báo
thông
số
2

4 5
6

Chương trình bù bit (OB35)

Hình 8.13. Tạo DB1 và chương trình ngắt OB35

Practice of automatic control and process control 17/8


Mechatronics Department Process control and SCADA lab

Chương trình hoàn chỉnh

Vùng nhớ PIW


của PLC, ô
nhớ, tùy theo
khi khai báo
phần

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

Practice of automatic control and process control 18/8


Mechatronics Department Process control and SCADA lab

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

Hình 8.15. Tạo bảng trong TIA portal

Các bước vận hành khi thí nghiệm/thực hành:


Bước 1: Kiểm tra mức nước có trong thùng chứa (01), lượng nước có trong
thùng chứa (01) phải >= 2/3 thể tích của thùng chứa (01)

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

Practice of automatic control and process control 19/8


Mechatronics Department Process control and SCADA lab

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ì:

- Làm vệ sinh thiết bị phục vụ thí nghiệm


- Tắt máy tính
- Tắt nguồn cấp cho trạm
- Báo cho giảng viên hướng dẫn đã hoàn thành bài thí nghiệm/thực
hành
Kết thúc thí nghiệm/thực hành

Practice of automatic control and process control 20/8


Mechatronics Department Process control and SCADA lab

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

Hình 9.1. Cách mở giao diện WINCC


- Chọn File – New hoặc click vào biểu tượng “New” để tạo mới một Project. Hộp
thoại WinCC Explorer hiện ra với bốn lựa chọn:

Hình 9.2. Hộp thoại WINCC Explorer

Practice of automatic control and process control 1/9


Mechatronics Department Process control and SCADA lab

▪ Single_User Project : một người dùng.


▪ Multi_User Project: Project nhiều người sử dụng hay cùng một Project mà
nhiều máy tính khác nhau có thể sử dụng. Các máy tính này phải có quyền ưu
tiên ngang nhau (đều ở cấp độ Server).
▪ Multi_Client Prpject: nhiều người sử dụng ở cấp độ Client có thể truy cập
cùng một cơ sở dữ liệu của một Project (ở cấp độ Server).
▪ Open an Existing project: mở một project đã có sẵn.
▪ Tùy theo ứng dụng mà bạn có thể có lựa chọn khác nhau. Ở đây ta chọn
Single_User Project và click chọn OK.
▪ Tiếp theo sẽ gặp hộp thoại “Creat a new project”. Ta được yêu cầu nhập tên
và đường dẫn nơi lưu trữ project. Project vừa tạo tên với phần mở rộng “.mcp”
(master control program).
▪ Khi mở WinCC giao diện sau đây được hiện ra:

Hình 9.3. Giao diện WinCC


- Nên nhớ sau lần mở đầu WinCC thì project được tạo sau cùng sẽ được mở mặc
định.
9.2 Tạo Driver kết nối giữa WinCC và PLC.
➢ Driver là phần giao tiếp (giao thức, protocol) giữa WinCC và thiết bị.
- Để tạo một Driver “right_click” vào Tag Management chọn Open
Practice of automatic control and process control 2/9
Mechatronics Department Process control and SCADA lab

Hình 9.4. WinCC Project


- Cửa sổ Add new driver hiện ra, ta chọn loại Driver tương thích. Với việc giao
tiếp của họ PLC SMATIC S7 300/400 chọn “SIMATIC S7 protocol Suit.CHN”,
nếu là S7-1200/S7-1500 chọn “SIMATIC S7-1200, S7-1500 Channel”

Hình 9.5. Chọn Driver kết nối giữa PLC và WinCC


- Sau khi chọn kênh Driver xong, “double click” vào kênh Driver vừa tạo và tùy
theo cấu hình mạng đang sử dụng ta chọn loại giao tiếp tương thích. Trong thực
hành này chọn mạng Ethernet TCP/IP thì thực hiện như sau

Practice of automatic control and process control 3/9


Mechatronics Department Process control and SCADA lab

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:

Practice of automatic control and process control 4/9


Mechatronics Department Process control and SCADA lab

Địa chỉ cổng IP của PLC là 192.168.0.1


Rack gắn CPU là 0
Vị trí slot trên thanh ray gắn CPU là 2

9.3 Tạo tag group


➢ Tạo tag group
Tag group dùng để nhóm các tag thành từng nhóm tag tùy theo từng trạm.

Hình 9.7. Cách tạo tag group


➢ Tạo tag
Tag là những thành phần trung tâm của các giá trị xử lí quá trình truy xuất
thông tin. Trong Project của WinCC, mỗi tag nhận một tên và kiểu dữ liệu duy
nhất. Kết nối logic từ PLC được ấn định đến giá trị xử lí tới những tag sử dụng. Tag
được lưu trữ trong cơ sở dữ liệu của project – wide. Khi máy chạy WinCC bắt đầu,
tất cả những tag thuộc project đều được load lên và cấu trúc run – time tương ứng
được thiết lập.
Đối với một ứng dụng, việc truy xuất các tiện ích của tag của người quản lí đều
rất rõ ràng. Nghĩa là ứng dụng không cần thông tin gì về nguồn hay đích của tag.
Trong WinCC có hai loại tag:

Practice of automatic control and process control 5/9


Mechatronics Department Process control and SCADA lab

▪ 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

- Bit nhớ (M).


- Đầu vào Input (I).
- Đầu ra Output (Q).
- Bộ Timer (T).
- Bộ Counter (C).
9.4 Tạo picture bằng công cụ Graphic Designer
Graphic Designer dùng để tạo những bức ảnh xử lí với các tính năng sau:
- Giao diện đơn giản, dễ sử dụng và tương thích với hoạt động trong Windows.
- Cấu hình hệ thống với thư viện thống nhất.
- Cho phép nhập các hình ảnh và hỗ trợ giao diện bằng phương tiện cấu hình
Script.
- Liên kết với các đối tượng Graphic mà ta có thể tự tạo.
9.4.1 Cách tạo một Picture:
- Từ WinCC Explore, right_click vào Graphics Designer và chọn New Picture.

Hình 9.8. Cách tạo mới một Graphic Designer.

Practice of automatic control and process control 7/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.

Hình 9.9. Cách thay đổi tên của Graphic Designer


- Cửa sổ New Name xuất hiện và ta có thể đặt tên cho picture. Sau đó bạn chon
vào nút OK.
- Để khởi tạo picture ta right_click vào picture vừa tạo và chọn Open Picture.

Hình 9.10. Cách tạo giao diện của Graphic Designer


- Giao diện của Graphic Designer:

Hình 9.11. Giao diện của WinCC


Practice of automatic control and process control 8/9
Mechatronics Department Process control and SCADA lab

❖ Các công cụ hỗ trợ trong Graphic Designer


- Color pallete: sử dụng bảng màu có sẵn của WinCC hoặc tạo ra các màu mới
cho từng object picture của bạn trong Graphic Designer. Bảng màu này dễ dàng tìm
thấy trong Graphic Designer bạn cũng có thể định nghĩa màu mới bằng cách click

vào biểu tượng .


- Object pallete: trong bảng Object pallete có hai tab, trong đó tab Standard sử
dụng các Object hiển thị trên các picture thông thường, còn tab Control dùng tạo
các Object điều khiển trong WinCC như: Alarm Control, Online Trend Control,…
Ơ đây ta chỉ giới thiệu về các thành phần của tab Standard còn các phần của tab
Control sẽ được giới thiệu sau.
- Standard Object: dùng để vẽ các đối tượng như: line, ellipse, rectangle..
- Smart Object:
▪ Application Window: là đối tượng được điều khiển bởi các message System
(Alarm Logging), từ các Archive System (Tag Logging), từ các ứng dụng của

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

Poperties” của tab”Event”.


- I/O Field: dùng để nhận giá trị từ các tag đã được kết nối với WinCC vào PLC,
những giá trị này cần hiển thị trên các Picture trong quá trình Run Time để bạn có

thể theo dõi và điều khiển giá trị này.


❖ Cách tạo ra một I/O Field:
- Mở rộng thư mục Smart Object ra

Hình 9.12. Bước đầu tạo I/O Field


Practice of automatic control and process control 9/9
Mechatronics Department Process control and SCADA lab

- 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

Chọn tag liên


kết 2
Thời gian cập
nhập giá trị
1 3
Font của số chỉ
thị trên I/O field
Màu của số chỉ thị
trên I/O field

Hình 9.13. I/O Field

- Graphic Object: dùng để chèn những File hình ảnh có đuôi “.bmp” vào trong
Picture WinCC

❖ Cách chèn một Graphic Object:


Practice of automatic control and process control 10/9
Mechatronics Department Process control and SCADA lab

- Double_click vào biểu tượng Graphic Object.


- Cửa sổ Graphic Object Configuration xuất hiện, click vào nút Find để tìm hình
ảnh mà ta muốn chèn vào trong Picture WinCC
- Sau khi click vào nút Find cửa sổ Picture hiện ra, ta phải chọn đến đường dẫn
chứa hình ảnh cần hiển thị.
Ví dụ: Chèn hình sau
Lưu ý: để thuận lợi thì hình nên ở dạng .bmp và được chép vào thư mục GraSC
của project

2 3

Hình 9.14. Chèn hình vào giao điện thiết kế

❖ Các chức năng có trong Window Object


Practice of automatic control and process control 11/9
Mechatronics Department Process control and SCADA lab

Hình 9.15. Cửa số của Window Object


▪ Button: dùng như một sự kiện, có thể tác động thông qua các thao tác click
chuột. Nút nhấn có thể sử dụng để set, reset giá trị một tag nhị phân được kết nối,
hoặc được dùng để kết nối một Picture,…
Cách tạo một Button
- Double_click vào biểu tượng Button trong Object
1

Hình 9.16 Cách tạo Button.


- Cửa sổ Button Configuration xuất hiện ta đặt tên, chọn kiểu font, màu chữ cho
Button.

1
Đặt tên cho button
2
Đặt font cho tên button 3

Chọn màu cho tên button

Hình 9.17. Thiết lập thông số cho button

Practice of automatic control and process control 12/9


Mechatronics Department Process control and SCADA lab

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

Practice of automatic control and process control 13/9


Mechatronics Department Process control and SCADA lab

Mức logic của


tag mà khi nhấn
nút này sẽ bị
thay đổi

Tên của tag cần


thay đổi mức
logic

Theo như ví dụ nhấn nút RUN thì tag “Run_Stop_Button” có mức logic là “1”

10
9

11

Tại bước 9 SetTagBit(“Run_Stop_Button”,1)


Ví dụ 2: Thuộc tính của button là nhấn nút RUN thì tag “Run_Stop_Button” tạo
cạnh lên. Các thao tác như ví dụ 1 chỉ khác ở thao tác thứ 12 của Hình 5.26

12

Practice of automatic control and process control 14/9


Mechatronics Department Process control and SCADA lab

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

Lưu ý: Screen03 đã tạo trước

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

Hình 9.21. Cách tạo biến cần vẽ đồ thị


❖ Lấy hình ảnh của các đối tượng có sẵn trong thư viện
Ví dụ lấy bồn chứa (có trong thư viện tanks của Siemens)
Practice of automatic control and process control 17/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 ý

Practice of automatic control and process control 18/9


Mechatronics Department Process control and SCADA lab

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

Hình 9.23. Chạy và dừng runtime

B. Phần thực tập


Practice of automatic control and process control 19/9
Mechatronics Department Process control and SCADA lab

Ví dụ giao diện bài mức và lưu lượng

Practice of automatic control and process control 20/9

You might also like