Main Content

PX4 Hardware-in-the-Loop (HITL) Simulation with Fixed-Wing Plant in Simulink

This example shows how to use the UAV Toolbox Support Package for PX4® Autopilots to verify the controller design by deploying the design on the Pixhawk hardware board. This is done in HITL mode with fixed-wing UAV Dynamics contained in Simulink®.

This example provides a reference model that helps you to design a flight controller algorithm to perform take-off, waypoint-following, loitering, and landing for a fixed-wing UAV plant model. Additionally, the example also provides a plant model which captures aspects such as aerodynamics, propulsion, ground contact, and sensors.

For information on fixed-wing plant model architecture, guidance logic, and conventions, see Fixed-Wing Plant and Controller Architecture.


Required Third-Party Software

Required Hardware

  • Pixhawk Series flight controller

  • Micro USB (Universal Serial Bus) type-B cable

  • Micro-SD card

  • Serial-to-USB FTDI converter

  • Mini USB cable for FTDI

  • Pixhawk serial port connectors

Run HITL Simulation

Note: Avoid running this example on a virtual machine. The virtual machine might display an error saying "unable to pace at specified rate" and the drone might crash.

Step 1: Make Hardware Connections and setup the Pixhawk in HITL mode

This diagram shows the HITL setup and the physical communication between various modules.

1. Connect your Pixhawk board to the host computer using the USB cable.

2. Configure the Pixhawk board in HITL mode as described in Setting up PX4 Autopilot in Hardware-in-the-Loop (HITL) Mode from QGroundControl.

3. Set up the PX4 Firmware as described in Set Up PX4 Firmware for Hardware-in-the-Loop Simulation.

Step 2: Open MATLAB Project

1. Open MATLAB.

2. Open the example MATLAB project by executing this command at the MATLAB command prompt:


3. Once you open the project, click the Project Shortcuts tab on the MATLAB toolstrip and click Launch Controller Model to launch the FixedWingGNC controller.

Alternatively, execute this command at the MATLAB command prompt.


4. In the Project Shortcuts tab, click Open UAV Dynamics to launch the Simulink plant model named UAV_Dynamics_Autopilot_Communication.

Alternatively, execute this command at the MATLAB command prompt.


5. In the Simulink plant model, set the serial ports in MAVLink Bridge Source and MAVLink Bridge Sink blocks.

Step 3: Configure Simulink Controller Model for HITL Mode

1. Follow the instructions as described in Configure Simulink Model for Deployment in Hardware-in-the-Loop (HITL) Simulation.

Note: These steps are not required in the pre-configured model. Complete these steps if you have changed the hardware or not using the pre-configured model.

2. Click Build, Deploy & Start from Deploy section of Hardware tab in the Simulink Toolstrip of the Controller model FixedWingGNC.

This deploys the controller code on the Pixhawk 4 and launches the QGroundControl (QGC).

Note: If you are using Ubuntu, QGC might not launch automatically. To launch QGC, open Terminal and go to the location where QGC is downloaded and run the following command:


Step 4: Run the UAV Dynamics Model, Upload Mission from QGroundControl and Fly UAV

1. In the Simulink toolstrip of the Plant model (UAV_Dynamics_Autopilot_Communication), on the Simulation tab, click Run to simulate the model.

2. Enable the Joystick in QGC:

Waypoints are hardcoded and stored in a InitWaypoints.m data file. For more information on the structure, see MissionData UAV Path Manager. As the waypoints are hardcoded, do not add a mission in QGC. Enable Virtual Joystick in QGC Application Settings as shown below,

3. Change the flight mode to Manual in the QGC.

Note: The mission is hardcoded in MATLAB and is executed by the GNC model once armed. It is not possible to arm the UAV in the mission mode.

4. Drag the left joystick to the bottom-right corner for a couple of seconds to arm the UAV. The flight starts to execute.

An image of the expected flight trajectory is shown here.