This example shows how to use the UAV Toolbox Support Package for PX4 Autopilots to verify the controller design using PX4 Host Target versus the simulator designed in Simulink®.
The UAV Toolbox Support Package for PX4 Autopilots enables you to use Simulink to design a flight controller algorithm to stabilize the vehicle based on the current vehicle attitude, position, and velocity and also track the desired attitude using Simulink. The MAVlink blocks in the UAV Toolbox enable you to read and write the MAVLink HIL_* messages and design the plant dynamics.
This example shows how to validate a position controller design on a medium-sized quadrotor plant using a single Simulink model, and then take the same controller and plant model and simulate it with the PX4 source code in, what the PX4 community calls it, Software In The Loop (SITL) simulation.
If you are new to Simulink, watch the Simulink Quick Start video.
Perform the initial Setup and Configuration tasks of the support package using Hardware Setup screens. In the Select a PX4 Autopilot and Build Target screen, select
PX4 Host Target as the PX4 Autopilot board from the drop-down list.
For more information on how to verify the controller design using the jMAVSim simulator, see Deployment and Verification Using PX4 Host Target and jMAVSim and Position Tracking for X-Configuration Quadcopter Using Rate Controller.
For more information on designing the controller model and verifying it using the simulator plant model designed in Simulink, see Integrate Simulator Plant Model Containing MAVLink Blocks with Flight Controller Running on PX4 Host Target.
To get started, launch the Simulink project by entering this command at the MATLAB command prompt:
Once the project launches, it will load the required workspace variables and open the top model.
This project consists of the following models:
The top model,
QuadcopterSimulation, consists of multiple subsystems and model references.
The Controller subsystem contains the
FlightController model reference, which contains the position and attitude controller. The controller designed in this example follows the same structure as described in Position Tracking for X-Configuration Quadcopter Using Rate Controller.
The Plant and Visualization subsystem contains the model reference
Quad_Plant_dynamics model reference, which has the Quadcopter plant and sensor dynamics.
px4Demo_FlightController_top is the harness model that contains the
FlightController model reference. The model is set up to run with the PX4 source code by creating a PX4 Host Target executable.
Quad_Plant_top harness model contains the
Quad_Plant_dynamics model reference. This harness model is set up to run in normal simulation pacing mode in lockstep with the
px4Demo_FlightController_top harness model.
The Project shortcuts guide you through the three tasks as you progress through the example.
1. Open the example project Px4DemoHostTargetWithSimulinkPlantStart.
QuadcopterSimulation model opens when the project starts. You can also open the model by clicking
Run Quadcopter full simulation in the Projects Shortcut tab.
2. Navigate through the different subsystems to learn about the model hierarchy and quadcopter dynamics.
3. The Controller and the Plant and Visualization subsystems exchange the HIL_ACTUATOR_CONTROLS, HIL_SENSOR, HIL_GPS, and HIL_STATE_QUATERNION MAVlink messages as described in Integrate Simulator Plant Model Containing MAVLink Blocks with Flight Controller Running on PX4 Host Target.
4. The Commands subsystem provides the desired X, Y, Z coordinates and the yaw values for the quadcopter.
5. To simulate the model, go to the Simulation tab in the Simulink model window and click Run. The lower-left corner of the model window displays status while Simulink prepares to run the model on the host computer.
6. Observe that the quadcopter hovers at an altitude commanded by the Dashboard slider block.
7. Click the Circular Guidance toggle switch to observe the quadcopter following a circular trajectory.
8. You can also bring up the UAV Animation window by clicking on the Show animation button on the UAV Animation block. To locate the block, go to Plant and Visualization > Quad_Plant_Dynamics > Visualization Subsystem.
9. Note that the controller gains are optimized for close tracking of a circular trajectory. It may be possible that for other types of input commands such as step or ramp, the response is not ideal. In such a scenario, the controller gains must be adjusted to meet the specified requirements. For more details on the controller tuning, see Position Tracking for X-Configuration Quadcopter.
In this task, we designed the multicopter plant and controller, and ensured that its performance is satisfactory in simulation.
In this task, we use the controller designed in Task 1 and run it with PX4 source code using the PX4 Host Target feature (for more information, see Integrate Simulator Plant Model Containing MAVLink Blocks with Flight Controller Running on PX4 Host Target).
1. Click the Monitor and Tune Host Target controller in the Project Shortcuts tab to open the
The model block references the
FlightController model, which was used in the
The uORB Read blocks are used to subscribe to the
vehicle_attitude_groundtruth topics. These topics contain data sent by the simulator plant model using HIL_STATE_QUATERNION and HIL_GPS messages.
The Signal Conditioning subsystem inside the FlightController model reference extracts the current position, current velocity, and current attitude data and feeds it to the Controller subsystem. The Controller subsystem designs the Rate controller and the Position and Attitude controller as explained in Position Tracking for X-Configuration Quadcopter Using Rate Controller.
FlightController model outputs the actuator values that are then fed to the PX4 PWM Output block.
2. In Configuration parameters > Hardware Implementation, set the Hardware board parameter to
PX4 Host Target.
3. Under Target hardware resources > Build Options, set Simulator to
4. In the Simulation tab, set the Simulation Stop time to inf.
5. On the Hardware tab, in the Mode section, select Run on board and then click Monitor & Tune to start signal monitoring and parameter tuning.
6. Wait for Simulink to complete the code generation. Click OK on the dialog box that appears and perform the next task.
In this task, we use the same plant dynamics that were used in Task 1 but in a separate harness model and then run the model in lockstep with the controller model described in Task 2.
Run quadcopter plant in Normal simulation in the Project Shortcuts tab to open the
The model block references the
Quad_Plant_dynamics model, which was used in
2. Ensure that the Simulation Pacing option for this model is enabled, as described in Integrate Simulator Plant Model Containing MAVLink Blocks with Flight Controller Running on PX4 Host Target.
3. In the Simulation tab, set the Simulation Stop Time to inf.
4. Click Run on the Simulation Tab.
You can see that the
Quad_Plant_top model and
px4Demo_FlightController_top model run in lockstep with each other. The
px4Demo_FlightController_top model runs in External mode.
5. Use the Dashboard slider and toggle switch in the
px4Demo_FlightController_top model to command the quadcopter. The corresponding flight path appears in
TCP read from PX4 Host Target MATLAB System block is used to read the MAVLink data sent from the
7. The Enabled subsystem has the MAVLink Deserializer block that extracts the HIL_ACTUATOR_CONTROLS message.
TCP write to PX4 Host Target MATLAB System block sends the HIL_SENSOR, HIL_GPS, and HIL_STATE_QUATERNION MAVLink messages to the
9. To stop the combined simulation, first stop the
px4Demo_FlightController_top model running in External mode, and then stop the
Quad_Plant_top model running in simulation.
You can reuse the
TCP Read from PX4 Host Target and the
TCP Write to PX4 Host Target MATLAB System blocks from this model in the simulator plant model that you design.
While running the combined simulation, pause and step forward the
Quad_Plant_top model running in simulation and to see that the
px4Demo_FlightController_top model also pauses as both the models are in lockstep. Design the controller and plant dynamics for a different configuration (for example, Hexacopter or Fixedwing), and run them in combined simulation in lockstep.