MATLAB and Simulink are industry standards for control systems engineering, providing an integrated graphical interface for block-diagram modeling and numerical computations. However, shifting entirely to an open-source Python ecosystem provides deep architectural benefits that align with modern software engineering and systems design:
- Vendor Lock-in and Licensing: MATLAB is proprietary and requires costly licenses. A Python-based architecture ensures the simulation remains open-source, reproducible, and deployable on any headless server, continuous integration (CI) pipeline, or local environment without licensing overhead.
- Integration and Scalability: Python seamlessly interfaces with modern web frameworks, data science pipelines, and embedded software testing frameworks. It allows for the integration of custom-built physics engines directly into deployment workflows.
- Version Control and Text-Based Diffing: Simulink models (
.slxfiles) are binary or heavily structured XML files that are notoriously difficult to track, merge, and diff via Git. Pure Python scripts are transparent, line-by-line text files, optimizing cooperation and deployment history tracking.
- Explicit State-Space and ODE Handling: Forcing explicit mathematical representation using libraries like
SciPyrather than dragging graphical blocks builds a fundamental understanding of numerical solvers (e.g., Runge-Kutta variations). - Software Engineering Architecture: Implementing object-oriented paradigms for sensors, actuators, and plants requires defining explicit software interfaces and custom multi-threaded or loop-based synchronization, mimicking real-world embedded firmware.
- Unified Visualizations: Merging state calculation with 2D/3D visualizers (such as
matplotlibor rendering engines) under a single language runtime removes the inter-process communication overhead often seen when coupling MATLAB with external engines.
- Development Overhead: Graphical modeling in Simulink handles structural algebraic loops and automatic routing out of the box. Python requires manual derivation, implementation of differential-algebraic equations (DAEs), and strict ordering of calculations.
- Real-Time Execution Constraints: Python’s Global Interpreter Lock (GIL) and high-level abstraction mean that real-time simulation loops can experience deterministic jitter unless managed through low-level C-extensions or precise loop-timing frameworks.
The project is divided into two distinct core mechatronic systems:
A highly unstable, underactuated nonlinear system where an inverted pendulum is stabilized at its vertical equilibrium point using the reaction torque generated by a coupled DC motor accelerating a flywheel. The simulation models the full system Lagrangian dynamics, open-loop response, linear PID control via small-angle linearization, explicit DC motor electrical/mechanical subsystems, mechanical 3D MultiBody simulation equivalents, and advanced mitigation strategies for flywheel velocity saturation.
A distributed robotics setup modeling a local master manipulator controlled by a human operator and a remote slave manipulator executing tasks in a separate workspace. The architecture simulates a non-ideal communication channel subject to asymmetric, time-varying transport delays. Control is achieved via Proportional-plus-Damping (P+D) structures to enforce strict position tracking between the joint spaces without destabilizing the system under delayed network dynamics.
mechatronic-python-simulation/
├── docs/
│ ├── part_a.pdf
│ └── part_b.pdf
├── outputs/
│ ├── plots/ # Static figures (PNG, PDF)
│ └── animations/ # Rendered simulations via ffmpeg (MP4, GIF)
├── src/
│ ├── reaction_wheel/
│ └── teleoperation/
├── README.md
└── requirements.txt
For video encoding, animation rendering, and 3D graphics hardware acceleration, you must install ffmpeg and OpenGL development libraries via your package manager:
sudo apt update
sudo apt install ffmpeg libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev
It is recommended to isolate these dependencies within a virtual environment rather than relying on global system packages:
# Create a virtual environment
python3 -m venv venv
# Activate the environment
source venv/bin/activate
# Upgrade pip to the latest version
pip install --upgrade pip
# Install project dependencies
pip install -r requirements.txt
Instead of heavy Python wrappers, this project utilizes native ffmpeg pipelines via matplotlib.animation.FFMpegWriter to encode simulation frames into high-quality MP4 or GIF formats, saving them directly to the outputs/animations/ directory.