Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Invalid Cast Error with Ros Subscriber

Discussion in 'Robotics' started by unity_NDDcRuLP5Tq6Jw, Dec 6, 2022.

  1. unity_NDDcRuLP5Tq6Jw

    unity_NDDcRuLP5Tq6Jw

    Joined:
    Aug 12, 2022
    Posts:
    4
    Hello,

    I publish and subscribe to a ROS topic in Unity with my own msg and srv. I followed this tutorial: https://github.com/Unity-Technologies/Unity-Robotics-Hub/tree/main/tutorials/ros_unity_integration
    Publishing works without issues but the subscriber gets this error: I still receive the message and can print it.

    InvalidCastException: Specified cast is not valid.
    (wrapper castclass) System.Object.__castclass_with_cache(object,intptr,intptr)
    Unity.Robotics.ROSTCPConnector.ROSConnection+<>c__DisplayClass78_0`1[T].<Subscribe>b__0 (Unity.Robotics.ROSTCPConnector.MessageGeneration.Message msg) (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/ROSConnection.cs:194)
    Unity.Robotics.ROSTCPConnector.RosTopicState+<>c__DisplayClass56_0.<OnMessageReceived>b__0 (System.Action`1[T] item) (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/RosTopicState.cs:94)
    System.Collections.Generic.List`1[T].ForEach (System.Action`1[T] action) (at <c2a97e0383e8404c9fc0ae19d58f57f1>:0)
    Unity.Robotics.ROSTCPConnector.RosTopicState.OnMessageReceived (System.Byte[] data) (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/RosTopicState.cs:94)
    Unity.Robotics.ROSTCPConnector.ROSConnection.Update () (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/ROSConnection.cs:600)
    UnityEngine.Debug:LogException(Exception)
    Unity.Robotics.ROSTCPConnector.ROSConnection:Update() (at Library/PackageCache/com.unity.robotics.ros-tcp-connector@c27f00c6cf/Runtime/TcpConnector/ROSConnection.cs:605)


    My subscriber:

    void Start()
    {
    //subscribe to topic name and call funtion Print
    ROSConnection.GetOrCreateInstance().Subscribe<DigitalTwinMsg>(topicName, Print);
    }

    //msg is the data from the publisher
    void Print(DigitalTwinMsg msg)
    {
    Debug.Log(msg);
    }
     
  2. LaurieUnity

    LaurieUnity

    Unity Technologies

    Joined:
    Oct 23, 2020
    Posts:
    77
    Hmm, it's not obvious what's happening here. Is it just your custom message that's failing in this way? Are you able to subscribe to other message types?
     
  3. unity_NDDcRuLP5Tq6Jw

    unity_NDDcRuLP5Tq6Jw

    Joined:
    Aug 12, 2022
    Posts:
    4
    Yes, other custom messages worked, although I only tried with a single string as a message, apart from the ones from the tutorial.
     
  4. unity_NDDcRuLP5Tq6Jw

    unity_NDDcRuLP5Tq6Jw

    Joined:
    Aug 12, 2022
    Posts:
    4
    The error was caused by a similar but different .srv file. I deleted it and rebuild my services and messages and its now working fine.