メインコンテンツ

Run ArduPilot Software-in-the-Loop Simulation with Fixed-Wing Plant in Simulink

R2026b
Since R2026b

This example shows you how to verify a fixed-wing controller design, by using software-in-the-loop (SITL) simulation, and to simulate the fixed-wing plant model in Simulink using the UAV Toolbox Support Package for ArduPilot® Autopilots.

Prerequisites

Autopilot name — ArduPilot Host Target

Board — sitl

ArduPilot hardware support package setup screen, showing Autopilot name and Board set to ArduPilot Host Target and sitl, respectively.

If you have already installed the support package, open the hardware setup screen and verify that Autopilot name is set to ArduPilot Host Target and Board is set to sitl.

Launch First MATLAB Instance

Launch the first instance of MATLAB. To access the Simulink model, supporting files, and project shortcuts that this example uses, open the ArduPlaneSITLSimulinkPlant.prj project file.

prj = openProject("ArduPlaneSITLSimulinkPlant");

Copy the current folder path of the first MATLAB instance to the clipboard.

clipboard("copy", pwd)

Fixed-Wing Dynamics Model Overview

To open the fixed-wing Plane_UAV_Dynamics.slx Simulink model, on the Project tab of the MATLAB Toolstrip, in the Shortcuts section, click the Step 1: Run Fixed-Wing Dynamics Simulation shortcut.

Step 1: Run Fixed-Wing Dynamics Simulation shortcut in the Shortcuts menu of the Projects section.

Plane_UAV_Dynamics.slx Simulink model

The Plane_UAV_Dynamics.slx Simulink model contains these main blocks and subsystems:

  • Read subsystem — Reads the PWM signals sent by the ArduPilot host target through a UDP connection.

  • Control Surfaces subsystem — Converts the PWM signals to control surface deflections.

  • Force and Moments subsystem — Calculates the forces and moments generated by the aerodynamics, propulsion system, gravity, and ground contact models based on the motor angular velocity and fixed-wing states. The subsystem passes the calculated forces and moments to the 6DOF (Euler Angles) block as inputs.

  • 6DOF (Euler Angles) (Aerospace Blockset) block — Calculates the fixed-wing states of the UAV by using six-degrees-of-freedom equations of motion.

  • Write subsystem — Sends the fixed-wing states to the ArduPilot host target through a UDP connection.

Run Fixed-Wing Plant Model in First MATLAB Instance

Get the IP address Windows uses to connect to the virtual network of the Windows Subsystem for Linux (WSL) instance running the ArduPilot flight stack. First, open the Windows Command Prompt. Then, at the command line, enter ipconfig and locate the entry for Ethernet adapter vEthernet (WSL (Hyper-V firewall)). The IP address of Windows on the virtual network is the IPv4 Address, which in this example is 172.21.176.1.

Output of ipconfig in the Windows Command Prompt. The IP address is highlighted by a red rectangle.

In the Plane_UAV_Dynamics.slx model, open the block mask of the Read subsystem. Specify the Local host parameter as the IP address of Windows on the WSL virtual network, and specify the Local port parameter as 9002. Click OK to save the changes.

Read subsystem of the Plane_UAV_Dynamics.slx model, with the Local host parameter containing the Windows virtual IP address highlighted by a red rectangle.

On the Simulation tab of the Simulink Toolstrip, in the Simulate section, select the Normal simulation mode. Then, to start the Plane_UAV_Dynamics.slx model, click Run .

Simulate section of the Simulink Toolstrip, the simulation mode dropdown and Run button highlighted by red rectangles.

For more information on connecting MATLAB to ArduPilot SITL, see the MATLAB Simulation section of the ArduPilot documentation.

Launch Second MATLAB Instance

Launch a second instance of MATLAB. Then, right-click the current working directory in the address bar context menu and paste the folder path of the first instance of MATLAB by selecting Paste and Go.

Paste and Go option in the MATLAB address bar.

To access the Simulink model, supporting files, and project shortcuts that this example uses, open the ArduPlaneSITLSimulinkPlant.prj project file.

prj = openProject("ArduPlaneSITLSimulinkPlant");

Fixed-Wing Controller Model Overview

To open the Plane_Controller.slx Simulink model, on the Project tab of the MATLAB Toolstrip, in the Shortcuts section, click the Step 2: Deploy Fixed-Wing Controller on Host Target shortcut.

Step 2: Deploy Fixed-Wing Controller on Host Target shortcut on the Project tab of the MATLAB Toolstrip.

Plane_Controller.slx Simulink model

The Fixed Wing Controller subsystem calculates the normalized throttle and servo actuator setpoints, which it passes to the ArduPlane Actuator Write block as inputs. The ArduPlane Actuator Write block writes normalized throttle and servo commands to the throttle, aileron, elevator, and rudder of the UAV.

The Fixed Wing Controller subsystem obtains the airspeed, altitude, and course setpoints from Constant blocks, which you can adjust using the Airspeed SP:Value, Altitude SP:Value, and Course SP:Value sliders, respectively, during simulation. The subsystem also accepts the L1 distance, which specifies the distance of the lookahead point used by the lateral controller.

The Fixed Wing Controller subsystem obtains state feedback from the Ground Truth Bus subsystem, which aggregates feedback from these blocks:

To visualize the simulation result, the Plane_Controller Simulink model uses Dashboard Scope (Simulink) blocks that visualize the setpoint and feedback signals for airspeed, altitude, and course.

Fixed Wing Controller Subsystem

The Fixed Wing Controller subsystem contains the Airspeed-Altitude Controller, Lateral Guidance Logic, and Attitude Controller subsystems.

Airspeed-Altitude Controller Subsystem

The Airspeed–Altitude Controller subsystem calculates the throttle and pitch setpoints based on airspeed and altitude tracking errors. It uses PID Controller (Simulink) blocks to generate throttle and pitch commands that enable the UAV to maintain the specified airspeed and altitude.

Lateral Guidance Logic Subsystem

The Lateral Guidance Logic subsystem calculates the roll setpoint, which it passes to the Attitude Controller subsystem as input. The Lateral Guidance Logic subsystem uses the L1 guidance law to determine the required lateral acceleration based on the current course, course setpoint, ground speed, and distance to the lookahead point [1].

Attitude Controller Subsystem

The Attitude Controller subsystem computes normalized aileron, elevator, and rudder commands to track the commanded roll, pitch, and yaw. It uses PID controllers to regulate attitude and angular rates, and includes a turn‑coordination algorithm implemented in a MATLAB Function block to compute the rudder command required for coordinated, no‑slip flight.

Verify Hardware Settings

On the Simulink Toolstrip of the Plane_Controller.slx model, on the Hardware tab, click Hardware Settings . In the left pane, select Hardware Implementations, and verify these options:

  • Hardware board is set to ArduPilot Host Target.

  • In the Hardware board settings section, under Target hardware resources, select the Build options tab of the Groups pane. Verify that Build action is set to Build, load and run, and Board is set to sitl.

  • Select the Vehicle tab of the Groups pane. Verify that Simulator is set to Simulink.

Plane_Controller.slx model settings, showing the Build options tab.

Plane_Controller.slx model settings, showing the Vehicle tab.

Click OK to save any changes and close the Configuration Parameters dialog box.

Run Fixed-Wing Controller Model in Second MATLAB Instance

To run the Plane_Controller.slx model in Monitor & Tune mode:

1. From the Simulink Toolstrip, select the Hardware tab.

2. Verify that the Mode section contains a Run on board button. If it instead contains a Connected IO button, click Connected IO, then Run on board (External mode).

3. In the Run on Hardware section, click Monitor & Tune .

Run on board (External mode) and Monitor & Tune buttons in Simulink toolstrip.

Start Simulation

The MAVProxy console and Windows terminal launch after you run both Simulink models. Wait until the MAVProxy console shows the message pre-arm good before proceeding to the next step.

MAVProxy console that shows pre-arm process is completed.

Arm the UAV by entering the command arm throttle in the Windows terminal.

Windows terminal with the arm throttle command.

After arming is complete, the UAV takes off and reaches the specified altitude and airspeed. You can then change the setpoints using the sliders. Use the sliders to adjust the airspeed, altitude, and course. The dashboard scope displays both the setpoints and the actual signal values.

Plane_Controller model after the UAV takes off.

To end the simulation after the mission has finished, first stop the Plane_UAV_Dynamics.slx Simulink model, then stop the Plane_Controller.slx Simulink model. If you instead stop the Plane_Controller.slx Simulink model first, then you must press Ctrl+C to stop the Plane_UAV_Dynamics.slx Simulink model.

References

[1] Park, Sanghyuk, John Deyst, and Jonathan How. “A New Nonlinear Guidance Logic for Trajectory Tracking.” Paper presented at AIAA Guidance, Navigation, and Control Conference and Exhibit, Providence, Rhode Island. AIAA Guidance, Navigation, and Control Conference and Exhibit, American Institute of Aeronautics and Astronautics, August 16, 2004. https://doi.org/10.2514/6.2004-4900.

See Also

Topics