Services and topics help different parts of a robot talk to each other. Services are for quick questions and answers. Topics are for sharing ongoing information.
You want to ask a robot to do something and wait for a reply, like turning on a light.
You want to send sensor data continuously, like camera images or laser scans.
You need to send commands that happen once and get a confirmation back.
You want to share status updates or measurements that many parts can listen to at the same time.
You want to trigger an action only when requested, not all the time.
This code creates a service that turns a light on or off when asked. It waits for requests and replies with success messages.
ROS
# Service server example in Pythonimport rospy
from std_srvs.srv import SetBool, SetBoolResponse
def handle_toggle_light(req):
if req.data:
rospy.loginfo("Light turned ON")
return SetBoolResponse(success=True, message="Light is ON")
else:
rospy.loginfo("Light turned OFF")
return SetBoolResponse(success=True, message="Light is OFF")
def toggle_light_server():
rospy.init_node('toggle_light_server')
s = rospy.Service('toggle_light', SetBool, handle_toggle_light)
rospy.loginfo("Ready to toggle light.")
rospy.spin()
if __name__ == "__main__":
toggle_light_server()
OutputSuccess
Important Notes
Use services when you need a clear answer right away.
Use topics when you want to share data regularly or with many listeners.
Services block the caller until a response comes back; topics do not.
Summary
Services are for one-time requests with replies.
Topics are for continuous data sharing.
Choose based on whether you need a quick answer or ongoing updates.
Practice
(1/5)
1. In ROS, when should you use a service instead of a topic?
easy
A. When you want to continuously stream sensor data.
B. When you need a single request and a single response.
C. When you want to broadcast messages to many nodes.
D. When you want to log data over time.
Solution
Step 1: Understand service communication
Services in ROS are designed for one-time request and reply interactions between nodes.
Step 2: Compare with topic usage
Topics are used for continuous data streaming or broadcasting messages to multiple subscribers.
Final Answer:
When you need a single request and a single response. -> Option B
A. True if the service call succeeds, otherwise an error message.
B. Always prints False.
C. SyntaxError due to missing import.
D. No output because the service is not called.
Solution
Step 1: Analyze service call behavior
The code waits for the 'toggle_led' service, then calls it with argument True, and prints the success field from the response.
Step 2: Understand possible outputs
If the service call succeeds, response.success will be True or False depending on service logic; if it fails, an exception message prints.
Final Answer:
True if the service call succeeds, otherwise an error message. -> Option A
Quick Check:
Service call prints success or error [OK]
Hint: ServiceProxy call prints success field if no error [OK]
Common Mistakes:
Assuming service call always returns False
Confusing service call with topic subscription
Ignoring exception handling
4. You wrote a ROS node that uses a service client but it never receives a response. What is the most likely cause?
medium
A. The service server is not running or not advertised.
B. The topic name is misspelled.
C. The node is publishing to the wrong topic.
D. The callback function is missing in the subscriber.
Solution
Step 1: Identify service client behavior
A service client waits for a service server to be available and respond to requests.
Step 2: Diagnose no response issue
If no response arrives, the service server might not be running or the service name is incorrect, so the client cannot connect.
Final Answer:
The service server is not running or not advertised. -> Option A
Quick Check:
No response means server missing [OK]
Hint: No service response? Check if server is running [OK]
Common Mistakes:
Confusing service client with topic subscriber
Checking wrong topic instead of service
Ignoring service server status
5. You want to control a robot arm to move to a specific position and wait for confirmation before sending the next command. Which ROS communication method should you use and why?
hard
A. Use a topic because continuous streaming of positions is required.
B. Use a service because topics do not support multiple subscribers.
C. Use a topic because services cannot send commands.
D. Use a service because you need a request and a reply for each move command.
Solution
Step 1: Understand the control requirement
The robot arm needs to receive a move command and confirm completion before the next command.
Step 2: Match communication type to requirement
Services provide a request-response pattern ideal for commands needing confirmation, unlike topics which are for continuous data streaming without guaranteed replies.
Final Answer:
Use a service because you need a request and a reply for each move command. -> Option D
Quick Check:
Command with confirmation = service [OK]
Hint: Commands needing reply? Use services [OK]
Common Mistakes:
Choosing topics for commands needing confirmation
Thinking services can't send commands
Confusing subscriber limits with communication type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.