How can I interrupt a simulation step in between, to wait for a response from a python script?

10 views (last 30 days)
Hi,
I am executing a simulink simulation from python script using matlabengine for python. From an entity server within simulation, I want to pause the simulaiton and wait for the python script to update a workspace variable. As soon as the workspace variable is filled, I want the simulation to resume. However, the pause doesn't pause the present simulation step immediately and it finished running. It only pauses the future simulations.
To pause, I am using an assertion block, with the call back as:
set_param(bdroot,'SimulationCommand','pause'); pause(0);run(append(Path,'/','wait_for_python.m'));
In the 'wait_for_python..m' script I have added a while loop that waits for the workspace variable to be filled.
var_action = 0;
while var_action == 0 %wait for an update from python script
%do nothing
pause(0.01) %in seconds
end
In python script, I fill the workspace variable and trigger the continue command to resume the simulation.
eng.workspace['var_action'] = action
eng.set_param(self.modelName,'SimulationCommand','continue',nargout=0)
The present simulation doesn't really pause in between the present simulation, and it continues to the next block, irrespective of the wait statement within the 'wait_for_python.m' file. It only stops after finishing the current simulation step. But this is too late as the workspace variable, 'var_action', is needed in the subsequent blocks.
Please let me know how to resolve this issue with pause. Kindly also let me know if there are any alternative mechnisms to wait for and consume data from python script from within a simulation.
I am very new to matlab and currently stuck with this issue. I have gone through a lot of articles and forums, but couldn't find any proper answer for this. I would greatly appreciate any help regarding this.
Thanks and Regards,
Sijin
  1 Comment
Sijin
Sijin on 8 Nov 2022
Update: This was an issue with the service time specified at the entity server, which was too small. As I increased the time, the pause triggered using the assertion block worked.

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!