VideoLibrary

Introduction to ROS Part 6: Services, Requests, and Responses with C++ | DigiKey

In this episode of our ROS 2 tutorial series, we explore how to use services in C++ to enable synchronous communication between nodes. While previous lessons focused on asynchronous messaging with topics and the Python implementation of services, this video demonstrates how to implement both a service server and client using C++ and the rclcpp library. Services in ROS 2 follow a request/response model, which is ideal for tasks that require direct interaction between nodes, such as sending commands or querying data. The written guide for this episode can be found here: https://www.digikey.com/en/maker/tutorials/2025/intro-to-ros-part-6-services-requests-and-responses-with-c The GitHub repository containing the Docker image and example code for this series can be found here: https://github.com/ShawnHymel/introduction-to-ros We begin by reviewing the purpose and structure of ROS 2 services, including how they differ from topics. Using the example_interfaces/srv/AddTwoInts interface, we walk through the process of creating a C++ server node that listens for incoming requests and responds with the sum of two integers. You'll learn how to use create_service() and handle requests using bound callback functions within a custom node class. Next, we implement the client side in C++. This node periodically sends random requests to the server using the async_send_request() function and handles responses using shared futures. We explain the role of shared_future in ROS 2, why it’s needed for asynchronous callbacks, and how it fits into the request lifecycle. These examples demonstrate best practices for managing node timing, error handling, and clean shutdown procedures. Along the way, we show how to test your service using built-in ROS 2 tools like ros2 service call and the rqt GUI for visual introspection. You'll also learn how to configure your CMakeLists.txt to build and install the nodes correctly, helping you avoid common pitfalls when developing in C++ with ROS 2. Our development takes place inside a Docker container for a clean and portable ROS 2 environment. By the end of this tutorial, you'll have a complete understanding of how to implement and test ROS 2 services using C++. This skill is essential for creating structured, responsive robotic applications. Subscribe and follow the full series to build your skills in ROS 2 communication patterns and real-world robotics programming.

10/28/2025 4:18:33 PM