Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Publish action from Unity with coordinates to ROS2 Action

Discussion in 'Robotics' started by jabrail_chumakov, Apr 20, 2023.

  1. jabrail_chumakov

    jabrail_chumakov

    Joined:
    Feb 17, 2020
    Posts:
    23
    Hello folks,
    I'm just wondering if it is possible to send action from Unity to ROS2. I tried different possible solutions but I constantly got an error with an unknown message class: Unknown message class 'nav2_msgs/NavigateToPoseActionGoal'
    Is there any possible solution for that? I generated my action type nav2_msgs/action/NavigateToPose using "Generate ROS Messages" in Unity, but it created 7 files for that. Here is an example of my script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Robotics.ROSTCPConnector;
    5. using RosMessageTypes.Std;
    6. using RosMessageTypes.Geometry;
    7. using RosMessageTypes.Nav2;
    8.  
    9. public class GoalPublisher : MonoBehaviour
    10. {
    11.     ROSConnection ros;
    12.     public string topicName = "/navigate_to_pose";
    13.  
    14.     public Camera mainCamera;
    15.     public GameObject plane;
    16.     public GameObject bluePoint;
    17.     public GameObject greenPoint;
    18.     private GameObject currentPoint;
    19.  
    20.     private Plane _plane;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         ros = ROSConnection.GetOrCreateInstance();
    26.         ros.RegisterPublisher<NavigateToPoseActionGoal>(topicName);
    27.         _plane = new Plane(plane.transform.up, plane.transform.position);
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
    34.         float distanceToPlane;
    35.  
    36.         if (_plane.Raycast(ray, out distanceToPlane))
    37.         {
    38.             Vector3 hitPoint = ray.GetPoint(distanceToPlane);
    39.             if (currentPoint == null)
    40.             {
    41.                 currentPoint = Instantiate(bluePoint, hitPoint, Quaternion.identity);
    42.             }
    43.             else
    44.             {
    45.                 currentPoint.transform.position = hitPoint;
    46.             }
    47.         }
    48.  
    49.         if (Input.GetMouseButtonDown(0))
    50.         {
    51.             Vector3 hitPointSend = ray.GetPoint(distanceToPlane);
    52.  
    53.             // Create a green point at the clicked position
    54.             Instantiate(greenPoint, hitPointSend, Quaternion.identity);
    55.  
    56.             NavigateToPoseActionGoal sendGoal = new NavigateToPoseActionGoal
    57.             {
    58.                 goal = new NavigateToPoseGoal
    59.                 {
    60.                     pose = new PoseStampedMsg
    61.                     {
    62.                         header = new HeaderMsg
    63.                         {
    64.                             // stamp = RosTime.UtcNow(),
    65.                             frame_id = "map"
    66.                         },
    67.                          pose = new PoseMsg
    68.                         {
    69.                             position = new PointMsg
    70.                             {
    71.                                 x = hitPointSend.z,
    72.                                 y = -hitPointSend.x,
    73.                                 z = hitPointSend.y
    74.                             },
    75.                             orientation = new QuaternionMsg
    76.                             {
    77.                                 x = 0,
    78.                                 y = 0,
    79.                                 z = 0,
    80.                                 w = 1
    81.                             }
    82.                         }
    83.                     }
    84.                 }
    85.             };
    86.             ros.Publish(topicName, sendGoal);
    87.         }
    88.     }
    89. }
    90.  
    I will really appreciate any help.