liveBook · Manning

Client

As explained in Robotics for Programmers

A service client is the ROS node-side component that invokes a service and receives its response.

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

How Robotics for Programmers covers it

How deep it goes
Explains the client-server RPC model and demonstrates asynchronous Python calls, but does not detail transport internals.
Examples
Uses a ROS 2 Python SetBool client that sends true periodically and logs the returned message.
What it assumes
Assumes familiarity with ROS nodes and Python while establishing services as networked function calls.
What you can do after
Builds a Python ROS 2 node that creates a service client, sends requests asynchronously, and handles responses.
How it connects
Connects clients with ROS services, servers, remote procedure calls, topics, requests, and responses.

Also explained in

Compare how 18 books explain client →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Robotics for Programmers. The section links point to the parts of the book it draws on.