Main Content

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

This example shows how to use the UAV Toolbox Support Package for PX4® Autopilots and take manual control inputs from Joystick / RC Transmitter and control the fixed-wing flight.

Before starting with Simulink®, ensure that you go through PX4 Hardware-in-the-Loop (HITL) Simulation with Fixed-Wing Plant in Simulink example and perform Step 1.

Launch MATLAB

  1. Open MATLAB®.

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

    openExample('px4/PX4HITLSimulationFixedWingPlantSimulinkExample')
  3. Once you open the project, click the Project Shortcuts tab on the MATLAB toolstrip and click Launch Manual Control Mode to launch the ManualControl controller.

    Alternatively, execute this command at the MATLAB command prompt.

    open_system('ManualControl')

    Manul control model

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

    Alternatively, execute this command at the MATLAB command prompt.

    open_system('UAV_Dynamics_Autopilot_Communication')
  5. In the Simulink plant model, set the serial ports in MAVLink Bridge Source and MAVLink Bridge Sink blocks.

  6. Click Build, Deploy & Start from Deploy section of Hardware tab in the Simulink Toolstrip of the controller model ManualControl.

Run the Simulink Plant Model, Configure QGroundControl and Fly UAV

  1. Run the Simulink plant model (UAV_Dynamics_Autopilot_Communication).

  2. Open QGC and connect to PX4.

  3. In the QGC, navigate to Vehicle Setup > Airframe.

    Airframe selection

    1. Select the Generic Standard Plane.

    2. Click Apply and Restart at the top right corner of the window.

    3. Reboot the Vehicle for the changes to take effect. This will stop the Simulink plant model.

    4. Run the Simulink plant model again.

  4. In the QGC, navigate to Vehicle Setup > Safety and perform these steps.

    1. In the Hardware in the Loop Simulation section, select HITL enabled option.

    2. Enable HITL

    3. Close the QGC and Reboot the vehicle manually.

      This will stop the Simulink plant model

    4. Run the Simulink plant model again and relaunch the QGC.

  5. In the QGC, navigate to Vehicle Setup > Actuators and choose the servos and motors for the channels as shown in this image.

    Actuator setup

    Note

    The sequence should match the sequence used in the Simulink plant model. You can see the sequence under UAV_Dynamics_Autopilot_Communication/UAV Dynamics with MAVLink Serialization UAV_Dynamics_DataSerialization/UAV Dynamics (UAV_Dynamics)/Dynamics model/Force and Moment Calculation/Actuator Model subsystem.

  6. In the QGC, navigate to Vehicle Setup > Parameters.

    1. Set the input device in COM_RC_IN_MODE parameter.

      • If you are using a RC Transmitter, Select RC Transmitter only option. For information on connection and calibration, see Radio (Remote Control) Setup.

      • If you are using a gamepad or joystick on the host, select Virtual RC by Joystick option. For more information, see Joystick Setup.

      Select input device

    2. Set the parameters COM_DISARM_LAND and COM_DISARM_PRFLT to -1.

  7. Click Radio and move each sticks that control aileron, elevator, rudder, and throttle. Observe the channel value changes in the Channel Monitor.

    Radio setup

    If the mapping is incorrect in the example model or between the channel number and controlled axes, correct it.

  8. Arm the vehicle from QGC and move the left stick towards right-bottom to arm the vehicle. Provide only the throttle for the drone to takeoff.

Limitations

The moments along roll, pitch, and yaw axis exerted by the ground is not modelled. High control inputs can make the simulated plant turn to improbable angles.