Example: Digital DC Motor Speed Control with PID Control

In this page, we will consider the digital control version of DC motor speed problem. A digital DC motor model can be obtained from conversion of the analog model, as we will describe. The controller for this example will be designed by a PID method.

From the Modeling a DC Motor Page, the open-loop transfer function for DC motor's speed was derived as:

Where:

*electrical resistance (R) = 1 ohm
*electrical inductance (L) = 0.5 H
*electromotive force constant (Ke=Kt) = 0.01 Nm/Amp
*moment of inertia of the rotor (J) = 0.01 kg*m^2/s^2
*damping ratio of the mechanical system (b) = 0.1 Nms
*input (V): Source Voltage
*output (theta dot): Rotating speed
*The rotor and shaft are assumed to be rigid

The design requirements for 1 rad/sec step input are

• Settling time: Less than 2 seconds
• Overshoot: Less than 5%
• Steady-state error: Less than 1%

## Continuous to Discrete Conversion

The first step in designing the discrete control system is to convert the continuous transfer function to a discrete transfer function. The MATLAB command c2d will do this for you. The c2d command requires the following three arguments: system, the sampling time (Ts) and method for conversion. In this example, we will use the zero-order hold ('zoh') method.

From the design requirement, let the sampling time, Ts equal 0.12 seconds, which is 1/10 the time constant of a system with a settling time of 2 seconds. Let's create a new m-file and enter the following commands:

```R = 1;
L = 0.5;
Kt = 0.01;
J = 0.01;
b = 0.1;

num = Kt;
den = [(J*L) (J*R)+(L*b) (R*b)+(Kt^2)];

motor = tf(num,den)

Ts = 0.12;
motor_d= c2d(motor,Ts,'zoh')
```

Running this m-file should return the following:

```
Transfer function:
0.009201 z + 0.005709
----------------------
z^2 - 1.088 z + 0.2369

Sampling time: 0.12
```

After you have done this, let's see what the closed-loop step response looks like with a proportional gain of 1. The step command will do this for you. Click here for more information. Add the following MATLAB code to the end of previous m-file and rerun it.

```
sys_cl = feedback(motor_d,1)
[Y,T] = step(sys_cl,12);
stairs(T,Y);
xlabel('Time (s)')
title('Stairstep Response:Original')
```
You should see the following plot:

## PID Controller

Recall that the continuous-time transfer function for a PID controller is:

There are several ways for mapping from the s-plane to z-plane. The most accurate one is . We cannot obtain PID transfer function in this way because the discrete-time transfer function would have more zeroes than poles, which is not realizable. Instead we are going to use the bilinear transformation shown below.

Thus we can derive the discrete PID controller with bilinear transformation mapping. For more detail on the derivation of the discrete PID controller, see the Discrete PID Controller Page. Equivalently, the c2d command in MATLAB will help you to convert the continuous-time PID compensator to discrete-time PID compensator by using the "tustin" method. The "tustin" method use the bilinear approximation to convert to discrete time. According to the PID Design Method for the DC Motor page, Kp = 100, Ki = 200 and Kd = 10 satisfy the design requirement. We will use all of these gains in this example. Now add the following MATLAB commands to your previous m-file and rerun it in MATLAB window.

```% Discrete PID controller with bilinear approximation
Kp = 100;
Ki = 200;
Kd = 10;

PID = tf([Kd Kp Ki],[1 0]);
contr = c2d(PID,Ts,'tustin');
```
Let's see if the performance of the closed-loop response with the PID compensator satisfies the design requirements. Now add the following code to the end of your m-file and rerun it. You should get the following closed-loop stairstep response.

```sys_cl = feedback(contr*motor_d,1);

[x2,T] = step(sys_cl,12);
stairs(T,x2)
xlabel('Time (seconds)')
title('Stairstep Response:with PID controller')
```

As you can see from the above plot, the closed-loop response of the system is unstable. Therefore there must be something wrong with compensated system. So we should take a look at root locus of the compensated system. Let's add the following MATLAB command into the end of your m-file and rerun it.

```rlocus(contr*motor_d)
title('Root Locus of Compensated System')
```

From this root-locus plot, we see that the denominator of the PID controller has a pole at -1 in the z-plane. We know that if a pole of a system is outside the unit circle, the system will be unstable. This compensated system will always be unstable for any positive gain because there are an even number of poles and zeroes to the right of the pole at -1. Therefore that pole will always move to the left and outside the unit circle. The pole at -1 comes from the compensator, and we can change its location by changing the compensator design. We choose it to cancel the zero at -0.62. This will make the system stable for at least some gains. Furthermore we can choose an appropriate gain from the root locus plot to satisfy the design requirements using rlocfind. Enter the following MATLAB code to your m-file.

```contr.den = conv([1 -1],[1/.62 1]);

rlocus(contr*motor_d)
title('Root Locus of Compensated System');
[K,poles] = rlocfind(contr*motor_d)
sys_cl = feedback(K*contr*motor_d,1);

[x3,T] = step(sys_cl,12);
stairs(T,x3)
xlabel('Time (seconds)')
title('Stairstep Response:with PID controller')
```
Note how the first line of code above allows you to change the denominator of the system transfer function directly, without having to redefine the system. The new closed loop poles will have a pole at -0.625 instead of -1, which almost cancels the zero of uncompensated system. In the MATLAB window, you should see the command asking you to select the point on the root-locus plot. You should click on the plot as the following:

Then MATLAB will return the appropriate gain and the corresponding compensated poles, and it will plot the closed-loop compensated response as follows.

The plot shows that the settling time is less than 2 seconds and the percent overshoot is around 3%. In addition, the steady state error is zero. Also, the gain, K, from root locus is 0.2425 which is reasonable. Therefore this response satisfies all of the design requirements.

Digital Control Examples
Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Motor Speed Examples
Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

Tutorials
MATLAB Basics | MATLAB Modeling | PID Control | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples