An Introduction to Automatic Code Generation Using RflySim and Simulink

An Introduction to Automatic Code Generation Using RflySim and Simulink

RflySim adopts the Model-Based Design (MBD) approach, which can be used for the control and safety testing of unmanned systems. It involves five stages: modeling, controller design, Software-In-the-Loop (SIL) simulation, Hardware-In-the-Loop (HIL) simulation, and real-flight testing. With the automatic code generation technology of MATLAB/Simulink, the controller can be easily and automatically deployed onto the hardware for HIL simulation and real-flight testing.

In MATLAB/Simulink, there are interfaces available for the design of multirotor controllers, allowing users (including beginners, students, or engineers) to rapidly design and validate controllers using their acquired knowledge. Once the controller design is completed, this platform offers code generation and download functionality, which can convert the designed Simulink control algorithm into C/C++ code, compile it into the PX4 autopilot firmware, and automatically download it to the autopilot. This platform also provides Hardware-In-the-Loop (HIL) simulation testing capabilities, enabling users to perform initial simulated testing of flight performance on a real Pixhawk autopilot system, identifying and addressing various potential issues before actual flight experiments. Once the testing is successful, the Pixhawk autopilot can be integrated into a multirotor hardware system, allowing for indoor and outdoor flight experiments, and performance evaluation of the designed control algorithms through experimental validation.

Basic Framework

The Pixhawk Pilot Support Package (PSP), an official toolbox from MathWorks for Pixhawk, is designed to facilitate the development and deployment of autopilot algorithms using Simulink. This toolbox offers several key features:
Simulation and Testing: PSP allows users to simulate and test various aircraft models and autopilot algorithms within Simulink. It automates the deployment of these algorithms to the Pixhawk hardware.
Example Models: The toolbox includes practical examples, such as light control, remote control data processing, and attitude controllers.
Interface Modules: It provides numerous interface modules for accessing both the software and hardware components of the Pixhawk.
Data Logging: PSP can automatically record flight data from sensors, actuators, and deployed controllers.
uORB Messaging: The toolbox supports subscribing to and publishing uORB topic messages. All data in the PX4 autopilot software is temporarily stored in a uORB message pool. Subscribing allows you to read specific topics of interest from this pool, while publishing enables you to add specific topics for use by other modules.
The PX4 software system is composed of several independent modules, each running as separate threads and communicating through the uORB messaging system. When Simulink-generated code is deployed to the PX4 autopilot, it doesn’t affect the native PX4 software but instead runs as an independent module called “px4_simulink_app.” This module operates alongside other modules in a multi-threaded environment. To avoid conflicts due to multiple modules accessing the same hardware resources, the deployment script provides an option to automatically block the native PX4 firmware from controlling actuators, ensuring that only the “px4_simulink_app” module can output motor control commands.
This approach allows for the integration of Simulink-generated control algorithms without interfering with the PX4’s native functionality, ensuring a seamless and isolated operation of the autopilot.

The PSP toolbox generates C code for control algorithms designed in Simulink. This code is then integrated into the source code of the PX4 software system, creating a standalone program named “px4_simulink_app.” The toolbox utilizes a compilation tool to compile all the code into a PX4 software firmware file with a “.px4” extension. This firmware is then downloaded and flashed onto the flight controller, allowing the flight controller to execute the PX4 software containing the newly generated algorithm code.

Simulink Configuration Interface

The Embedded Coder module in MATLAB/Simulink is capable of generating readable, compact, and fast C and C++ code suitable for embedded processors used in large-scale production. It extends the functionality of MATLAB Coder and Simulink Coder, allowing precise control over the generated functions, files, and data through advanced optimizations. These optimizations enhance code efficiency and aid in the integration with existing code, data types, and calibration parameters. Third-party development tools can be integrated to build a full deployment executable for embedded systems or rapid prototyping boards.
To generate code for Pixhawk series flight controller hardware, follow these settings in Model Configuration Parameters within Simulink after completing the construction of your model:

  1. Set the solver type to “Fixed-step” and choose the solver as “Discrete,” which means selecting a discrete fixed-step solver.
  2. In the “Hardware Implementation” section, configure the onboard hardware to be “Pixhawk PX4.” Select the operating system as “Nuttx,” which is supported by the PX4 software system. Set the clock frequency to 250. For the “Target hardware resource” compilation settings, choose “Build only.”
  3. In the “Code Generation” settings, select the system target file as “ert.tlc” from Embedded Coder and choose the C language as the language for code generation. Set the toolchain to “Pixhawk Toolchain,” and configure the compilation settings for “Faster Builds.”
  4. In the “Code Generation” interface settings, configure the code’s dependency library as “GCC ARM Cortex-A9.” For the “support” settings, make the configurations as shown in the provided image.

PX4 Code Modification Interface

The one-click installation script for the RflySim platform makes some modifications to the official PX4 source code to ensure better compatibility with the platform. The core modifications include:

  1. In *\PX4PSP\Firmware\boards, locate the corresponding compilation command CMake file, such as “px4_fmu-v6x_default” in *\PX4PSP\Firmware\boards\px4\fmu-v6x\default.cmake. Add module compilation registration for “px4_simulink_app” in this file.
  2. In *\PX4PSP\Firmware\ROMFS\px4fmu_common\init.d\rcS startup script, add automatic startup for “px4_simulink_app.”
  3. In *\PX4PSP\Firmware\src\modules directory, create a “px4_simulink_app” folder and generate source code that meets the compilation requirements.
  4. Modify the source code under Firmware to disable PX4’s native motor outputs, allowing “px4_simulink_app” to gain control over the motors.

Custom Source Code Import Interface

The installation package of the RflySim platform contains the “2.FirmwareZip” directory, which stores various PX4 source code firmware versions and supports importing your own developed PX4 source code. When the one-click installation script redeploy the firmware, it first deletes the \PX4PSP\Firmware folder. Then, based on the selected option, it extracts “2.FirmwareZip\PX4Firmware**.zip” into the \PX4PSP directory. Finally, it extracts the contents from “" and forcefully overwrites them into the Firmware directory.
The "PX4Firmware
*.zip” contains the official source code, downloaded from GitHub, without any modifications. "" contains the files we’ve modified, which will override the Firmware directory. To deploy your own source code, you can do it in two ways:
Directly compress your modified Firmware directory and rename it according to your version as a file in PX4Firmware
**.zip format (naming rules can be found in “2.FirmwareZip\readme.txt”). Delete the PX4Firmware* file. In this case, the one-click installation script will use your customized script for deployment.
You can also place your modified source code directly inside PX4Firmware according to the directory structure. During deployment, it will be copied in and forcefully replace the original code.
The RflySim platform also supports other PX4 firmware versions, such as 1.9.2, 1.10.2, and more, as indicated in the “2.FirmwareZip\readme.txt” file, as shown in the provided image.

For more detailed information, please visit . The basic and advanced trial versions of RflySim can be obtained by providing your email address through the download link at . For the full-featured complete version, please inquire at .