> 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/archive/aero-lqr/2-dof-helicopter-2.md).

# B. Control Design & Implementation (Week 2)

## Controller Design

### Background

#### Linear State-Space Representation

Given the linear state-space equations: $$\dot{x}=Ax+Bu$$ and $$y=Cx+Du$$, we define the state for the&#x20;Quanser Aero Experiment as

&#x20;                                                            $$x=\begin{bmatrix} \theta(t), & \psi (t), & \dot{\theta}(t), & \dot\psi(t) \end{bmatrix}^T \tag{38}$$                                               &#x20;

the output vector as                       $$y= \begin{bmatrix} \theta(t), & \psi(t), & \dot{\theta}(t), & \dot{\psi}(t) \end{bmatrix}^T \tag{39}$$                                                                 &#x20;

and the control variables as

$$u=\begin{bmatrix} V\_\theta & V\_\psi\end{bmatrix}^T \tag{40}$$                                                                &#x20;

where $$\theta$$and $$\psi$$are the pitch and yaw angles, respectively,  and $$V\_\theta$$and $$V\_\psi$$ are the motor voltages applied to the pitch and yaw rotors (i.e. the main and tail rotors). Using the equations of motion in Equations (1) and (2), the state-space matrices are

$$A=\begin{bmatrix} 0&0&1&0\ 0 &0&0&1\ -K\_{\theta}/J\_\theta & 0& -D\_\theta/J\_\theta& 0 \ 0 &0 &0&-D\_\psi/J\_\psi \end{bmatrix} \tag{41.a}$$​

$$B=\begin{bmatrix} 0& 0\ 0&0\ K\_{\theta\theta}/J\_\theta & K\_{\theta\psi}/J\_\theta \ K\_{\psi\theta}/J\_{\psi} & K\_{\psi\psi}/J\_\psi \end{bmatrix} \tag{41.b}$$

$$C=\begin{bmatrix} 1&0&0&0\ 0&1&0&0  \ 0&0&1&0 \ 0&0&0&1\end{bmatrix} \tag{41.c}$$

$$D=\begin{bmatrix} 0&0\ 0&0 \ 0&0\  0 &  0\end{bmatrix} \tag{41.d}$$                                         &#x20;

In this section, a state-feedback controller is designed to regulate the pitch and yaw angles of the Quanser Aero Experiment to desired *angles.* Using the previous state-space model, we can find a control gain  $$K$$ based on the coupled dynamics to stabilize the system. The control gains are computed using **Linear-Quadratic Regulator (LQR)** theory. The general state-feedback control is illustrated in Figure 11. The state-feedback controller is defined

![Fig. 11: Model used to acquire free-oscillation response about pitch](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-MOD0FwsE4XTIDPzOA8G%2F-MOD4s6si5_S8c-7BK45%2Fimage.png?alt=media\&token=31803107-24ca-4e45-a948-798a55cb19e4)

&#x20;                                                                            $$u=K(x\_d-x) \tag{42}$$                                                                    &#x20;

where $$x$$ is the state defined in Equation (38)

&#x20;                                                                    $$x\_d=\begin{bmatrix}\theta\_d, & \psi\_d, & 0,&0\end{bmatrix} ^ T\tag{43}$$                                                           &#x20;

is the reference command (or setpoint) state with the desired pitch and yaw angles, $$\theta\_d$$ and $$\psi\_d$$ , and

&#x20;                                                                               $$u=\begin{bmatrix} V\_\theta\&V\_\psi\end{bmatrix} ^ T\tag{44}$$                                                                  &#x20;

is the control input where $$V\_\theta$$ is the front/pitch motor voltage and $$V\_\psi$$ is the tail/yaw motor voltage.

**Linear Quadratic Regulator** (LQR) optimization can be used for finding the control gain parameters&#x20;of the Quanser Aero Experiment flight control. Given the state-space representation, the LQR algorithm&#x20;computes a control law $$u$$ to minimize the performance criterion or cost function:$$J=\int\_0^\infty(x\_{\rm ref}-x(t))^TQ(x\_{\rm ref}-x(t))+u(t)^TRu(t)dt. \tag{45}$$                                &#x20;

The design matrices $$Q$$ and $$R$$ hold the penalties on the deviations of state variables from their setpoint and&#x20;the control actions, respectively. When an element of $$Q$$ is increased, therefore, the cost function **increases&#x20;the penalty associated with any deviations from the desired setpoint of that state variable**, and thus the&#x20;specific control gain will be larger. When the values of the $$R$$ matrix are increased, a **larger penalty is&#x20;applied to the aggressiveness of the control action**, and **the control gains are uniformly decreased**.

Since there are four states , $$Q\in \mathbb{R}^{4\times 4}$$, and two control variables, $$R\in\mathbb{R}^{2\times 2}$$ . The setpoint, $$x\_d$$ (Equation 43) is given&#x20;above the control strategy used to minimize cost function J is thus given by

&#x20;                            $$u=K(x\_d-x)=k\_{p,\theta}(\theta\_d-\theta)+k\_{p,\psi}(\psi\_d-\psi)-k\_{d,\theta}\dot\theta-k\_{d,\psi}\dot\psi \tag{46}$$                    &#x20;

### Designing an LQR Controller

Various control software already has LQR optimization routines that can be used to generate the state feedback control gain $$K$$. In order for the closed-loop response to satisfy certain time-domain specifications,&#x20;the closed-loop system is typically simulated using its dynamic model, in software, first. This is an iterative&#x20;process. By adjusting the weighting matrices $$Q$$ and $$R$$ and then running the simulation, we can find a control that satisfies the user's requirements. Further, we must ensure that the control signal $$u$$ is smooth&#x20;(i.e. does not chatter) and does not surpass the limits of the actuator.

#### LQR Control Design and Simulation

LQR is used to find the state-feedback control gain $$K$$ ($$K$$ is $$2\times 4$$matrix) that will stabilize the Quanser Aero Experiment to the user's desired pitch and yaw angles. Our desired closed-loop response should match the following specifications. Use MATLAB command `K = lqr(A,B,Q,R)` to obtain gain, $$K$$, matrix.

#### Desired closed-loop response specifications for pitch

1. Steady-state error: pitch $$e\_{\rm ss}$$ ≤ 2 $$\deg$$, yaw $$e\_{\rm ss}$$ ≤ 2 $$\deg$$.

2. Peak time: $$t\_{\rm p}$$ ≤ 2 s.

3. Percent Overshoot: $$\rm PO$$ ≤ 7.5%.

4. No actuator saturation: $$|V\_\psi|$$ ≤ 24V  and $$|V\_\theta|$$ ≤ 24V .

The state-space matrices derived in Equation (41) are entered in the State-Space block in SIMULINK and the control gain is set to the MATLAB variable K.

#### Running the closed-loop state-feedback LQR simulation

1. Using parameters from Part A, obtain state-space matrices of the system neglecting coupling effect.

2. Design $$Q,R$$ , and $$K$$. To start with try with following $$Q$$, $$R$$ and $$K$$.\
   `Q = diag([200 75 0 0 ]);` \
   `R = 0.01*eye(2,2);` \
   `K = lqr(A,B,Q,R)`<br>

3. Build the SIMULINK model of the system

4. Simulate the closed-loop response of the system with pitch-only command: $$\theta\_d=10\space \deg$$. (Make sure to change deg to rad in SIMULINK)

5. Simulate the closed-loop response of the system with yaw only command: $$\psi\_d=45 \space \deg$$. (Make sure to change deg to rad in SIMULINK)

6. Simulate the closed-loop response of the system with pitch and yaw command: $$\theta\_d= 10\space \deg$$ and $$\psi\_d = 45 \space \rm deg$$. (Make sure to change deg to rad in SIMULINK)

7. Check whether your controller meets the desired specifications.

8. If it fails to meet the specifications, tune $$Q$$ and/or $$R$$ to meet the specifications

9. Save the data and plot the closed-loop system response for pitch and yaw

10. Now, use system matrices with coupling effect and use the controller without coupling

11. Repeat steps 4-6

12. The closed-loop response in this step does not have to meet the desired specifications

13. Now, design $$Q,R$$ , and $$K$$ with the coupling effect

14. Build the SIMULINK model of the system

15. Repeat steps 4-6

16. Check whether your controller meets the desired specifications.

17. If it fails to meet the specifications, tune $$Q$$ and/or $$R$$ to meet the specifications

18. Save the data and plot the closed-loop system response for pitch and yaw

#### Things to check

* [ ] Results from part A  &#x20;
* [ ] System matrices neglecting the coupling effect
* [ ] System response of the system neglecting coupling effect (pitch & yaw)
* [ ] System matrices with coupling effect
* [ ] System response of the system with coupling effect (pitch & yaw)

#### Analysis

1. What happened when you neglect the coupling effect?
2. Is there any systematic way to design $$Q$$ and $$R$$ ?

## Controller Implementation

In this section, the state-feedback control is implemented on the Quanser Aero Experiment using the q\_aero\_2dof\_lqr\_control SIMULINK diagram shown in Figure 12 with QUARC.

Download the file into the quanser\_aero folder that was used for Week 1.

{% file src="/files/6LddURiujAwgqYLIYfX1" %}

![Fig. 12: SIMULINK model used to run LQR controller](https://1205030739-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MIedmBPpkGaOOtNl4mm%2F-MOD0FwsE4XTIDPzOA8G%2F-MODGSUahjTGey32anEq%2Fimage.png?alt=media\&token=ff2839a0-0338-49f1-a668-6869f5b392fa)

### Desired Closed-Loop Response Specifications

1. Steady-state error: pitch $$e\_{\rm ss}$$ ≤ 2 $$\deg$$, yaw $$e\_{\rm ss}$$ ≤ 2 $$\deg$$.
2. Peak time: $$t\_{\rm p}$$ ≤ 2 s.
3. Percent Overshoot: $$PO$$ ≤ 7.5%.
4. No actuator saturation: $$|V\_\psi|$$ ≤ 24V and $$|V\_\theta|$$ ≤ 24V .

### Running the closed-loop state-feedback LQR simulation

### Neglecting Coupling Effect

#### Pitch Command Only

1. Unlock both pitch and yaw axes to enable the full 2 DOF motion.

2. Open *q\_aero\_2dof\_lqr\_control SIMULINK file.*

3. Use the gain $$K$$ obtained in Part B. neglecting the coupling effect.

4. Use pitch command (10 deg) only by changing Amp\_y to zero.

5. To build the model, click the down arrow on **Monitor & Tune** under the Hardware tab and then click **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). This generates the controller code.

6. Click **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 run SIMULINK by clicking **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">.

7. Copy *`aerolqrrsp.mat`* to your own folder and rename it as nocouple\_pitch\_only.

8. Examine the obtained closed-loop response and see if it matches the desired specifications.

#### Yaw Command Only

1. Use a yaw command (45 deg) only by changing **Amp\_p** to zero.
2. Run SIMULINK
3. Copy *`aero_lqr_rsp.mat`* to your own folder and rename it as nocouple\_yaw\_only.
4. Examine the obtained closed-loop response and see if it matches the desired specifications

#### Both Yaw and Pitch&#xD;

1. Use both pitch and yaw command
2. Run SIMULINK
3. Copy *aerolqrrsp.mat* to your own folder and rename it as nocouple\_pitch\_yaw.
4. Examine the obtained closed-loop response and see if it matches the desired specifications

### With Coupling Effect

1. Use the gain $$K$$obtained in Part B. with the coupling effect.
2. Repeat above Pitch Only, Yaw Only and both Yaw and Pitch runs and rename the files as couple\_####\_###.
3. Close the Simulink. DO NOT SAVE THE CHANGE!

### Analysis Question

1. Did your controller successfully meet the specs? If not, why?

2. How does the coupling affect the performance of the controller?

3. How can we improve the controller?
