Skip to content

Lab 3.2. ROS 2 Services

Objectives

At the end of this self-learning lab, you should be able to:

  • write a service server and client in C++
  • Understand how nodes communicate via services

Communication between nodes via ROS Services: Basic concepts

Recommended reading: ROS 2 Tutorial: Understanding Services

  • In programming, a function has optionally input parameters, a function body and optionally a return value. A function can be "called" by other programs.
  • In ROS, sometimes we wish that a node provides "functions" that other nodes can "call".
    • For example, we may want to have one node that is responsible for performing the function "addition of two integers", and this function can be called by other nodes that are responsible for receive input.
  • A "function provided by a node that other nodes can call" in ROS is called a service.
  • Services in ROS are commonly used to:
    • Trigger some start of an event via the function body
    • Perform some calculation based on input parameters, and return the result
    • Avoid repeated code, especially for features that are used by many nodes
  • The node that hosts the service is called a server.
  • The node that is calling the service is called a client.
  • The input of a service (function parameters) is called a request.
  • The output of a service (return values) is called a response.
  • Similar to communication via topics, we also need to define the format and structure of the request and response of a service. The format (or service type) is called a srv.
    • Note that it is possible for the request and/or response to be blank and not contain any variables. For example, if both are empty, then it is similar to a void function without any parameters.
  • Services are distinguished by their names. For any one service, only one node can be the server of that service. In other words, exactly one node will receive the service request and is responsible for making the service response.
  • Multiple nodes can make requests (be the client) to the same service.
  • The same node can be the server of multiple services and the client of multiple (other) services simultaneously.

ROS 2 Official tutorials about services (Required reading)

You can now follow and self-learn the ROS 2 official tutorials on service servers and client here.