The request-response communication model lets one part ask for information or action, and another part reply back. It helps different parts of a robot system talk clearly and get answers.
Request-response communication model in ROS
ros::ServiceServer service = nh.advertiseService("service_name", &callbackFunction); bool callbackFunction(RequestType &req, ResponseType &res) { // process request // fill response return true; // success }
The service server waits for requests and sends back responses.
The callback function handles the request and prepares the response.
bool addTwoInts(AddTwoInts::Request &req, AddTwoInts::Response &res) { res.sum = req.a + req.b; return true; }
ros::ServiceClient client = nh.serviceClient<AddTwoInts>("add_two_ints"); AddTwoInts srv; srv.request.a = 5; srv.request.b = 3; if (client.call(srv)) { ROS_INFO("Sum: %d", srv.response.sum); }
This ROS node creates a service called "add_two_ints". When it receives a request with two numbers, it adds them and sends back the sum.
#include "ros/ros.h" #include "beginner_tutorials/AddTwoInts.h" bool add(beginner_tutorials::AddTwoInts::Request &req, beginner_tutorials::AddTwoInts::Response &res) { res.sum = req.a + req.b; ROS_INFO("Adding %ld + %ld", (long int)req.a, (long int)req.b); return true; } int main(int argc, char **argv) { ros::init(argc, argv, "add_two_ints_server"); ros::NodeHandle nh; ros::ServiceServer service = nh.advertiseService("add_two_ints", add); ROS_INFO("Ready to add two ints."); ros::spin(); return 0; }
Services are synchronous: the client waits for the server to respond.
Use services for tasks that need a clear answer, not continuous data streams.
Make sure request and response types match between client and server.
Request-response model lets one part ask and another reply.
Services in ROS implement this model with request and response messages.
Use services for commands or queries needing a direct answer.
Practice
Solution
Step 1: Understand the request-response model in ROS
This model involves one node sending a request message and waiting for a response from another node.Step 2: Compare options with this model
Only One node sends a request and waits for a reply from another node describes this direct ask-and-reply behavior, while others describe different communication types.Final Answer:
One node sends a request and waits for a reply from another node -> Option AQuick Check:
Request-response = direct ask and reply [OK]
- Confusing request-response with continuous publishing
- Thinking request-response is broadcasting
- Assuming no reply is needed
Solution
Step 1: Recall ROS Python service server syntax
In ROS, a service server is created using rospy.Service with the service name, type, and handler function.Step 2: Identify correct function
Only rospy.Service matches this pattern; Publisher and Subscriber are for topics, Client is not a rospy function.Final Answer:
service = rospy.Service('service_name', ServiceType, handler_function) -> Option AQuick Check:
Service server uses rospy.Service [OK]
- Using Publisher or Subscriber instead of Service
- Confusing client and server creation
- Wrong function names like rospy.Client
import rospy
from example.srv import AddTwoInts
def call_service():
rospy.wait_for_service('add_two_ints')
try:
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
response = add_two_ints(20, 22)
print(response.result)
except rospy.ServiceException as e:
print("Service call failed", e)
call_service()Solution
Step 1: Understand the service client call
The client waits for the service, calls it with 20 and 22, and expects a response with a 'result' attribute.Step 2: Analyze the response printing
The code prints response.result, which is given as 42 in the question.Final Answer:
42 -> Option BQuick Check:
Print response.result = 42 [OK]
- Confusing request parameters with response
- Expecting print of input values instead of response
- Assuming exception without error
import rospy
from example.srv import Multiply
def handle_multiply(req):
return req.a * req.b
rospy.init_node('multiply_server')
service = rospy.Service('multiply', Multiply, handle_multiply)
rospy.spin()Solution
Step 1: Check the handler function return type
In ROS services, the handler must return a response message object, not a raw value.Step 2: Identify the error in return statement
Returning req.a * req.b (a number) is incorrect; it should return MultiplyResponse(req.a * req.b) or equivalent.Final Answer:
The handler must return a response object, not just a number -> Option CQuick Check:
Handler returns response object, not raw value [OK]
- Returning raw values instead of response objects
- Forgetting to import response message class
- Missing rospy.spin() (though present here)
Solution
Step 1: Understand the condition for sum
The sum should be returned only if both req.a and req.b are positive (greater than zero).Step 2: Check each option's condition logic
def handle_sum(req): if req.a > 0 and req.b > 0: return SumTwoIntsResponse(req.a + req.b) else: return SumTwoIntsResponse(0) uses 'and' to check both positive, returning sum or zero correctly. def handle_sum(req): return SumTwoIntsResponse(req.a + req.b if req.a > 0 or req.b > 0 else 0) uses 'or' incorrectly and missing parentheses in ternary. def handle_sum(req): if req.a > 0 or req.b > 0: return SumTwoIntsResponse(req.a + req.b) else: return SumTwoIntsResponse(0) uses 'or' which is wrong logic. def handle_sum(req): return SumTwoIntsResponse(req.a + req.b) always returns sum ignoring condition.Final Answer:
def handle_sum(req): if req.a > 0 and req.b > 0: return SumTwoIntsResponse(req.a + req.b) else: return SumTwoIntsResponse(0) -> Option DQuick Check:
Both positive check with 'and' = def handle_sum(req): if req.a > 0 and req.b > 0: return SumTwoIntsResponse(req.a + req.b) else: return SumTwoIntsResponse(0) [OK]
- Using 'or' instead of 'and' for both positive check
- Returning sum without condition
- Incorrect ternary syntax in return
Ready to prove it?
Pass the test and this pattern moves unknown
