ros2 service Command Line Tool – Debug ROS2 Services

In this tutorial you’ll use the ros2 service command line tool to debug the services that you start from within your nodes – or nodes that already exist in your graph.

As a reminder, a service has 2 sides: the server (unique), and the client (multiple clients possible).

With topics, you can use ros2 topic to debug publishers as well as subscribers. However, with services, you’ll be able to only debug Service Servers, and place yourself as a client.

>> Watch this video as an additional resource to this article:

After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!


You want to learn ROS2 efficiently?

Check out ROS2 For Beginners and learn ROS2 step by step, in 1 week.


The node we’ll use

Let’s write a very simple node with a service server.

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from example_interfaces.srv import SetBool

class ActivateRobotNode(Node):
    def __init__(self):
        super().__init__("activate_robot_server")
        self.activated_ = False
        self.service_ = self.create_service(
            SetBool, "activate_robot", self.callback_activate_robot)

    def callback_activate_robot(self, request, response):
        self.activated_ = request.data
        response.success = True
        if self.activated_:
            response.message = "Robot has been activated"
        else:
            response.message = "Robot has been deactivated"
        return response

def main(args=None):
    rclpy.init(args=args)
    node = ActivateRobotNode()
    rclpy.spin(node)
    rclpy.shutdown()

if __name__ == "__main__":
    main()

Now, install and run this node:

  • Add a line to your setup.py in the ‘console_scripts’ array (as this is a Python node in a Python package): "activate_robot_node = ros2_tutorials_py.activate_robot:main" .
  • Install the executable with colcon build, inside your ROS2 workspace.
  • Source your ROS2 workspace.
  • Start the node with ros2 run ros2_tutorials_py activate_robot_node.

ros2 service list – Find all the Services available on your graph

You can get all the services that are currently advertised in your ROS2 graph/network.

$ ros2 service list
/activate_robot
/activate_robot_server/describe_parameters
/activate_robot_server/get_parameter_types
/activate_robot_server/get_parameters
/activate_robot_server/list_parameters
/activate_robot_server/set_parameters
/activate_robot_server/set_parameters_atomically

Here we find the name of the service we created: /activate_robot.

And you can see 6 more services! Those 6 services are automatically created for each node. They will allow you to manage (get/set) parameters which are specific to this node, here the “activate_robot_server” node.

ros2 service type – See what you need to send/receive to use the Service

Once you know the name of the service, you’ll also need to find what kind of data you have to send/receive to use the service. Use ros2 service type to get that information.

$ ros2 service type /activate_robot
example_interfaces/srv/SetBool

And when you have the interface name, you can find the details with ros2 interface show.

$ ros2 interface show example_interfaces/srv/SetBool 
# This is an example of a service to set a boolean value.
# This can be used for testing but a semantically meaningful
# one should be created to be built upon.

bool data # e.g. for hardware enabling / disabling
---
bool success   # indicate successful run of triggered service
string message # informational, e.g. for error messages

So, now you have all the info you need to interact with this service server.

ros2 service call – Test a Service Server from the terminal

This command line tool is very useful so you can test your application without having to write nodes for both sides of the communication. Once you’ve created the service server within a node, you can start sending requests to this service with ros2 service call.

You will need to know the name of the service, the name of the interface, and the details of that interface. Well, we just got all those info from the previous steps!

To call a service, type: ros2 + service + call + service name + service type + request.

$ ros2 service call /activate_robot example_interfaces/srv/SetBool "{data: True}"
requester: making request: example_interfaces.srv.SetBool_Request(data=True)

response:
example_interfaces.srv.SetBool_Response(success=True, message='Robot has been activated')

...

$ ros2 service call /activate_robot example_interfaces/srv/SetBool "{data: False}"
waiting for service to become available...
requester: making request: example_interfaces.srv.SetBool_Request(data=False)

response:
example_interfaces.srv.SetBool_Response(success=True, message='Robot has been deactivated')

For the request, use quotes and curly brackets “{}”, and put all the fields of the request inside, with YAML syntax.

This command line tool will:

  • Wait for the service to become available – try to use ros2 service call when the node containing the server is not up. The command will hang until you start the server node again.
  • Create a service client, a request, and send the request to the server.
  • Wait for the server to process the request and respond.
  • Once the server has sent the response, print the response, and exit.

As you can see ros2 service call is really practical, so you don’t have to create a new node doing all those steps, just to test the server.

You can use this command to test different behaviors of the server, by sending different data inside the request.

Once you start having a bigger application with many nodes and services running on your robot, you can also use this command to trigger some actions, or simulate a behavior in your robot for live testing.

Find service info directly from a node’s name

One additional thing to know: you can also find all the info for a service server, using the ros2 node info command.

Once you know the name of your node:

$ ros2 node info /activate_robot_server 
/activate_robot_server
  Subscribers:

  Publishers:
    /parameter_events: rcl_interfaces/msg/ParameterEvent
    /rosout: rcl_interfaces/msg/Log
  Service Servers:
    /activate_robot: example_interfaces/srv/SetBool
    /activate_robot_server/describe_parameters: rcl_interfaces/srv/DescribeParameters
    /activate_robot_server/get_parameter_types: rcl_interfaces/srv/GetParameterTypes
    /activate_robot_server/get_parameters: rcl_interfaces/srv/GetParameters
    /activate_robot_server/list_parameters: rcl_interfaces/srv/ListParameters
    /activate_robot_server/set_parameters: rcl_interfaces/srv/SetParameters
    /activate_robot_server/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically
  Service Clients:

  Action Servers:

  Action Clients:

Here in the “Service Servers” category, we find all the 7 servers running in the node: the one we created and the 6 automatically started to manage parameters.

Also, you directly get all the info you need: the service name and service type. Then, with the service type you can simply run ros2 interface show to get the details.

ros2 service: Find and debug your Services

The ros2 service command line tool is a great addition to the tool list ROS2 provides.

It will help you quickly check if your new service servers are working as expected (if you have an error from the server code, you may be able to spot uncaught exceptions and fix/catch them right away).

Also, from an already running ROS2 application, you can quickly find out what services you can call, and which interface you need to use. So you can launch the app, and test some behaviors in no time.

Want to learn how to program with ROS2?

Don't miss this opportunity:

ROS2 For Beginners - Step by Step Course


>> Learn ROS2 in 1 Week <<