Example: A State-Space Controller for DC Motor Speed

From the main problem, the dynamic equations in state-space form are the following:

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 reference added to the system, the design criteria are:

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

Create a new m-file and type in the following commands (refer to the main problem for the details of getting these commands).

```J = 0.01;
b = 0.1;
K = 0.01;
R = 1;
L = 0.5;
A = [-b/J   K/J
-K/L   -R/L];
B = [0
1/L];
C = [1   0];
D = 0;
sys = ss(A,B,C,D);
```

## Designing the full-state feedback controller

Since both of the state variab in our problem are very easy to measure (simply add an ammeter for current and a tachometer for the speed), we can design a full-state feedback controller for the system without worrying about having to add an observer. The schematic for a full-state feedback system is:

Recall that the characteristic polynomial for this closed-loop system is the determinant of (sI-(A-BK)) where s is the Laplace variable. Since the matrices A and B*K are both 2x2 matrices, there should be 2 poles for the system. By designing a full-state feedback controller, we can move these two poles anywhere we want them. We shall first try to place them at -5 + i and -5-i (note that this corresponds to a zeta = 0.98 which gives 0.1% overshoot and a sigma = 5 which leads to a 1 sec settling time). Once we come up with the poles we want, MATLAB will find the controller matrix, K, for us. Simply add the following code to the end of your m-file :
```p1 = -5 + i;
p2 = -5 - i;
K = place(A,B,[p1 p2]);
```
Now look at the schematic above again. We see that after adding the K matrix into the system, the state-space equations become:

We can see the closed-loop response by simply adding the following line to the end of your m-file:
```t = 0:0.01:3;
sys_cl = ss(A-B*K,B,C,D);
step(sys_cl,t)```
Run your m-file in the command window, You should see the following plot:

From this plot we see that the steady-state error is too large. In contrast to the other design methods, where we feed back the output and compare it to the reference to compute an error, here we are feeding back both states. We need to compute what the steady-state value of the states should be, multiply that by the chosen gain K, and use this new value as our reference for computing the input. This can be done in one step by adding a constant gain Nbar after the reference:

We can find this Nbar factor by using the MATLAB command rscale:
```Nbar=rscale(sys,K)
```
Note that the function rscale is not a standard function in MATLAB. You will have to copy it before you use it. Click here for more information. Now we can plot the step response by adding the following line of code to your m-file:
```t = 0:0.01:10;
step(sys*Nbar,t)
title('Step Response with K Controller and Nbar')
```

This time, the steady-state error is much less than 1%, and all the other design criteria have been met as well.

State Space 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