Example: PID Design Method for DC Motor Speed Control

From the main problem, the dynamic equations and the open-loop transfer function of the DC Motor are:

and the system schematic looks like:

For the original problem setup and the derivation of the above equations, please refer to the Modeling a DC Motor page.

With a 1 rad/sec step input, the design criteria are:

• Settling time less than 2 seconds
• Overshoot less than 5%
• Steady-stage error less than 1%

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 the Modeling page for the details of getting these commands).

```J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
num=K;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];
motor=tf(num,den);
```
Recall that the transfer function for a PID controller is:

Proportional control

Let's first try using a proportional controller with a gain of 100. To determine the closed-loop transfer function, we use the feedback command. Add the following code to the end of your m-file:

```Kp=100;
contr=Kp;
sys_cl=feedback(contr*motor,1);
```
Now let's see how the step response looks. Add the following to the end of your m-file, and run it in the command window:
```t=0:0.01:5;
step(sys_cl,t)
title('Step response with Proportional Control')
```
You should get the following plot:

PID control

From the plot above we see that both the steady-state error and the overshoot are too large. Recall from the PID tutorial page that adding an integral term will eliminate the steady-state error and a derivative term will reduce the overshoot. Let's try a PID controller with small Ki and Kd. Change your m-file so it looks like the following. Running this new m-file gives you the following plot.

```J=0.01;
b=0.1;
K=0.01;
R=1;
L=0.5;
num=K;
den=[(J*L) ((J*R)+(L*b)) ((b*R)+K^2)];
motor=tf(num,den);

Kp=100;
Ki=1;
Kd=1;
contr=tf([Kd Kp Ki],[1 0]);
sys_cl=feedback(contr*motor,1);
step(sys_cl)
title('PID Control with small Ki and Kd')
```

Tuning the gains

Now the settling time is too long. Let's increase Ki to reduce the settling time. Go back to your m-file and change Ki to 200. Rerun the file and you should get a plot like this:

Now we see that the response is much faster than before, but the large Ki has worsened the transient response (big overshoot). Let's increase Kd to reduce the overshoot. Go back to the m-file and change Kd to 10. Rerun it and you should get this plot:

So now we know that if we use a PID controller with

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

all of our design requirements will be satisfied.

PID 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