Synchronising UDP send/receive with external application

5 views (last 30 days)
I have a Simulink model (the aircraft) running in real time which I am trying to control with a second simulink model (the controller)(running on different versions of MATLAB) via UDP packets. The controller will eventually be running on an external application so using 2 Simulink versions is a temporary way to simulate that.
The packets are being sent and received without issue, however they appear to be out of sync, i.e. Simulink appears to be reading an old UDP packet from the buffer.
What I need is some way to synchronize the UDP packets, ensuring the one being read by the aircraft is the most recent sent from the controller and vice versa. How do I implement a system like this, e.g. using a timestamp?

Answers (1)

Walter Roberson
Walter Roberson on 29 Jul 2015
A sequence number would be standard. Have each side include an integer which increments each time a packet is sent. The receiver should discard any packet that contains a sequence number lower than the greatest one it has already processed.
Be careful about how wide you make the integer: if left long enough eventually it will overflow.
A timestamp can be treated as an integer sequence number: it does not matter what the integers "mean" as long as they are ever-increasing. The difficulty with saying that it will be a timestamp is that you start running into the problem that timestamps read out from the system are not necessarily unique if your processing is fast enough -- not unless you use a time routine that guarantees uniqueness (and some of them do.) But the time routines that guarantee uniqueness sometimes have to wait until the next "tick", which can slow down your processing.
The x64 architecture has a chip-level cycle count that is very tempting to use, as it would certainly be unique (until resets.) However it turns out that it takes several cycles to fetch that number from the processor; incrementing an integer is faster.

Categories

Find more on Multicore Processor Targets in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!