Skip to content

MATLAB/Simulink — Modeling and Control Development Platform


MATLAB/Simulink is the core platform for modeling and low-level control algorithm development in the RflySim toolchain, supporting the Model-Based Design (MBD) workflow and covering the entire pipeline from model construction to automatic code generation and firmware deployment.


Role in RflySim

graph LR
    A[Simulink Modeling] --> B[Controller Design]
    B --> C[Offline Simulation Validation]
    C --> D[Automatic C/C++ Code Generation]
    D --> E[Compilation and Upload to Pixhawk]
    E --> F[HITL Hardware-in-the-Loop Simulation]
    F --> G[Real-World Deployment]
  • Dynamic Modeling: Build dynamic models for multirotors, fixed-wing aircraft, unmanned ground vehicles, and other platforms in Simulink
  • Controller Design: Design attitude and position controllers using a modular drag-and-drop workflow
  • Automatic Code Generation: Automatically convert Simulink models into C/C++ code
  • Automatic Firmware Deployment: The generated controller is loaded into PX4 as the px4_simulink_app module

Requirement Description
MATLAB Version R2022b or newer
Simulink Required core component

Key Toolboxes

Toolbox Function
Aerospace Blockset/Toolbox Aircraft dynamic modeling, mission planning, environment analysis
MATLAB Coder MATLAB code → C/C++ conversion
Simulink Coder Simulink model → C/C++ code generation
Embedded Coder Embedded code optimization and download
UAV Toolbox UAV controller design and mission planning
ROS/ROS 2 Toolbox ROS communication interface

Installation Tip

When installing MATLAB, please check Simulink, Aerospace, and all related toolboxes to ensure full functionality of the RflySim toolchain.


RflySim includes two built-in Simulink libraries:

SimulinkPSP (Pixhawk Target Blocks)

Provided officially by MathWorks, for low-level control algorithm development targeting Pixhawk/PX4 flight controllers:

Sub-library Function
ADC and Serial ADC analog-to-digital conversion reading, serial port read/write
Miscellaneous Utility Blocks Log storage, print functions, QGC parameter transfer
Sensors and Actuators Battery monitoring, remote control commands, motor control, LED, sensor data
uORB Read and Write Reading and writing of PX4 uORB messages

RflySimPSP

A high-level function library extended and developed by the RflySim team:

Sub-library Function
RflySim APIs Advanced low-level control library: attitude/velocity/position/torque read/write interfaces
RflySim Model Dynamic model library: modeling modules + external communication + 3D model communication
RflySim PHM Fault modeling and prognosis health management: battery/wind/load/GPS/motor/sensor fault injection
RflySim Swarm Swarm control library: multi-agent state acquisition, motion capture data subscription, MAVLink encoding and decoding
RflySim Vision Visual control library: subscribe visual images within Simulink to realize visual closed-loop control

MATLAB Command-Line Interfaces

RflySim provides dedicated MATLAB command-line interfaces:

Command Function
PX4Upload Upload compiled firmware to Pixhawk
PX4CMD Switch PX4 firmware versions
PX4SitlSet Generate firmware for SITL mode
PX4AppName Rename controller module (supports multiple controllers)

Automatic Code Generation Workflow

  1. Design the controller in Simulink: Build control logic using RflySimPSP/SimulinkPSP modules
  2. Generate C/C++ code with Simulink Coder: Automatically convert to embedded code
  3. Build as px4_simulink_app: Compile as an independent PX4 module
  4. Communicate via uORB message bus: Subscribe to sensor data and publish control commands
  5. Upload to Pixhawk: Use the PX4Upload command
  6. Take over actuator control: Replace the native PX4 control module

About uORB

uORB (Micro Object Request Broker) is the core publish-subscribe message bus for data transmission in the PX4 system, similar to the middleware mechanism of ROS. All sensor data, control commands, and intermediate states are transmitted between modules via uORB messages.


Example Path
Simulink modeling RflySimAPIs\4.VehicleModeling\
Controller design and code generation RflySimAPIs\5.RflySimFlyCtrl\1.BasicExps\e5-AttitudeCtrl
Automatic code generation experiments RflySimAPIs\2.RflySimUsage\1.BasicExps\