using RosMessageTypes.UnityRoboticsDemo; using UnityEngine; using Unity.Robotics.ROSTCPConnector; using Unity.Robotics.ROSTCPConnector.ROSGeometry; /// /// Example demonstration of implementing a UnityService that receives a Request message from another ROS node and sends a Response back /// public class RosUnityServiceExample : MonoBehaviour { ROSConnection ros; [SerializeField] string m_ServiceName = "obj_pose_srv"; void Start() { // register the service with ROS ros = ROSConnection.instance; ros.ImplementService(m_ServiceName, GetObjectPose); } /// /// Callback to respond to the request /// /// service request containing the object name /// service response containing the object pose (or 0 if object not found) private ObjectPoseServiceResponse GetObjectPose(ObjectPoseServiceRequest request) { // process the service request Debug.Log("Received request for object: " + request.object_name); // prepare a response ObjectPoseServiceResponse objectPoseResponse = new ObjectPoseServiceResponse(); // Find a game object with the requested name GameObject gameObject = GameObject.Find(request.object_name); if (gameObject) { // Fill-in the response with the object pose converted from Unity coordinate to ROS coordinate system objectPoseResponse.object_pose.position = gameObject.transform.position.To(); objectPoseResponse.object_pose.orientation = gameObject.transform.rotation.To(); } return objectPoseResponse; } }