How to interrupt Simulink when running through Python? (Requirements: Monitoring? Waiting for the interrupt signal from users)

16 views (last 30 days)
Hello! I'm new to Matlab and now working to control simulink through python.
  • Goal: I want to interrupt the running of Simulink with the help of the keyboard, but it should be done in a GUI on python. When the simulink model pauses, I can update the parameters of some blocks(or switch the mode of the model).
  • Problem: The interruption point of running of the Simuink can only be set before I run the simulink model. E.g. I can set the model to pause at 2s. But I can not interrupt it at any time. Although we can click "pause" on Simluink GUI but I have to only control the simulink model thorugh a GUI built in Python.
  • Code: Here I used "set_param" to realize "stop","start","update parameters". But I can not interrupt the model whenenver I want(without setting interruption points bevorhand). The code can not caputure the keyboard event, when I pressed ‘space’ on thekeyboard.
This is the code of Python(3.8).
import matlab
import matlab.engine
import numpy as np
import matplotlib.pyplot as plt
import keyboard #monitor the event from keyboard
def Change2VM():
eng = matlab.engine.start_matlab()
env_name = 'simpleSinEnvV3' # define the name of Simulink model
# eng.load_system(env_name) # load
eng.set_param(env_name, 'SimulationCommand','Pause') # interrupt the model
eng.set_param('simpleSinEnvV3/Modus','value', '1', nargout=0) #change the mode to VM
eng.set_param(env_name, 'SimulationCommand', 'continue', nargout=0); #cancel pause and continue the simulation
print('VM Modus is on\n')
def main():
# load the simulink model
eng = matlab.engine.start_matlab()
env_name = 'simpleSinEnvV3'
eng.load_system(env_name)
duration = 5 # in seconds. How long dose this simulation run.
eng.set_param(env_name, 'StopTime', str(duration), nargout=0) # when to stop
eng.set_param('simpleSinEnvV3/Modus','value', '2', nargout=0) # set the modus to "EM" at the beginning
eng.set_param(env_name, 'SimulationCommand', 'start', nargout=0) # run the model
print('Wait for key')
keyboard.add_hotkey('space', Change2VM, suppress=False, timeout=1, trigger_on_release=False) # triger the intrruption
eng.set_param(env_name, 'SimulationCommand', 'stop', nargout=0) # stop running the model
t = np.array(eng.eval('out.t.Data'))
y = np.array(eng.eval('out.y.Data'))
eng.quit()
# show the results
fig = plt.figure(figsize=(5, 5))
plt.plot(t, y[:, 0], '-', label='VM')
plt.plot(t, y[:, 1], '-.', label='EM')
plt.plot(t, y[:, 2], '--', label='HB')
plt.legend()
plt.show()
main()
This is my simulink model(Matlab2016a), i want to shift the output from EM(case2) to VM(case1), when I pressed "Space" on keyboard.
  1 Comment
鹏涛 谢
鹏涛 谢 on 11 Jun 2023
But it' doesn't work. The program has no response, when I pressed "Space".
From the picture we can konw, "case 2" is never been activated.

Sign in to comment.

Answers (1)

Ayush
Ayush on 15 Aug 2023
I understand that you want to pause the simulation in between, perform some action, and resume the simulation later. You can utilize the Python’s capability of detecting a key press to pause/resume the simulation. Here is the skeleton code for that:
# to pause the simulation
while True:
if keyboard.is_pressed("your_key"):
eng.set_param('modename','SimulationCommand','pause')
break
# to resume the simulation
while True:
if keyboard.is_pressed("your_key"):
eng.set_param('modename','SimulationCommand', 'continue')
break
You may refer to Run Simulations Programmatically - MATLAB & Simulink - MathWorks India for further information on how to pause and continue the simulation.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!