Matlab's Simulink UDP send to unity

50 次查看(过去 30 天)
Zainab
Zainab 2024-11-1,20:25
回答: Mark McBroom 2024-11-16,22:23
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,5:52
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,22:23
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