Preface
In 2016, while cofounding a robotics start-up, I was searching for tools and technologies to build the software for a new six-axis robotic arm, and I stumbled upon ROS. Somehow, I got the intuition that ROS seemed to be exactly what I was looking for. Yet, I couldn't really comprehend what it was, or what it was doing.
It took me a long time to understand ROS, and as I was learning about it, I realized how painful the process was. There was-and still is-a lack of clear teaching materials and online resources, especially for beginners. As I continued to use ROS over the years, I also realized it was not just me-many other developers were still lost. This led me to create online courses to teach ROS and other related topics, with the goal of making ROS more accessible to everyone.
A few years forward, this book is a continuation of this process. With more experience using and teaching ROS (or ROS 2-the difference will be explained later), I wrote the book I wish I had when I first got started. When working on the book, I tried as much as possible to place myself in a beginner's shoes and to avoid two common obstacles to learning.
First, in tech circles, you can sometimes see toxic behavior shown by some experts who look down on you, and say stuff like "How can you not already know how to do that-it's so basic?", or explain things to you very fast while using jargon, and then make you feel stupid when you don't understand. This behavior is not helpful, and won't motivate you to learn.
Second, I don't know why, but so many people like to overcomplicate things, and this is not just related to ROS or even technology. Most of the time, once concepts are clearly understood, they can be explained in very simple steps. There's no need to make them sound complicated if it's not needed, and there's no need to spend one hour explaining something if it can be done in five minutes. That creates noise and confusion.
In this book, I want to do the opposite: teach without judgment over a lack of skills, and give priority to clear and simple explanations. With this, I hope you can learn efficiently, and finish this book being less confused than you are now, and more motivated to continue your journey with ROS 2 and robotics.
Who this book is for
This book is for engineers, researchers, students, teachers, developers, and hobbyists who want to learn ROS 2 from scratch in an efficient way, without wasting any time.
Even though you don't have to be an expert in anything, this book is not for complete beginners in software engineering. You need some good basics in Linux and Python-C++ is optional. Having a good grasp and some experience with those technologies will make your learning much easier.
No ROS (or ROS 1) experience is required.
What this book covers
Chapter 1, Introduction to ROS 2 - What is ROS 2?, explains what ROS 2 is exactly, and clears most of the doubts and confusions you could have.
Chapter 2, Installing and Setting Up ROS 2, leads you through the installation and set up of Ubuntu, ROS 2, and additional tools, so you have everything you need to work with ROS 2.
Chapter 3, Uncovering ROS 2 Core Concepts, introduces the main ROS 2 concepts through experimentation and hands-on discovery, the goal being to develop an intuition of how things work.
Chapter 4, Writing and Building a ROS 2 Node, shows you how to write ROS 2 programs, install them, and run them. Both Python and C++ are used, and additional challenges are given to make you practice more (the same applies to the following chapters).
Chapter 5, Topics - Sending and Receiving Messages between Nodes, explains how to communicate between two nodes with topics. We start with an explanation of the concept using a real-life analogy and then dive into the code.
Chapter 6, Services - Client/Server Interaction between Nodes, follows the same outline as the previous chapter-this time to work on the second most important communication type in ROS 2.
Chapter 7, Actions - When Services Are Not Enough, introduces the third and last ROS 2 communication type. This chapter is a bit more advanced and can be skipped during the first read.
Chapter 8, Parameters - Making Nodes More Dynamic, shows you how to add parameters to your nodes, in order to provide different settings at runtime.
Chapter 9, Launch Files - Starting All Your Nodes at Once, provides you with a way to start a complete ROS 2 application from a single file.
Chapter 10, Discovering TFs with RViz, introduces you to one of the most important concepts, so you can track the different coordinates of a robot over time. That will be the backbone of almost any ROS 2 application you create.
Chapter 11, Creating a URDF for a Robot, has you start a new project in which you create a custom robot with ROS 2. With URDF, you can create the robot description.
Chapter 12, Publishing TFs and Packaging the URDF, explains how to correctly package your application and generate the required TFs thanks to the URDF you have created.
Chapter 13, Simulating a Robot in Gazebo, teaches you how to adapt a robot for Gazebo (3D simulation tool), how to spawn the robot, and how to control it, so as to get a simulation that's as close as possible to a real robot.
Chapter 14, Going Further - What to Do Next, gives you more perspectives on the different paths you can take after finishing this book, depending on your personal goals.
To get the most out of this book
You need basic knowledge of the following:
- Linux, especially on how to use the command line (with auto-completion) and write code with text editors, and you should understand a bit about the file system and how the environment works (with files such as .bashrc).
- Python programming: most of the code will be in Python 3, using object-oriented programming. The better your Python skills, the easier it will be.
- C++ programming: you could decide to only start by following the Python examples, and thus you don't need C++. If you want to follow C++ examples as well, of course, C++ is needed.
Regarding software and operating systems, you will need to install Ubuntu on your computer (better as a dual boot, and also works with a virtual machine). This book targets Ubuntu 24.04 and ROS 2 Jazzy, but you should be able to get the most out of it with the later versions as well. Step-by-step instructions on how to install Ubuntu (in a virtual machine) and ROS 2 will be provided in the book.
If you are using the digital version of this book, we advise you to type the code yourself or access the code from the book's GitHub repository (a link is available in the next section). Doing so will help you avoid any potential errors related to the copying and pasting of code.
Download the example code files
You can download the example code files for this book from GitHub at https://github.com/PacktPublishing/ROS-2-from-Scratch. If there's an update to the code, it will be updated in the GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Conventions used
There are a number of text conventions used throughout this book.
Code in text: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "For example, if you want to install the abc_def package on ROS Jazzy, then you will need to run sudo apt install ros-jazzy-abc-def."
A block of code is set as follows:
#!/usr/bin/env python3 import rclpy from rclpy.node import Node
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
entry_points={ 'console_scripts': [
"test_node = my_py_pkg.my_first_node:main" ], },
Any command-line input or output is written as follows:
$ sudo apt update $ sudo apt upgrade
Bold: Indicates a new term, an important word, or words that you see onscreen. For instance, words in menus or dialog boxes appear in bold. Here is an example: "To start the VM, double click on it in VirtualBox Manager, or select it...