import matplotlib.pyplot as plt
import keyboard #monitor the event from keyboard
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')
# 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
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'))
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')