# B & C. Control Design and Controller Implementation (Week 2)

## Objective

The objective is to design a controller that allows rapid reorientation of the link with very little vibrations from the link flexibility. This analysis is a preliminary approach to containing structural vibrations in real-world applications like aircraft and spacecraft structures as well as robotic links/manipulators

## B. Control Design

The methodology used is to design two types of controllers, viz., Servo Controller and Link Controller. We will compare the closed loop performance between the two controllers.

* In the Servo Controller design, partial state feedback is used, i.e., only the servo angle $$({\theta})$$ and its rate ($$\dot{\theta}$$) are used as feedback variables. We will make use of the *controlSystemDesigner* tool in MATLAB with simultaneous loop closure architecture for the controller.
* In the Link Controller design, full state feedback is used, i.e, the servo angle $$({\theta})$$ and its rate $$(\dot{\theta})$$, as well as the link angle $$({\alpha})$$ and its rate $$(\dot{\alpha})$$, are used as feedback variables. The method we will use for full state feedback is based on the linear quadratic regulator (LQR) theory, which requires the given system to be state controllable.

### State Space Model &#x20;

The equations that describe the motions of the servo and the link with respect to the servo motor voltage are obtained using the lumped mass method and can be written as&#x20;

$$
(J\_{eq} + J\_l)\ddot{\theta} + J\_l\ddot{\alpha} + B\_{eq}\dot{\theta} = \tau \quad \quad \tag{1}
$$

$$
J\_{l}\ddot{\theta} + J\_l\ddot{\alpha} + B\_{l}\dot{\theta} + K\_s\alpha= 0  \quad \quad \tag{2}
$$

Assuming the viscous damping of the link is negligible, i.e. $$B\_l$$ = 0 and substituting the torque $$\tau$$ using the expression,

$$
\tau = \displaystyle\frac{\eta\_gK\_g\eta\_mk\_t(V\_m - K\_gk\_m\dot\theta)}{R\_m} = C\_1V\_m - C\_2\dot\theta
$$

the EOM can be rearranged as

$$
\ddot{\theta} = -(\frac{B\_{eq} + C\_2}{J\_eq})\dot{\theta} + \frac{K\_s}{J\_{eq}}\alpha + \frac{C\_1}{J\_{eq}}V\_m \quad \quad \tag{3}
$$

$$
{\ddot{\alpha}} = (\frac{B\_{eq}+C\_2}{J\_{eq}}){\dot{\theta}} - K\_s(\frac{J\_{eq}+J\_l}{J\_{eq}J\_l}){\alpha }- \frac{C\_1}{J\_{eq}}V\_m \quad \quad \tag{4}
$$

The various system parameters can be found in Table 2 of Part A: System Identification.&#x20;

Equations 3 and 4 can be represented in state-space form

$$
\dot{x} = Ax + Bu \\
y = Cx + Du
$$

with$$x=\[\theta  ;; \alpha ;; \dot{\theta} ;; \dot{\alpha}]^T$$ and$$u = V\_m$$, where&#x20;

$$
A = \begin{bmatrix}    0 &0 & 1 &0 \  0 &0 & 0 &1 \  0 &\frac{K\_s}{J\_{eq}} & -(\frac{B\_{eq}+C\_2}{J\_{eq}}) &0 \ 0 &-K\_s(\frac{J\_{eq}+J\_l}{J\_{eq}J\_l}) & (\frac{B\_{eq}+C\_2}{J\_{eq}}) & 0 \end{bmatrix} \quad \quad \tag{5}
$$

$$
B = \begin{bmatrix} 0 \ 0\ \frac{C\_1}{J\_{eq}} \ -\frac{C\_1}{J\_{eq}} \end{bmatrix} \quad \quad \tag{6}
$$

In the output equation, only the position of the servo and link angles are being measured.

As such, the velocities of the servo and link angles can be computed in the digital controller if required, e.g., by taking the derivative and filtering the result though a high-pass filter. Hence, with all the states of the system as outputs, the C and D matrices are

$$
C = \begin{bmatrix} 1 & 0& 0& 0\ 0 & 1& 0& 0\ 0 & 0& 1& 0\ 0 & 0& 0& 1\ \end{bmatrix} \qquad \qquad \tag{7}
$$

$$
D = \begin{bmatrix} 0\ 0 \0 \ 0 \end{bmatrix} \qquad \qquad \tag{8}
$$

### Linear Quadratic Regulator (LQR) Design

#### Controllability &#x20;

If the control input $$'u'$$ of a system can take each state variable, $$x\_i$$ where $$i = 1 . . . n$$, from an initial state to a final state in finite time, then the system is controllable, otherwise it is uncontrollable. The controllability of a system can be determined using the ‘Rank Test’. \
\
According to the Rank Test, the system is controllable if the rank of its controllability matrix &#x20;

$$
T=\[B ;; AB ;;A^2B… A^{n−1}B]
$$

&#x20;equals the number of states of the system,&#x20;

$$
rank(T)=n
$$

#### LQR controller&#x20;

If (A,B) are controllable, then the Linear Quadratic Regular optimization method can be used to find full state feedback controller gains. Given the plant model in state-space form where $$x$$ represents the state vector, this technique finds a control input ‘$$u$$’ that minimizes the cost function &#x20;

$$
J= \int\_0^\infty(x^TQx+ u^TRu)dt \qquad \qquad \tag{9}
$$

&#x20;where Q and R are the user-defined weighting matrices $$Q = Q^T$$ and $$R = R^T$$. It is assumed that Q is positive semi-definite (all the eigenvalues of$$Q$$ are non-negative) and $$R$$ is positive definite (all the eigenvalues of $$R$$ are positive). The weighting matrices affect how LQR minimizes the function and are, essentially, tuning variables. Here, $$x$$ is the state vector$$\[\theta ;; \alpha ;; \dot{\theta} ;; \dot{\alpha}]^T$$ and$$u$$ is the servo motor voltage input $$V\_m$$.&#x20;

Given the control law $$u = −Kx$$, the state-space model of the closed loop system becomes

$$
\dot{x} = Ax+B(−Kx)=(A−BK)x \quad \quad \tag{10}
$$

where $$K$$ is given by&#x20;

$$
K=\[k\_{\theta} ;;k\_{\alpha};;k\_{\dot{\theta}};;k\_{\dot{\alpha}}] \qquad \qquad \tag{11}
$$

is the controller gain vector. Note that the input voltage $$Vm$$ is scalar, and therefore, $$R$$ is also scalar. In practice, the weighting matrices $$Q$$ and $$R$$ are chosen to be diagonal. A simple way for choosing $$Q$$ and $$R$$ is using the **Bryson's rule**. This method works best when control design specifications regarding the system response and control input are given in terms of limits on excursions from equilibrium. &#x20;

Assuming the time-domain design specifications are&#x20;

$$
|x\_i| \leq x\_{i\_{\max}}  ; i = 1, 2, ..., n ; ; ; \ |u\_j| \leq u\_{j\_{\max}} ; j = 1, 2, ..., m
$$

Then one can use the Bryson’s rule to choose the weighting matrices as

$$
Q=\displaystyle {diag\left (\frac{1}{x\_{i\_{\max}}^2}\right) ; ; i = 1, 2, ..., n} \quad \quad (12) \ R = diag\left (\frac{1}{u\_{j\_{\max}}^2}\right) ; ; j = 1, 2, ..., m \quad \quad (13)
$$

Further tuning of $$Q$$ and $$R$$ may be required for improving the controller performance. Note that increasing $$R$$ means increasing the penalty on the control input (using less control effort). Similarly, increasing $$Q$$ means increasing the penalty on the states (states reaching equilibrium position quickly and with less overshoot). Matrix $$Q$$ sets the weight on the states and determines how $$u$$ will minimize $$J$$ (and hence how it generates gain matrix $$K$$).&#x20;

In our case,&#x20;

$$
Q = \begin{bmatrix} \frac{1}{\theta\_{max}^2} & 0& 0& 0\ 0 & \frac{1}{\alpha\_{max}^2}& 0& 0\ 0 & 0& \frac{1}{\dot{\theta}*{max}^2}& 0\ 0 & 0& 0& \frac{1}{\dot{\alpha}*{max}^2}\ \end{bmatrix} \qquad \qquad \tag{14}
$$

$$
R = \frac{1}{V^2\_{m, max}} \quad \quad (15)
$$

Substituting  $$Q$$ and $$R$$ in Eqn. 9 gives&#x20;

$$
J= ∫*0^\infty (q*{θ}\theta^2+q\_{α}\alpha^2+ q\_{\dot{θ}}{\dot{\theta}}^2+q\_{\dot{α}}{\dot{\alpha}}^2+RV\_m^2)dt \quad \quad \tag{16}
$$

where $$q\_\theta= \displaystyle\frac{1}{\theta\_{max}^2}$$,  $$q\_\alpha= \displaystyle\frac{1}{\alpha\_{max}^2}$$, $$q\_{\dot{\theta}}= \displaystyle\frac{1}{\dot{\theta}*{max}^2}$$, $$q*{\dot{\alpha}}= \displaystyle\frac{1}{\dot{\alpha}*{max}^2}$$, $$R=\displaystyle\frac{1}{V*{m,max}^2}$$

On observing Eq. 12, it can be seen that increasing $$q\_i$$ causes control input $$V\_m$$ to work harder to minimize state $$x\_i$$, which will predominately increase $$k\_i$$. If $$q\_{\theta}$$ is increased, then $$k\_{\theta}$$ will increase to compensate for the larger weight placed on state $$\theta$$. Depending on the model, changing a single $$q\_i$$ can affect multiple $$k\_i$$ gains because each state is not independent. If $$R$$ is increased, then control input $$V$$ has to work less to minimize $$J$$. In that case, LQR would generate a lower overall value of $$K$$. \
\
A block diagram of the system with fill state feedback controller is shown in Figure 1.

<figure><img src="/files/OaEx69YRVJdPuECmHlav" alt=""><figcaption><p>Figure 1. State-feedback Control Loop (Note:<span class="math">C</span> is <span class="math">4×4</span> identity matrix and <span class="math">D</span> is <span class="math">4×1</span> column of zeros.)  </p></figcaption></figure>

The feedback control loop in Figure 1 is designed to stabilize the servo to a desired position,$$θ\_d$$, while minimizing the deflection of the flexible link, where$$x$$ is the state column vector and$$K$$ is the gain row vector as shown earlier.  Note that$$C$$ is $$4×4$$ identity matrix and $$D$$ is $$4×1$$ column of zeros in Figure 1.

The reference state is defined as&#x20;

$$
x\_d=\[θ\_d ;; 0;; 0;;0]^T
$$

and the controller is&#x20;

$$
u=K(x\_d−x)
$$

### Controller Specifications:&#x20;

The following specifications in the time domain are to be met when the rotary arm is tracking a $$30°$$step change in its angular position. &#x20;

Specification 1: Servo angle rise time: $$t\_r≤0.2s$$&#x20;

Specification 2: Servo angle percentage overshoot: $$PO≤5 %$$

Specification 3: Maximum link angle deflection:$$|α|\_{max}≤10°$$

Specification 4: Maximum control effort (voltage): $$|V\_{m}|\_{max}≤10 V$$&#x20;

### Controller Design Procedure:&#x20;

Note:  The results from the following steps have to be included in the Analysis section of the report.&#x20;

**State Space Model**

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

1. Download A\_and\_B\_matrices.m file and input your $$K\_{s}$$ value from System Identification.
2. Obtain the $$A$$ and $$B$$ matrices of the system using the parameters from Part A: System identification.  **Check your A and B matrices with the TA.**

**Servo Controller Design (PD Controller):**

1. For a rapid rotation of the link, we select the proportional gain based on using the maximum voltage input at the beginning. For a 30 degrees  $$( = \frac{\pi}{6} rad)$$ of step command, required proportional gain  $$= \frac{V\_{max}}{\theta\_{command}} = \frac{10}{\frac{\pi}{6}}$$ = 19.1. Select Proportional gain $$(K\_{\theta})$$ = 19.
2. With the servo angle $$({\theta})$$ as output, construct the state space model in MATLAB.\
   \>> `sys_servo = ss(A,B,[1,0,0,0],0)`
3. Open the *controlSystemDesigner* tool in MATLAB.\
   \>> `controlSystemDesigner(sys_servo)`
4. Under *Edit Architecture,* select the controller structure shown in the figure.\
   **Note** **that C1 is the proportional gain block and C2 is the derivative gain block.** **Hence, you need to add a differentiator in C2, not a real zero**.

<figure><img src="/files/q8bRf0pfRUbIyzcH7eNt" alt="" width="467"><figcaption></figcaption></figure>

5. Set the gain value in C1 to 19 (see step 1).
6. Use the step response plot with the rise time and peak response characteristics selected, and adjust the derivative gain C2 to meet the given specs for the rise time and the percentage overshoot. The value of C2 is the feedback gain for the servo angle rate, i.e., $$(K\_{\dot{\theta}})$$. **Check your gain values with the TA.**

**Link Controller Design:**

1. Using *Bryson’s rule* (Eq. 12-15) calculate $$Q$$ and $$R$$ matrices, based on the following suggested maximum values of state and control excursions:&#x20;

$$θ\_{max, initial} = 0.05 rad,  α\_{max}=0.1 rad,$$  $$\dot{θ}*{max}=1rad/s, \dot{α}*{max}=0.5rad/s$$     \
$$V\_{max}=1 V$$  \
Check if $$Q \ge 0$$ and $$R > 0$$ (by finding eigenvalues of the matrix).

2. Using the *`lqr`* command in MATLAB, calculate the optimal feedback gain vector $$K$$. The *`lqr`* command solves the Algebraic *Riccati equation* which is required to determine the gain vector $$K$$ is

\>> `[K,S,E] = lqr(A,B,Q,R);`\
\
where $$K$$ is the gain matrix, $$S$$ is the solution to the *Riccati* *equation* and E are closed loop system poles. **Check your gain matrix K with the TA.**

3. Check if the gain associated with servo angle $$({\theta})$$ feedback matches with its selected value in the servo controller design, i.e., $$K\_{\theta}$$ = $$K(1,1)$$ = 19. If not, adjust the weight $$q\_{\theta}$$ (increase the weight to increase the gain and decrease the weight to decrease the gain) while keeping the rest of the weights same. Repeat the *lqr* command till  $$K\_{\theta}$$ = $$K(1,1)$$ = 19. **Check your gain matrix K with the TA.**

**Simulation Evaluation:**

1. Using Figure 1 as a guide, develop a Simulink model of the system.\
   \
   **Note:** SIMULINK references the current MATLAB workspace when setting variables. Use the variable names for the system matrices $$A$$ and $$B$$ and the control gain matrix $$K$$ from your MATLAB script file for the LQR design.\
   \
   Make sure to include servo position command input, saturation blocks, and appropriate outputs. Use scopes and *To Workspace blocks* so that reference, output, and response signals can be tracked. (Make sure to maintain consistency between deg and rad in SIMULINK)
2. Using the controller gains found in step 3 of Link Controller Design, simulate the closed loop system for a step servo position command of $$30\degree$$over a 10s duration.&#x20;

* Record (and plot for lab report) the servo angle ($$\theta$$), link angle ($$\alpha$$) and voltage input $$V\_m$$.&#x20;

3. Using the controller gains found in step 6 of Servo Controller Design, simulate the closed loop system for a step servo position command of $$30\degree$$over a 10s duration.&#x20;
   * This is easily done by selecting the gain matrix K used in your Simulink model as $$K\_{\theta}$$ and $$K\_{\dot{\theta}}$$ values from step 6 of Servo Controller Design and $$K\_{\alpha}$$=0, and $$K\_{\dot{\alpha}}$$=0. **Check your plots with the TA.**
   * Record (and plot for lab report) the servo angle ($$\theta$$), link angle ($$\alpha$$)  and voltage input  $$V\_m$$

## C. Controller Implementation and Evaluation

In this experiment, the servo position is controlled while minimizing the link deflection using the Link Controller found. Measurements will then be taken to ensure that the specifications are satisfied. Also, we will implement the Servo Controller found for comparison.

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

{% hint style="danger" %}
**Warning:** <mark style="color:red;">**In closed loop experiments, the link may swing around rather quickly. Stay clear of it during closed loop experiments.**</mark>
{% endhint %}

### Experiment

1. Turn the amplifier power on.
2. Download the q\_flex.zip and open the Simulink file q\_flex for controller implementation. This is the block diagram for this part of the experiment. Make sure you have K gain variable in the MATLAB workspace.\
   \
   **Note:** The Smooth Signal Generator block generates a 0.33 Hz square wave (with amplitude of 1) that is passed through a Rate Limiter block to smooth the signal (Rising slew rate of 100 and falling slew rate of -100). The Amplitude (deg) gain block is used to change the desired servo position command. The state-feedback gain K is set in the Link Control gain block and is read from the MATLAB workspace.<br>

   <figure><img src="/files/c5xNLwG69LchB32Qc6VE" alt=""><figcaption></figcaption></figure>
3. To build the model, click down arrow on **Monitor & Tune** under Hardware tab and then **Build** **for monitoring** ![](/files/F2dOEp0PbFGdcI8vs0cM).  This generates the controller code.&#x20;
4. Align the flexible link along the 0 mark on the servo stand.
5. Double click on the scopes and open them.
6. Ensure that the manual switch is connected to the full state feedback.
7. Click **Connect** <img src="/files/uORNyY7T0XmLTYaSZyVj" alt="" data-size="line"> button under Monitor & Tune and then click **Start** <img src="/files/OTdtk1LFdBfwxlcgsJX8" alt="" data-size="line">
8. A periodic square wave is given as commanded link rotation to the servo motor, causing the link to move left and right alternately for 10 seconds.
9. Copy the response data to your group folder. The response data is saved in variables data\_theta, data\_alpha, and data\_vm. *DO NOT DELETE or SAVE THE SIMULINK MODEL*.
10. Connect the manual switch to the partial state feedback and repeat steps 4-9 again using the gains obtained from your Servo Controller Design. Make sure your files name is different from full state feedback.
11. Turn the power off.

## Results & Questions for Report

### Control Design

&#x20; **Results**

1. Include the A and B matrices.
2. Include the results from the Servo Controller Design procedure (steps 1 through 6).
3. Include the results from the Link Controller Design procedure (steps 1 through 3).

&#x20; **Questions**

1. Estimate the bandwidth for your Link and Servo controllers. Hint: Use 'bandwidth(sysclosed)' command in MATLAB with link rotation ($$\theta$$)  as the output.

\>> sysclosed = ss((A-B\*K), B, \[1 0 0 0], 0);

\>> bw = bandwidth(sysclosed)

\>> bode(sysclosed)

The bode() function produces closed loop system frequency response plot. Include Bode plots of the closed loop system and list the bandwidth of the system with Link and Servo controllers.

2. Estimate the stability margins (gain and phase margins) of your Link and Servo controllers. Hint: Use 'margin(sysopen)' command in MATLAB where 'sysopen' is the state space model of the open loop system with the controller.

\>> sysopen = ss(A,B,K,0)

\>> margin(sysopen)

We get bode plot of the open loop system with the controller with margins listed on the plots. Include the Bode plots for the Link and Servo controllers.

### &#x20;Controller Implementation

&#x20;  **Results**

1. Run the Simulink that you built in control design with the same command input used in the experiment.\
   **Note:** Average the first few seconds of the experimental data to obtain initial conditions for your state-space block in Simulink.
2. Plots of servo angle, link deflection angle, and input voltage vs time for Link Controller and Servo Controller. On the same plot, show the experimental results and simulation results with the Link Controller and the Servo Controller. Clearly label your plots.
3. Compare the experimental results using the two controllers and explain the differences.
4. Compare the experimental results with the simulation results individually for each controller and explain the differences.

&#x20;  **Questions**

1. Explain why the link deflection angle goes opposite to servo rotation initially.
2. Explain the impact of reducing the control weight R on the gain matrix K.

&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gtae.gitbook.io/ae4610/lab-3-rotary-flexible-link/b-and-c.-control-design-and-controller-implementation-week-2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
