Discover how a simple question-and-answer system can make robots work together like a dream team!
Why Request-response communication model in ROS? - Purpose & Use Cases
Imagine you have two robots that need to talk to each other to get tasks done. One robot asks for information, and the other robot must reply. If you try to handle this by manually sending messages back and forth without a clear system, it quickly becomes confusing and messy.
Manually managing these message exchanges is slow and error-prone. You might miss replies, get messages out of order, or waste time waiting without knowing if the other robot received your request. This leads to robots that don't work well together and tasks that fail.
The request-response communication model in ROS organizes this interaction clearly. One robot sends a request, and the other robot sends back a response. This pattern ensures messages are matched correctly, making communication reliable and easy to manage.
sendMessage('GetStatus') // wait and check for reply manually if (replyReceived) { process(reply) }
response = client.call(request)
if response.success:
handle(response.data)This model enables robots to ask for and receive information or commands in a clear, dependable way, making complex tasks possible through smooth communication.
A robot arm asks a sensor node for the current temperature before deciding how to handle a fragile object. The sensor replies with the temperature, allowing the arm to adjust its grip safely.
Manual message handling is confusing and unreliable.
Request-response model organizes communication clearly.
It makes robot interactions dependable and easier to build.
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
