Challenge - 5 Problems
ROS Service Client Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat is the output of this ROS service client code?
Consider a ROS service client that calls a service named
/add_two_ints which adds two integers. What will be printed if the service returns the sum correctly?ROS
import rospy from beginner_tutorials.srv import AddTwoInts def add_two_ints_client(x, y): rospy.wait_for_service('/add_two_ints') try: add_two_ints = rospy.ServiceProxy('/add_two_ints', AddTwoInts) resp = add_two_ints(x, y) print(f"Sum is: {resp.sum}") except rospy.ServiceException as e: print(f"Service call failed: {e}") if __name__ == '__main__': rospy.init_node('add_two_ints_client') add_two_ints_client(3, 4)
Attempts:
2 left
💡 Hint
Think about what the service is supposed to do with the inputs 3 and 4.
✗ Incorrect
The service
/add_two_ints adds the two integers 3 and 4, so the sum is 7. The client prints this sum.📝 Syntax
intermediateWhich option correctly creates a ROS service client for a service named '/multiply' with type 'MultiplyTwoInts'?
Select the code snippet that correctly creates a ROS service client for the service '/multiply' with service type 'MultiplyTwoInts'.
Attempts:
2 left
💡 Hint
Remember that ServiceProxy is used to create a client, and the order is (service_name, service_type).
✗ Incorrect
Option C correctly uses rospy.ServiceProxy with the service name first and the service type second. Options B and D use rospy.Service which is for creating a service server, not a client. Option C reverses the arguments.
🔧 Debug
advancedWhat error does this ROS service client code raise?
Given the following code snippet, what error will occur when running it if the service '/divide' is not available?
ROS
import rospy from beginner_tutorials.srv import DivideTwoInts def divide_client(a, b): divide = rospy.ServiceProxy('/divide', DivideTwoInts) resp = divide(a, b) print(f"Result: {resp.result}") if __name__ == '__main__': rospy.init_node('divide_client') divide_client(10, 2)
Attempts:
2 left
💡 Hint
What happens if you try to call a service that hasn't started yet?
✗ Incorrect
If the service '/divide' is not available, rospy.wait_for_service (which is missing here) would raise rospy.ROSException. Since wait_for_service is not called, the ServiceProxy call will block or raise ROSException indicating the service is not available.
❓ state_output
advancedWhat is the value of variable 'response' after this ROS service client call?
Given the service client call below, what is the type and content of the variable
response?ROS
import rospy from beginner_tutorials.srv import AddTwoInts rospy.init_node('test_client') add_two_ints = rospy.ServiceProxy('/add_two_ints', AddTwoInts) response = add_two_ints(5, 6)
Attempts:
2 left
💡 Hint
ServiceProxy returns a response object, not a simple type.
✗ Incorrect
The response is an instance of AddTwoIntsResponse with the attribute sum set to the sum of the inputs, here 11.
🧠 Conceptual
expertWhich statement about ROS service clients is true?
Select the correct statement about ROS service clients.
Attempts:
2 left
💡 Hint
Think about what happens if the service is not ready when the client calls it.
✗ Incorrect
Clients should call rospy.wait_for_service to wait until the service is available before calling it. Without this, the call may fail or block indefinitely. Clients must initialize a ROS node before calling services. Clients do not retry automatically. Services can be called across packages.
