Bird
Raised Fist0
ROSframework~10 mins

Why services handle request-response interactions in ROS - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a ROS service that handles a request and sends a response.

ROS
def handle_request(req):
    response = [1]()
    response.result = req.a + req.b
    return response
Drag options to blanks, or click blank then click option'
AAddTwoIntsResponse
BAddTwoIntsRequest
CService
DPublisher
Attempts:
3 left
💡 Hint
Common Mistakes
Using the request type instead of the response type
Returning nothing from the handler
2fill in blank
medium

Complete the code to create a ROS service server that uses the handler function.

ROS
service = rospy.Service('add_two_ints', [1], handle_request)
Drag options to blanks, or click blank then click option'
AAddTwoIntsRequest
BAddTwoIntsResponse
CAddTwoIntsService
DAddTwoInts
Attempts:
3 left
💡 Hint
Common Mistakes
Using only the request or response type instead of the service type
3fill in blank
hard

Fix the error in the service client call to correctly wait for the service and send a request.

ROS
rospy.wait_for_service('add_two_ints')
add_two_ints = rospy.ServiceProxy('add_two_ints', [1])
response = add_two_ints(5, 3)
Drag options to blanks, or click blank then click option'
AAddTwoInts
BAddTwoIntsResponse
CAddTwoIntsRequest
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using only request or response types in the proxy
Not waiting for the service before calling
4fill in blank
hard

Fill both blanks to define a service handler that checks the request and returns a response.

ROS
def handle_request(req):
    if req.a > 0 and req.b > 0:
        result = req.a + req.b
    else:
        result = 0
    return [1](result=[2])
Drag options to blanks, or click blank then click option'
AAddTwoIntsResponse
BAddTwoIntsRequest
Cresult
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the request type
Using wrong field names in the response
5fill in blank
hard

Fill all three blanks to create a service client that sends a request and prints the response.

ROS
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])
Drag options to blanks, or click blank then click option'
AAddTwoInts
BAddTwoIntsRequest
Cresponse.result
DAddTwoIntsResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using response type for proxy
Not creating a request object
Printing the whole response instead of the result field

Practice

(1/5)
1. Why do ROS services handle request-response interactions instead of continuous data streams?
easy
A. Because services continuously send data without waiting
B. Because services are designed for immediate answers to specific requests
C. Because services only work with sensor data streams
D. Because services store data for later use

Solution

  1. Step 1: Understand the purpose of ROS services

    ROS services are meant to handle specific requests and provide immediate responses, unlike topics which handle continuous data streams.
  2. Step 2: Compare services with other communication methods

    Services wait for a request, process it, and send back a response, making them ideal for commands or queries needing quick answers.
  3. Final Answer:

    Because services are designed for immediate answers to specific requests -> Option B
  4. Quick Check:

    Request-response = immediate answers [OK]
Hint: Services = ask once, get answer once [OK]
Common Mistakes:
  • Confusing services with continuous data streams
  • Thinking services store data permanently
  • Assuming services only handle sensor data
2. Which of the following is the correct way to define a ROS service in code?
easy
A. service = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
B. service = rospy.Publisher('add_two_ints', AddTwoInts, queue_size=10)
C. service = rospy.Subscriber('add_two_ints', AddTwoInts, callback)
D. service = rospy.ActionServer('add_two_ints', AddTwoInts, execute_cb)

Solution

  1. Step 1: Identify the correct ROS service creation syntax

    In ROS Python, a service is created using rospy.Service with the service name, service type, and handler function.
  2. Step 2: Differentiate from other communication types

    Publisher and Subscriber are for topics, ActionServer is for actions, not services.
  3. Final Answer:

    service = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints) -> Option A
  4. Quick Check:

    rospy.Service creates services [OK]
Hint: Services use rospy.Service, not Publisher or Subscriber [OK]
Common Mistakes:
  • Using Publisher or Subscriber instead of Service
  • Confusing ActionServer with Service
  • Missing the handler function argument
3. Given this ROS service client code snippet, what will be the output if the service returns sum=7?
import rospy
from beginner_tutorials.srv import AddTwoInts
rospy.wait_for_service('add_two_ints')
add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
response = add_two_ints(3, 4)
print(response.sum)
medium
A. 3
B. 4
C. 7
D. None

Solution

  1. Step 1: Understand the service call and response

    The client calls the service with arguments 3 and 4, expecting the service to add them and return the sum.
  2. Step 2: Check the printed output

    The response.sum contains the result from the service, which is 7, so print(response.sum) outputs 7.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    3 + 4 = 7 output [OK]
Hint: Service response holds result in response.sum [OK]
Common Mistakes:
  • Printing input arguments instead of response
  • Assuming response is None before service call
  • Confusing service call with topic publish
4. What is wrong with this ROS service server code?
def handle_add_two_ints(req):
    return req.a + req.b

service = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
rospy.spin()
medium
A. The handler function is missing parameters
B. The service name is invalid
C. rospy.spin() should be called before service creation
D. The handler must return a response object, not just a sum

Solution

  1. Step 1: Check the handler function return type

    The handler must return a response object of the service type, not just a raw sum value.
  2. Step 2: Understand ROS service response structure

    Returning only req.a + req.b causes an error; the correct return is AddTwoIntsResponse(req.a + req.b).
  3. Final Answer:

    The handler must return a response object, not just a sum -> Option D
  4. Quick Check:

    Handler returns response object [OK]
Hint: Handler returns response object, not raw value [OK]
Common Mistakes:
  • Returning raw values instead of response objects
  • Calling rospy.spin() too early
  • Incorrect service name format
5. You want to design a ROS system where a robot arm must receive a command and confirm completion immediately. Why is using a service better than a topic for this interaction?
hard
A. Because services provide a direct request-response, ensuring the command was received and processed
B. Because topics automatically guarantee message delivery and response
C. Because topics are faster for one-time commands
D. Because services can send continuous sensor data streams

Solution

  1. Step 1: Analyze the communication needs for command and confirmation

    The robot arm needs to know immediately if the command was received and completed, requiring a synchronous interaction.
  2. Step 2: Compare services and topics for this use case

    Services provide a request-response pattern, confirming completion, while topics are asynchronous and do not guarantee immediate feedback.
  3. Final Answer:

    Because services provide a direct request-response, ensuring the command was received and processed -> Option A
  4. Quick Check:

    Request-response ensures confirmation [OK]
Hint: Use services for commands needing immediate confirmation [OK]
Common Mistakes:
  • Thinking topics guarantee immediate response
  • Using services for continuous data streaming
  • Confusing speed with communication pattern
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard