What's the fastest way to send out data from Simulink during simulation?

7 次查看(过去 30 天)
Hello everyone, I'm looking for the fastest way to send out Simulink data to Python during simulation. As a way of sampling the simulation in a "real time" manner.
I've tried MQTT publishing with Industrial Communication Toolbox but the delay is 0.5s on average (which is too much for my application, RL training). Also running the get_param command from Python takes about 0.3s.
Is there a way to obtain a signal's data "on the go" with minimal delay from Simulink to Python? Even if data is lost from time to time. I'm interested in speed more than security level.
Thank you!

回答(1 个)

Vijay
Vijay 2022-12-26
编辑:Vijay 2022-12-26
Data is passed by value from MATLAB to python so, if your data is large, it will take time. However, you mentioned you need to sample a signal from SIMULINK.
You can do the following
  1. Write the instantaneous value of signal in a workspace variable using toworkspace block.
  2. Using matlabengine api from python read that variable.
Sample code:
Import matlab.engine
eng = matlab.engine.connect_matlab();
while True
Val = eng.workspace[instantaneousSinglaValue];
#do soemthing with val
Use the link for further information
Hope that helps!
  1 个评论
Enzo Yacometti
Enzo Yacometti 2022-12-27
编辑:Enzo Yacometti 2022-12-27
Thanks but I can't read the data from the "To Workspace" block during simulation. I understand it only saves the data to workspace once the simulation is over. The only solution for reading data during simulation seems to be using RunTime objects, or did I misunderstand your first step?

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by