You are on page 1of 43

Introduction

Digital Controller Design


In this section we will discuss converting continuous time models into discrete time (or difference
equation) models. We will also introduce the z-transform and show how to use it to analyze and
design controllers for discrete time systems.
Introduction
The figure below shows the typical continuous feedback system. Almost all of the continuous
controllers can be built using analog electronics.

The continuous controller, enclosed in the dashed square, can be replaced by a digital controller,
shown below, that performs same control task as the continuous controller. The basic difference
between these controllers is that the digital system operates on discrete signals (or samples of the
sensed signal) rather than on continuous signals.

Different types of signals in the above digital schematic can be represented by the following plots.

1
Zero-Hold Equivalence
In the above schematic of the digital control system, we see that the digital control system contains
both discrete and the continuous portions. When designing a digital control system, we need to find
the discrete equivalent of the continuous portion so that we only need to deal with discrete functions.
For this technique, we will consider the following portion of the digital control system and rearrange
as follows.

The clock connected to the D/A and A/D converters supplies a pulse every T seconds and each D/A
and A/D sends a signal only when the pulse arrives. The purpose of having this pulse is to require that
Hzoh(z) have only samples u(k) to work on and produce only samples of output y(k); thus, Hzoh(z) can
be realized as a discrete function.
The philosophy of the design is the following. We want to find a discrete function Hzoh(z) so that for a
piecewise constant input to the continuous system H(s), the sampled output of the continuous system
equals the discrete output. Suppose the signal u(k)
represents a sample of the input signal. There are
techniques for taking this sample u(k) and holding it
to produce a continuous signal uhat(t). The sketch
below shows that the uhat(t) is held constant at u(k)
over the interval kT to (k+1)T. This operation of
holding uhat(t) constant over the sampling time is
called zero-order hold.
The zero-order held signal uhat(t) goes through H2(s)
and A/D to produce the output y(k) that will be the
piecewise same signal as if the discrete signal u(k)
goes through Hzoh(z) to produce the discrete output
y(k).

2
Now we will redraw the schematic, placing Hzoh(z) in place of the continuous portion.

By placing Hzoh(z), we can design digital control systems dealing with only discrete functions.
Note: There are certain cases where the discrete response does not match the continuous response
due to a hold circuit implemented in digital control systems.
Conversion Using c2d
There is a MATLAB function called c2d that converts a given continuous system (either in transfer
function or state-space form) to a discrete system using the zero-order hold operation explained
above. The basic command for this in MATLAB is sys_d = c2d(sys,Ts,'zoh')
The sampling time (Ts in sec/sample) should be smaller than 1/(30*BW), where BW is the closed-loop
bandwidth frequency.
Example: Mass-Spring-Damper
Transfer Function
Suppose you have the following continuous transfer function

(1)
Assuming the closed-loop bandwidth frequency is greater than 1 rad/sec, we will choose the sampling
time (Ts) equal to 1/100 sec. Now, create a new m-file and enter the following commands.
M = 1;
b = 10;
k = 20;
s = tf('s');
sys = 1/(M*s^2+b*s+k);
Ts = 1/100;
sys_d = c2d(sys,Ts,'zoh')
sys_d =
4.837e-05 z + 4.678e-05
-----------------------
z^2 - 1.903 z + 0.9048
Sample time: 0.01 seconds
Discrete-time transfer function.
Introduction: Simulink Modeling
In Simulink, it is very straightforward to represent and then simulate a mathematical model
representing a physical system. Models are represented graphically in Simulink as block diagrams. A
wide array of blocks is available to the user in provided libraries for representing various phenomena
and models in a range of formats. One of the primary advantages of employing Simulink (and
simulation in general) for the analysis of dynamic systems is that it allows us to quickly analyze the
response of complicated systems that may be prohibitively difficult to analyze analytically. Simulink is

3
able to numerically approximate the solutions to mathematical models that we are unable to, or don't
wish to, solve "by hand."
In general, the mathematical equations representing a given system that serve as the basis for a
Simulink model can be derived from physical laws. In this section we will demonstrate how to derive a
mathematical model and then implement that model in Simulink. This model is then employed in the
Introduction: Simulink Control section in order to demonstrate how to employ Simulink to design and
simulate the control for a system.
Train system
In this example, we will consider a toy train consisting of an engine and a car. Assuming that the train
only travels in one dimension (along the track), we want to apply control to the train so that it starts
and comes to rest smoothly, and so that it can track a constant speed command with minimal error in
steady state.
The mass of the engine and the car will be represented by and , respectively. Furthermore, the
engine and car are connected via a coupling with stiffness . In other words, the coupling is modeled
as a spring with a spring constant . The force represents the force generated between the wheels of
the engine and the track, while represents the coefficient of rolling friction.

Free-body diagram and Newton's second law


The first step in deriving the mathematical equations that govern a physical system is to draw the
free-body diagram(s) representing the system. This is done below for our train system.

From Newton's second law, we know that the sum of the forces acting on a body is equal to the
product of the mass of the body and its acceleration. In this case, the forces acting on the engine
in the horizontal direction are the spring force, the rolling resistance, and the force generated at the
wheel/track interface. The forces acting on the train car in the horizontal direction are the spring
force and the rolling resistance. In the vertical direction, the weight forces are balanced by the normal
forces applied by the ground . Therefore, there will be no acceleration in the vertical
direction.
We will model the spring as generating a force that is linearly proportional to the deformation of the
spring, , where and are the displacements of the engine and car, respectively. Here it is
assumed that the spring is undeformed when and equal zero. The rolling resistance forces are

4
modeled as being linearly proportional to the product of the corresponding velocities and normal
forces (which are equal to the weight forces).
Applying Newton's second law in the horizontal direction based on the above free-body diagrams
leads to the following governing equations for the train system.

(1)

(2)

Constructing the Simulink model


This set of system equations can now be represented graphically without further manipulation.
Specifically, we will construct two copies (one for each mass) of the general expression or
. First, open Simulink and open a new model window. Then drag two Sum blocks (from
the Math Operations library) into your model window and place them approximately as shown in the
figure (1) below.
The outputs of each of these Sum blocks represents the
sum of the forces acting on each mass. Multiplying each
output signal by will give us the corresponding
acceleration of each mass. Now drag two Gain blocks
(from the Math Operations Library) into your model and 1
attach each one with a line from the output of one of the
Sum blocks. Label these two signals as "Sum_F1" and
"Sum_F2" in order to make your model clearer. This is
accomplished by double-clicking in the space above each
of the two signal lines and entering the desired label.
These Gain blocks should contain for each of the
masses. We will define the variables M1 and M2 in the
MATLAB workspace, so we can just enter the corresponding
variable names in each of the Gain blocks. Double-click on
the upper Gain block and enter "1/M1" into the Gain field.
Similarly, enter "1/M2" in the Gain field of the second Gain
block figure (2). 2
You will notice that the gains did not appear in the image of
the Gain blocks, rather the blocks display a value of -K-. This
is because the blocks are too small on the screen to show
the full variable name inside the triangle. The blocks can be
resized so that the actual gain value can be seen. To resize a
block, select it by clicking on it once. Small squares will
appear at the corners. Drag one of these squares to stretch the
block. Your model should appear as figure (3).
The outputs of these gain blocks are the accelerations of each
of the masses (the train engine and car). The governing 3
5
equations we derived above depend on the velocities and displacements of the masses. Since velocity
can be determined by integrating acceleration, and position can be determined by integrating
velocity, we can generate these signals employing integrator blocks. Drag a total of four Integrator
blocks from the Continuous library into your model, two for each of our two accelerations. Connect
these blocks and label the signals as shown in figure (4). Specifically, the first inegrator takes the
acceleration of mass 1 ("x1_ddot") as an input and generates the velocity of mass 1 ("x1_dot"). The
second integrator then takes this velocity and outputs the displacement of the first mass ("x1"). The
same pattern holds for the integrators for the second mass.
Now, drag two Scopes from the Sinks library into your model and connect them to the outputs of
these integrators. Label them "x1" and "x2" figure (5).
Now we are ready to add the forces acting
on each mass. First, we need to adjust the
inputs on each Sum block to represent the
proper number of forces. Since there is a
total of three forces acting on mass 1,
double-click on the corresponding Sum
block and change the List of signs field to "| 5
+++". The symbol "|" serves as a spacer.
There are only 2 forces acting on mass 2,
therefore, we can leave that Sum block
alone for now
The first force acting on mass 1 is just the
input force, F. Drag a Signal Generator block
from the Sources library and connect it to
the uppermost input of the corresponding
Sum block. Label this signal as "F"

The next force acting on mass 1 is the rolling resistance force. Recall that this force is modeled as
follows.

(3)
To generate this force, we can tap off the velocity signal and multiply by an appropriate gain. Drag a
Gain block into your model window. Tap off the "x1_dot" signal and connect it to the input of this new
Gain block (draw this line in several steps if necessary). Connect the output of the Gain block to the
second input of the Sum block. Double-click the Gain block and enter "mu*g*M1" into the Gain field.
The rolling resistance force, however, acts in the negative direction. Therefore, change the list of signs
of the Sum block to "|+-+". Next, resize the Gain block to display the full gain and label the output of
the Gain block "Frr1". Your model should now appear as follows.

6
The last force acting on mass 1 is the spring force. Recall that this force is equal to the following.

(4)

Therefore, we need to generate a signal which we can then be multiplied by a gain to create

the force. Drag a Subtraction block (or a Sum block or an Addition block) below the rest of your

model. In order to change the direction of this block, right-click on the block and choose Format > Flip

block from the resulting menu. Alternatively, you can select the block then hit Ctrl-I. Now, tap off the

"x2" signal and connect it to the negative input of the Subtract block. Also, tap off the "x1" signal and

connect it to the positive input. This will cause signal lines to cross. Lines may cross, but they are only
actually connected where a small block appears (such as at a tap point).

7
Now, we can multiply this difference by the spring constant to generate the spring force. Drag a Gain
block into your model to the left of the Subtraction block. Change the value of the Gain block to "k"
and connect the output of the Subtract block to its input. Then connect the output of the Gain block
to the third input of the Sum block for mass 1 and label the signal "Fs". Since the spring force acts on
mass 1 in the negative direction, it is necessary to again change the list of signs of the Sum block to "|
+--". Your model should appear as follows.

We can now apply forces to mass 2. For the first force, we will use the same spring force we just
generated, except that it is applied to mass 2 in the positive direction. Simply tap off the spring force
signal "Fs" and connect it to the first input of the Sum block for mass 2.

8
The last force applied to mass 2 is its rolling resistance force. This force is generated in an analogous
manner to the rolling resistance force applied to mass 1. Tap off the signal "x2_dot" and multiply it by
a Gain block with value "mu*g*M2". Then connect the output of the Gain block to the second input of
the corresponding Sum block and label the signal "Frr2". Changing the second input of the Sum block
to be negative will lead to the following model.

9
Now the model is complete. We simply need to supply the proper input and define the output of
interest. The input to the system is the force generated by the engine. Within the Simulink model,
we have already defined the force to be the output of a Signal Generator block. The output of the
system, which we will observe and ultimately try to control, will be the velocity of the train engine.
Add another Scope block to your model from the Sinks library. Tap a line from the "x1_dot" signal and
connect it to the Scope block. Label this scope as "x1_dot" and your model should appear as in the
following.

Running the model


Before running the model, we need to assign numerical values to each of the variables used in the
model. For the train system, we will employ the following values.
 = 1 kg
 = 0.5 kg
 = 1 N/sec
 =1N
 = 0.02 sec/m
 = 9.8 m/s^2
Create a new m-file and enter the following commands.
M1 = 1; M2 = 0.5; k = 1;

10
F = 1; mu = 0.02; g = 9.8;
Execute your m-file in the MATLAB command window to define these values. Simulink will recognize
these MATLAB variables for use in the model.
Now, we need to give an appropriate input to the engine. Double-click on the Signal Generator block
(outputs "F"). Select square from the Wave form drop-down menu and set the Frequency field to
equal "0.001". You may leave the Units as the default Hertz. Also enter "-1" into the Amplitude field
(positive amplitude steps negative before stepping positive).

The last step before running the simulation is to select an appropriate simulation time. To view one
cycle of the 0.001 Hz square wave, we should simulate the model for 1000 seconds. Select
Parameters from the Simulation menu at the top of the model window and change the Stop Time
field to "1000". Close the dialog box.
Now, run the simulation and open the "x1_dot"
scope to examine the velocity output (hit auto-
scale). The input was a square wave with two
steps, one positive and one negative. Physically,
this means that the engine first went forward,
then backward. The velocity output reflects this.

11
DC Motor Speed: System Modeling
Physical setup
A common actuator in control systems is the DC motor. It directly provides rotary motion and,
coupled with wheels or drums and cables, can provide translational motion. The electric equivalent
circuit of the armature and the free-body diagram of the rotor are shown in the following figure.

For this example, we will assume that the input of the system is the voltage source (V) applied to the
motor's armature, while the output is the rotational speed of the shaft d(theta)/dt. The rotor and
shaft are assumed to be rigid. We further assume a viscous friction model, that is, the friction torque
is proportional to shaft angular velocity.
The physical parameters for our example are:
(J) moment of inertia of the rotor 0.01 kg.m^2
(b) motor viscous friction constant 0.1 N.m.s
(Ke) electromotive force constant 0.01 V/rad/sec
(Kt) motor torque constant 0.01 N.m/Amp
(R) electric resistance 1 Ohm
(L) electric inductance 0.5 H
System equations
In general, the torque generated by a DC motor is proportional to the armature current and the
strength of the magnetic field. In this example we will assume that the magnetic field is constant and,
therefore, that the motor torque is proportional to only the armature current i by a constant factor Kt
as shown in the equation below. This is referred to as an armature-controlled motor.
(1)
The back emf, e, is proportional to the angular velocity of the shaft by a constant factor Ke.
(2)

In SI units, the motor torque and back emf constants are equal, that is, Kt = Ke; therefore, we will use

K to represent both the motor torque constant and the back emf constant.

From the figure above, we can derive the following governing equations based on Newton's 2nd law

and Kirchhoff's voltage law.


(3)

12
(4)

1. Transfer Function
Applying the Laplace transform, the above modeling equations can be expressed in terms of the
Laplace variable s.
(5)
(6)
We arrive at the following open-loop transfer function by eliminating I(s) between the two above
equations, where the rotational speed is considered the output and the armature voltage is
considered the input.

(7)
2. State-Space
In state-space form, the governing equations above can be expressed by choosing the rotational
speed and electric current as the state variables. Again the armature voltage is treated as the input
and the rotational speed is chosen as the output.

(8)

(9)
Design requirements
First consider that our uncompensated motor rotates at 0.1 rad/sec in steady state for an input
voltage of 1 Volt. Since the most basic requirement of a motor is that it should rotate at the desired
speed, we will require that the steady-state error of the motor speed be less than 1%. Another
performance requirement for our motor is that it must accelerate to its steady-state speed as soon as
it turns on. In this case, we want it to have a settling time less than 2 seconds. Also, since a speed
faster than the reference may damage the equipment, we want to have a step response with
overshoot of less than 5%.
In summary, for a unit step command in motor speed, the control system's output should meet the
following requirements.
 Settling time less than 2 seconds
 Overshoot less than 5%
 Steady-state error less than 1%
MATLAB representation
1. Transfer Function
We can represent the above open-loop transfer function of the motor in MATLAB by defining the
parameters and transfer function as follows. Running this code in the command window produces the
output shown below.
J = 0.01;
b = 0.1;

13
K = 0.01;
R = 1;
L = 0.5;
s = tf('s');
P_motor = K/((J*s+b)*(L*s+R)+K^2)
P_motor =
0.01
---------------------------
0.005 s^2 + 0.06 s + 0.1001
Continuous-time transfer function.
2. State Space
We can also represent the system using the state-space equations. The following additional MATLAB
commands create a state-space model of the motor and produce the output shown below when run
in the MATLAB command window.
A = [-b/J K/J
-K/L -R/L];
B = [0
1/L];
C = [1 0];
D = 0;
motor_ss = ss(A,B,C,D)
motor_ss =
a=
x1 x2
x1 -10 1
x2 -0.02 -2
b=
u1
x1 0
x2 2
c=
x1 x2
y1 1 0

d=
u1
y1 0

Continuous-time state-space model.


The above state-space model can also be generated by converting your existing transfer function
model into state-space form. This is again accomplished with the ss command as shown below.
motor_ss = ss(P_motor);

14
DC Motor Speed: Digital Controller Design
In this section, we will consider the digital version of the DC motor speed control problem. A sampled-
data DC motor model can be obtained from conversion of the analog model, as we will describe. In
this example we will design a PID controller.
The continuous open-loop transfer function for an input of armature voltage and an output of angular
speed was derived previously as the following.

(1)
For a 1-rad/sec step reference, the design criteria are the following.
 Settling time less than 2 seconds
 Overshoot less than 5%
 Steady-state error less than 1%

Creating a sampled-data model of the plant


The first step in the design of a digital control system is to generate a sampled-data model of the
plant. Therefore, it is necessary to choose a frequency with which the continuous-time plant is
sampled. In choosing a sampling period, it is desired that the sampling frequency be fast compared to
the dynamics of the system in order that the sampled output of the system captures the system's full
behavior, that is, so that significant inter-sample behavior isn't missed.
Let's create a continuous-time model of the plant. Create a new m-file and add the following MATLAB
code (refer to the main problem for the details of getting these commands). Running the m-file within
the MATLAB command window will generate the output shown below.
J = 0.01;
b = 0.1;
K = 0.01;
R = 1;
L = 0.5;
s = tf('s');
P_motor = K/((J*s+b)*(L*s+R)+K^2);
zpk(P_motor)

ans =

2
-------------------
(s+9.997) (s+2.003)

Continuous-time zero/pole/gain model.

15
The use of the zpk command above transforms the transfer function into a form where the zeros,
poles, and gain can be seen explicitly. Examining the poles of the plant (or its frequency response), the
dominant pole of the plant (sigma approximately equal to 2) corresponds to a settle time of
approximately 2 seconds (4 / sigma). Therefore, choosing a sampling period of 0.05 seconds is
significantly faster than the dynamics of the plant. This sampling period is also fast compared to the
speed that will be achieved by the resultant closed-loop system.
In this case, we will convert the given transfer function from the continuous Laplace domain to the
discrete z-domain. MATLAB can be used to achieve this conversion through the use of the c2d
command. The c2d command requires three arguments: a system model, the sampling time (Ts), and
the type of hold circuit. In this example, we will assume a zero-order hold (zoh) circuit. Adding the
following commands to your m-file and running in the MATLAB command window generates the
sampled-data model shown below.
Ts = 0.05;
dP_motor = c2d(P_motor,Ts,'zoh');
zpk(dP_motor)
ans =

0.0020586 (z+0.8189)
---------------------
(z-0.9047) (z-0.6066)

Sample time: 0.05 seconds


Discrete-time zero/pole/gain model.

We would now like to analyze the closed-loop response of the system without any additional
compensation. First, we have to close the loop of the transfer function by using the feedback
command. After closing the loop, let's inspect the closed-loop step response with the zero-order hold.
This can be accomplished by using the step and stairs commands. Since the step command is fed a
discrete model, it will output a vector of discrete samples at the sample time Ts associated with the
model (click here for further details). The syntax below specifies to simulate the step response for 0.5
seconds. The stairs command draws these discrete data points as a stairstep, just like what would be
produced by a zero-order hold circuit. Add the following MATLAB code at the end of your previous m-
file and rerun it. You should generate the plot shown below.
sys_cl = feedback(dP_motor,1);
[y,t] = step(sys_cl,12);
stairs(t,y);
xlabel('Time (s)')
ylabel('Velocity (rad/s)')
title('Stairstep Response: Original')

16
Examination of the above shows that the steady-state error is much too large and the settle time may
be a little slow.

17
DC Motor Speed: Simulink Modeling
Building the model with Simulink
This system will be modeled by summing the torques acting on the rotor inertia and integrating the
acceleration to give velocity. Also, Kirchoff's laws will be applied to the armature circuit. First, we will
model the integrals of the rotational acceleration and of the rate of change of the armature current.

(3)

(4)
To build the simulation model, open Simulink and open a new model window. Then follow the steps
listed below.
 Insert an Integrator block from the
Simulink/Continuous library and draw lines to and
from its input and output terminals.
 Label the input line "d2/dt2(theta)" and the output
line "d/dt(theta)" as shown below. To add such a
label, double-click in the empty space just below the
line.
 Insert another Integrator block above the previous
one and draw lines to and from its input and output
terminals.
 Label the input line "d/dt(i)" and the output line "i".

Next, we will apply Newton's law and Kirchoff's law to the


motor system to generate the following equations:

(5)

(6)
The angular acceleration is equal to 1 / J multiplied by the sum of two terms (one positive, one
negative). Similarly, the derivative of current is equal to 1 / L multiplied by the sum of three terms
(one positive, two negative). Continuing to model these equations in Simulink, follow the steps given
below.
 Insert two Gain blocks from the Simulink/Math Operations library, one attached to each of the
integrators.
 Edit the Gain block corresponding to angular acceleration by double-clicking it and changing its
value to "1/J".
 Change the label of this Gain block to "Inertia" by clicking on the word "Gain" underneath the
block.
 Similarly, edit the other Gain's value to "1/L" and its label to "Inductance".

18
 Insert two Add blocks from the Simulink/Math Operations library, one attached by a line to
each of the Gain blocks.
 Edit the signs of the Add block
corresponding to rotation to "+-"
since one term is positive and one is
negative.
 Edit the signs of the other Add block
to "-+-" to represent the signs of the
terms in the electrical equation.

Now, we will add in the torques which are


represented in the rotational equation. First,
we will add in the damping torque.
 Insert a Gain block below the "Inertia" block. Next right-click on the block and select Format >
Flip Block from the resulting menu to flip the block from left to right. You can also flip a
selected block by holding down Ctrl-I.
 Set the Gain value to "b" and rename this block to "Damping".
 Tap a line (hold Ctrl while drawing or right-click on the line) off the rotational Integrator's
output and connect it to the input of the "Damping" block.
 Draw a line from the "Damping" block output to the negative input of the rotational Add block.
Next, we will add in the torque from the armature.
 Insert a Gain block attached to the positive input of the rotational Add block with a line.
 Edit its value to "K" to represent the motor constant and Label it "Kt".
 Continue drawing the line leading from the current Integrator and connect it to the "Kt" block.

19
Now, we will add in the voltage terms which are represented in the electrical equation. First, we will
add in the voltage drop across the armature resistance.
 Insert a Gain block above the "Inductance" block and flip it from left to right.
 Set the Gain value to "R" and rename this block to "Resistance".
 Tap a line off the current Integrator's output and connect it to the input of the "Resistance"
block.
 Draw a line from the "Resistance" block's output to the upper negative input of the current
equation Add block.
Next, we will add in the back emf from the motor.
 Insert a Gain block attached to the other negative input of the current Add block with a line.
 Edit it's value to "K" to represent the motor back emf constant and Label it "Ke".
 Tap a line off the rotational Integrator's output and connect it to the "Ke" block.
 Add In1 and Out1 blocks from the Simulink/Ports & Subsystems library and respectively label
them "Voltage" and "Speed".
The final design should look like the example shown in the figure below.

20
In order to save all of these components as a single subsystem block, first select all of the blocks, then
select Create Subsystem from the Edit menu. Name the subsystem "DC Motor" and then save the
model. Your model should appear as follows.

21
DC Motor Speed: Simulink Controller Design

A linear model of the system can be extracted from the Simulink model into the MATLAB workspace.
This can have accomplished employing the MATLAB command linmod or from directly within Simulink
as we will do here.
Recall that the physical parameters have to be set if they have not previously been defined in the
workspace.
J = 0.01;
b = 0.1;
K = 0.01;
R = 1;
L = 0.5;
We then need to identify the inputs and outputs of the model we wish to extract. First right-click on
the signal representing the Voltage input in the Simulink model. Then choose Linearization > Input
Point from the resulting menu. Similarly, right-click on the signal representing the Speed output and
select Linearization > Output Point from the resulting menu. The input and output signals should now
be identified on your model by arrow symbols as shown in the figure below.

22
In order to perform the extraction, select from the menus at the top of the model window Tools >
Control Design > Linear Analysis. This will cause the Linear Analysis Tool to open. Within the Linear
Analysis Tool window, the Operating Point to be linearized about can remain the default, Model
Initial Condition. In order to perform the linearization, next click the Linearize button identified by the
green triangle. The result of this linearization is the linsys1 object which now appears in the Linear
Analysis Workspace as shown below. Furthermore, the open-loop step response of the linearized
system was also generated automatically.

23
Open-loop response
The open-loop step response can also be generated directly within Simulink, without extracting any
models to the MATLAB workspace. In order to simulate the step response, the details of the
simulation must first be set. This can be accomplished by selecting Configuration Parameters from
the Simulation menu. Within the resulting menu, define the length for which the simulation is to run
in the Stop time field. We will enter "3" since 3 seconds will be long enough for the step response to
reach steady state. Within this window you can also specify various aspects of the numerical solver,
but we will just use the default values for this example.
Next we need to add an input signal and a means for displaying the output of our simulation. This is
done by doing the following:
 Remove the In1 and Out1 blocks.
 Insert a Step block from the Simulink/Sources library and connect it with a line to the Voltage
input of the motor subsystem.
 To view the Speed output, insert a Scope from the Simulink/Sinks library and connect it to the
Speed output of the motor subsystem.
 To provide an appropriate unit step input at t=0, double-click the Step block and set the Step
time to "0".
The final model should appear as shown in the following figure.

24
Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation
is finished, double-click on the scope and hit its autoscale button. You should see the following
output.

This response is identical to that obtained by MATLAB above using the extracted model. This is again
to be expected because this Simulink model includes only linear blocks.

25
DC Motor Position: System Modeling

Physical setup
A common actuator in control systems is the DC motor. It directly provides rotary motion and,
coupled with wheels or drums and cables, can provide translational motion. The electric equivalent
circuit of the armature and the free-body diagram of the rotor are shown in the following figure.

For this example, we will assume the following values for the physical parameters. These values were
derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab.
(J) moment of inertia of the rotor 3.2284E-6 kg.m^2
(b) motor viscous friction constant 3.5077E-6 N.m.s
(Kb) electromotive force constant 0.0274 V/rad/sec
(Kt) motor torque constant 0.0274 N.m/Amp
(R) electric resistance 4 Ohm
(L) electric inductance 2.75E-6H
In this example, we assume that the input of the system is the voltage source (V) applied to the
motor's armature, while the output is the position of the shaft (theta). The rotor and shaft are
assumed to be rigid. We further assume a viscous friction model, that is, the friction torque is
proportional to shaft angular velocity.
System equations
In general, the torque generated by a DC motor is proportional to the armature current and the
strength of the magnetic field. In this example we will assume that the magnetic field is constant and,
therefore, that the motor torque is proportional to only the armature current i by a constant factor Kt
as shown in the equation below. This is referred to as an armature-controlled motor.
(1)
The back emf, e, is proportional to the angular velocity of the shaft by a constant factor Kb.
(2)
In SI units, the motor torque and back emf constants are equal, that is, Kt = Ke; therefore, we will use
K to represent both the motor torque constant and the back emf constant.

26
From the figure above, we can derive the following governing equations based on Newton's 2nd law
and Kirchhoff's voltage law.
(3)

(4)
1. Transfer Function
Applying the Laplace transform, the above modeling equations can be expressed in terms of the
Laplace variable s.
(5)
(6)
We arrive at the following open-loop transfer function by eliminating I(s) between the two above
equations, where the rotational speed is considered the output and the armature voltage is
considered the input.

(7)
However, during this example we will be looking at the position as the output. We can obtain the
position by integrating the speed, therefore, we just need to divide the above transfer function by s.

(8)
2. State-Space
The differential equations from above can also be expressed in state-space form by choosing the
motor position, motor speed and armature current as the state variables. Again the armature voltage
is treated as the input and the rotational position is chosen as the output.

(9)

(10)
Design requirements
We will want to be able to position the motor very precisely, thus the steady-state error of the motor
position should be zero when given a commanded position. We will also want the steady-state error
due to a constant disturbance to be zero as well. The other performance requirement is that the
motor reaches its final position very quickly without excessive overshoot. In this case, we want the
system to have a settling time of 40 ms and an overshoot smaller than 16%.
If we simulate the reference input by a unit step input, then the motor position output should have:
 Settling time less than 40 milliseconds
 Overshoot less than 16%
 No steady-state error, even in the presence of a step disturbance input

27
MATLAB representation
1. Transfer Function
We can represent the above open-loop transfer function of the motor in MATLAB by defining the
parameters and transfer function as follows. Running this code in the command window produces the
output shown below.
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))
P_motor =

0.0274
-------------------------------------------
8.878e-12 s^3 + 1.291e-05 s^2 + 0.0007648 s

Continuous-time transfer function.

2. State Space
We can also represent the system using the state-space equations. The following additional MATLAB
commands create a state-space model of the motor and produce the output shown below when run
in the MATLAB command window.
A = [0 1 0
0 -b/J K/J
0 -K/L -R/L];
B = [0 ; 0 ; 1/L];
C = [1 0 0];
D = [0];
motor_ss = ss(A,B,C,D)
motor_ss =
a=
x1 x2 x3
x1 0 1 0
x2 0 -1.087 8487
x3 0 -9964 -1.455e+06
b=
u1
x1 0
x2 0
x3 3.636e+05

28
c=
x1 x2 x3
y1 1 0 0

d=
u1
y1 0

Continuous-time state-space model.

The above state-space model can also be generated by converting your existing transfer function
model into state-space form. This is again accomplished with the ss command as shown below.
motor_ss = ss(P_motor);

29
DC Motor Position: Digital Controller Design

The continues open-loop transfer function for DC motor's position in the Laplace domain is the
following.

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

the design requirements for a 1-radian step reference 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

Creating a sampled-date model of the plant


The first step in the design of a digital control system is to generate a sampled-data model of the
plant. Therefore, it is necessary to choose a frequency with which the continuous-time plant is
sampled. In choosing a sampling period, it is desired that the sampling frequency be fast compared to
the dynamics of the system in order that the sampled output of the system captures the system's full
behavior, that is, so that significant inter-sample behavior isn't missed.
Let's create a continuous-time model of the plant. Create a new m-file and add the following MATLAB
code (refer to the main problem for the details of getting these commands). Running the m-file within
the MATLAB command window will generate the output shown below.
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));
zpk(P_motor)

30
ans =

3086245930.9988
------------------------
s (s+1.454e06) (s+59.23)

Continuous-time zero/pole/gain model.

The use of the zpk command above transforms the transfer function into a form where the zeros,
poles, and gain can be seen explicitly. Examining the poles of the plant (or its frequency response), it
is clear that the pole at -1.45e06 contributes very little to the response of the plant. The gain
crossover frequency of the plant is approximately 5 Hz. Therefore, choosing a sampling period of
0.001 seconds (frequency of 1000 Hz) is significantly faster than the dynamics of the plant. This
sampling period is also fast compared to the speed that will be achieved by the resultant closed-loop
system. A sample time of 0.001 seconds is specifically 1/100 of the required time constant and 1/40 of
the required settling time.
In this case, we will convert the given transfer function from the continuous Laplace domain to the
discrete z-domain. MATLAB can be used to achieve this conversion through the use of the c2d
command. The c2d command requires three arguments: a system model, the sampling time (Ts), and
the type of hold circuit. In this example, we will assume a zero-order hold (zoh) circuit. Adding the
following commands to your m-file and running in the MATLAB command window generates the
sampled-data model shown below.
Ts = 0.001;
dP_motor = c2d(P_motor, Ts, 'zoh');
zpk(dP_motor)
ans =

0.0010389 (z+0.9831) (z+9.256e-07)


----------------------------------
z (z-1) (z-0.9425)

Sample time: 0.001 seconds


Discrete-time zero/pole/gain model.

From the above, notice that there is a pole and zero very near to z = 0 that effectively cancel. This
cancellation in the transfer function can be accomplished by applying the minreal command with a
tolerance of 0.001. Cancellation of this pole and zero will reduce the order of our transfer function
and will help to avoid numerical difficulties in MATLAB. Applying the minreal command, therefore,
produces the following reduced order transfer function. Note the absence of the pole and zero near z
= 0.

31
dP_motor = minreal(dP_motor,0.001);
zpk(dP_motor)
ans =

0.0010389 (z+0.9831)
--------------------
(z-1) (z-0.9425)
Sample time: 0.001 seconds
Discrete-time zero/pole/gain model.

We would now like to analyze the closed-loop response of the system without any additional
compensation. First, we have to close the loop of the transfer function by using the feedback
command. After closing the loop, let's inspect the closed-loop step response with the zero-order hold.
This can be accomplished by using the step and stairs commands. Since the step command is fed a
discrete model, it will output a vector of discrete samples at the sample time Ts associated with the
model (click here for further details). The syntax below specifies to simulate the step response for 0.5
seconds. The stairs command draws these discrete data points as a stairstep, just like what would be
produced by a zero-order hold circuit. Add the following code at the end of your previous m-file and
rerun it. You should generate a plot like the one shown below.
sys_cl = feedback(dP_motor,1);
[x1,t] = step(sys_cl,.5);
stairs(t,x1)
xlabel('Time (seconds)')
ylabel('Position (radians)')
title('Stairstep Response: Original')
grid

32
DC Motor Position: Simulink Modeling

Building the model with Simulink


This system will be modeled by summing the torques acting on the rotor inertia and integrating the
rotor's angular acceleration to give the velocity, and integrating the velocity to get position. Also,
Kirchoff's laws will be applied to the armature circuit. First, we will model the integrals of the rotor
acceleration and of the rate of change of armature current shown below.

(3)

(4)
To build the simulation model, open Simulink and open a new model window. Then follow the steps
listed below.
 Insert an Integrator block from the Simulink/Continuous library and draw lines to and from its
input and output terminals.
 Label the input line "d2/dt2(theta)" and the output line "d/dt(theta)" as shown below. To add
such a label, double-click in the empty space just above the line.
 Insert another Integrator block attached to the output of the previous one and draw a line
from its output terminal.
 Label the output line "theta".
 Insert a third Integrator block above the first one and draw lines to and from its input and
output terminals.

33
 Label the input line "d/dt(i)" and the output line "i".

Next, we will apply Newton's law and Kirchoff's law to the motor system to generate the following
equations.

(5)

(6)
The angular acceleration is equal to 1 / J multiplied by the sum of two terms (one positive, one
negative). Similarly, the derivative of current is equal to 1 / L multiplied by the sum of three terms
(one positive, two negative). Continuing to model these equations in Simulink, follow the steps given
below.
 Insert two Gain blocks from the Simulink/Math Operations library, one attached to each of the
leftmost integrators.
 Edit the Gain block corresponding to angular acceleration by double-clicking on it and
changing its value to "1/J".
 Change the label of this Gain block to "Inertia" by clicking on the word "Gain" underneath the
block.
 Similarly, edit the other Gain's value to "1/L" and its label to "Inductance".
 Insert two Add blocks from the Simulink/Math Operations library, one attached by a line to
each of the Gain blocks.
 Edit the signs of the Add block corresponding to rotation to "+-" since one term is positive and
one is negative.
 Edit the signs of the other Add block to "-+-" to represent the signs of the terms in Kirchoff's
equation.

34
Now, we will add in the torques which are represented in Newton's equation. First, we will add in the
damping torque.
 Insert a Gain block below the "Inertia" block, then select it by single-clicking on it. Next select
Flip Block from the Format menu (reached by right-clicking) to flip the block from left to right.
You can also flip the selected block by holding Ctrl-I.
 Set the Gain value to "b" and rename the block "damping".
 Tap a line (hold Ctrl while drawing or right-click) off the first rotational Integrator's output
(d/dt(theta)) and connect it to the input of the damping Gain block.
 Draw a line from the damping Gain output to the negative input of the rotational Add block.
Next, we will add in the torque from the armature.
 Insert a Gain block attached to the positive input of the rotational Add block with a line.
 Edit it's value to "K" to represent the motor constant and label it "Kt".
 Continue drawing the line leading from the current Integrator and connect it to the Kt Gain
block.

35
Now, we will add in the voltage terms which are represented in Kirchoff's equation. First, we will add
in the voltage drop across the armature resistance.
 Insert a Gain block above the Inductance block and flip it left to right.
 Set the Gain value to "R" and rename this block "Resistance".
 Tap a line off the current Integrator's output and connect it to the input of the Resistance Gain
block.
 Draw a line from the Resistance Gain output to the upper negative input of the current
equation Add block.
Next, we will add in the back emf from the motor.
 Insert a Gain block and attach it to the other negative input of the current Add block with a
line.
 Edit it's value to "K" to represent the motor back emf constant and label it "Ke".
 Tap a line off the first rotational Integrator's output (d/dt(theta)) and connect it to the Ke Gain
block.

36
Now the model is built and we just need to add the voltage input and monitor the position output.
We will insert In1 and Out1 ports from the Simulink/Ports & Subsystems library as shown in the
following figure so that we may save the motor model as a subsystem.

In order to save all of these components as a single subsystem block, first select all of the blocks, then
select Create Subsystem from the Edit menu. Name the subsystem "Motor_pos" and then save the
model. Your model should appear as follows.

37
DC Motor Position: Simulink Controller Design
Open-loop response
Begin with the above model saved as a subsystem and follow the steps given below.

38
 Remove the In and Out ports.
 Insert a Step block from the Simulink/Sources library and connect it with a line to the Voltage
input.
 To view the output position, insert a Scope from the Simulink/Sinks library and connect it to
the Position output.
 To provide an appropriate unit step input at t=0, double-click the Step block and set the Step
time to "0".
The system should now appear as in the following figure.

In order to simulate this system, the details of the simulation must first be set. This can be
accomplished by selecting Configuration Parameters from the Simulation menu. Within the resulting
menu, define the length for which the simulation is to run in the Stop time field. We will enter "0.2"
since 0.2 seconds will be long enough for the step response to reach steady state. Also in the
Configuration Parameters dialog box, the numerical solver employed in the simulation can be
specified. Go ahead and change the Solver field from the default ode45 (Dormand-Prince) to the
ode15s (stiff/NDF) solver. Since the time scales in this example are very small, this stiff system
integration method is more efficient than the default integration method.

The physical parameters must now be set. Enter the following commands at in the MATLAB command
window.
J = 3.2284E-6;
b = 3.5077E-6;
K = 0.0274;
R = 4;
L = 2.75E-6;

Then run the simulation (press Ctrl-T or select Start from the Simulation menu). When the simulation
is finished, double-click on the scope and hit its autoscale button. You should see the following output
which goes unstable and obviously does not meet our design requirements.

39
Implementing digital control in Simulink

40
In the DC Motor Position: Digital Controller Design section a digital controller was designed with the
following transfer function.

(2)

We can use this compensator to simulate the associated closed-loop digital control system in
Simulink. Starting from the Simulink model we used above for extraction, delete the Input and Output
ports and add the following:

 Two Zero Order Hold blocks from the Simulink/Discrete library

 Discrete Zero Pole block from the Simulink/Discrete library

 Sum block from Simulink/Math Operations library

Place one Zero Order Hold block on the input of the Motor_pos subsystem which is a continuous
model of the plant. This Zero Order Hold blocks converts a discrete-time signal to a stepwise-constant
continuous signal. The other Zero Order Hold block is placed on the output of the Motor_pos
subsystem and serves to take discrete samples of the output signal of the plant. Edit the Zero Order
Hold blocks such that the Sample time fields are set to "0.001".

Next edit the Discrete Zero Pole block to model the discrete controller transfer function described
above. Specifically, edit the Zeros field to "[0.95 0.80 0.80]", the Poles field to "[-0.98 0.6 1]", the Gain
field to "800", and the Sample time field to "0.001". Then label the block "Controller" and resize it to
view its entire contents.

Next, enter "|+-" to the List of signs field of the Sum block. The character "|" serves as a spacer
between input ports. Now we need to set up the simulation to observe the motor's position for a step
input.

 Attach a Step block to the positve input of the Sum block and attach a Scope block to the plant
output.

 Double-click the Step block and set the Step time to "0".

Finally, connect the blocks as shown below.

41
Before we can simulate the closed-loop system, we again need to set an appropriate simulation time.
Select Configuration Parameters... from the Simulation menu and enter "0.08" in the Stop time field.
We choose this stop time in order to observe the system long enough to see if it satisfies the 0.04
seconds settling time requirement. If you have started a new session of MATLAB or skipped the open-
loop response part of this example, then the physical parameters for the simulation must be set
again. Run the following commands at the MATLAB prompt if necessary.
J = 3.2284E-6;
b = 3.5077E-6;
K = 0.0274;
R = 4;
L = 2.75E-6;
Now the simulation can finally be run. Recall that this can be accomplished by pressing Ctrl-T or
selecting Start from the Simulation menu. When the simulation is finished, double-click on the scope
and hit its autoscale button. You should see the following output.

From the above figure, it appears that the overshoot is less than 16% and the settling time is less than
0.04 seconds as required. This is consistent with the analysis employed in designing the digital

42
controller. We have not, however, assessed the response of the system with a disturbance present. A
step disturbance can be added in a similar manner to the way that the step reference was added as
shown in the following figure. Sum the disturbance between the controller and plant following the
Zero Order Hold block since the disturbance signal is continuous.

Within the model, set the disturbance Step time to occur at "0.03" seconds. Then re-run the
simulation and observe the scope output as described above. The resulting position graph should
appear as shown below.

From inspection of the above, you can see that there is a slight bump beginning at 0.03 seconds due
to the disturbance, but the system is able to reject its effect and the steady-state error still goes to
zero as required.

43

You might also like