UDP Communication between Unity and Matlab/Simulink

Cihad Doğan
4 min readSep 16, 2019

In past months, I needed a communication mechanism between Unity and Matlab/Simulink for controlling my computer simulation projects from Simulink to apply automatic control methods(PID in my case). For that purpose, I selected UDP protocol because of its lightweight and fast nature.

Simulink Model Part

In default, Simulink models do not work in realtime. So firstly, we have to find a way to make Simulink model work in realtime. We will use “Real-Time Synchronization” block for that purpose.

Real-Time Synchronization block and parameters.

We have to set a sample time(in seconds) and maximum missed ticks. Sample time as 10ms was enough for my project. Also I selected maximum missed ticks as high as possible to prevent simulink model to stop even there is lots of lag. Also, I have to mention that in Windows-10 “Real-Time Synchronization” block was only working with Matlab 2018a or newer versions of Matlab(Mine was 2018a). In older versions, computer was giving blue screen and restarting immediately after I run the model. I am not sure that this is a generic problem but in my case problem occurred in matlab 2016 and 2017. You also have to install Real-Time Kernel of Matlab to use Real-Time Synchronization block. You can learn how to install in below link.

https://www.mathworks.com/help/sldrt/ug/real-time-windows-target-kernel.html

Now, we can start to create UDP protocol in Simulink.We have to use “UDP Send” ve “UDP Receive“ blocks for that purpose.

UDP send(transmitter) block and parameters.

“UDP Send” block is used for transmitting data from Simulink to Unity.I set remote ip as “127.0.0.1” which refers localhost. Also my receiver port will be 25000 in Unity side.

UDP receiver block and parameters.

“UDP Receive” block is used for receiving data from Unity. I set my receiver port as 26000. I leaved remote ip address and receive buffer size as default. I set data type as double which is 8 byte per value. So, complete Simulink model became as below;

Simulink model with all blocks.

Now we are ready to start C# coding in Unity.

Unity Part

I created project with Unity 2018.4.3f.1. I have only one Scene named as “Main”. I created a Gameobject named as “Scripts” and attached three scripts on it UDPReceiver.cs, UDPTransmitter.cs and CommunicationController.cs.

Sample scene in Unity project. You can see “Scripts” object and UDPReceiver.cs and UDPTransmitter.cs scripts attached on it.

As Its understood from the name, UDPReceiver.cs is responsible of receiving data sent from Simulink to particular ip and port.

Complete code of UDPReceiver.cs

I set ip address and port of UDPReceiver.cs in Inspector as ‘127.0.0.1:25000’.

UDPReceiver script on Inspector.

Also, UDPTransmitter.cs script is responsible of transmitting data to Simulink over destination ip and port.

Complete code of UDPTransmitter.cs

I set destination ip address and port of UDPTransmitter.cs in Inspector panel as ‘127.0.0.1:26000’.

UDPTransmitter script on Inspector.

CommunicationController.cs is responsible of reading received data from the UDPReceiver.cs and deliver it to UDPTransmitter.cs.

Test and Result

For testing the communication, I added a sinus signal as input to “UDP Send” block in Simulink. In Unity, I am resending message to Simulink after I received the message from Simulink. My purpose is to see how many data lost and delay on UDP communication by comparing received and sent data in Simulink model.

Sent(Red) and received(Blue) data signal in Simulink.

If we look closer to scope above ;

Closer look of sent and received sinus signal.

We can easily see the 0.01 second delay between sent and received data which caused by sample time in “Real-Time Synchronization” block. Also we can clearly see some lost packages in communication between 2–2.1 seconds which might happen in UDP.

Performance of the communication was enough for my project so I stopped here at the moment. You can get example Unity project and Simulink file from my Github page;

https://github.com/CihadDogan/UDPExample

My personal pages;

https://www.linkedin.com/in/cihad-dogan-1b029499/

--

--

Cihad Doğan

Software engineer by morning, game developer by night.