using System; using RosMessageTypes.RoboticsDemo; using UnityEngine; using Debug = UnityEngine.Debug; public class RosServiceExample : MonoBehaviour { public ROSConnection ros; public string serviceName = "pos_srv"; public GameObject cube; // Cube movement conditions public float delta = 1.0f; public float speed = 2.0f; private Vector3 destination; float awaitingResponseUntilTimestamp = -1; void Start() { destination = cube.transform.position; } private void Update() { // Move our position a step closer to the target. float step = speed * Time.deltaTime; // calculate distance to move cube.transform.position = Vector3.MoveTowards(cube.transform.position, destination, step); if (Vector3.Distance(cube.transform.position, destination) < delta && Time.time > awaitingResponseUntilTimestamp) { Debug.Log("Destination reached."); PosRot cubePos = new PosRot( cube.transform.position.x, cube.transform.position.y, cube.transform.position.z, cube.transform.rotation.x, cube.transform.rotation.y, cube.transform.rotation.z, cube.transform.rotation.w ); PositionServiceRequest positionServiceRequest = new PositionServiceRequest(cubePos); // Send message to ROS and return the response ros.SendServiceMessage(serviceName, positionServiceRequest, Callback_Destination); awaitingResponseUntilTimestamp = Time.time+1.0f; // don't send again for 1 second, or until we receive a response } } void Callback_Destination(PositionServiceResponse response) { awaitingResponseUntilTimestamp = -1; destination = new Vector3(response.output.pos_x, response.output.pos_y, response.output.pos_z); Debug.Log("New Destination: " + destination); } }