PID Autotuning for UAV Quadcopter
This example shows you how to automatically obtain a set of quadcopter controller gains to improve the performance and reduce delivery time. <to tidy up>
Getting Started
The example uses the same quadcopter controller model as the UAV Package Delivery example. To get started, open the scdUAVPIDAutotuning Simulink® project <what does the project do? mention shortcut>
prj = openProject('scdUAVPIDAutotuning');Click the Getting Started project shortcut, which sets up the Simulink model with a high-fidelity quadcopter plant model and to set up a four-waypoint mission.

UAV Package Delivery Model Overview
Open the uavPIDAutotuning.slx Simulink model.
open_system("uavPIDAutotuning.slx");

The uavPIDAutotuning.slx Simulink model contains the Multirotor subsystem that contains the plant model and controller model.
The top model consists of the following subsystems and model references:
Ground Control Station — Used to control and monitor the aircraft while in flight.
External Sensors - Lidar & Camera — Used to connect to a previously-designed scenario or a photorealistic simulation environment. These produce Lidar readings from the environment as the aircraft flies through it.
On Board Computer — Used to implement algorithms meant to run in an onboard computer independent from the Autopilot.
Multirotor — Includes a low-fidelity and mid-fidelity mode, as well as a flight controller including its guidance logic.
The model's design data is contained in a Simulink data dictionary in the data folder (uavPackageDeliveryDataDict.sldd). Additionally, the model uses variant subsystems to manage different configurations of the model. Variables placed in the base workspace configure these variants without the need to modify the data dictionary. For more details on variant subsystems, see Variant Subsystem (Simulink).
Quadcopter Controller Subsystem Overview
Open the controller subsystem.
blockpath = Simulink.BlockPath("MultirotorModel/Inner Loop and Plant Model/High-FidelityModel/Control System/Controller/motors_refout");
open(blockpath)

Position Controller
Text for position controller.
Command to open
baseline gains
Attitude Controller
Text for attitude controller
Command to open
baseline gains
Yaw Controller
Text for yaw controller
Command to open
baseline gains
Simulate with Baseline Controller gains
To simulate the quadcopter in a mission with four waypoints, Run the uavPIDAutotuning.slx Simulink model. Observe that the quadcopter takes almost 150 seconds to complete the mission.

Set Up Model for Tuning
This example shows you how to tune these controller gains to improve the time for quadcopter to complete the mission :
Pitch rate controller — Kp, Ki, Kd
Roll rate controller — Kp, Ki, Kd
Pitch angle controller — Kp
Roll angle controller — Kp
X velocity controller — Kp, Ki
Y velocity controller — Kp, Ki
X position controller — Kp
Y position controller — Kp
Click the Autotune PID Controllers project shortcut, which sets up the model to hover at a low altitude and automatically tune the four PID Controllers, then runs the same four-waypoint mission from first step.
<image of shorcut>
This diagram shows how the eight controllers are set up with the Closed-Loop PID Autotuner blocks in order to perform autotuning. (REMAKE THIS DIAGRAM)

The Closed-Loop PID Autotuner blocks inject perturbation signals to the output of each of the eight existing PID Controllers. The autotuners then use the feedback signals and the output of the PID Controllers in order to perform the autotuning process. With the exception of the innermost control loops, pitch and roll rate, the two axes being controlled are decoupled from each other. For example, the x velocity and the y velocity loops are decoupled from each other. This allows you to tune these two loops simultaneously which reduces the overall time to perform autotuning. For the pitch rate and roll rate loops, tune the control loops sequentially because they are coupled. This results in the following sequence for tuning the PID Controllers:
Pitch Rate
Roll Rate
Pitch and Roll
X and Y Velocity
X and Y Position
Setting autotuner blocks
Intro about how autotuner find gains based on bandwidth and phase margin
Bandwidth is the frequency at which the closed-loop magnitude response is equal to -3 dB and/or the phase is shifted by -45 degree, as illustrated in the following bode plot. a higher closed-loop bandwidth corresponds to a faster rise time, therefore a more responsive controller.

Phase margin is the amount of change in open-loop phase needed to make a closed-loop system unstable. The phase margin is the difference in phase between −180° and the phase at the gain cross-over frequency that gives a gain of 0 dB, as illustrated in the following bode plot. In this example, the phase margin for each loop should be set to 60 degrees as this value is typically a good compromise between performance and damping.

The Closed-Loop PID Autotuner blocks for each control loop are set up with different performance criteria depending on the control loop. For cascaded control, such as that used in this example, the inner loop should have a higher bandwidth than the outer loop to avoid instabilities. For this example, that means the pitch and roll rate control loops have the highest bandwidth while the x and y position control loops have the slowest bandwidths.
In addition, sample time also required. Summarize this: To maximize performance, the bandwidth for the pitch and roll rate loops is set to 50 rad/sec. The sampling time of the UAV control system is 0.005 seconds and the Closed-Loop PID Autotuner requires that the bandwidth must satisfy , which means that bandwidth must be 60 rad/sec or less. Choose the bandwidth such that it is less than the required 60 rad/sec. The other bandwidths are set to be as large as possible while not causing stability issues with inner loops.
Position Controller Subsystem Autotuner
<Command to open position controller subsystem>
Subsystem contains tuner for position and velocity controller
Verify these settings used for the x and y position loops are:
Bandwidth — 1 rad/sec
Phase margin — 60 degrees
Perturbation amplitude — 0.1
Values are stored in xxx and yyy dictionary variables
<Block screenshot>
The settings used for the x and y velocity loops are:
Bandwidth — 5 rad/sec
Phase margin — 60 degrees
Perturbation amplitude — 0.02
Values are stored in xxx and yyy dictionary variables
<Block screenshot>
Attitude Controller Subsystem Autotuner
<Command to open position controller subsystem>
The settings used for the pitch and roll loops are:
Bandwidth — 20 rad/sec
Phase margin — 60 degrees
Perturbation amplitude — 0.1
Values are stored in xxx and yyy dictionary variables
<Block screenshot>
The settings used for the pitch and roll rate loops are:
Bandwidth — 50 rad/sec
Phase margin — 60 degrees
Perturbation amplitude — 0.001
Values are stored in xxx and yyy dictionary variables
<Block screenshot>
Run Autotuning Mission
Run the uavPIDAutotuning model, which shows the multirotor takeoff, hover, autotune the PID Controllers, fly, and land in a 3-D plot.

During the autotuning process the gains are updated for the eight controllers:
Pitch rate — Kp = 0.00425, Ki = 0.01479, Kd = 0.0000045, N = 398
Roll rate — Kp = 0.003477, Ki = 0.01215, Kd = 0.0000031, N = 398
Pitch angle — Kp = 19.38
Roll angle — Kp = 18.95
X velocity — Kp = 0.5153, Ki = 0.2581
Y velocity — Kp = 0.5201, Ki = 0.2979
X position — Kp = 0.9365
Y position — Kp = 0.9291
Using Data Inspector to Compare
Use the Simulation Data Inspector to visualize the UAVState output of the multirotor model.

As you can see, the multirotor hovers for a period of time in order to perform autotuning. After the autotuning process is complete, around 185 seconds into the simulation, the multirotor follows the same four-waypoint path as in project first step, but the quadcopter is able to complete the path in a much shorter time due to the tuned gains increasing performance.


These plots show the position and attitude responses for the multirotor over the path. The blue line shows the multirotor performance with the baseline set of gains while the red line shows the multirotor performance with the tuned gains. With the tuned set of gains, the multirotor is able to complete the path in about 45 seconds. Meanwhile, with the baseline set of gains, the multirotor takes almost 150 seconds.
When you are done exploring the models, close the project file.
close(prj)