Ask and answer
Goal: build a service that stops and resumes the robot while it drives.
The idea
A topic is a stream: messages keep flowing, and nobody waits for an answer. Sometimes you want a question and an answer: "stop now", and a reply that says "done". That is a service.
A program offers a service under a name. Another program, the client, sends a request and waits for the response. The request and the response have a type, like messages do. Yours uses SetBool: the request holds one field, data (True or False), and the response holds success (did it work?) and message (a text).
Look at it live
- Run
ros2 service list -tto see the services that exist and their types. - Run
ros2 interface show std_srvs/srv/SetBool. The part above the line of three dashes is the request, the part below is the response.
Read the code
This service switches a "turbo" mode on and off. The server is a function that receives the request and fills in the response.
from std_srvs.srv import SetBool
# in __init__: offer the service (type, name, the function to call)
self.srv = self.create_service(SetBool, 'set_turbo', self.set_turbo_callback)
self.turbo = False
def set_turbo_callback(self, request, response):
self.turbo = request.data # use what the client sent
response.success = True # fill in the response
response.message = 'turbo is on' if request.data else 'turbo is off'
return response # always return it
- The callback is called when a client sends a request. It must
return response, or the client waits forever. - The node keeps its state in an attribute (
self.turbo). Its timer reads it on every tick.
Your mission
Open starter_kit/ask_and_answer.py. It is the complete driver: it drives, brakes in front of obstacles, and obeys an emergency stop (E-Stop).
Part A: the E-Stop service
- TODO 1: create a service called
toggle_estopusingSetBool. - TODO 2: store the E-Stop state in
self.estopped. - TODO 3: in
timer_callback, checkself.estoppedfirst and publish zero velocity when it isTrue. - TODO 4: in
toggle_estop_callback, setself.estoppedfromrequest.data, fill inresponse.successandresponse.message, and returnresponse.
Part B: bring BONK back
- TODO 6: like Challenge 4, declare the
bonk_thresholdparameter, subscribe to/scan, and finishscan_callback. It only setsself.blocked.
One driver at a time
Stop any other driver with Ctrl+C before you start this one.
Run your node with ros2 run starter_kit ask_and_answer. Then, from a second terminal while the robot is driving:
ros2 service call /toggle_estop std_srvs/srv/SetBool "{data: true}"
ros2 service call /toggle_estop std_srvs/srv/SetBool "{data: false}"
What you should see
Sending true halts the robot within one publish cycle, and the call prints your success and message. Sending false resumes the motion, with no restart. The robot still stops in front of obstacles.
Topics or services?
Topics (publisher and subscriber, asynchronous) carry a continuous stream. They are ideal for sensor data (/scan, odometry) and continuous movement (/cmd_vel).
Services (server and client, synchronous) answer an on-demand request. They are ideal for discrete state changes, triggering a computation, or gating a behavior like the E-Stop.
Stuck?
Hint 1
ros2 interface show std_srvs/srv/SetBool shows what the request and the response contain.
Hint 2
If the call never returns, your callback probably forgot to return response.
Hint 3
ros2 service list should show /toggle_estop while your node runs. If it does not, check TODO 1.
Submit
When it works, open the Submit tab, choose 5 · Ask and Answer, and submit.