Published in SiliconWit Open Engineering & Computing. This article was peer reviewed before publication.
SiliconWit Open Engineering & ComputingPaper No. X894EPSiliconWit
A modular Python framework for real-time simulation of mechatronic systems
Abstract
Teaching and prototyping mechatronic systems benefits from simulations that run in real time against real hardware. We present an open-source Python framework that composes mechanical, electrical, and control subsystems as interchangeable modules with a fixed-step solver and a hardware-in-the-loop bridge. On a two-wheeled balancing robot, the framework sustained a one-kilohertz control loop on a single-board computer while streaming state to a browser dashboard. We describe the module interface, the solver design, and the latency budget that keeps simulation and hardware in step, and we release worked examples so readers can reproduce each result.
Keywords: Modelling & simulation, Mechatronics, Hardware-in-the-loop, Open source
Cite as: Alan Mwangi & Faith Njeri (2025). A modular Python framework for real-time simulation of mechatronic systems. SiliconWit Open Engineering & Computing. https://quorumpress.org/papers/modular-python-framework-real-ltjz0
© 2025 Mwangi and Njeri. Open access under CC BY 4.0.
1. Introduction
Mechatronic design couples mechanical dynamics, electronics, and control, and errors in any one layer surface only when the layers run together. Real-time simulation lets a designer close the control loop against realistic dynamics before committing to hardware, and hardware-in-the-loop (HIL) testing lets the same controller drive a simulated plant at full rate [1], [2]. These techniques are standard in industry but are usually locked inside heavyweight commercial tools that are hard to inspect and to teach from [3].
We present an open-source Python framework that keeps hard real-time behaviour while staying small enough to read in an afternoon. Our contributions are: (i) a minimal module interface that composes mechanical, electrical, and control subsystems into a single fixed-step system; (ii) a deterministic solver with a measured timing budget; and (iii) an HIL bridge that swaps a simulated subsystem for real hardware without changing the rest of the model.
2. Related work
Block-diagram environments such as Simulink and Modelica are expressive but proprietary and opaque to students [3]. Robotics simulators such as Gazebo target rigid-body scenes rather than mixed electrical-mechanical-control models and do not guarantee a fixed real-time step [4]. Lightweight Python control libraries provide excellent analysis but stop at offline simulation [5]. Our framework occupies the gap: open, readable, and real-time, with a first-class HIL path.
3. Framework architecture
A model is a directed graph of modules. Every module implements the same interface, so the solver treats a spring-mass plant, a motor driver, and a PID controller identically:
reset(): set initial state;
step(inputs, h): advance the module by one step of size h and return outputs;
outputs are wired to inputs to form the system graph.
Because the contract is this small, a subsystem can be reimplemented in C for speed, or replaced by real hardware over the HIL bridge, without touching its neighbours.
4. Real-time solver
Each module exposes continuous dynamics that the solver integrates with a fixed step . We use the classical fourth-order Runge-Kutta update
which gives fourth-order local accuracy at a fixed, predictable cost per step, so the per-step time is bounded and schedulable [6]. The loop must satisfy the real-time constraint
where the three terms are the integration, input/output, and control times. We schedule the loop against a monotonic clock and record the jitter to verify the budget is met.
5. Case study: a two-wheeled balancing robot
Linearised about the upright equilibrium, the robot is a fourth-order plant with state (body angle, angular rate, position, velocity). We stabilise it with a linear-quadratic regulator , where minimises
The controller and plant run as two modules at ; a third module streams state to a browser dashboard over a non-blocking queue so visualisation never steals from the control budget.
6. Results
On a single-board computer the framework sustained the one-kilohertz loop for two hours. Table 1 summarises timing across three plant complexities; even the coupled model kept mean jitter well under the step and mean CPU below half a core.
Model | Mean loop (ms) | Max jitter (ms) | CPU (%) |
|---|---|---|---|
Single mass | 0.21 | 0.09 | 18 |
Balancing robot | 0.38 | 0.14 | 31 |
Robot + HIL motor | 0.52 | 0.19 | 44 |
With the motor subsystem replaced by real hardware over the HIL bridge, the closed loop remained stable and the added input/output time stayed within the budget of equation (2).
7. Discussion and conclusion
The framework shows that hard real-time mechatronic simulation does not require a heavyweight toolchain: a small, uniform module interface plus a fixed-step Runge-Kutta solver is enough for both teaching and prototyping. The main limitation is single-node execution; distributing modules across cores while preserving determinism is future work. The framework, the balancing-robot model, and every figure in this paper are released as open source [7].
References
[1] M. Bacic, “On hardware-in-the-loop simulation,” Proc. IEEE Conf. Decision and Control, 2005.
[2] R. Isermann, J. Schaffnit and S. Sinsel, “Hardware-in-the-loop simulation for the design of electronic control systems,” Control Engineering Practice, vol. 7, pp. 643-653, 1999.
[3] K. Ogata, “Modern Control Engineering,” Prentice Hall, 2010.
[4] N. Koenig and A. Howard, “Design and use paradigms for Gazebo, an open-source multi-robot simulator,” Proc. IEEE/RSJ IROS, 2004.
[5] S. Fuller, B. Greiner, J. Moore, R. Murray, R. van Paassen and R. Yorke, “The Python Control Systems Library,” Proc. IEEE Conf. Decision and Control, 2021.
[6] E. Hairer, S. P. Norsett and G. Wanner, “Solving Ordinary Differential Equations I: Nonstiff Problems,” Springer, 1993.
[7] SiliconWit, “Real-time mechatronic simulation framework (source code),” siliconwit.com, 2025.
Cite this paper
Alan Mwangi, Faith Njeri. A modular Python framework for real-time simulation of mechatronic systems. SiliconWit Open Engineering & Computing, 2025. https://quorumpress.org/papers/modular-python-framework-real-ltjz0
Publication ID: ltjz0