Matlab Simulink DC Motor
Matlab Simulink DC Motor
Matlab Simulink DC Motor
November 1999
2 Modeling a DC Motor
In this example we will learn how to develop a linear model for a DC motor, how to analyze the model under
MATLAB (poles and zeros, frequency response, time-domain response, etc.), how to design a controller, and
how to simulate the open-loop and closed-loop systems under SIMULINK.
+ T
+
J
V Vb = Kω
-
-
bω
The rotor and the shaft are assumed to be rigid. Consider the following values for the physical parameters:
T = Ki . (1)
The back electromotive force (emf), Vb , is related to the angular velocity by:
dθ
Vb = Kω = K . (2)
dt
1
From Figure 1 we can write the following equations based on the Newton’s law combined with the Kirchhoff’s
law:
d2 θ dθ
J 2
+b = Ki, (3)
dt dt
di dθ
L + Ri = V − K . (4)
dt dt
where s denotes the Laplace operator. From (6) we can express I(s):
V (s) − Ksθ(s)
I(s) = , (7)
R + Ls
and substitute it in (5) to obtain:
V (s) − Ksθ(s)
Js2 θ(s) + bsθ(s) = K . (8)
R + Ls
This equation for the DC motor is shown in the block diagram in Figure 2.
Armature Load
Voltage Torque Velocity Angle
V(s) + K T(s) 1 w( ) s 1 q( ) s
- Ls + R Js + b s
Vb(s)
K
Back emf
From equation (8), the transfer function from the input voltage, V (s), to the output angle, θ, directly follows:
θ(s) K
Ga (s) = = . (9)
V (s) s[(R + Ls)(Js + b) + K 2 ]
From the block diagram in Figure 2, it is easy to see that the transfer function from the input voltage, V (s), to
the angular velocity, ω, is:
ω(s) K
Gv (s) = = . (10)
V (s) (R + Ls)(Js + b) + K 2
3 MATLAB Representation
The above transfer function can be entered into Matlab by defining the numerator and denominator polyno-
mials, using the conventions of the MATLAB’s Control Toolbox. The coefficients of a polynomial in s are
2
entered in a descending order of the powers of s.
Furthermore, we will make use of the function conv(A,B), which computes the product (convolution) of the
polynomials A and B. Open the M-file motor.m. It already contains the definition of the motor constants:
J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
The transfer function (9) can be entered in M ATLAB in a number of different ways.
1. As Ga (s) can be expressed as Gv (s) · 1s , we can enter these two transfer functions separately and
combine them in series:
Here, we made use of the function feedback to create a feedback connection of two transfer functions
and the multiplication operator *, which is overloaded by the LTI class of the Control System Toolbox
such that is computes the product of two transfer functions.
2. Instead of using convolution, the first of the above three commands can be replaced by the product of
two transfer functions:
3. Another possibility (perhaps the most convenient one) is to define the transfer function in a symbolic
way. First introduce a system representing the Laplace operator s (differentiator) and then enter the
transfer function as an algebraic expression:
s = tf([1 0],1);
Gv = K/((L*s + R)*(J*s + b) + Kˆ2);
Ga = Gv/s;
It is convenient to label the inputs and outputs by the names of the physical variables they represent:
Gv.InputName = ’Voltage’;
Gv.OutputName = ’Velocity’;
Ga.InputName = ’Voltage’;
Ga.OutputName = ’Angle’;
Now by calling motor from the workspace, we have both the velocity (Gv) and the position (Ga) transfer
functions defined in the workspace.
3.1 Exercises
1. Convert Gv and Ga into their respective state-space (function ss) and zero-pole-gain (function zpk) rep-
resentations.
2. What are the poles and zeros of the system? Is the system stable? Why?
3. How can you use M ATLAB to find out whether the system is observable and controllable?
3
4 Analysis
The Control System Toolbox offers a variety of functions that allow us to examine the system’s characteristics.
G = [Gv; Ga];
Another way is to first convert Ga into its state-space representation and then add one extra output being equal
to the second state (the velocity):
G = ss(Ga);
set(G,’c’,[0 1 0; 0 0 1],’d’,[0;0],’OutputName’,{’Velocity’;’Angle’});
Note that this extension of the state-space model with an extra output has to be done in one set command in
order to keep the dimensions consistent.
Now, we can plot the step, impulse and frequency responses of the motor model:
figure(1); step(G);
figure(2); impulse(G);
figure(3); bode(G);
0.08
0.15
To: Velocity
To: Velocity
0.06
0.1
0.04
0.05
0.02
Amplitude
Amplitude
0 0
0.25 0.1
0.2 0.08
0.15 0.06
To: Angle
To: Angle
0.1 0.04
0.05 0.02
0 0
0 0.5 1 1.5 2 2.5 3 0 0.5 1 1.5 2 2.5 3
4.2 Exercise
1. Simulate and plot in M ATLAB the time response of the velocity and of the angle for an input signal
cos 2πt, where t goes from 0 to 5 seconds.
4
Bode Diagrams
From: Voltage
0
−50
−100
0
Phase (deg); Magnitude (dB)
To: Velocity
−100
−200
200
−200
0
To: Angle
−200
−400
−2 −1 0 1 2
10 10 10 10 10
Frequency (rad/sec)
5 Control Design
Let us design a PID feedback controller to control the velocity of the DC motor. Recall that the transfer
function of a PID controller is:
U (s) Ki Kd s2 + Kp s + Ki
C(s) = = Kp + + Kd s = , (11)
E(s) s s
where u is the controller output (in our case the voltage V ), e = uc − y is the controller input (the control
error), and Kp , Kd , Ki are the proportional, derivative and integral gains, respectively. A block diagram of
the closed-loop system is given in Figure 5.
Voltage Velocity
r + e V ω
PID DC Motor
−
Kp = 100;
Gc = feedback(Gv*Kp,1);
Gc.InputName = ’Desired velocity’;
5
Here Gc is the closed-loop transfer function. To see the step response of the closed-loop system, enter:
figure(4); step(Gc,0:0.01:2);
Step Response
From: Desired velocity
1.4
1.2
0.8
Amplitude
To: Velocity
0.6
0.4
0.2
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Time (sec.)
To eliminate the steady-state error, an integral action must be used. To reduce the overshoot, a derivative
action can be employed. In the following section, a complete PID controller is designed.
Kp = 1;
Ki = 0.8;
Kd = 0.3;
C = tf([Kd Kp Ki],[1 0]);
rlocus(Ga*C);
Kp = rlocfind(Ga*C);
Gc = feedback(Ga*C*Kp,1);
figure(9); step(Gc,0:0.01:5)
The rlocus and rlocfind functions are used to select the overall gain of the PID controller, such that the con-
troller is stable and has the desired location of the poles (within the defined ratio among the Kp , Ki and Kd
constants). If the design is not satisfactory, this ratio can be changed, of course. We should obtain a plot
similar to the one in Figure 7:
5.3 Exercise
1. Use the root locus and the Nyquist criterion to find out for what value of the gain Kp the proportional
controller for the angle Ga (s) becomes unstable.
6
Step Response
1.4
1.2
0.8
Amplitude
To: Angle
0.6
0.4
0.2
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
Time (sec.)
6 SIMULINK Model
The block diagram from Figure 2 can be directly implemented in SIMULINK, as shown in the figure Figure 8:
K theta
To Workspace
t angular
speed
Clock To Workspace
omega
To Workspace
Set the simulation parameters and run the simulation to see the step response. Compare with the response in
Figure 3. Save the file under a new name and remove the position integrator along with the ‘Graph’ and ‘To
Workspace’ blocks. Group the block describing the DC motor into a single block and add a PID controller
according to Figure 5. The corresponding SIMULINK diagram is given in Figure 9. Experiment with the
controller. Compare the responses with those obtained previously in M ATLAB.
7
Step Input
+
PID DC motor
−
Sum angular
speed
omega
t To Workspace
Clock To Workspace
Armature Load
1 + K(s) 1 1
1
Inport − Ls+R Js+b s
Outport
Figure 10: SIMULINK block diagram of the DC motor with ‘Inport’ and ‘Outport’ blocks.
7.1 Exercise
1. Convert the four matrices A, B C and D into a corresponding state space LTI object. Convert this one
into a transfer function and compare the result with the transfer function entered previously in M ATLAB.
A-D z
Airgap
Computer
D-A
Current i
Fgrav Fdist
The position and the motion of the object in the magnetic field are dependent on the forces that act on it.
These forces are: (i) the gravitational force, (ii) the electromagnetic force, and (iii) a disturbance force. The
8
dynamic equation of the system is derived from the basic law F = ma,
d2 1
2
y(t) = (Fgrav + Fdist − FR ), (12)
dt m
where Fgrav = mg, Fdist is an unknown disturbance, and the electromagnetic force is
1/u^2
1 u2 Kmag/m
y’’ y’
Current
1 1
1
2 1/m g s s Air gap
Disturbance
force
Check limits
Figure 12: Nonlinear Simulink model (bearing.mdl) of the magnetic levitation system.
8.3 Linearization
Let us linearize the nonlinear model around an operating point y0 = 2 mm. There are two possibilities to
linearize a nonlinear model:
• Analytically: by hand or using symbolic maths software such as Mathematica, Maple or the Symbolic
Toolbox of M ATLAB.
The second possibility will be explored here (you can do the first one as an exercise). Let us use the following
script (lin.m):
9
The trim function numerically searches for an equilibrium of the nonlinear system. A reasonable initial guess
(x0, u0 and y0) must be provided. The additional parameters of this function are indices of the inputs, states
and outputs that are not free to vary during the search. A typical example of such a variable is the state variable
corresponding to the operating point.
The linmod function extracts the matrices of a linear model obtained by numerical linearization at the equilib-
rium. Once this model is available, it can be used for analysis or control design.
8.4 Exercise
1. Choose another operating point and extract a linear model at that point. Compare to the model obtained
above in terms their gains, poles and zeros.
9 Concluding Remarks
The authors hope that this text has been useful and would appreciate receiving feedback from you. Let us
know if you have found any errors and omissions or if you have suggestions for improvements. Send them
preferable by e-mail to: R.Babuska@ITS.TUDelft.NL.
10