Preface
ROS 2, the Robot Operating System 2, is a major milestone in robotics software development. More than just a tool, it is a modular, open-source ecosystem designed to support the creation of scalable, distributed, and intelligent robotic systems. Its adoption has grown rapidly across sectors-from startups developing autonomous robots, to aerospace and manufacturing companies, to leading research institutions-thanks to its strong community, robust architecture, and a rich set of tools that streamline the development of complex robotics applications. Mastering ROS 2 for Robotics Programming is a comprehensive guide designed for developers and roboticists who want to go beyond the basics and learn how to leverage the full capabilities of ROS 2. Whether you're building mobile robots, manipulators, drones, or hybrid systems, this book will provide you with the knowledge and practical skills needed to develop and integrate advanced robotic behaviors using ROS 2.
We will begin by introducing the ROS 2 architecture and programming model, guiding you through setting up your development environment and creating your first nodes using C++. From there, we will cover key ROS 2 concepts-topics, services, actions, parameters, and launch systems-providing real-world examples along the way. As we move forward, you'll learn how to:
- Build 3D models of robots and use them with simulators like Gazebo to test behavior in realistic environments
- Implement robot control systems with the
ros2_control
framework - Develop behavior trees for task planning using the
BehaviorTree.CPP
library - Use ROS 2 Navigation Stack (Nav2) to enable autonomous movement
- Apply robot manipulation with MoveIt 2 for grasping and motion planning
- Incorporate sensor data and vision systems into perception pipelines
- Work with aerial robots using ROS 2 interfaces and control systems
Advanced chapters will introduce you to modern practices, such as writing tests, setting up continuous integration and deployment pipelines for ROS 2 packages, and using cloud-native tools to build robust robotics software. You'll also explore how to interface ROS 2 with artificial intelligence, including:
- Task planning using large language models
- Training reinforcement learning agents with ROS 2 environments
- Creating custom visualization and simulation plugins for more interactive development
Additionally, you will learn how to connect motors with ROS 2 nodes using Raspberry Pi boards.
This book is filled with practical examples helping you improve programming ROS 2 applications. The code examples in the book are based on ROS 2's jazzy, the latest long-term-support distributions and compatible with widely used robot platforms. We hope this book becomes a valuable companion in your own robotics journey and helps you to truly master ROS 2.
Let's get started!
Who this book is for
This book is intended for robotic software developers and IT professionals who have a foundational understanding of robotics and ROS programming. Readers should be familiar with Ubuntu Linux and command-line tools, have basic experience with C++, and have the foundational knowledge of robotics concepts to fully benefit from the content.
What this book covers
Chapter 1, Introduction to ROS 2, introduces the Robot Operating System 2 (ROS 2), its architecture, communication mechanisms, and its advantages over ROS 1. It sets the foundation for developing modern, distributed robotic systems.
Chapter 2, Getting Started with ROS 2 Programming, walks you through setting up a ROS 2 development environment on Ubuntu, creating and building packages, and writing basic publisher and subscriber nodes in C++.
Chapter 3, Implementing ROS 2 Concepts, dives into core ROS 2 functionalities, such as services, actions, parameters, and lifecycle nodes, demonstrating how to build modular and maintainable robotic software.
Chapter 4, Working with Robot 3D Modeling in ROS 2, explains how to create 3D robot models using URDF and Xacro, including sensor and actuator integration, and how to visualize the models using RViz.
Chapter 5, Simulating Robots in a Realistic Environment, shows how to set up robot simulations using Gazebo, Webots, and Nvidia Omniverse, integrating ROS 2 interfaces for testing robot behavior in virtual environments.
Chapter 6, Controlling Robots Using the ros2_control Package, explores how to configure and use the ros2_control
framework to manage hardware interfaces and controllers for differential, wheeled, and articulated robots.
Chapter 7, Implementing ROS 2 Applications Using BehaviorTree.CPP, introduces the BehaviorTree.CPP library and explains how to create flexible and reactive decision-making logic for robotic tasks.
Chapter 8, ROS 2 Navigation Stack: Nav2, covers the setup and configuration of the ROS 2 Navigation Stack (Nav2), including map building, localization, path planning, and obstacle avoidance for mobile robots.
Chapter 9, Robot Manipulation Using MoveIt 2, provides a practical guide to using MoveIt 2 for robot arm planning, motion execution, collision checking, and integration with perception systems.
Chapter 10, Working with ROS 2 and Perception Stack, introduces tools and libraries for integrating cameras and depth sensors with ROS 2, processing sensor data, and using it for object detection and scene understanding.
Chapter 11, Aerial Robotics and ROS 2, explores the integration of ROS 2 with aerial robot platforms such as drones, including flight control, localization, and sensor integration.
Chapter 12, Designing and Programming a DIY Mobile Robot from Scratch, explains how to build and interface with ROS 2 and Nav 2, a cheap Do It Yourself robot, using a Raspberry pi board and eclectic motors and micro controllers.
Chapter 13, Testing, Continuous Integration, and Continuous Deployment with ROS 2, presents best practices for testing ROS 2 packages, setting up CI pipelines, and deploying software updates reliably in robotic systems.
Chapter 14, Interfacing Large Language Models with ROS 2, explores how to integrate LLMs with ROS 2 for high-level task planning, command interpretation, and natural language interfaces.
Chapter 15, ROS 2 and Deep Reinforcement Learning, introduces deep reinforcement learning concepts and demonstrates how to train and evaluate policies for robot control using ROS 2 environments.
Chapter 16, Implementing ROS 2 Visualization and Simulation Plugins, covers how to extend RViz and simulation tools with custom plugins to support visualization and interaction with specialized robot components and data.
To get the most out of this book
Before diving into the content, it's important to clarify the foundational knowledge and tools that will help you make the most of this book. This book assumes that you have a basic understanding of C++ and are comfortable working in a Linux environment, particularly Ubuntu, as many of the examples and tools are designed and tested on this platform. You do not need to be an expert, but familiarity with command-line usage and package management.
As for hardware requirements, a standard computer is enough for most chapters, though in a few cases, having a GPU-enabled system with an NVIDIA card is suggested but not required. Finally, standard webcams and depth sensors are used in the computer vision chapter.
Download the example code files
The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Mastering-ROS-2-for-Robotics-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing. Check them out!
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781836209010.
This book contains some wide screenshots. These have been captured to provide readers with an overview of the entire console. As a result, the text on these images may appear small at 100% zoom. Kindly refer to color images in the graphics bundle above for better comprehension.
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, folder names, filenames, file extensions, pathnames, dummy URLs or user command input. Here is an example: "After enabling the universe
repository, let's add the ROS 2 GPG
key and apt sources
list "
A block of code is set as follows:
<inertia ixx="${mass / 12.0 * (width*width + height1*height1)}" ixy="0.0" ixz="0.0" ...