Example: DC Motor Position Modeling in Simulink

Physical setup
Building the model
Open-loop response
Extracting a digital model
Implementing digital control
Closed-loop response

## 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 transitional motion. The electric circuit of the armature and the free body diagram of the rotor are shown in the following figure:

For this example, we will assume the following values for the physical parameters. These values were derived by experiment from an actual motor in Carnegie Mellon's undergraduate controls lab.

* moment of inertia of the rotor (J) = 3.2284E-6 kg.m^2/s^2
* damping ratio of the mechanical system (b) = 3.5077E-6 Nms
* electromotive force constant (K=Ke=Kt) = 0.0274 Nm/Amp
* electric resistance (R) = 4 ohm
* electric inductance (L) = 2.75E-6 H
* input (V): Source Voltage
* output (theta): position of shaft
* The rotor and shaft are assumed to be rigid

The motor torque, T, is related to the armature current, i, by a constant factor Kt. The back emf, e, is related to the rotational velocity by the following equations:

In SI units (which we will use), Kt (armature constant) is equal to Ke (motor constant).

## Building the Model

This system will be modeled by summing the torques acting on the rotor inertia and integrating the acceleration to give the velocity, and integrating velocity to get position. Also, Kirchoff's laws will be applied to the armature circuit. Open Simulink and open a new model window. First, we will model the integrals of the rotational acceleration and of the rate of change of armature current.

• Insert an Integrator block (from the Linear block 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.
• Label the input line "d/dt(i)" and the output line "i".

Next, we will start to model both Newton's law and Kirchoff's law. These laws applied to the motor system give the following equations:

The angular acceleration is equal to 1/J multiplied by the sum of two terms (one pos., one neg.). Similarly, the derivative of current is equal to 1/L multiplied by the sum of three terms (one pos., two neg.).

• Insert two Gain blocks, (from the Linear block library) one attached to each of the leftmost 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 it's label to Inductance.
• Insert two Sum blocks (from the Linear block library), one attached by a line to each of the Gain blocks.
• Edit the signs of the Sum block corresponding to rotation to "+-" since one term is positive and one is negative.
• Edit the signs of the other Sum block to "-+-" to represent the signs of the terms in Kirchoff's equation.

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, select it by single-clicking on it, and select Flip from the Format menu (or type Ctrl-F) to flip it left-to-right.
• Set the gain value to "b" and rename this block to "damping".
• Tap a line (hold Ctrl while drawing) 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 Sum block.
Next, we will add in the torque from the armature.
• Insert a gain block attached to the positive input of the rotational Sum 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.

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 coil 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 to "Resistance".
• Tap a line (hold Ctrl while drawing) 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 Sum 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 Sum block with a line.
• Edit it's value to "K" to represent the motor 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.

The third voltage term in the Kirchoff equation is the control input, V. We will apply a step input.

• Insert a Step block (from the Sources block library) and connect it with a line to the positive input of the current Sum block.
• To view the output speed, insert a Scope (from the Sinks block library) connected to the output of the second rotational integrator (theta).
• To provide a appropriate unit step input at t=0, double-click the Step block and set the Step Time to "0".

## Open-loop response

To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "0.2" in the Stop Time field. 0.2 seconds is long enough to view the open-loop response. Also in the Parameters dialog box, it is helpful to change the Solver Options method. Click on the field which currently contains "ode45 (Dormand-Prince)". Select the option "ode15s (stiff/NDF)". Since the time scales in this example are very small, this stiff system integration method is much more efficient than the default integration method.

The physical parameters must now be set. Run the following commands at the MATLAB prompt:
```
J=3.2284E-6;
b=3.5077E-6;
K=0.0274;
R=4;
L=2.75E-6;
```
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.

## Extracting a Digital Model into MATLAB

A linear digital model of this continuous-time system (in state space or transfer function form) can be extracted from a Simulink model into MATLAB. Conversion to a discrete-time (digital) system is done with Zero-Order Hold blocks on both the inputs and outputs of the system, which act as both D/A (sample-and-hold) and A/D devices. The extraction of a model makes use of In and Out Connection blocks and the MATLAB function dlinmod. We will start with the model which we just build. You can download a complete version here. We will first group all of the system components (except for the Step and Scope which aren't really part of the system) into a Subsystem block.

• Drag the mouse from one corner of your model window to the other to highlight all of the components. If possible, avoid highlighting the Step and Scope blocks, but if you do, hold the shift key and single click on either of the Step and Scope blocks to un-highlight them. Your model window should appear as shown below.

• Select Create Subsystem on the Edit menu (or hit Ctrl-G). This will group all of the selected blocks into a single block. Your window should appear as shown below.

• Change the label of the Subsystem block to "Continuous Plant". If you like, you can resize this block so the words "In1" and "Out1" inside of it don't overlap. To resize a block, highlight it by single clicking it and drag the corners to the desired size.
• Replace the Step Block and Scope Block with Zero Order Hold blocks (from the Discrete block library). One Zero Order Hold block is used to convert a discrete-time signal to a stepwise-constant continuous signal. The other Zero Order Hold block is used to take discrete samples of the output from the plant.
• Edit the Zero Order Hold blocks and set the Sample Time fields to 0.001 (this is fast compared to the desired step response in the MATLAB tutorial.)
• Connect an In Connection Block to the input of the first Zero Order Hold block, and an Out Connection Block to the output of the second Zero Order Hold block. (these blocks can be found in the Connections block library). This defines the input and output of the system for the extraction process.
• Drag each block in your model so that they are arranged in a line.

Save your file as "motorpos.mdl" (select Save As from the File menu). MATLAB will extract the linear model from the saved model file, not from the open model window. At the MATLAB prompt, enter the following commands:

```[A,B,C,D]=dlinmod('motorposmodel',.001)
[num,den]=ss2tf(A,B,C,D)
```
The extra parameter in dlinmod provides the sample time for the discrete conversion. You should see the following output, providing discrete time models of the system both in state-space and transfer function form.
```A =

1.0000    0.0000    0.0010
0    0.0000   -0.0065
0    0.0055    0.9425

B =

0.0010
0.2359
2.0589

C =

1     0     0

D =

0

num =

0    0.0010    0.0010    0.0000

den =

1.0000   -1.9425    0.9425         0
```
As noticed in above results, both numerator and denominator of the discrete transfer function have one extra root at z = 0. These cancel each other, and the discrete-time transfer function to the motor position output from the voltage input is:

To verify the model extraction, we will generate an open-loop step response of the extracted transfer function in MATLAB. Enter the following commands in MATLAB.

```[x1] = dstep(num,den,201);
t=0:0.001:0.2;
stairs(t,x1)
```
You should see the following plot which is equivalent to the Scope's output.

## Implementing Digital Control

In the motor speed control digital example a digital controller was designed with the following transfer function.

• Bring up the model window containing the digital system which was just extracted into MATLAB. (You can download our version here)
• Delete the "In" and "Out" blocks.

We will first feed back the plant output.

• Insert a Sum block and assign "+-" to it's inputs.
• Tap a line of the output line of the output Zero Order Hold line and draw it to the negative input of the Sum block.

The output of the Sum block will provide the error signal. We will feed this into the digital controller.

• Insert a Discrete Transfer Function Block (from the Discrete block library) after the summer and connect them with a line.
• Edit this block and change the Numerator field to "450*conv([1 -.85],[1 -.85])", the Denominator field to "conv([1 .98],[1 -.7])", and the Sample Time to ".001".
• Label this block "Controller" and resize it to view the entire contents.

Finally, we will apply a step input and view the output on a scope.

• Attach a Step block to the free input of the feedback Sum block and attach a Scope block to the plant output.
• Double-click the Step block and set the Step Time to "0".

## Closed-loop response

To simulate this system, first, an appropriate simulation time must be set. Select Parameters from the Simulation menu and enter "0.05" in the Stop Time field. The design requirements included a settling time of less than 0.04 sec, so we simulate for 0.05 sec to view the output. The physical parameters must now be set. Run the following commands at the MATLAB prompt:
```
J=3.2284E-6;
b=3.5077E-6;
K=0.0274;
R=4;
L=2.75E-6;
```
Run the simulation (Ctrl-t or Start on the Simulation menu). When the simulation is finished, double-click on the scope and hit its autoscale button. You should see the following output.