Synchronizing 2 Simulink Models on 2 different machines via UDP

14 views (last 30 days)
Hi, I'm trying to sinchronize 2 simulink models in 2 different machines to divide the work load of each. So far this is a test and my model is a simple mass, damper, spring moving only on the x-asis connected to a PID controller. I change the reference point with a joystick and visualize via VR Sink. Everything works fine if ran in one machine.
So what I did was divide the model so I put the PID controller in one machine, and the Plant with the physics in another machine and connected the models via UDP. If I start the models at the same time (by clicking the run button on both machines at the same time) the model works excellent with what appears to be zero delay.
The problem arises when I start the model in one machine first, and then, few seconds later I start the other. If I do this, the mass behaves like it's on crack and flyes everywhere. I believe this is because there is old data stored in some type of buffer somewhere and the model that starts late gets old data feed to it and it never schincronizes again; however I am not sure of this because in theory UDP is a best effort protocol and old data should be disregarded, right?
Anyways, some more useful information:
Ran on Matlab 2012a.
The model is compiled as a "Real-time windows target" and I use the Analog Input to get data from the joystick and Package output/Input to send/receive UDP data between machines.
From one machine I send the output from the PID controller and the other sends position data from the Physics Plant, so both computers send data to the other.
I did the experiment using a simple wireless router, and also tried Wireless Ad-Hoc connection to make sure the router wasn't causing the problem.
I appreciate any help given. Thanks!
  3 Comments
Xenoepist
Xenoepist on 17 Jul 2012
Edited: Xenoepist on 17 Jul 2012
I finally fixed my problem, and you are right, if the PID controller starts first, there is still wrong data from the PID controller trying to control something that is not there because the integral part of the PID keeps incrementing. But even so, the instability of my system stayed no matter which model started first. My problem was due to UDP packages getting stored in some type of queue. I will post the solution that worked for me so if anyone has a similar problem, they can try that fix. Thanks for your answer.

Sign in to comment.

Accepted Answer

Xenoepist
Xenoepist on 17 Jul 2012
Edited: Walter Roberson on 7 Aug 2019
Ok, I found a good workarround my problem. Like Mr. Roberson said, the old packages were stored somewhere in the receiving computer and were not disregarded even thou the program was nor running yet.
So what I did was to consume those packets faster than they were being sent, by changing the sample time on the Packet Input block to be 100 times faster

More Answers (2)

Walter Roberson
Walter Roberson on 14 Jul 2012
Data that manages to make its way between systems, and is matched to an open socket, is not discarded in UDP, not unless the receiving system requests discarding.
UDP packets that are received at a system that has no socket for them should be discarded by that system. But since there is no particular length of time defined for delivery, the packets could in theory be "in transit" for an undetermined amount of time. Your application needs to be able to detect and disregard "old" packets... even in cases where "old" turns out just to be "out of order" (a newer packet was received before an older one.)
  7 Comments
Xenoepist
Xenoepist on 18 Jul 2012
After I resolved my problem I noticed that I had little or no clue of what was going on under the hood of simulink so I tried a couple of experiments to see if I could understand the issue at its heart.
I did figure out a couple of things. 1) There is a buffer of 16384 bytes that stores the first packets that enter it... I really think the real size is 8192 (2^13) bytes and that I did something wrong with my experiment, but 16384 is the number that I got (2^14). 2) This buffer starts saving data as soon as I click the "Connect To Model" button on the receiving end. I am not sure, but I think that this buffer has nothing to do with the UDP protocol and everything to do with the Packet Input box of simulink. I wish I knew how to reset it. Thanks for your answers, thou, they helped me out a great lot.

Sign in to comment.


K E
K E on 16 Jul 2012
A workaround to your original problem might be to run your Simulink model with the Parallel Computing toolbox one just one machine so that Simulink runs faster, but you do not need both machines. Here are some tips on faster Simulink execution. If I have misunderstood your problem, sorry.
  3 Comments
saiful azrie
saiful azrie on 6 Aug 2019
i did the same project, but i cannot find the problem like u before. can i know how to perform the problem?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!