【发布时间】:2018-07-29 13:50:41
【问题描述】:
我需要从一个用 Python 编写的外部应用程序控制 Simulink 控制方案。我需要做的是逐步完成模拟,并在每一步检索输出并让 Python 应用程序确定新的输入集。这是一个固定的时间段。 有没有办法做到这一点?我承认我很难尝试使用 matlab 脚本来实现这一点,更不用说 Python。 这是可行的吗?如果没有,有没有办法将 Python 模块插入到 simulink 方案中?
谢谢
编辑:这是我设法解决的方法
为了逐步运行模拟,我创建了这个带有时钟、关系运算符和断言块的块结构
其中 Tmp 是每次暂停的时间戳
Tmp=get_param(bdroot,'SimulationTime')
断言块包含以下指令:
set_param(bdroot,'SimulationCommand','pause')
这样,模拟在每一步之后都会暂停,即 (clockTime-Tmp)=timeStep。
现在,我创建了一个 Python 脚本来启动模拟(请参阅接受的答案)并像这样迭代:
#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)
这对我来说似乎很好,但如果有更好的选择,请告诉我。
【问题讨论】:
标签: python matlab interface simulink