Main Content

PX4 Stock Autopilot in HITL Simulation with UAV Dynamics modeled in Simulink

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

The UAV Toolbox Support Package for PX4 Autopilots enables you to use Simulink to design a flight controller algorithm to follow the mission set in the QGroundControl (QGC). Additionally, you can design the UAV Dynamics in Simulink and simulate sensor values and use the same to communicate with Autopilot in HITL mode instead of third-party simulators.


Required Third-Party Software This example requires this third-party software:

Required Hardware To run this example, you will need the following 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

Make Hardware Connections and setup the Pixhawk in HITL Mode

The diagram below illustrates the HITL setup and the physical communication between various modules.

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

2. Ensure that you have configured the Pixhawk board in HITL mode as documented in Setting Up PX4 Autopilot in Hardware-in-the-Loop (HITL) Mode from QGroundControl.

Open MATLAB Project and Controller and UAV Dynamics Model

The support package includes an example project having the PX4 controller and the UAV to follow the mission set in the QGroundControl (QGC).

1. Open MATLAB.

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


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

Upload Firmware to PX4 Autopilot

1. Select and download the firmware file for your Pixhawk board from PX4 github. For example, download px4_fmu-v5_multicopter.px4 for Pixhawk 6x.

2. Upload the downloaded firmware to PX4 Autopilot. For steps to upload the firmware, see Loading Firmware.

Run the UAV Dynamics Model, Upload Mission from QGroundControl, and Fly the 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. In the QGC, navigate to the Plan View.

3. Create a mission. For information on creating a mission, see Plan View.

After you create a mission, it is visible in QGC.

4. Click the Upload button in the QGC interface to upload the mission from QGroundControl.

5. Navigate to Fly View to view the uploaded mission.

6. Start the Mission in QGC. The UAV should follow the mission path.