A ROS service client is the node-side component that initiates a remote procedure call, sends request parameters, and receives the server’s return value. Like a function call across a network, the interaction connects one client to one service server and commonly waits for processing to finish. section 4.6
A client typically:
- Creates a client for a named service and its interface type. section 4.6
- Builds a request, sets its fields, and invokes the service asynchronously. section 4.6
- Processes the returned response in a callback, such as logging a success message. section 4.6
Calling SetBool
A Python ROS 2 node creates a SetBool client for my_set_bool_service, periodically sets request.data to true, calls call_async(request), and logs response.message when the response arrives. section 4.6
Listing 4.4
import rclpy
from rclpy.node import Node
from std_srvs.srv import SetBool
class MyRosSrvClient(Node):
def __init__(self):
super().__init__('my_ros_srv_client')
self.cli = self.create_client(SetBool, 'my_set_bool_service') # #1
self.timer = self.create_timer(5.0, self.timer_callback) # #2
def timer_callback(self):
request = SetBool.Request() # #3
request.data = True
response_future = self.cli.call_async(request) # #4
response_future.add_done_callback(self.response_callback) # #5
def response_callback(self, response_future): # #6
response = response_future.result() # #7
self.get_logger().info(
'Response from my_set_bool_service: %s' % response.message)
def main():
rclpy.init()
my_ros_srv_client = MyRosSrvClient() # #8
my_ros_srv_client.get_logger().info('Running MyRosSrvClient.')
rclpy.spin(my_ros_srv_client)
rclpy.shutdown()
if __name__ == '__main__':
main()
Inspect the complete client pattern, including node setup, timer-driven requests, asynchronous invocation, and response handling. section 4.6

















