RobotecAI / ros2-for-unity

High-performance ROS2 solution for Unity3D
Apache License 2.0
446 stars 58 forks source link

Unity crashes when receiving map message #104

Open jvmoraiscb opened 4 months ago

jvmoraiscb commented 4 months ago

I have the code below that receives a message of type OccupancyGrid and for now, it does nothing. I have published some "fake" maps that are very small (50x50) and I can receive the message normally. However, when receiving an actual map (e.g., 300x300), Unity crashes instantly. The code is as follows:

using UnityEngine;
using ROS2;

public class MapSubscriber : MonoBehaviour{
    [Header("Map Settings")]
    [SerializeField] private string nodeName = "MapSub_Unity";
    [SerializeField] private string topicName = "map";

    // [Header("CmdVel Dependencies")]
    // [SerializeField] private GameObject wallPrefab;

    private ROS2UnityComponent ros2Unity;
    private ROS2Node ros2Node;
    private ISubscription<nav_msgs.msg.OccupancyGrid> sub;

    private void Start(){
        ros2Unity = GetComponent<ROS2UnityComponent>();
    }

    private void Update(){
        if(!ros2Unity.Ok()) return;
        if(ros2Node == null){
            ros2Node = ros2Unity.CreateNode(nodeName);
            sub = ros2Node.CreateSubscription<nav_msgs.msg.OccupancyGrid>(topicName, msg => MapCallback(msg));
        }
    }

    private void MapCallback(nav_msgs.msg.OccupancyGrid msg){
        Debug.Log(msg.Info.Height + " - " + msg.Info.Width);
    }
}

Is the fact that Unity cannot receive this message some limitation of the library itself or is there an additional step that I haven't taken?