# 8. Modeling and Simulation

![Transportable Rotorcraft Electronic Control System](https://1229384491-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZ-_pPVcp9OBMJPFcXn%2F-MkhoncqyfgpeE4NEBsk%2F-MkipA0h4606M8nNgF8N%2FTRECSDino.jpeg?alt=media\&token=e188bb09-783f-402c-b560-117d591f4b49)

Instructions and Problems

### **1. Physical Device and Testing**

The goal of this section is to verify that the device has been built properly and operates as expected.

1. Demonstrate the you have a safe, working device. Provide proof (photos, screenshots, or a link to a video) that the device has been assembled and has passed each of the 4 tests.
2. Using the **default PID controller gains**, plot the device’s response to a step input of 0° along with the drive signal on the same figure. What do you observe about the response and how might it be improved?

**Notes:** If doing the DIY option, provide proof that the device has been assembled and has passed your own tests to ensure all sensors, actuators, and logging works properly. If doing the DIY option, choose an initial guess of PID gains and plot the device’s response to a baseline reference signal that is analogous to an input of 0° for the T-RECS.

### **2. Dynamics Modeling**

The goal of this section is to derive a mathematical model for the system that can be used to simulate the device’s response and be compared to the actual, physical response.

![Figure 1. Simplified model of the T-RECS device.](https://1229384491-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Ftrecs-user-manual%2F-Mid7JcKhZfvkOlKCNc3%2F-Mid7pPMDh1p2Gvdka8k%2F0.png?generation=1630630271412260\&alt=media)

**Notation:**

* The mass of the motor is given by$$m$$, moment of inertia of the system is given by$$J$$&#x20;
* The frictional coefficient on the lever arm is given by$$b$$
* The thrust produced by the propeller is given by$$F$$, where$$\omega$$is the angular velocity of the propeller and$$A$$is a proportional coefficient:

$$
F=A\omega^2
$$

**Assumptions:**

* &#x20;$$\omega$$is linearly proportional to the drive signal,$$u$$
* The lever arm’s mass is negligible

1. Derive the equation of motion for the system shown in **Figure 1** above. Be sure to note the assumptions and notation provided with the figure. Your equation should include$$\theta$$and its derivatives, accounting for damping due to the friction of the lever arm, and the drive signal input,$$u$$ . As a note, this system is nonlinear (with squares, sine/cosine, etc.)
2. Simplify the equation of motion by reducing it to standard form and replacing each term with a single coefficient. (That is, all the state terms on the left-hand side and the input on the right-hand side of the equation)

**Note:** If doing the DIY option, please provide a free body diagram and the equation(s) of motion specific to your system, if applicable. Provide as much detail as possible. Complete steps (1) and (2) above for your model.

### **3. Model Approximation**

The goal of this section is to experimentally approximate the system’s particular model in order to overcome the nonlinearities and computationally test its response for the controller in the following section.

1. Determine a numerical value for the coefficient of the$$\theta$$term. Consider conducting a “drop test” experiment to solve for the coefficient. What constraints will help you to simplify and solve?
2. Determine a numerical value for the coefficient of the$$u$$term. Consider conducting a “hover test” experiment to solve for the coefficient. What constraints will help you to simplify and solve?
3. Determine a numerical value for the coefficient of the$$\theta˙$$term. Consider applying MATLAB’s *ode45* tool to approximate the coefficient by testing various values. You may wish to use the data from the previous two experiments to verify your coefficient’s value.

**Note:** If doing the DIY option, you must derive the coefficients for your model however you see fit. Please record the test procedure as you will need the procedure and test data for your report. Provide as much detail as possible. Answer all questions for steps (1)-(3).

### **4. PID Controller Tuning, Testing, and Analysis**

The goal of this section is to examine different types of controllers and analyze the importance of feedback control on stabilizing the system.

![Figure 2. T-RECS feedback controller.](https://1229384491-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Ftrecs-user-manual%2F-Mid7JcKhZfvkOlKCNc3%2F-Mid7pPNxrNSv_qLCsXU%2F1.png?generation=1630630271401525\&alt=media)

* Write the equation for the error term,$$e(t)$$, in terms of the commanded signal,$$r$$, and the current state,$$\theta(t)$$

#### **4.1 Consider a Proportional Controller, or P Controller.**

1. Write the equation for the input of the controller,$$u$$, in terms of the proportional gain,$$K\_P$$, and error term,$$e(t)$$.
2. Choose a value for the controller gain and plot the simulated response using MATLAB’s *ode45* function to a commanded signal of 0°.
3. Set the device’s gain to that value (make sure to set the integral and derivative gains to 0). Record the device’s response to a commanded signal of 0°. Plot this response on the same figure as the simulated response.
4. Repeat Steps 2) and 3) for two different values of the controller gain.
5. Was the system able to reach the desired signal in any of the three tests, why or why not?
6. What, if any, differences are there between the simulated response and the actual response? What factors may have caused this?

#### **4.2 Consider a Proportional Derivative Controller, or PD Controller.**

1. Write the equation for the input of the controller,$$u$$, in terms of the proportional gain,$$K\_P$$, derivative gain,$$K\_D$$, and error term,$$e(t)$$.
2. Choose a value for each of the controller gains and plot the simulated response using MATLAB’s *ode45* function to a commanded signal of 0°. Continue choosing values until the response has a nicely damped response.
3. Set the device’s gains to those values (make sure to set the integral gain to 0). Record the device’s response to a commanded signal of 0°. Plot this response on the same figure as the simulated response.
4. Repeat Steps 2) and 3) for two different sets of controller gains, aiming to improve upon the previous responses.
5. Was the system able to reach the desired signal in any of the three tests, why or why not?
6. What do you observe about the transient response? (That is, the portion between rest and stabilization).

#### **4.3 Consider a Proportional Integral Derivative Controller, or PID Controller.**

1. Write the equation for the input of the controller,$$u$$, in terms of the proportional gain,$$K\_P$$, integral gain,$$K\_I$$, derivative gain,$$K\_D$$, and error term,$$e(t)$$.
2. Choose a value for each of the controller gains and plot the simulated response using MATLAB’s *ode45* function to a commanded signal of 0°. Continue choosing values until the response has a smooth, short transient region and settles with low steady state error.
3. Choose a value for each of the controller gains and set the device’s gains to the appropriate values. Record the device’s response to a commanded signal of 0°. Plot this response on the same figure as the simulated response.
4. Repeat Steps 2) and 3) for two different sets of controller gains, aiming to improve upon the previous responses.
5. Was the system able to reach the desired signal in any of the three tests, why or why not?
6. Were you able to produce a set of gains that resulted in a response with minimal overshoot, quick rise time, quick settling time, and low steady state error? Why or why not?

#### **4.4 Step Change Test.**

1. Using the set of gains that produced the most satisfactory response, command the device to track a signal of 0°. After the device has stabilized, command it to track a signal of 15°. Record the response starting from when the device was hovering at 0°. (You do not need to plot a simulated response in MATLAB)
2. How is this response different from that of the previous section? Why is it different?

**Note:** If doing the DIY option, complete the same steps as above but with a baseline reference signal analogous to the 0° input for the T-RECS. For step 4.4, you should start the system at the baseline then move to another reference signal analogous to the 15° input for the T-RECS. Answer all questions for the steps above. Please provide as much detail as possible to explain how your answers and results for this section were obtained for your system.

### **5. Optimal Control using LQR**

The goal of this section is to expose the students to one method of optimal control theory using a Linear Quadratic Regulator (LQR). Many complex control problems require the use of optimization to determine satisfactory gains and this section serves to introduce these advanced concepts.

**5.1:** Construct a state-space model of the system using the original model derived in Section 2, step 2. You will need to linearize the system around$$\theta=0°$$in order to simplify the result. Recall the structure of the state-space representation is:

$$
ẋ = Ax+Bu
$$

The LQR algorithm is a process involving a state “weight” matrix, Q and an input “weight” matrix, R. We will begin by analyzing the LQR’s application to a PD controller. Since this system has 2 states:$$\theta$$and$$θ˙$$, the Q matrix will be a 2x2 diagonal matrix with each component representing the “weight” or importance of the corresponding state. Since this system has 1 input,$$u$$, the R matrix will be 1x1 (scalar) with the value representing the “weight” or importance of the input.

**5.2:** Begin with an identity matrix for each and use MATLAB’s *lqr* function to compute the optimum PD gains. Using those gains, plot the simulated and actual response to a commanded input of 0°. How did this controller compare to the gains you tested earlier?

Now we will move onto the LQR+I algorithm which applies the same idea to a PID controller by adding a new state,$$\theta\_i$$, which represents the integral of$$\theta$$.

**5.3:** Modify your state-space model to include$$\theta\_i$$, which should only involve the addition of one more row to the A and B matrices.

**5.4:** Repeat the same procedure as in **5.2** except using the PID controller. How did this controller compare to the gains you tested earlier? Were you able to safely test the device using the computed PID gains?

**Note:** If doing the DIY option, complete the same steps as above but with a baseline reference signal analogous to the 0° input for the T-RECS. Answer all questions for the steps above. Please provide as much detail as possible to explain how your answers and results for this section were obtained for your system.

### **Extra Credit: Online Auto-Tuning of PID Controllers**

The goal of this section is to provide the students with an opportunity to explore machine learning in control system applications to earn extra credit on their overall project. Many systems operate in unknown environments so it is crucial for the system to adapt in real time, or “online,” and this learning process can be incorporated into PID control.

1. Read through the attached paper, “*Online Optimal Auto-Tuning of PID Controllers for Tracking in a Special Class of Linear Systems*” by M. Miranda and K. Vamvoudakis.
2. Plan an approach to incorporating the methods introduced in the paper into your device. Briefly describe your approach as to how your device, model, and/or code will apply these methods. (This may include equations, citations from the paper, pseudocode, figures, etc.)
3. Prepare some evidence of these methods being applied on your device. (This may include plots, tables, videos, pictures, code snippets, etc.). Briefly discuss your results and findings.
