Python – Step through the Simulink model from Python

Step through the Simulink model from Python… here is a solution to the problem.

Step through the Simulink model from Python

I need to control the Simulink control scheme from an external application written in Python. What I need to do is step through the simulation and retrieve the output at each step and let the Python application determine the new set of inputs. This is a fixed period of time.
Is there any way to do this? I admit it’s hard for me to try to achieve this using matlab scripts, let alone Python.
Is this feasible? If not, is there a way to insert Python modules into the simulink scheme?

Thanks


EDIT: That’s how I managed to fix it

To run the simulation step by step, I created this block structure with a clock, relational operators, and assertion block

Simulation stepper in simulink

where Tmp is the timestamp of each pause

Tmp=get_param(bdroot,'SimulationTime')

The assertion block contains the following directive:

set_param(bdroot,'SimulationCommand','pause')

In this way, the simulation pauses after each step, i.e. (clockTime-Tmp)=timeStep.

Now, I’ve created a Python script to start the simulation (see accepted answers) and iterate like this:

#While the simulation is running
while eng.get_param('simpleTest','SimulationStatus')!=('stopped' or 'terminating'):
    if eng.get_param('simpleTest','SimulationStatus')=='paused':
        #do your evaluations and operations 
        eng.set_param('simpleTest','SimulationCommand','update',nargout=0) #if you have updated any simulation parameters
        eng.set_param('simpleTest','SimulationCommand','continue',nargout=0)

This seems to work fine for me, but let me know if there is a better option.

Solution

Use matlab.engine bindings in Python You can start an instance of the MATLAB engine and send individual commands as strings to MATLAB if you have not already used it. This technique lets you enter strings as if you were entering them on the MATLAB command line. For example:

 >>>import matlab.engine # load engine functionality
 >>>eng = matlab.engine.start_matlab() # init instance of engine
 >>>eng.sim("simulinkModelName") # start a simulink model by calling it through the engine instance

This also lets you pass data to MATLAB from Python, according to the documentation. According to what you said, this should be enough to fulfill your requirements.


However, I thought of another way, which is to use a TCP/IP connection to communicate between two processes (Python GUI to Simulink). This will allow you to send messages from one program to the next, and then you can parse them accordingly. (Simulink, Matlab, and Python all have TCP/IP options!)

In this idea, I would have the GUI act as a server and listen/send messages to the client (simulink) in a background asynchronous thread. For example, you can send a command to start the simulation, then stop at some point and wait to receive data from Python.

This may require a more complex understanding of threading, and I recommend looking for threading in Python, as well using sockets in Python .

If you do want to switch to GUI development in another language, the TCP/IP commands will be the same for the future implementation.


I hope this helps you with your task!

Related Problems and Solutions