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_controlframework - Develop behavior trees for task planning using the
BehaviorTree.CPPlibrary - 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"
iyy="${mass / 12.0 * (height1*height1 + width*width)}" iyz="0.0"
Any command-line input or output is written as follows:
colcon build --symlink-install
Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: “In a few seconds, the calibration will be ready, and the COMMIT button will be enabled.”
Warnings or important notes appear like this.
Tips and tricks appear like this.
Get in touch
Feedback from our readers is always welcome.
General feedback: Email feedback@packtpub.com and mention the book’s title in the subject of your message. If you have questions about any aspect of this book, please email us at questions@packtpub.com.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packtpub.com/submit-errata, click Submit Errata, and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at copyright@packtpub.com with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit http://authors.packtpub.com/.
Join our community on Discord
Join our community’s Discord space for discussions with the authors and other readers: https://packt.link/embeddedsystems

Share your thoughts
Once you’ve read Mastering ROS 2 for Robotics Programming, Fourth Edition, we’d love to hear your thoughts! Scan the QR code below to go straight to the Amazon review page for this book and share your feedback.

https://packt.link/r/1836209010
Your review is important to us and the tech community and will help us make sure we’re delivering excellent quality content.
Download a free PDF copy of this book
Thanks for purchasing this book!
Do you like to read on the go but are unable to carry your print books everywhere?
Is your eBook purchase not compatible with the device of your choice?
Don’t worry, now with every Packt book you get a DRM-free PDF version of that book at no cost.
Read anywhere, any place, on any device. Search, copy, and paste code from your favorite technical books directly into your application.
The perks don’t stop there, you can get exclusive access to discounts, newsletters, and great free content in your inbox daily.
Follow these simple steps to get the benefits:
- Scan the QR code or visit the link below:

https://packt.link/free-ebook/9781836209010
- Submit your proof of purchase.
- That’s it! We’ll send your free PDF and other benefits to your email directly.