Simulink Desktop Real-Time and UDP communication with Python
이전 댓글 표시
I have a main Python program that has two(three) subprocesses:
- execMainModel (a matlab function that it calls a simulink model, in this model I have a real-time sync block, an UDP receiver block, and two UDP sender blocks)
- play.py (a Python program that sends 2 UDP messages every 0.1 s { [p1],[p2], wait......,[][]...}
- reed.py (read Simulink output, debugging script)
Currently my flow is:
1) Run mainPythonScript
2) call execMainModel.m function (subprocess, bash command), initialize some dictionary parameters, and start Simulink model.
3) Time = 0.0 send over UDP1 a "sync" message to the pythonMain.
4) When the sync message is received the real sender is started (play.py).
5) The loop is completed, I can proceed with the real execution loop where I read the UDP packet and, after some processing, send it or a similar copy via UDP2 to another process (this is not so important).
Currently, my model rate is 50 Hz, and the UDP buffer can contain only two messages (my choice), blocking time = 0 (or very small).
Execution output:
I receive and resend correctly the first UDP message from player.py (after the sync) then I lose 2 to 6 messages (it's variable), then the program goes on without problems and synchronized. If I increase the buffer size (5/10 elements) it proceeds without losing data packets. At the same time, if I wait 1 second after the sync acknowledgement before starting player.py it works well.
My thoughts:
Also if Simulink sends the sync message it seems that Simulink requires more init time to start.. it's possibile to solve this problem without waiting and keeping the small buffer? real-time sync block parameters can affect this problem? (sample time / maximum missed ticks)
MATLAB release 2022b
thanks.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Model Preparation for Real-Time Simulation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!