Matlab's Simulink UDP send to unity
    9 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
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 Kommentar
  Swastik Sarkar
      
 am 15 Nov. 2024
				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 !
Antworten (1)
  Mark McBroom
    
 am 16 Nov. 2024
        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.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


