Complete the code to define a ROS service that handles a request and sends a response.
def handle_request(req): response = [1]() response.result = req.a + req.b return response
The service handler creates a response object of type AddTwoIntsResponse to send back the result.
Complete the code to create a ROS service server that uses the handler function.
service = rospy.Service('add_two_ints', [1], handle_request)
The service type is AddTwoInts, which includes both request and response definitions.
Fix the error in the service client call to correctly wait for the service and send a request.
rospy.wait_for_service('add_two_ints') add_two_ints = rospy.ServiceProxy('add_two_ints', [1]) response = add_two_ints(5, 3)
The client uses the service type AddTwoInts when creating the proxy to call the service.
Fill both blanks to define a service handler that checks the request and returns a response.
def handle_request(req): if req.a > 0 and req.b > 0: result = req.a + req.b else: result = 0 return [1](result=[2])
The handler returns an AddTwoIntsResponse object with the result field set.
Fill all three blanks to create a service client that sends a request and prints the response.
rospy.wait_for_service('add_two_ints') add_two_ints = rospy.ServiceProxy('add_two_ints', [1]) request = [2](a=10, b=20) response = add_two_ints(request) print('Sum is:', [3])
The client uses the service type AddTwoInts for the proxy, creates a AddTwoIntsRequest object, and accesses response.result to print the sum.
