# C. Control Design (Week 2)

### Objective

The objective of this experiment is to design a position control system for the motor to meet a given set of specifications in frequency-domain.

### Experiment Notes

Now that we have identified the time constant and the DC gain for the system in the modeling part of this experiment, the next step is to design a position control system in order to convert the given DC motor into a position servo. A classical PID controller will be used to control the position of the motor subject to a given set of specifications, for example, bandwidth, steady-state error requirements, etc.

Recall that the transfer function of the motor from $$V\_m$$ to $$\Omega\_l$$ in [Modelling](https://gtae.gitbook.io/ae4610/lab-1-rotary-servo-base/b.-modeling-week-1) is approximated as

$$
\displaystyle\frac{\Omega\_l(s)}{V\_m(s)} = \frac{A}{\tau s+1} \qquad \qquad \qquad \tag{2.1}
$$

let  $$A = K$$, DC gain of the motor.

#### Proportional (P) Control

Since $$\dot{\theta} = \Omega$$, where $$\theta$$ is the angular position of the motor, the transfer function from the applied voltage to the motor angular position is

$$
G(s) = \displaystyle\frac{\theta (s)}{V\_m (s)}  = \frac{A}{s(\tau s+1)} \qquad \qquad \qquad \tag{3.1}
$$

The block diagram of a DC motor with a proportional controller is shown in Fig.1. In this case, the controller transfer function ($$H$$) is a simple gain $$K\_P$$. From Fig. 1, the closed-loop transfer function from the commanded angular position $$\theta\_c$$ to the actual angular position $$\theta$$ is given by

$$
\frac{\theta(s)}{\theta\_c(s)} = \frac{AK\_p}{\tau s^2 + s + AK\_p} = \frac{\cfrac{AK\_p}{\tau}}{s^2 + \cfrac{1}{\tau}s + \cfrac{AK\_p}{\tau}}
$$

Comparing the above transfer function to the standard form for a second-order system, i.e.,

$$
\frac{\theta(s)}{\theta\_c(s)} = \frac{\omega\_n^2}{s^2 + 2\zeta \omega\_n s + \omega\_n^2}
$$

we notice that the proportional gain $$K\_p$$ affects the natural frequency (and hence the bandwidth) of the closed-loop system.

![Figure 1: Closed-loop block diagram for the position control of a DC motor using a proportional controller](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-MMvGupWAxNzgDhMwOQL%2F-MMvTvYHEzkylBj8s8qe%2Fimage.png?alt=media\&token=78a6bced-c672-4527-a864-c23b3a2180ec)

#### Proportional-plus-Derivative (PD) Control

The block diagram of a DC motor with an inner loop angular rate (i.e., derivative) feedback and an outer loop angular position error (proportional) feedback is shown in Fig. 2. From Fig. 2, the closed-loop transfer function from the commanded angular position $$\theta\_c$$ to the actual angular position $$\theta$$ is given by

$$
\frac{\theta(s)}{\theta\_c(s)} = \frac{AK\_p}{\tau\_m s^2 + (1 + AK\_d)s + AK\_p} = \frac{\cfrac{AK\_p}{\tau\_m}}{s^2 + \cfrac{(1 + AK\_d)}{\tau\_m}s + \cfrac{AK\_p}{\tau\_m}}
$$

![Figure 2: Closed-loop block diagram for the position control of a DC motor using a proportional-plus-derivative (PD) controller.](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-Mi9ATb3iyK6AhyNUcEn%2F-Mi9SmVQfmKPwtzxjFzE%2Ftemp.PNG?alt=media\&token=60fe4bf1-717e-4ec1-bd6e-df6c93c128df)

Comparing the above transfer function with the standard form for a second-order system, we notice that the derivative gain $$K\_d$$ affects the damping while the proportional gain $$K\_p$$ affects the natural frequency of the closed-loop system.

#### Proportional-plus-Integral-plus-Derivative (PID) Control

The block diagram of a DC motor with an inner loop angular rate (i.e., derivative) feedback and an outer loop angular position error plus integral of angular position error (i.e., proportional plus integral) feedback is shown in Fig. 3.&#x20;

![Figure 3: Closed-loop block diagram for the position control of the DC motor using a proportional-plus-Integral-plus-derivative (PID) controller](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-MMvGupWAxNzgDhMwOQL%2F-MMvUsJf8YvgOm66BSOB%2Fimage.png?alt=media\&token=83c1e654-c36e-4b97-ac13-c550ade202f4)

From Fig. 3, the open-loop transfer function $$GH$$ becomes

$$
GH = \frac{(K\_ps+K\_i)}{s} \frac{A}{(\tau\_ms+1+AK\_d)}\frac{1}{s}
$$

indicating that with integral feedback, the type of the system increases from 1 to 2 and hence, results in zero steady-state error to both ramp and step command inputs.

#### Controller Gain Limits

In control design, a factor to be considered is saturation. This is a nonlinear element and is represented by a saturation block as shown in Figure 4. In a system like the Rotary Servo Base Unit, the computer calculates a numeric control voltage value. This value is then converted into a voltage, $$V\_{\text{dac}}(t)$$, by the digital-to-analog converter of the data-acquisition device in the computer. The voltage is then amplified by a power amplifier by a factor of $$K\_a$$. If the amplified voltage, $$V\_{\text{amp}}(t)$$, is greater than the maximum output voltage of the amplifier or the input voltage limits of the motor (whichever is smaller), then it is saturated (limited) at $$V\_{\text{max}}$$. Therefore, the input voltage $$V\_m(t)$$ is the effective voltage being applied to the Rotary Servo Base Unit motor.

The limitations of the actuator must be taken into account when designing a controller. For instance, the voltage entering the Rotary Servo Base Unit motor should never exceed  $$V\_{\text{max}} = 10.0 V \qquad \qquad \qquad \tag{3.2}$$

<figure><img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2FMBne1Ul8u3wFQrWldjEK%2Fgain%20limit.png?alt=media&#x26;token=84ff15d9-86ba-4d8f-bb05-cd662b8ae198" alt=""><figcaption><p>Figure 4. Actuator saturation.</p></figcaption></figure>

#### Sampling Time

The most important difference between analog and digital control is that digital systems operate using a clock. The timing of this clock and the number of operations necessary to implement a controller place a limit on how frequently the controller can access sensors, make calculations, and modify the control inputs. (In addition, other elements of the control system themselves may introduce an additional time delay. For this reason, the sampling time $$T\_s$$ of the digital system is an important parameter.) Digital controllers, in general, will have a maximum $$T\_s$$. Increasing the $$T\_s$$ further will make the closed-loop system unstable.

### Design Specifications

The closed-loop DC motor system must meet the following specifications.

1. **Bandwidth frequency of at least 25 rad/s** (Bandwidth is an important measure of the frequency range over which the system output follows well the command signal. **It is defined as the frequency at which the magnitude ratio is -3 dB in the closed-loop system frequency response plot**. Sinusoidal inputs with frequencies less than the bandwidth frequency are tracked ‘reasonably well’ by the system.)
2. **Phase margin of at least 70 deg**. (Gain and phase margins are stability margins to accommodate model variations. A flight control system actuator is typically subject to varying load, resulting in model variations. Hence, it is important to design a controller with sufficient stability margins to accommodate such variations and other effects such as wear with usage.)
3. **Zero steady-state error to both step and ramp commands.**

{% hint style="warning" %}

### **Guidelines for saving plots**

1. Save root locus open-loop Bode, closed-loop bode and step response plots as **separate figures**.
2. In open-loop or closed-loop Bode plots, you should show **Bandwidth frequency** and **Phase margin values**.
3. In the step response plot, you should show the **steady-state value**.
4. Only for **controlSystemDesigner** or **rltool** only, you may use the Snipping tool and save the figures in a Word document.
5. For **other Simulink-generated data**, you need to use the **`plot` MATLAB command**.
   {% endhint %}

**Results from each step below must be checked with TAs before proceeding to the next:**

* [ ] Step 5 - Plots and Kp value
* [ ] Step 6 - Plots and Kp and Kd values
* [ ] Step 7-9 - Plots and Kp, Kd and Ki values
* [ ] Step 10 - Closed loop TF
* [ ] Step 11 - Closed loop poles
* [ ] Step 12 - Simulink model
* [ ] Step 13 - One response plot with PD and PID, One error plot with PD and PID.

### Procedure

1. Using the model, i.e., K and tau values, you have obtained from the Modeling - Model Validation, input the transfer function $$G(s)$$ (Equation 3.1) into MATLAB. Use K and tau from the **model validation values**.

2. First consider a proportional controller, i.e., `C = Kp`. Input `C = 1` into MATLAB.

3. Run `controlSystemDesigner(G,C)` (you can alternatively run `rltool(G,C)` for versions R2020b and older).&#x20;
   1. Select ‘New Plot -> New Step’  to launch the step response plot (if it is not automatically launched). In the time response plot, only the **‘IOTransfer\_r2y' (closed-loop)** should be visible. Always do this in all the labs.&#x20;
   2. Select ‘New Plot -> New Bode’ and choose the appropriate option **‘IOTransfer\_r2y'** to launch the **closed-loop Bode plot**. In the closed-loop Bode response window, select the magnitude plot only and readjust the magnitude plot limits to -3.5 dB and -2.5 dB under properties and limits. The bandwidth frequency (defined above) can be read from the **closed-loop** **Bode plot**. It may be useful to add a grid to the bode plot.\
      \
      **Note**: If the **closed-loop Bode plot** is **not visible after adjusting magnitude plot limits**, it indicates that the design specification is not met, which would require gain tuning. This will be performed in Step 5.\\

4. If **Open-loop Bode Editor** is not automatically launched. In **Control System Designer** or **rltool**, select ‘Open-loop Bode’ under ‘Tuning Methods -> Bode Editor’. The phase margin (PM) of the system can be read off the open-loop bode plot. \
   \
   **Note**: The LoopTransfer\_C Bode is the open-loop Bode.

5. Change the controller gain by double-clicking ‘C’ under ‘Controllers and Fixed Blocks’ or by using your cursor on the root locus and check if all the design specifications can be met using a proportional controller, which would be optimal from a design standpoint. **If not,** at least select a value of $$K\_p$$ such that the bandwidth is more than **25 rad/sec**. **Save** the root locus, open and closed-loop Bode, time response plot and P gain.&#x20;

6. Consider a proportional and derivative controller. In the compensator editor window under ‘C’, add a zero around -60. Using the cursor or manually input values, adjust the zero location of the controller first and see if the specifications can be met. If adjusting the zero location cannot achieve the specification, readjust the proportional gain (if necessary) such that, at least, the bandwidth and phase margin specifications are met.  (Note that with a PD controller for this system, the open-loop transfer function $$GC$$ is still of Type 1. Hence, the zero steady-state error specification for ramp input cannot be met with a PD controller.) **Save** the root locus, open and closed-loop Bode, time response plot and PD gains.\
   \
   The compensator transfer function in **Control System Designer** or **rltool** in this step is of the form shown below. Use it to determine the gains $$K\_p$$ and $$K\_d$$. Save these gains.\
   \
   &#x20;                                                      <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-MiEvxIWsYio8Pb6Oja_%2F-MiEwDpX5K7dT1c690xD%2Ftemp.PNG?alt=media&#x26;token=79c373f7-460e-4799-9aa4-04712d68ef6f" alt="" data-size="original">                                                 \\

   **Save** a screenshot of the control structure that you now have in **Control System Designer** or **rltool** (go to Edit Architecture tab). This screenshot will be required to answer a question for the report (refer to [Questions for Report](#questions-for-report)).

7. Consider a PID controller. Using $$K\_p$$ and $$K\_d$$ values you have obtained from the previous steps, edit the compensator (i.e. add poles and zeros to match the format below) to include integral feedback with the integral gain set to a small value (i.e., $$K\_i$$ is 1% of $$K\_p$$).  The controller transfer function for this step is\
   &#x20;                                                  <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-Mj4RBa9K-aTt7GVsecW%2F-Mj4Ra4fOxqjj5upbcRc%2Ftemp.JPG?alt=media&#x26;token=cc8a280b-81fd-4e46-ba01-f6e6a6667086" alt="" data-size="original"> \
   \
   **Hint:** We have all PID values, plug into above controller transfer function and solve for A and B in $$K\_i\frac{(1+As)(1+Bs)}{s}$$ form. This should give you value of $$AB = \frac{K\_d}{K\_i}, A+B = \frac{K\_p}{K\_i}$$. To put it in the compensator the zero locations are -1/A and -1/B.<br>

8. Readjust the proportional and derivative gains (**if necessary**, by modifying the zeros, not the overall gain, $$K\_i$$) such that bandwidth and phase margin specs are met.&#x9;

9. **Save** the root locus plot, open and closed-loop system Bode plots, and time response plot as graphs. Also, save your gains (if you readjusted any values in Step 8, you must convert from the **Control System Designer** or **rltool** compensator format, back to our conventional gain representation – $$K\_p$$ , $$K\_d$$ , $$K\_i$$).

10. Using the controller you have designed, **obtain** the closed-loop transfer function and **save** your result. (Do this in MATLAB using the transfer function variables of $$G$$ and $$C$$ , or export the closed-loop transfer function from **Control System Designer** or **rltool(T\_r2y)**, using the ‘Export’.) (May be listed as ‘IOTransfer\_r2y’). In MATLAB, change the exported state space into a transfer function using `tf(IOTransfer_r2y)`

11. **Obtain** poles of the closed-loop system. **Compute** the natural frequency and damping of the dominant poles of the closed-loop system. You can do all this from your exported transfer function using the function `damp()`. **Save** your results.

12. Construct a SIMULINK diagram using Fig. 3 as a guide. Use a transfer function block for the plant (use K and tau from the model validation). Add saturation of max 10V and min -10V block before the Plant transfer function. Set the solver type to Fixed-step with size of 0.002 (located under “Modeling” -> “Model Settings” -> “Solver Details”). Include the controller gains from your design. **Do not** use the transfer function block or the inbuilt PID controller block for the controller gains.  **Save** your Simulink model for further use in Part C of the experiment. **Save** the Simulink block diagram as a .jpg or .png imag&#x65;**.**

13. Run the responses to a unit ramp input first with a PD controller (by setting `Ki = 0`) and then with a PID controller (`Ki = 1% Kp`). Run the simulation for about 30 sec.&#x20;    Make comparison plots of ramp responses with PD and PID controllers (on the same plot) to show any differences between the two responses, especially in steady-state response (i.e., for t>>0, also you may zoom in to see the difference clearly). The difference can be more easily spotted by plotting the error variable (i.e. error between the commanded position and the actual position) for each controller. **Save** the comparison plot and error plot.

{% hint style="warning" %}
You must have your work checked out by one of the TAs before leaving the lab.
{% endhint %}

## Results for Report

**Note:**

* The **plots for controller design**, i.e., root locus, open-loop Bode, closed-loop Bode, and step response, **must be shown as separate figures**.
* In **open-loop** or **closed-loop Bode plots**, you should show **Bandwidth frequency and Phase margin values**.
* In the **step response plot**, you should show the **steady-state value**. This can be done by right-clicking on the plot -> "Characteristics" -> "Steady-state".
* **Only** for **controlSystemDesigner** or **rltool plots**, you can include the plots as obtained using the Snipping tool.
* For **other Simulink-generated data**, you need to use the **`plot` MATLAB command**.

### (A) From Step 5

1. Root locus plot
2. Open-loop Bode plot
3. Closed-loop Bode plot
4. Time response plot
5. P (proportional) gain

### (B) From Step 6

1. Root locus plot
2. Open-loop Bode plot
3. Closed-loop Bode plot
4. Time response plot
5. PD (proportional and derivative) gains

### (C) From Step 9

1. Root locus plot
2. Open-loop Bode plot
3. Closed-loop Bode plot
4. Time response plot
5. PID (proportional, derivative and integral) gain values (*mention if the gains are the same values or readjusted values from Step 8*)

### (D) From Step 10

1. Closed-loop transfer function

### (E) From Step 11

1. Poles of the closed-loop system
2. Natural frequency of the closed-loop system dominant poles
3. Damping of the closed-loop system dominant poles

### (F) From Step 12

1. PID controller SIMULINK block diagram (image)

### (G) From Step 13

1. Plots comparing the ramp responses of PD and PID controllers (on the same figure)
2. Plots comparing the error variable of PD and PID controllers (on the same figure)

## Questions for Report

1. **What is the difference** between Fig. 2 and the control structure screenshot that you saved in Step 6?\
   \
   **Hint:** Focus on where $$K\_d$$ is located in the control loop and what it is multiplied by.
2. **Why is this difference** between the two control structures **acceptable** when dealing with a step input?\
   \
   **Use and modify the Simulink model that you built in Step 12** for the two control structure cases (i.e.,$$K\_d$$ is multiplied by the derivative of error or speed directly). You may create two separate Simulink files or build two models within the same Simulink file. **Run each Simulink with a quarter-step input for 5 seconds.** Use the PD gains found in Step 6 (Result B.5) and set `Ki = 0`. **Create plots below to help you explain the question asked above**:&#x20;
   1. Step responses (two plots on the same figure)
   2. Derivative of the error signal and speed value in one figure for the step input from both models
   3. Explain how the forward-feed and feedback derivative gain influence system behavior as observed in the input voltage plot.
