Abstract
In control systems engineering, MATLAB/Simulink remains the standard for high-fidelity physical modeling, while Python has emerged as the premier ecosystem for Reinforcement Learning (RL). This paper presents a framework that bridges these domains by compiling Simulink models into C-based Dynamic Link Libraries (DLLs) for use within Python's Gymnasium environment. Using the Quanser Aero 2 dual-rotor system as a benchmark, we demonstrate that this approach allows for the utilization of modern RL libraries like Stable Baselines3 (SB3), resulting in superior control performance and faster development cycles compared to native MATLAB RL tools.
Introduction
Engineers designing complex control systems often face a significant "software silo" problem. MATLAB and Simulink provide unparalleled tools for block-diagram modeling and hardware-in-the-loop (HIL) simulation. Conversely, the rapid advancement of Artificial Intelligence is largely centered on Python, which hosts powerful RL frameworks such as Stable Baselines3 and Gymnasium.
Historically, researchers had to choose between the less flexible RL toolsets within MATLAB or the arduous task of manually rewriting complex Simulink physics equations into Python code. This paper addresses this friction by introducing a systematic method to export Simulink models as standalone binaries, enabling high-performance RL training in Python without sacrificing the fidelity of the original model.
Methodology
The proposed integration framework follows a four-step process to transition a graphical Simulink model into a Python-compatible environment:
- C-Code Generation: Utilizing Simulink Coder, the graphical block diagram is translated into optimized C-code.
- DLL Compilation: This C-code is compiled into a Dynamic Link Library (DLL), creating a standalone binary that operates independently of the MATLAB environment.
- Python Interfacing: The
ctypeslibrary is used to wrap the DLL, allowing Python to invoke essential simulation functions such asinitialize,step(to advance the simulation state), andterminate. - Gymnasium Wrapping: The interface is encapsulated within a standard Gymnasium environment. This abstraction allows the Simulink model to be treated as a standard RL environment, compatible with any Python-based agent.
Experimental Setup
The framework was validated using the Quanser Aero 2, a dual-rotor laboratory helicopter designed for aerospace control research. The primary task was a 1-Degree of Freedom (1-DOF) balancing challenge, where the RL agent was required to adjust motor voltages to maintain a specific beam angle.
Training was conducted in the simulated environment derived from the compiled Simulink model. Following training, the resulting policy was deployed on physical Quanser Aero 2 hardware using the Quanser Python API and an HIL card to evaluate the efficacy of the "Sim-to-Real" transfer.
Key Results and Discussion
The experimental results highlighted several advantages of the Python-based approach over traditional MATLAB-based RL workflows:
1. Performance and Accuracy
The Python-trained agent, utilizing the default Proximal Policy Optimization (PPO) implementation from Stable Baselines3, significantly outperformed previous benchmarks. The agent achieved a mean average deviation from the target tilt of only 4.6°, a marked improvement over the 5.6° deviation reported in prior MATLAB-based studies. Furthermore, the highest return reached in simulation was -64.87, compared to the previous benchmark of -77.93.
2. Algorithmic Robustness
A notable finding was the robustness of Python's RL implementations. While MATLAB’s default PPO implementation failed to solve the balancing task entirely without extensive tuning, the SB3 PPO agent succeeded using standard hyperparameters. This suggests that the Python ecosystem may offer more mature or better-optimized implementations of core RL algorithms for this class of control problems.
3. Computational Efficiency
By bypassing the MATLAB GUI and execution engine, the DLL-based simulation loop proved to be highly efficient and portable. This reduced the computational overhead during training and simplified the transition from simulation to real-world hardware deployment.
Conclusion and Future Work
This research demonstrates that bridging Simulink and Python via C-compilation is a viable and superior path for developing RL-based controllers. The approach preserves the modeling strengths of Simulink while unlocking the advanced algorithmic capabilities of the Python AI ecosystem.
Future research will focus on scaling this framework to 2-DOF configurations involving more complex nonlinear dynamics. Additionally, the team intends to explore direct training on physical hardware and conduct more rigorous head-to-head benchmarking between Python’s SB3 and the MATLAB Reinforcement Learning Toolbox across a broader range of industrial control tasks.
Summary updated