Matlab's Simulink UDP send to unity

조회 수: 10 (최근 30일)
Zainab
Zainab 2024년 11월 1일
답변: Mark McBroom 2024년 11월 16일
Hello everyone, I am trying to use Simulink UDP send to send semg signals to unity in order to move a ball up in down however I am running into errors on my code. The main one is that unity is saying, " Recieved non-numerical data". I am sending the data as a string from simulink and converting it into a float. Does anyone know why i am recieving this error. Thank you in advance
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
public class BallUdpReceiver : MonoBehaviour
{
private UdpClient udpClient;
private Rigidbody rb;
public int port = 50043; // Port to listen on (ensure it matches the port in Simulink)
public float speed = 5f; // Movement speed multiplier
private bool isReceiving = true; // Flag to control data reception thread
private float receivedValue = 0f; // Store the latest received value
private void Start()
{
// Set up the UDP client to listen on the specified port
udpClient = new UdpClient(port);
// Get the Rigidbody component for applying movement
rb = GetComponent<Rigidbody>();
// Start the data receiving thread
Thread receiveThread = new Thread(ReceiveData);
receiveThread.Start();
}
private void ReceiveData()
{
while (isReceiving)
{
try
{
// Listen for incoming data
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, port);
byte[] data = udpClient.Receive(ref remoteEndPoint);
// Decode received data to a string
string receivedData = Encoding.UTF8.GetString(data);
// Log the raw received data for debugging
Debug.Log($"Raw received data: {receivedData}");
// Process the incoming data
ProcessData(receivedData);
}
catch (SocketException se)
{
Debug.LogError($"SocketException: {se.Message}");
}
catch (System.Exception e)
{
Debug.LogError($"Error receiving data: {e.Message}");
}
// Sleep briefly to prevent excessive CPU usage
Thread.Sleep(10);
}
}
private void ProcessData(string data)
{
// Trim whitespace and check if the data is empty
data = data.Trim();
if (string.IsNullOrEmpty(data))
{
Debug.LogWarning("Received empty data");
return;
}
// Convert the received data to a float
if (float.TryParse(data, out float value))
{
receivedValue = value; // Store the latest received value
}
else
{
Debug.LogWarning($"Received non-numerical data: {data}");
}
}
private void Update()
{
// Apply movement based on the last received value
Vector3 movement = new Vector3(0, receivedValue * speed * Time.deltaTime, 0);
rb.MovePosition(rb.position + movement);
}
private void OnApplicationQuit()
{
// Stop data reception and close the UDP client when the application closes
isReceiving = false;
udpClient.Close();
}
}
  댓글 수: 1
Swastik Sarkar
Swastik Sarkar 2024년 11월 15일
Consider using a String to ASCII block to convert the string to a vector of uint8 before sending it to a UDP Send block. Adjusting the byte-order of the UDP Send block could also be useful. More information can be found here:
Hope this helps !

댓글을 달려면 로그인하십시오.

답변 (1개)

Mark McBroom
Mark McBroom 2024년 11월 16일
Ive not worked with unity, but have you tried just sending and receiving a float? Simulink can do this no problem. This would make your Simulink model and unity code much simpler.

카테고리

Help CenterFile Exchange에서 String에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by