ROS 2 for Robot Arms, step 5
Asking and answering (services)
Ask the robot where the balls are and how to reach them, visit each one, and offer a /go_home service of your own.
Request and response
A topic streams messages to whoever listens. A service answers one question: a client sends a request, one server replies with a response. A .srv file defines both, split by ---:
# crack_interfaces/srv/GetBalls: the balls still on the table, in base_link
---
string[] names
geometry_msgs/Point[] positions
# crack_interfaces/srv/ComputeIK: joint angles that put tool0 at target, gripper down
geometry_msgs/Point target
---
bool success
float64[] joint_positions # [base, shoulder, elbow, wrist]
string message
# std_srvs/srv/Trigger
---
bool success
string message
This robot serves /detect_balls (GetBalls) and /compute_ik (ComputeIK).
Calling without waiting
self.balls = self.create_client(GetBalls, "/detect_balls")
future = self.balls.call_async(GetBalls.Request())
future.add_done_callback(self.on_balls)
call_async sends the request and returns a future at once: a box the answer lands in later. The executor fills it while your node spins, then runs the done callback, where future.result() is the response. wait_for_service() waits until a server exists; a request sent before then is lost.
Why not call()?
client.call(request) waits for the response. But the response can only be delivered by the executor, which is busy running the callback that's waiting: a deadlock. On a robot the node just hangs; here call() raises an error that explains it.
Serving
self.create_service(Trigger, "/go_home", self.on_go_home)
The callback gets (request, response), fills in response and returns it. Answer quickly: set a goal and reply, and let the motion loop do the moving. Long jobs that report progress are actions (step 7).
Your task
The Mover node has a motion loop: it moves towards self.goal and takes waypoints from self.queue.
- Ask
/detect_balls, then/compute_ikfor the pointHOVER(10 cm) above each ball, and queue the answers'joint_positions. - Serve
/go_home: forget the queue, head forHOME, and answersuccess = True.
Visit all four before the grader calls /go_home at 8 s.