A service server in ROS lets your robot answer questions or perform tasks when asked. It waits for requests and sends back replies.
Creating a service server in ROS
def handle_request(request): # process the request return response service = rospy.Service('service_name', ServiceType, handle_request) rospy.spin()
The handle_request function processes incoming requests and returns responses.
rospy.Service creates the service server with a name, type, and handler.
def add_two_ints(req): sum = req.a + req.b return AddTwoIntsResponse(sum) service = rospy.Service('add_two_ints', AddTwoInts, add_two_ints) rospy.spin()
def toggle_light(req): if req.state: turn_on_light() else: turn_off_light() return ToggleLightResponse(True) service = rospy.Service('toggle_light', ToggleLight, toggle_light) rospy.spin()
This complete ROS Python node creates a service server named 'add_two_ints'. It waits for requests with two integers, adds them, and sends back the sum. It logs the operation and stays running to serve requests.
import rospy from beginner_tutorials.srv import AddTwoInts, AddTwoIntsResponse def handle_add_two_ints(req): sum = req.a + req.b rospy.loginfo(f"Adding {req.a} + {req.b} = {sum}") return AddTwoIntsResponse(sum) def add_two_ints_server(): rospy.init_node('add_two_ints_server') service = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints) rospy.loginfo("Ready to add two ints.") rospy.spin() if __name__ == '__main__': add_two_ints_server()
Always call rospy.spin() to keep the service server running and ready to handle requests.
Service names should be unique and descriptive to avoid confusion.
Use rospy.loginfo() to print helpful messages for debugging and monitoring.
A service server waits for requests and sends back replies in ROS.
Define a handler function to process requests and return responses.
Create the server with rospy.Service and keep it running with rospy.spin().
Practice
Solution
Step 1: Understand the service server role
A service server in ROS waits for incoming requests from clients and sends back replies after processing.Step 2: Differentiate from other ROS nodes
Unlike publishers or subscribers, a service server handles request-response communication, not continuous data streams.Final Answer:
To wait for requests and send back replies -> Option BQuick Check:
Service server = request and reply [OK]
- Confusing service servers with publishers
- Thinking service servers subscribe to topics
- Assuming service servers only log data
Solution
Step 1: Identify the correct rospy function
The function to create a service server is rospy.Service, which takes the service name, service type, and handler function.Step 2: Exclude other ROS communication methods
Publisher and Subscriber are for topics, Client is not a rospy function for servers.Final Answer:
server = rospy.Service('service_name', ServiceType, handler_function) -> Option DQuick Check:
Service server creation = rospy.Service [OK]
- Using rospy.Publisher instead of rospy.Service
- Confusing service server with subscriber
- Trying to use a non-existent rospy.Client
import rospy
from beginner_tutorials.srv import AddTwoInts, AddTwoIntsResponse
def handle_add_two_ints(req):
return AddTwoIntsResponse(req.a + req.b)
rospy.init_node('add_two_ints_server')
service = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
rospy.spin()Solution
Step 1: Analyze the handler function
The function handle_add_two_ints returns the sum of req.a and req.b wrapped in AddTwoIntsResponse.Step 2: Understand the server behavior on request
When a client sends a request with data=5 (assuming req.a=5 and req.b is provided), the server replies with their sum.Final Answer:
The server returns the sum of req.a and req.b -> Option AQuick Check:
Handler returns sum = The server returns the sum of req.a and req.b [OK]
- Assuming multiplication instead of addition
- Forgetting to return a response object
- Confusing request fields names
import rospy
from beginner_tutorials.srv import AddTwoInts, AddTwoIntsResponse
def handle_add(req):
sum = req.a + req.b
rospy.init_node('add_two_ints_server')
service = rospy.Service('add_two_ints', AddTwoInts, handle_add)
rospy.spin()Solution
Step 1: Check the handler function
The handler function calculates sum but does not return AddTwoIntsResponse, which is required.Step 2: Verify other parts of the code
The service name, node initialization, and service type are correct and valid.Final Answer:
The handler function does not return a response object -> Option AQuick Check:
Handler must return response [OK]
- Forgetting to return AddTwoIntsResponse
- Misnaming the handler function
- Not calling rospy.init_node
Solution
Step 1: Understand the requirement
The server must return the maximum of two integers sent by the client.Step 2: Evaluate each handler function
def handle_max(req): return MaxIntsResponse(max(req.a, req.b)) uses max() but assumes MaxIntsResponse accepts a single argument correctly; def handle_max(req): if req.a > req.b: return MaxIntsResponse(req.a) else: return MaxIntsResponse(req.b) explicitly returns the max value using if-else and returns response properly; def handle_max(req): return MaxIntsResponse(req.a + req.b) returns sum, which is incorrect; def handle_max(req): return MaxIntsResponse(min(req.a, req.b)) returns minimum, which is wrong.Final Answer:
def handle_max(req): if req.a > req.b: return MaxIntsResponse(req.a) else: return MaxIntsResponse(req.b) -> Option CQuick Check:
Correct max logic with proper return = def handle_max(req): if req.a > req.b: return MaxIntsResponse(req.a) else: return MaxIntsResponse(req.b) [OK]
- Returning sum instead of max
- Returning min instead of max
- Incorrectly using max() without proper response
Ready to prove it?
Pass the test and this pattern moves unknown
