UDP Communication between Unity and Matlab/Simulink
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.
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” 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 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;
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.
As Its understood from the name, UDPReceiver.cs is responsible of receiving data sent from Simulink to particular ip and port.
I set ip address and port of UDPReceiver.cs in Inspector as ‘127.0.0.1:25000’.
Also, UDPTransmitter.cs script is responsible of transmitting data to Simulink over destination ip and port.
I set destination ip address and port of UDPTransmitter.cs in Inspector panel as ‘127.0.0.1:26000’.
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.
If we look closer to scope above ;
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;