> For the complete documentation index, see [llms.txt](https://gtae.gitbook.io/ae4610/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gtae.gitbook.io/ae4610/lab-1-rotary-servo-base/dc-servomotor-1.md).

# C. Controller Implementation & Evaluation (Week 3)

## Objectives

In this experiment, the controller will be evaluated using both the Quanser Digital Twin and the physical Servo Base hardware. The same reference inputs and controller gains should be used for both systems whenever possible.

For each Step Response and Ramp Response experiment:

1. Run the experiment using the Digital Twin and save the response data.
2. Run the same experiment using the physical hardware and save the response data.
3. Compare the Digital Twin and hardware responses.

## Equipment Required

The following is a list of the required equipment to perform this experiment:

* [ ] Q8-USB or Q2-USB interface board

* [ ] MATLAB and SIMULINK software

* [ ] Servo Base<br>

## Controller Implementation

### 1. Digital Twin Experiments

1. Run `QLabs.launch` in the command window.&#x20;
2. Open the Rotary Servo Base Workspace.

{% file src="/files/UpYW7LM8XSGcAOlw2AWU" %}

#### A. Step Response Experiment

1. Download and extract files from Digital Twin.zip. Open `setup_servo_pos_cntrl.m` file. Do not change anything.
2. ***PD Controller Test***: Run the `setup_servo_pos_cntrl.m`  file. Enter the values of $$K\_p$$ and $$K\_d$$ as the values obtained in Step 6 of Control Design, and set the value for $$K\_i$$ to 0 in the command window.
3. Now open `q_servo_pos_cntrl.slx` file, check that the sampling time is set to 0.002 s (located under “Modeling” -> “Model Settings” -> “Solver Details”).
4. Double-click on the Signal Generator and ensure that the following parameters are set to generate a square wave (i.e. step reference):
   * Signal Type = *square*
   * Amplitude = 1
   * Frequency = 0.4 Hz
5. In the Simulink diagram, set the Amplitude gain block to 0.2 and the Constant offset gain block to 0.2. Note that these gain blocks outside the Signal Generator block are introduced to generate the square wave with amplitude limits of 0 V and 0.4 V.
6. To build the model, go to **QUARC** tab and select "quarc\_win64.tlc" under the QUARC Targets. Then press **Monitor and Tune** <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2F5kUOzUmpIz5Mob4x7xd1%2Fimage.png?alt=media&amp;token=60cb4298-b97c-4538-8240-2d44c2d721b0" alt="" data-size="line">. Run the experiment for 5 seconds.
7. Keep the Quanser Interactive Labs window next to the Simulink window to observe the experiment.
8. Open the position scope.
9. After each simulation run, each scope automatically saves its response to a variable in the MATLAB workspace. That is, the *`Position (rad)`* scope saves its response to the variable called `data_pos` and the *`Vm (V)`* scope saves its data to the `data_vm` variable. The `data_pos` variable has the following structure: `data_pos(:,1)` is the time vector, `data_pos(:,2)` is the setpoint, and `data_pos(:,3)` is the simulated angle. For the `data_vm` variable, `data_vm(:,1)` is the time and `data_vm(:,2)` is the simulated input voltage.
10. **Save** `Vm` and `data_pos` data.
11. Tune the values of $$K\_p$$ and $$K\_d$$ such that **PO < 5%** and **PeakTime < 0.4s**.&#x20;
12. Use `[m,idx] = max(data_pos(:,3))`  to get the peak value and time for the response.
13. Determine the PO by `(((m-0.4)*100)/0.4)` .
14. Determine peak time by `data_pos(idx,1)` .
15. **Save** `Vm` and `data_pos` data of the tuned PD controller.
16. ***Sampling Rate Test***: Change the sampling time to 0.04 sec. To set the sampling time, you need to go to the block diagram file, and choose “Modeling” -> “Model Settings” -> “Solver Details”. Set the fixed step to 0.04 sec (it should have been 0.002 previously). Run the PD Controller with the tuned values, observe the behaviour and **save** the data with an appropriate filename (e.g. -  *PD\_sampling\_0.04).*

#### B. Ramp Response Experiments

1. Double-click on the Signal Generator and set the following parameters to generate a triangular wave (i.e. ramp reference):
   * Signal Type = *triangle*
   * Amplitude = 1
   * Frequency = 0.4 Hz
2. In the Simulink diagram, set the Amplitude gain block to $$\pi/6$$ and the Constant offset gain block to $$\pi/6$$. This will generate a triangular wave with amplitude between 0 to $$60^{\circ}$$.
3. Check that the sampling time is set to 0.002 s (located under “Modeling” -> “Model Settings” -> “Solver Details”).
4. ***PD Controller Test***: Run the `setup_servo_pos_cntrl.m`  file. Enter the tuned values of $$K\_p$$ and $$K\_d$$ as obtained in Step 10 of the Step Response Experiment of Digital Twin, and set the value for $$K\_i$$ to 0 in the command window.
5. To build the model, press **Start** <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2F5kUOzUmpIz5Mob4x7xd1%2Fimage.png?alt=media&amp;token=60cb4298-b97c-4538-8240-2d44c2d721b0" alt="" data-size="line">. Run the experiment for 5 seconds.
6. Keep the Quanser Interactive Labs window next ot the Simulink window to observe the experiment.
7. Open the position scope.
8. **Save** `Vm` and `data_pos` data.
9. ***PID Controller Test***: Now, add an integral gain ($$K\_i$$), notice how the system characteristics change. Set $$K\_p$$ , $$K\_d$$ to the respective tuned values found in Step 10 of the Step Response Experiment of Digital Twin, and $$K\_i$$ as 1% of $$K\_p$$ . Simulate. Observe the behavior.
10. **Save** `Vm` and `data_pos` data.
11. Close MATLAB.

### 2. Hardware Experiments

{% file src="/files/jgqZN9K7BzQ5nwe5VjUK" %}

#### A. Step Response Experiment

1. Download and open `q_servo_pos_cntrl.slx` file. Do not change anything except the PID gains.
2. Double-click on the Signal Generator and ensure that the following parameters are set to generate a square wave (i.e. step reference):
   * Signal Type = *square*
   * Amplitude = 1
   * Frequency = 0.4 Hz
3. In the Simulink diagram, set the Amplitude gain block to 0.2 and the Constant offset gain block to 0.2. Note that these gain blocks outside the Signal Generator block are introduced to generate the square wave with amplitude limits of 0 V and 0.4 V.
4. Check that the sampling time is set to 0.002 s (located under “Modeling” -> “Model Settings” -> “Solver Details”).
5. ***PD Controller Test***: Enter the tuned values of $$K\_p$$ and $$K\_d$$ as obtained in Step 10 of the Step Response Experiment of Digital Twin, and set the value for $$K\_i$$ to 0.
6. Turn on the power amplifier.
7. To build the model, click down arrow on **Monitor & Tune** under Hardware tab and then **Build** **for monitoring** ![](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2FCq5iMDIRj3JN7fcFsx2w%2Fimage.png?alt=media\&token=6ffaf234-92d6-4de2-be27-7a37a4fe9dcc).&#x20;
8. Open the position scope.
9. Press **Connect** <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2F1VD5IqnbVgR6hbi4YOei%2Fimage.png?alt=media&amp;token=02e68127-9611-4c74-b03f-b7cac5c04a34" alt="" data-size="line"> button under Monitor & Tune and then press **Start** <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2F5kUOzUmpIz5Mob4x7xd1%2Fimage.png?alt=media&amp;token=60cb4298-b97c-4538-8240-2d44c2d721b0" alt="" data-size="line">. Run the experiment for 5 seconds.
10. **Save** your data. No need to Build again unless you change any blocks or configuration settings in the model.
11. ***Sampling Rate Test***: Change the sampling time to 0.04 sec. To set the sampling time, you need to go to the block diagram file, and choose “Modeling” -> “Model Settings” -> “Solver Details”. Set the fixed step to 0.04 sec (it should have been 0.002 previously). Run PD Controller with the tuned values, observe the behaviour and **save** the data with an appropriate filename (e.g. -  *PD\_sampling\_0.04).*

#### B. Ramp Response Experiments

1. Double-click on the Signal Generator and set the following parameters to generate a triangular wave (i.e. ramp reference):
   * Signal Type = *triangle*
   * Amplitude = 1
   * Frequency = 0.4 Hz
2. In the Simulink diagram, set the Amplitude gain block to $$\pi/6$$ and the Constant offset gain block to $$\pi/6$$. This will generate a triangular wave with amplitude between 0 to $$60^{\circ}$$.
3. Check that the sampling time is set to 0.002 s (located under “Modeling” -> “Model Settings” -> “Solver Details”).
4. ***PD Controller Test***: Enter the tuned values of $$K\_p$$ and $$K\_d$$ as obtained in Step 10 of the Step Response Experiment of Digital Twin, and set the value for $$K\_i$$ to 0.
5. Turn on the power amplifier.
6. To build the model, click down arrow on **Monitor & Tune** under Hardware tab and then **Build** **for monitoring** ![](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2FCq5iMDIRj3JN7fcFsx2w%2Fimage.png?alt=media\&token=6ffaf234-92d6-4de2-be27-7a37a4fe9dcc).&#x20;
7. Open the position scope.
8. Press **Connect** <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2F1VD5IqnbVgR6hbi4YOei%2Fimage.png?alt=media&amp;token=02e68127-9611-4c74-b03f-b7cac5c04a34" alt="" data-size="line"> button under Monitor & Tune and then press **Start** <img src="https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MIedmBPpkGaOOtNl4mm%2Fuploads%2F5kUOzUmpIz5Mob4x7xd1%2Fimage.png?alt=media&amp;token=60cb4298-b97c-4538-8240-2d44c2d721b0" alt="" data-size="line">. Run the experiment for 5 seconds.
9. **Save** your data. No need to Build again unless you change any blocks or configuration settings in the model.
10. ***PID Controller Test***: Now, add an integral gain ($$K\_i$$), notice how the system characteristics change. Set $$K\_p$$ , $$K\_d$$ to the respective tuned values found in Step 10 of the Step Response Experiment of Digital Twin, and $$K\_i$$ as 1% of $$K\_p$$ . Simulate. Observe the behavior.
11. **Save** `Vm` and `data_pos` data.

Results and Questions for Report

### (A) Step Response

#### i) PD Controller

1. Plot angle or rotary position, i.e., commanded, experimental(hardware), and simulation(digital twin) responses on one figure with the tuned PD gains.
2. Why is there no steady-state error in a PD controller with a step command?

#### iii) Sampling Rate

1. Plot angle or rotary position, i.e., commanded, experimental(hardware), and simulation(digital twin) responses on one figure.
2. What is the effect of sampling rate on closed-loop system behaviour?

### (B) Ramp Response

#### i) PD Controller

1. Plot angle or rotary position, i.e., commanded, experimental(hardware), and simulation(digital twin) responses on one figure.

#### ii) PID Controller

1. Plot angle or rotary position, i.e., commanded, experimental(hardware), and simulation(digital twin) responses on one figure.

#### iii) Hardware vs Digital Twin

1. Compare the percentage overshoot and peak time of the digital twin and hardware for PID controller. Are they similar?

### (C) Questions for Report

1. Determine the bandwidth of the closed-loop system with tuned gains.
2. Determine the phase margin of the closed-loop system with tuned gains.
3. Discuss the dead zone in a DC motor and how it affects steady-state error.
