📖 Scenario: You are building a ROS node that needs to request data from another node using a service. This is common in robotics when one node needs to ask another node for information or to perform an action.
🎯 Goal: Create a ROS service client node that calls a service named /add_two_ints which adds two integers and returns the sum.
📋 What You'll Learn
Create a ROS node named add_two_ints_client
Create a service client for the service /add_two_ints using the AddTwoInts service type
Send a request with two integers: a = 5 and b = 3
Receive and handle the response from the service
Print the sum received from the service
💡 Why This Matters
🌍 Real World
Robots often need to request information or commands from other nodes. Service clients let one node ask another for specific tasks, like adding numbers or moving a robot arm.
💼 Career
Understanding how to create and use service clients is essential for robotics software engineers working with ROS to build modular and interactive robot applications.
Progress0 / 4 steps
1
Set up the ROS node and import necessary modules
Write code to import rclpy and AddTwoInts from example_interfaces.srv. Then initialize the ROS client library and create a node named add_two_ints_client.
ROS
Hint
Use rclpy.init() to start ROS and Node('add_two_ints_client') to create the node.
2
Create the service client for /add_two_ints
Create a service client named client for the service /add_two_ints using the AddTwoInts service type on the node node.
ROS
Hint
Use node.create_client() with the service type and service name.
3
Send a request with two integers and wait for the service
Create a request object from AddTwoInts.Request() and set a = 5 and b = 3. Wait for the service to be available using client.wait_for_service().
ROS
Hint
Create the request object and assign values before waiting for the service.
4
Call the service and handle the response
Call the service asynchronously with client.call_async(request) and wait for the future to complete using rclpy.spin_until_future_complete(node, future). Then print the sum from future.result().sum. Finally, destroy the node and shutdown rclpy.
ROS
Hint
Use call_async to send the request and spin_until_future_complete to wait for the reply.
Practice
(1/5)
1. What is the main purpose of creating a service client in ROS?
easy
A. To request data or actions from another node and receive a response
B. To publish messages to a topic continuously
C. To subscribe and listen to messages from a topic
D. To launch multiple nodes at once
Solution
Step 1: Understand service client role
A service client in ROS is used to send a request to another node and wait for a reply.
Step 2: Compare with other communication types
Publishing and subscribing are for continuous message streams, not request-response.
Final Answer:
To request data or actions from another node and receive a response -> Option A
Quick Check:
Service client = request and response [OK]
Hint: Service clients ask and wait for answers, unlike topics [OK]
Common Mistakes:
Confusing service clients with publishers or subscribers
Thinking service clients launch nodes
Assuming service clients send continuous data
2. Which of the following is the correct way to create a service client in ROS Python?
easy
A. client = rospy.ServiceProxy('service_name', ServiceType)
B. client = rospy.ServiceClient('service_name', ServiceType)
C. client = rospy.Service('service_name', ServiceType, callback)
D. client = rospy.Publisher('service_name', ServiceType)
Solution
Step 1: Recall ROS Python service client syntax
In ROS Python, service clients are created using rospy.ServiceProxy with the service name and type.
Step 2: Identify incorrect options
ServiceClient does not exist, Service is for servers, Publisher is for topics.
Final Answer:
client = rospy.ServiceProxy('service_name', ServiceType) -> Option A
Quick Check:
Service client uses ServiceProxy [OK]
Hint: Service clients use ServiceProxy, servers use Service [OK]
Common Mistakes:
Using rospy.ServiceClient which does not exist
Confusing service client with service server creation
Using Publisher instead of ServiceProxy
3. Given this ROS Python code snippet, what will be printed if the service responds with success=True?
B. ServiceProxy should be called with a string, not a service type
C. Missing rospy.init_node() before creating client
D. The response attribute 'result' may not exist
Solution
Step 1: Check ROS node initialization
ROS nodes must call rospy.init_node() before using any ROS features like ServiceProxy.
Step 2: Verify other parts
ServiceProxy usage is correct, service name format is valid, response attribute depends on service definition but missing init_node is a critical error.
Final Answer:
Missing rospy.init_node() before creating client -> Option C
Quick Check:
Always init node before service client [OK]
Hint: Always call rospy.init_node() before creating clients [OK]
Common Mistakes:
Forgetting to initialize the ROS node
Confusing service type and name parameters
Assuming response attributes without checking service definition
5. You want to create a ROS service client that waits for the service to be available, sends a request asynchronously, and processes the response without blocking the main program. Which approach correctly implements this behavior?
hard
A. Create client with rospy.ServiceProxy, call client() inside a separate thread to avoid blocking
B. Use rospy.wait_for_service(), then call client.call_async() and add a callback to handle response
C. Use rospy.wait_for_service(), then call client() synchronously and block until response
D. Call client() repeatedly in a loop until response is received
Solution
Step 1: Wait for service availability
Using rospy.wait_for_service() ensures the service is ready before sending requests.
Step 2: Use asynchronous call with callback
client.call_async() allows sending request without blocking; a callback handles the response smoothly.
Step 3: Avoid blocking main program
Calling client() synchronously or looping blocks the program; threading is possible but less clean than async calls.
Final Answer:
Use rospy.wait_for_service(), then call client.call_async() and add a callback to handle response -> Option B
Quick Check:
Async call with callback = non-blocking client [OK]
Hint: Use call_async() with callback for smooth non-blocking calls [OK]
Common Mistakes:
Calling client() synchronously and blocking program
Using loops to wait for response instead of async
Not waiting for service before calling client
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.