Solved: add service files in ROS2

In recent years, the field of robotics has seen a surge in advancements and adopted technologies. One of them is ROS2 (Robot Operating System 2), an open-source middleware framework that provides tools, libraries, and conventions to develop, test and maintain robots’ applications. This article delves into adding service files in a ROS2 package using, an essential skill for developers working with this platform. We will start with a brief introduction, present a solution to the problem, walk you through a step-by-step guide, and discuss related topics for deeper understanding.

Setting up a service file in ROS2 package involves two main steps: creating a service message definition file (.srv) and adding necessary configurations in the package.xml and files. In this tutorial, we will create a simple service that takes an input string and returns the length of the input provided.

First, let’s prepare our workspace. Create a ROS2 package named “string_length_service” with the following command:

ros2 pkg create –build-type ament_python string_length_service

Now, create a directory named “srv” inside the “string_length_service” package to store our service message definition file:

mkdir srv

Inside the “srv” directory, create a file named “StringLength.srv” with the following content:

string input_string

int64 length

In the package.xml file, make sure to add the necessary dependencies:


Now, let’s move to the core part of this article – adding service files in Open the “” file and include the following code:

from setuptools import setup
from glob import glob

package_name = ‘string_length_service’

[‘resource/’ + package_name]),
(‘share/’ + package_name, [‘package.xml’]),
(‘share/’ + package_name, glob(‘srv/*’))
description=’A service for calculating string length’,
‘console_scripts’: [
‘string_length_server = ‘ + package_name + ‘.string_length_server:main’,
‘string_length_client = ‘ + package_name + ‘.string_length_client:main’,

Let’s now discuss what this code does: The file uses setuptools to distribute and manage our package. The package_name variable holds the name of our package. The data_files list specifies the necessary resource, package.xml, and service files to be distributed along with the package. install_requires lists the required packages, and entry_points define our service executable files. Make sure to replace the ‘developer_name_here’ and ‘developer_email_here’ with proper values.

Using rclpy and std_srvs

After setting up the service files in, it’s necessary to implement the server and client code using the ROS2 Python Client Library, rclpy, and std_srvs for the standard service types.

Create the files “” and “” inside your package directory and implement the server and client code. You can find details on how to write server and client code in the ROS2 documentation.

Building and testing the service

Finally, build and test your package using the following commands:

source /opt/ros/foxy/setup.bash
colcon build –packages-select string_length_service
source install/setup.bash

After the successful build, run the server and client using two separate terminals:

ros2 run string_length_service string_length_server

ros2 run string_length_service string_length_client

This guide presented an in-depth explanation of adding service files in for ROS2 packages while touching on related topics like rclpy and std_srvs. By following these steps, you can create and distribute your ROS2 services in an efficient and organized manner, facilitating interactions within your robotic applications. Good luck and happy coding!

Related posts:

Leave a Comment