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

7 次查看(过去 30 天)
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 个评论
Sijin
Sijin 2022-11-8
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by