Can I interact with a Simulink Real Time Simulation from Python?
47 views (last 30 days)
MathWorks Support Team on 31 Oct 2018
I would like to interact with our Speedgoat Performance machine remotely to get informations and update tunable parameters during runtime.
The goal is to be able to do this via a Python script.
1) What would be the best way? Do you plan to have a Python API?
2) Can we do it as well from a deployed MATLAB application without the Real-Time toolbox installed?
MathWorks Support Team on 9 Oct 2020
Edited: MathWorks Support Team on 9 Oct 2020
1) Interacting with a Real-Time Simulation through a Python script
We offer a MATLAB Engine API for use with Python. The landing page for the API can be found on the following documentation page:
The Python API allows you to open and pass command line statements to a MATLAB instance that behaves like a regular instance of MATLAB, with some minor limitations detailed on the following page:
What this means in essence is that anything that is achievable by interacting with MATLAB via command line is theoretically possible through the API, including communication with a Target Machine and manipulation of the simulation that is running there.
Communication with a Target is done through the use of a SimulinkRealTime.target object, detailed here:
When using this object with the API, one limitation is that certain objects cannot be communicated back from MATLAB to Python, and this object is likely one of them. This means you would want to assign this object to the MATLAB instance workspace instead of attempting to port it into Python, and communicate with it via function calls to the MATLAB instance. Once this object is instantiated you can control various aspects of the simulation via the target methods. This includes tuning parameters like in the following example:
One final thing I would suggest while developing this python interface is using the "-desktop" startup option when creating the engine:
This will make the MATLAB instance visible, and will no doubt be useful in testing how the Python API interacts with the MATLAB instance for debugging and development purposes.
2) Deploying a MATLAB application to interact with a Real Time Simulation
The following documentation page is an excellent resource that talks about possibilities for deploying a MATLAB application that can interact with a real time simulation:
Please note that this workflow expects that you have the following toolboxes available:
Simulink®, Simulink Real-Time™, MATLAB® Compiler™, MATLAB Compiler SDK™