Hi, I am planning on same thing to implement. Let me know if you get any direction!
Python pybamm and MATLAB/Simulink integration
12 views (last 30 days)
Show older comments
I have a battery model written in python (py 3.11) with pybamm 24.11.2 and casadi solver 3.6.7.
I want to use this model in simulink in real time. I want to dynamically run the current dependent pybamm model.
Websearch and other attempts did not work. How can I integrate simulink and pybamm?
Answers (1)
Himanshu
on 10 Apr 2025
Hello,
I see that you are attempting to integrate PyBaMM (a Python-based tool) with MATLAB/Simulink in a real-time environment. MATLAB provides functionality to directly call Python using external language interfaces. The main challenge is enabling communication between Python and Simulink, as Simulink typically requires compiled C/C++ code or MATLAB functions. PyBaMM’s reliance on Python/CasADi complicates direct integration.
One viable approach is to use MATLAB’s Python Engine to call the PyBaMM model from Simulink via a MATLAB Function block. Ensure that MATLAB recognizes your Python environment by using:
pyenv("Version", "your_python_executable_path")
Then, create a MATLAB wrapper function that interacts with PyBaMM.
Another method is to use co-simulation via TCP/IP or UDP. The idea is to run the PyBaMM model as a separate process that communicates with Simulink over a network. In Simulink, you can use TCP/IP Send and Receive blocks to exchange data. On the Python side, set up a server to listen for Simulink's commands, run the model, and send back results. This decouples the two environments but requires handling synchronization and latency.
Please refer to the attached documentation for more information:
- Call Python from MATLAB: https://www.mathworks.com/help/matlab/call-python-libraries.html
- Send and Receive Data over TCP/IP Network: https://www.mathworks.com/help/instrument/send-and-receive-data-over-a-tcpip-network.html
- TCP/IP Receive: https://www.mathworks.com/help/instrument/tcpipreceive.html
I hope this helps.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!