How to Write a ROS 2 Service in C++ – ROS 2 Lyrical

In this tutorial, we’ll create a ROS 2 service using C++. 

A ROS 2 service is a way for different parts of a robot system to communicate with each other by sending a request and receiving a response, similar to making a phone call and waiting for an answer. This form of communication is different from the publish-subscribe communication method, where one part of the system continuously broadcasts information, and any other parts that are interested can listen in, like a radio station broadcasting to many listeners. 

With a service, the communication is direct and specific, where the caller sends a request to a particular service provider and waits for a response before proceeding. In contrast, publish-subscribe allows for more flexible and asynchronous communication between multiple parts of the system without waiting for specific responses.

Services are useful when you need a quick but specific task done, and you need to wait for it to finish before moving on. You generally do not want to use a service if you need to make continuous calls for information. If you need to make continuous calls for information, topics are better suited.

Real-World Applications

Here are some real-world examples of how ROS 2 service and client nodes can be used in robotic applications:

  • Starting a Robot Vacuum Cleaner
    • Service: A service node called start_cleaning is created. When this service is called, it initiates the cleaning process of the robot vacuum cleaner.
    • Client: A client node calls the start_cleaning service to begin the cleaning process. It waits for a response from the service confirming that the cleaning process has started.
  • Localization on a Map
    • Service: A service node determines the current position and orientation of a robot on a map based on a provided map and LIDAR scans.
    • Client: A client node requests the current position and orientation of a robot on a map.
  • Gripper Control
    • Service: A service node controls the robotic arm’s gripper to open or close.
    • Client: A client node sends requests to the gripper service to open the gripper to release an object into a bin.

Prerequisites

All my code for this project is located here on GitHub.

Creating a Cleaning Service for a Robotic Vacuum Cleaner  

Let’s imagine we have a fictitious robotic vacuum cleaner:

  • If this service is called with a value of True, the robot will start cleaning.
  • If this service is called with a value of False, the robot will stop cleaning.

The name of the service is /set_cleaning_state.

We will call the service using the following syntax:

ros2 service call <service_name> <service_type> <arguments>

To start cleaning, we will run this command:

ros2 service call /set_cleaning_state yahboom_rosmaster_msgs/srv/SetCleaningState "{desired_cleaning_state: true}"

To stop cleaning, we will run this command:

ros2 service call /set_cleaning_state yahboom_rosmaster_msgs/srv/SetCleaningState "{desired_cleaning_state: false}"

Create a Custom Service Definition

Create the srv folder inside the yahboom_rosmaster_msgs package:

cd ~/ros2_ws/src/yahboom_rosmaster/yahboom_rosmaster_msgs
mkdir srv

Create the service definition file SetCleaningState.srv inside the srv folder:

cd srv

Open the SetCleaningState.srv file and add the following content:

# Request
bool desired_cleaning_state  # Request: true to start cleaning, false to stop
---
bool success                 # Response: whether the request was successful
string message               # Response: information about the result

Update CMakeLists.txt

Add these lines to yahboom_rosmaster_msgs/CMakeLists.txt:

# Find required packages
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)

# Generate interfaces
rosidl_generate_interfaces(${PROJECT_NAME}
  "action/TimedRotation.action"
  "srv/SetCleaningState.srv"
)

ament_package()

Build the Service Messages

cd ~/ros2_ws
colcon build
source ~/.bashrc

Confirm the service creation:

ros2 interface show yahboom_rosmaster_msgs/srv/SetCleaningState
1-create-a-service

Write the Service Node

Let’s write the service node.

Create the set_cleaning_state_service.cpp file inside the src directory of yahboom_rosmaster_system_tests:

cd ~/ros2_ws/src/yahboom_rosmaster/yahboom_rosmaster_system_tests/src

Add the following content:

/**
 * @file set_cleaning_state_service.cpp
 * @brief ROS 2 node that implements a service to set the cleaning state of a robot
 *
 * This program implements a ROS 2 node that provides a service to set the cleaning state
 * of a robot. It demonstrates the use of service servers in ROS 2.
 *
 * Subscription Topics:
 *   None
 *
 * Publishing Topics:
 *   None
 *
 * Services:
 *   /set_cleaning_state (yahboom_rosmaster_msgs/srv/SetCleaningState):
 *     Sets the cleaning state of the robot
 *
 * @author Addison Sears-Collins
 * @date August 26, 2026
 */
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "yahboom_rosmaster_msgs/srv/set_cleaning_state.hpp"

/**
 * @class SetCleaningStateService
 * @brief A ROS 2 node that provides a service to set the cleaning state of a robot
 */
class SetCleaningStateService : public rclcpp::Node
{
public:
  /**
   * @brief Constructor for the SetCleaningStateService class.
   *
   * Initializes the node with the name "set_cleaning_state_service"
   * and creates the service server for the "/set_cleaning_state" service.
   */
  SetCleaningStateService()
  : Node("set_cleaning_state_service")
  {
    service_ = this->create_service<yahboom_rosmaster_msgs::srv::SetCleaningState>(
      "/set_cleaning_state",
      std::bind(&SetCleaningStateService::set_cleaning_state_callback, this,
                std::placeholders::_1, std::placeholders::_2));

    RCLCPP_INFO(this->get_logger(), "Cleaning Service Server is ready.");
  }

private:
  /**
   * @brief Callback function for the set_cleaning_state service.
   *
   * This function is called when a request is received for the "/set_cleaning_state" service.
   * It checks the desired cleaning state in the request and sets the appropriate response.
   *
   * @param request The request shared pointer for the set_cleaning_state service.
   * @param response The response shared pointer for the set_cleaning_state service.
   */
  void set_cleaning_state_callback(
    const std::shared_ptr<yahboom_rosmaster_msgs::srv::SetCleaningState::Request> request,
    std::shared_ptr<yahboom_rosmaster_msgs::srv::SetCleaningState::Response> response)
  {
    if (request->desired_cleaning_state) {
      RCLCPP_INFO(this->get_logger(), "Starting cleaning...");
      response->success = true;
      response->message = "Robot started cleaning";
    } else {
      RCLCPP_INFO(this->get_logger(), "Stopping cleaning...");
      response->success = true;
      response->message = "Robot stopped cleaning";
    }

    RCLCPP_INFO(this->get_logger(), "Response sent: %s", response->message.c_str());
  }

  /// Service server for setting cleaning state
  rclcpp::Service<yahboom_rosmaster_msgs::srv::SetCleaningState>::SharedPtr service_;
};

/**
 * @brief Main function to run the SetCleaningStateService node.
 *
 * @param argc Number of command-line arguments.
 * @param argv Array of command-line arguments.
 * @return int Exit status of the program.
 */
int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  auto node = std::make_shared<SetCleaningStateService>();
  rclcpp::spin(node);
  rclcpp::shutdown();
  return 0;
}

Update CMakeLists.txt

Edit the CMakeLists.txt file:

add_executable(set_cleaning_state_service 
  src/set_cleaning_state_service.cpp
)

target_link_libraries(set_cleaning_state_service
  ${rclcpp_TARGETS}
  ${yahboom_rosmaster_msgs_TARGETS}
)

install(TARGETS
  set_cleaning_state_service
  DESTINATION lib/${PROJECT_NAME}
)

Build the Package

cd ~/ros2_ws
colcon build
source ~/.bashrc

Run the Service Node

Open a new terminal window, and type:

ros2 run yahboom_rosmaster_system_tests set_cleaning_state_service
2-cleaning-service-server-is-ready
ros2 service list -t

The ros2 service list -t command lists all the available services along with their corresponding service types. It shows that the /set_cleaning_state service uses the yahboom_rosmaster_msgs/srv/SetCleaningState service type.

3-ros2-service-list

Open a new terminal window, and call the service to start cleaning:

ros2 service call /set_cleaning_state yahboom_rosmaster_msgs/srv/SetCleaningState "{desired_cleaning_state: true}"
4-ros2-service-call

Now call the service to stop cleaning:

ros2 service call /set_cleaning_state yahboom_rosmaster_msgs/srv/SetCleaningState "{desired_cleaning_state: false}"
5-ros2-service-call-false

You see how we called the service via the terminal. You can also create an actual program that is the service client that calls the service just like we did in the terminal.

This tutorial showed you a simple service that:

  • Listens for requests
  • Processes the requests
  • Returns appropriate responses

All services are structured this way, so you can use this as a template for any service you create.

That’s it! Keep building!

Using ros2 doctor to Identify Issues – ROS 2 Lyrical

In this tutorial, we will explore how to use the ros2 doctor command to identify and diagnose issues in your ROS 2 system.

The purpose of ros2 doctor is to check the health and configuration of your ROS 2 setup. It analyzes various aspects such as environment variables, network configuration, and running systems to identify potential issues or warnings.

Real-World Applications

ros2 doctor is useful when:

  • Setting up a new ROS 2 environment to ensure proper configuration
  • Troubleshooting issues or errors in your ROS 2 system
  • Collaborating on ROS 2 projects to ensure consistent setups across team members

Prerequisites

All my code for this project is located here on GitHub.

Check Your Setup

Open a new terminal window.

Run the following command:

ros2 doctor

Review the report for any warnings or errors.

If ros2 doctor finds no warnings, you’ll see “All <n> checks passed”. 

You don’t have to worry about UserWarnings. UserWarnings indicate non-ideal configurations, but the setup is still usable.

What you do need to pay attention to are errors. If any errors are found, you will see “UserWarning: ERROR:”. In this case, the check is considered failed, and you should address the errors.

Check a System

Let’s use ros2 doctor to analyze an actual system.

We will launch the robot and use ros2 doctor to analyze the running system.

Open a terminal window, and launch your robot.

I will type the following command:

x3 

or

bash ~/ros2_ws/src/yahboom_rosmaster/yahboom_rosmaster_bringup/scripts/rosmaster_x3_gazebo.sh

Open another terminal window, and move the robot around:

ros2 run yahboom_rosmaster_system_tests square_mecanum_controller

In a separate terminal, display the messages that are broadcasting over the /cmd_vel topic:

ros2 topic echo /mecanum_drive_controller/cmd_vel

While the robot is running, open another terminal and run:

ros2 doctor

Review the report to see if there are any warnings or errors related to the running nodes, topics, or services.

Get a Full Report

To get a more detailed report from ros2 doctor, use the –report argument.

Open a terminal window, and run the following command:

ros2 doctor --report

The report will provide detailed information categorized into nine groups:

  • ACTION LIST
  • ROS ENVIRONMENT
  • NETWORK CONFIGURATION
  • PACKAGE VERSIONS
  • PLATFORM INFORMATION
  • QOS COMPATIBILITY LIST
  • RMW MIDDLEWARE
  • SERVICE LIST
  • TOPIC LIST

Cross-reference the information in the report with the warnings from running ros2 doctor to gain deeper insights into the issues.

That’s it. Keep building!

Record and Play Back Data Using ROS 2 Bag – ROS 2 Lyrical

In this tutorial, we will explore ROS 2 Bag, a powerful tool for recording and playing back data in a ROS 2 system. We will learn about the purpose of ROS 2 Bag, its real-world applications, and how to use it effectively. By the end of this tutorial, you will be able to record data from your ROS 2 system, analyze the recorded data, play it back for various purposes, and convert it to a CSV file (i.e. comma-separated text file).

Prerequisites

All my code for this project is located here on GitHub.

Purpose of ROS 2 Bag

ROS 2 Bag allows you to record data from various topics and services in your ROS 2 system and store it in a file format called a “bag.” This recorded data can be later played back, analyzed, or used for debugging and testing purposes. 

ROS 2 Bag is particularly useful when you need to capture data from sensors, robot states, or any other relevant information during runtime.

Real-World Applications

The following are several real-world applications of ROS 2 bag:

  • Autonomous Vehicle Development: ROS 2 Bag can be used to record sensor data, such as LIDAR scans, camera images, and GPS information, during autonomous vehicle testing. This recorded data can be used for offline analysis, algorithm development, and simulation purposes.
  • Robot Debugging: When troubleshooting issues with a robot, ROS 2 Bag can be used to record data from various topics during the occurrence of the problem. This recorded data can be analyzed later to identify the root cause of the issue and facilitate debugging efforts.
  • Dataset Creation: ROS 2 Bag is often used to create datasets for machine learning and computer vision applications. By recording data from sensors and ground truth information, researchers can build datasets for tasks such as object detection, semantic segmentation, and localization.

I will be working with the mobile robot I set up in this tutorial. I want to record the velocity commands, and then play them back.

Setup

Open a terminal window, and launch your robot.

I will type the following command:

x3 

or

bash ~/ros2_ws/src/yahboom_rosmaster/yahboom_rosmaster_bringup/scripts/rosmaster_x3_gazebo.sh

Choose a Topic

In another terminal window, type:

cd ~/Downloads/
mkdir bag_files && cd bag_files

Check out the active topics:

ros2 topic list

The /mecanum_drive_controller/cmd_vel topic contains the velocity messages.

ros2 bag record

Open a new terminal window, and move the robot in a square-shaped pattern using this command:

ros2 run yahboom_rosmaster_system_tests square_mecanum_controller

To start recording data, use the ros2 bag record command with the –topics flag, followed by the topic or topics you want to record. You used to be able to list the topics on their own, without the flag. That no longer works, so if you leave the flag off you will get an “unrecognized arguments” error. 

For example, to record the /mecanum_drive_controller/cmd_vel topic, run:

ros2 bag record --topics /mecanum_drive_controller/cmd_vel

The recording will start, and you will see output indicating the number of messages recorded for each topic.

1-record-ros2-bag

To stop the recording, press Ctrl + C in the terminal where the ros2 bag record command is running.

Close down the robot and the square mecanum controller as well using CTRL + C.

The recorded data will be saved in a bag file with a name format of rosbag2_year_month_day-hour_minute_second. This folder contains a metadata.yaml folder and the bag file.

dir

Here is what I see:

rosbag2_2024_11_26-07_00_48

The metadata.yaml file provides important information about the recorded bag file, such as the duration of the recording, the starting time, the number of messages recorded, and the details of each recorded topic. This metadata helps in understanding the contents of the bag file and can be useful for analysis and playback purposes.

If you want to record multiple topics and change the name of the topic, type the following command:

ros2 bag record -o <bag_file_name> --topics <topic1> <topic2> ... <topicN>

If you want to record all topics, you would type this command:

ros2 bag record -a

You can find other options here at the ROS 2 Bag GitHub page.

ros2 bag info

To view information about a recorded bag file, use the `ros2 bag info` command followed by the path to the bag file. For example:

ros2 bag info rosbag2_2024_11_26-07_00_48

The command will display information such as the duration of the recording, the number of messages recorded for each topic, and the start and end times of the recording.

2-ros2-bag-info

ros2 bag play

Open a terminal window, and launch your robot.

I will type the following command:

x3 

or

bash ~/ros2_ws/src/yahboom_rosmaster/yahboom_rosmaster_bringup/scripts/rosmaster_x3_gazebo.sh

To play back a recorded bag file, use the ros2 bag play command followed by the path to the bag file. 

cd ~/Downloads/bag_files

Run the bag file:

ros2 bag play rosbag2_2024_11_26-07_00_48

You can use the –loop option to play the bag file in a loop:

ros2 bag play rosbag2_2024_11_26-07_00_48 --loop

Let’s play it in a loop.

The recorded data will be played back, and you will see the messages being published to the respective topic.

ros2 topic echo /mecanum_drive_controller/cmd_vel

Your robot will start moving as well.

To pause the playback, press the spacebar in the terminal where the ros2 bag play command is running. Your robot will continue based on the last velocity message it received.

Press the spacebar again to resume playback.

To stop the playback, press Ctrl+C in all terminal windows.

Convert the ROS 2 Bag File to a CSV File

Let’s convert the ROS 2 bag file into CSV format. 

cd ~/Downloads/bag_files/rosbag2_2024_11_26-07_00_48

If you take a look inside the ROS 2 bag folder, you see two files.

  • 0_rosbag2_2024_11_26-07_00_48.mcap: The ROS 2 bag file. The number now comes at the front of the name.
  • metadata.yaml: This is a supplementary file that provides extra detail about the data in the .mcap file. It is written in YAML format to make it easy for both humans and computers to read and understand.

Now let’s convert the ROS 2 bag file (in mcap format, the default for ROS 2) into .csv format.

First, type this command to install the package we need (it is usually already installed):

sudo apt-get install ros-${ROS_DISTRO}-rosbag2-storage-mcap
cd ~/Downloads/bag_files/

Create a new file called ros2bag_to_csv.py.

Add this code.

#!/usr/bin/env python3
"""
Convert a ROS 2 bag file to CSV format.

This script reads messages from a ROS 2 bag file and converts them to CSV format,
creating separate CSV files for each topic in the bag.

Usage:
  python3 ros2bag_to_csv.py <input_bag_path> <output_folder>

Subscription Topics:
    None (reads from bag file)

Publishing Topics:
    None (writes to CSV files)

:author: Addison Sears-Collins
:date: August 26, 2026
"""

import argparse
import csv
import os

from rclpy.serialization import deserialize_message
from rosidl_runtime_py.utilities import get_message
import rosbag2_py
from std_msgs.msg import String


def read_messages(input_bag: str):
    """
    Read messages from a ROS 2 bag file.

    :param input_bag: Path to the input bag file.
    :return: A generator yielding (topic, message, timestamp) tuples.
    """
    # Create a SequentialReader to read messages from the bag file
    reader = rosbag2_py.SequentialReader()

    # Open the bag file for reading
    reader.open(
        rosbag2_py.StorageOptions(uri=input_bag, storage_id="mcap"),
        rosbag2_py.ConverterOptions(
            input_serialization_format="cdr", output_serialization_format="cdr"
        ),
    )

    # Get all the topic types available in the bag file
    topic_types = reader.get_all_topics_and_types()

    def typename(topic_name):
        """
        Get the message type for a given topic.

        :param topic_name: The name of the topic.
        :return: The message type as a string.
        :raises ValueError: If the topic is not found in the bag.
        """
        # Iterate through the topic types to find the matching topic name
        for topic_type in topic_types:
            if topic_type.name == topic_name:
                return topic_type.type
        raise ValueError(f"topic {topic_name} not in bag")

    # Iterate through the messages in the bag file
    while reader.has_next():
        topic, data, timestamp = reader.read_next()
        msg_type = get_message(typename(topic))
        msg = deserialize_message(data, msg_type)
        yield topic, msg, timestamp

    # Clean up the reader
    del reader


def main():
    """
    Main function to parse arguments and convert ROS 2 bag to CSV.
    """
    # Create an argument parser
    parser = argparse.ArgumentParser(description="Convert ROS 2 bag to CSV")
    parser.add_argument("input", help="Input bag path (folder) to read from")
    parser.add_argument("output", help="Output folder to save CSV files")

    # Parse the command-line arguments
    args = parser.parse_args()

    # Create the output folder if it doesn't exist
    if not os.path.exists(args.output):
        os.makedirs(args.output)

    # Initialize dictionaries to store topic files and writers
    topic_files = {}
    topic_writers = {}

    # Iterate through the messages in the bag file
    for topic, msg, timestamp in read_messages(args.input):
        # If the topic hasn't been processed before, create a new CSV file and writer
        if topic not in topic_files:
            output_file = os.path.join(args.output, topic.replace('/', '_') + '.csv')
            topic_files[topic] = open(output_file, mode='w',
                                      newline='', encoding='utf-8')
            topic_writers[topic] = csv.writer(topic_files[topic])
            topic_writers[topic].writerow(['timestamp', 'data'])

        # Write the message data to the corresponding CSV file
        if isinstance(msg, String):
            topic_writers[topic].writerow([timestamp, msg.data])
        else:
            topic_writers[topic].writerow([timestamp, str(msg)])

    # Close all the topic files
    for file in topic_files.values():
        file.close()


if __name__ == "__main__":
    main()

Save the file.

Change the file’s permissions:

chmod +x ros2bag_to_csv.py 

Run the script:

python3 ros2bag_to_csv.py rosbag2_2024_11_26-07_00_48 myros2bag

Here is the syntax:

python3 ros2bag_to_csv.py <ROS 2 bag folder> <desired output folder>
cd myros2bag

Inside this folder you will see your .csv file.

3-csv-file-from-ros2-bag

That’s it!

You can now use this knowledge to capture and analyze data from your ROS 2 system, debug issues, and create datasets for various applications.

Keep building!