ROS 2 from Scratch - Edouard Renard - E-Book

ROS 2 from Scratch E-Book

Edouard Renard

0,0
28,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

ROS 2 from Scratch, written by the cofounder of a robotics company with years of experience in teaching and engineering, will equip you with the skills necessary to develop cutting-edge robotic systems efficiently.
This book will help you understand why you should use Robot Operating System (ROS), clarifying what ROS is and isn’t. It focuses on applied learning—no unnecessary filler, no abstract theory, and no abrupt leaps into complex topics. You’ll discover how to install ROS 2, set up your environment, and explore concepts through hands-on experiments, as well as create a ROS 2 application for a custom robot. The chapters expand upon core concepts such as nodes, communication via topics, client-server interactions with services, complex behaviors using actions, runtime configurations with parameters, and seamless node orchestration via launch files. Each concept is clearly explained with the help of real-life analogies and practical examples, using Python and C++. The book will also guide you through creating a real-world project that incorporates TFs, URDF, RViz, and Gazebo.
By the end of this book, you’ll be able to create new software for any robot in no time and easily join existing projects for new job roles, research endeavors, and beyond.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 555

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



ROS 2 from Scratch

Get started with ROS 2 and create robotics applications with Python and C++

Edouard Renard

ROS 2 from Scratch

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager: Preet Ahuja

Publishing Product Manager: Suwarna Patil

Book Project Manager: Ashwin Dinesh Kharwa

Senior Editor: Akanksha Gupta

Technical Editor: Rajat Sharma

Copy Editor: Safis Editing

Proofreader: Akanksha Gupta

Indexer: Pratik Shirodkar

Production Designer: Jyoti Kadam

DevRel Marketing Coordinator: Rohan Dobhal

First published: November 2024

Production reference: 1291024

Published by Packt Publishing Ltd.

Grosvenor House

11 St Paul’s Square

Birmingham

B3 1RB, UK

ISBN 978-1-83588-140-8

www.packtpub.com

Contributors

About the author

Edouard Renard is a software engineer, entrepreneur, and robotics teacher. In 2016, he cofounded a robotics start-up as CTO, developing and selling an educational 6-axis robotic arm based on Robot Operating System (ROS). Since 2019, he has been teaching robotics programming online. He is the best-selling instructor on Udemy for ROS 2 and has published many additional free tutorials on his website and YouTube channel (Robotics Back-End). He also leads offline workshops for professionals in Singapore and the US. His teaching philosophy is as follows: step by step, practical, and to the point.

I’d like to thank my friends, who supported me throughout the writing of this book, chapter after chapter. Thank you to every member of the Packt team who worked on the book—I’ve come to appreciate how much goes into publishing a book, beyond the writing itself. A special thanks to Lentin Joseph and Kenichi Kato for their thorough technical review, and for being great people to work with.

About the reviewers

Kenichi Kato brings over two decades of expertise in IT, manufacturing, and R&D, focusing extensively on embedded systems for autonomous and robotics applications. At the Singapore Institute of Technology, he leverages this expertise in his role, where he designs vital modules within the robotics systems curriculum that focus on electrical engineering, coding, and ROS.

His collaborative work with industry partners enhances the practical learning experience, preparing students to tackle real-world challenges with innovative solutions. Kenichi’s approach not only bridges the gap between academia and industry but also equips students with essential skills for the future.

I would like to extend my heartfelt gratitude to my wife for her enduring support and patience during the writing of this review. Her encouragement was invaluable. I am also grateful to the wider robotics community, whose continuous advancements have made ROS universally accessible. Special thanks to Edouard, the author of this book, whose insightful contributions have significantly enriched our understanding and application of ROS 2.

Lentin Joseph is an author, roboticist, and robotics entrepreneur from India. He runs a robotics software company called RUNTIME Robotics in Kochi/Kerala. He is also the founder/blogger of robocademy.com.

He has 14 years of experience in robotics, primarily working with ROS, Gazebo OpenCV, and PCL. He has authored and reviewed more than 10 books on ROS and robotics.

He has a master’s in robotics and automation from Amrita Vishwa Vidyapeetham University in India and worked at the Robotics Institute, CMU, USA. He is also a TEDx speaker.

I extend my sincere gratitude to the author, whose work I had the privilege to review. Your insights have broadened my understanding of the subject matter. I am thankful to the editorial team for their guidance and support throughout the review process. Your patience and expertise have been invaluable. Lastly, to the readers, I hope my review provides a useful perspective as you explore this book. Your engagement with my work is deeply appreciated.

Table of Contents

Preface

Part 1: Getting Started with ROS 2

1

Introduction to ROS 2 – What Is ROS 2?

Terminology

What is ROS, when should we use it, and why?

Why ROS?

What is ROS?

When to use ROS

ROS 1 versus ROS 2

A quick story of ROS, and how we got to ROS 2

Is ROS 1 dead already?

Prerequisites for starting with ROS 2

Knowledge prerequisites

Hardware and software

How to follow this book

Summary

2

Installing and Setting Up ROS 2

Which ROS 2 distribution to choose

What is a ROS 2 distribution?

LTS and non-LTS distributions

How to choose a ROS distribution

Installing the OS (Ubuntu)

The relationship between ROS 2 and Ubuntu

Installing Ubuntu 24.04 natively with a dual boot

Installing Ubuntu 24.04 on a VM

Installing ROS 2 on Ubuntu

Setting the locale

Setting up the sources

Installing ROS 2 packages

Setting up the environment for ROS 2

Sourcing ROS 2 in the environment

Adding the source line to the .bashrc file

Extra tools for ROS development

Visual Studio Code

The Terminal and other tools

Summary

3

Uncovering ROS 2 Core Concepts

Running your first node

Starting a node from the terminal with ros2 run

Introspecting the nodes with rqt_graph

Running a 2D robot simulation

Recap – nodes

Topics

Running a topic publisher and subscriber

A name and an interface (data type)

More experimentation with topics

Recap – topics

Services

Running a service server and client

A name and an interface (data type)

Sending a request from the terminal

More experimentation with services

Recap – services

Actions

Running an action server

A name and an interface (data type)

Sending a goal from the terminal

Recap – actions

Parameters

Getting the parameters for a node

Setting up a parameter value for a node

Recap – parameters

Launch files

Starting a launch file

Recap – launch files

Summary

Part 2: Developing with ROS 2 – Python and C++

4

Writing and Building a ROS 2 Node

Technical requirements

Creating and setting up a ROS 2 workspace

Creating a workspace

Building the workspace

Sourcing the workspace

Creating a package

What is a ROS 2 package?

Creating a Python package

Creating a C++ package

Building a package

How are nodes organized in a package?

Creating a Python node

Creating a file for the node

Writing a minimal ROS 2 Python node

Building the node

Running the node

Improving the node – timer and callback

Creating a C++ node

Writing a C++ node

Building and running the node

Node template for Python and C++ nodes

Template for a Python node

Template for a C++ node

Introspecting your nodes

ros2 node command line

Changing the node name at run time

Summary

5

Topics – Sending and Receiving Messages between Nodes

What is a ROS 2 topic?

A publisher and a subscriber

Multiple publishers and subscribers

Multiple publishers and subscribers inside one node

Wrapping things up

Writing a topic publisher

Writing a Python publisher

Writing a C++ publisher

Writing a topic subscriber

Writing a Python subscriber

Writing a C++ subscriber

Running the Python and C++ nodes together

Additional tools to handle topics

Introspecting topics with rqt_graph

The ros2 topic command line

Changing a topic name at runtime

Replaying topic data with bags

Creating a custom interface for a topic

Using existing interfaces

Creating a new topic interface

Topic challenge – closed-loop control

Challenge

Solution

Summary

6

Services – Client/Server Interaction between Nodes

What is a ROS 2 service?

A server and a client

Multiple clients for one service

Another service example with robotics

Wrapping things up

Creating a custom service interface

Finding an existing interface for our service

Creating a new service interface

Writing a service server

Writing a Python service server

Writing a C++ service server

Writing a service client

Writing a Python service client

Running the client and server nodes together

Writing a C++ service client

Additional tools to handle services

Listing and introspecting services

Sending a service request

Changing a service name at runtime

Service challenge – client and server

Challenge

Solution

Summary

7

Actions – When Services Are Not Enough

What is a ROS 2 action?

Why actions?

How do actions work?

Wrapping things up

Creating a custom action interface

Defining the application and the interface we need

Creating a new action interface

Writing an action server

Writing a Python action server

Writing a C++ action server

Writing an action client

Writing a Python action client

Creating an action client

Writing a C++ action client

Taking advantage of all the action mechanisms

Adding the feedback mechanism

Adding the cancel mechanism

Additional tools to handle actions

Listing and introspecting actions

Sending a goal from the Terminal

Topics and services inside actions

Summary

8

Parameters – Making Nodes More Dynamic

What is a ROS 2 parameter?

Why parameters?

Example of a node with parameters

ROS 2 parameters – wrapping things up

Using parameters in your nodes

Declaring, getting, and using parameters with Python

Providing parameters at runtime

Parameters with C++

Storing parameters in YAML files

Loading parameters from a YAML file

Parameters for multiple nodes

Recapping all parameters’ data types

Additional tools for handling parameters

Getting parameters’ values from the terminal

Exporting parameters into YAML

Setting a parameter’s value from the terminal

Parameter services

Updating parameters with parameter callbacks

Python parameter callback

C++ parameter callback

Parameter challenge

Challenge

Solution

Summary

9

Launch Files – Starting All Your Nodes at Once

What is a ROS 2 launch file?

Why launch files?

Example of a launch file with seven nodes

Creating and installing an XML launch file

Setting up a package for launch files

Writing an XML launch file

Installing and starting a launch file

Creating a Python launch file – XML or Python for launch files?

Writing a Python launch file

XML versus Python for launch files

Configuring nodes inside a launch file

Renaming nodes and communications

Parameters in a launch file

Namespaces

Launch file challenge

Challenge

Solution

Summary

Part 3: Creating and Simulating a Custom Robot with ROS 2

10

Discovering TFs with RViz

Technical requirements

Visualizing a robot model in RViz

Installation and setup

Starting RViz with a robot model

What are TFs?

Links

TFs

Relationship between TFs

Parent and child

The /tf topic

Visualizing the TF tree

What problem are we trying to solve with TFs?

What we want to achieve

How to compute TFs

Summary

11

Creating a URDF for a Robot

Creating a URDF with a link

Setting up a URDF file

Creating a link

Customizing the link visual

The process of assembling links and joints

Step 1 – adding a second link

Step 2 – adding a joint

Step 3 – fixing the joint origin

Step 4 – setting up the joint type

Step 5 – fixing the visual origin

Recap – the process to follow every time

Writing a URDF for a mobile robot

What we want to achieve

Adding the wheels

Adding the caster wheel

Extra link – base footprint

Improving the URDF with Xacro

Making a URDF file compatible with Xacro

Xacro properties

Xacro macros

Including a Xacro file in another file

Summary

12

Publishing TFs and Packaging the URDF

Understanding how to publish TFs with our URDF

The robot_state_publisher node

Inputs for the robot_state_publisher

Recap – how to publish TFs

Starting all nodes from the terminal

Publishing the TFs from the terminal

Visualizing the robot model in RViz

Creating a package to install the URDF

Adding a new workspace

Creating a _description package

Installing the URDF and other files

Writing a launch file to publish TFs and visualize the robot

The XML launch file

The Python launch file

Summary

13

Simulating a Robot in Gazebo

Technical requirements

How Gazebo works

Clarifying – Gazebo versus RViz

Starting Gazebo

How Gazebo works with ROS 2

Adapting the URDF for Gazebo

Inertial tags

Collision tags

Spawning the robot in Gazebo

Spawning the robot from the terminal

Spawning the robot from a launch file

Controlling the robot in Gazebo

What do we need to do?

Adding Gazebo systems

Bridging Gazebo and ROS 2 communications

Testing the robot

Summary

14

Going Further – What To Do Next

ROS 2 roadmap – exploration phase

Common stacks and frameworks

More exploration topics

Learning for a specific goal

What to learn for a project?

What to learn to get a job?

Summary

Index

Other Books You May Enjoy

Part 1:Getting Started with ROS 2

In this first part, you will get a global overview of ROS 2 and understand what it is, when to use it, and why. You will install ROS 2, as well as all necessary tools on your computer, and discover the core concepts through hands-on experimentation.

This part contains the following chapters:

Chapter 1, Introduction to ROS 2 – What Is ROS 2?Chapter 2, Installing and Setting Up ROS 2Chapter 3, Uncovering ROS 2 Core Concepts

1

Introduction to ROS 2 – What Is ROS 2?

Robot Operating System (ROS) can be confusing, as evidenced by its name. It’s difficult to know what it is exactly, what it contains, and what it does. Also, why do you even need ROS, and when should you use it?

Before getting started, it is okay to be confused—most people are. Although ROS is one of the best tools to learn and develop robotics applications, it also comes with a steep learning curve, with the first roadblock being understanding what it is.

In this quick first chapter, I will explain the terminology we will use throughout this book. You will then see why ROS exists, and what problems it can solve for you. After that, we will dive a bit deeper into the four pillars of ROS to understand what it is. You will also see a few examples of when and when not to use it.

By the end of this chapter, you will have a better understanding of the global picture behind ROS and be clear of the most common confusions. You will also understand what prerequisites you need before you get started with ROS, as well as how to follow this book to get the most out of it. This will help you get started on the right foot.

In this chapter, we are going to cover the following topics:

TerminologyWhat is ROS, when should we use it, and why?ROS 1 versus ROS 2Prerequisites for starting with ROS 2How to follow this book

Terminology

You might have seen the terms ROS, ROS 1, ROS 2, and other kinds of variations (with or without a space), which can be confusing.

Let’s clear this up now:

ROS 1 is (was) the first version of ROSROS 2 is the second and newer version of ROS and will be the focus of this book

In this book, I will use the following convention:

ROS: When talking about general ROS concepts, philosophy, and so onROS 1: When talking specifically about the first version of ROS. However, this will be quite rare since the focus here is on ROS 2ROS 2: When talking about the second version of ROS

Note

I may sometimes write ROS or ROS 2 interchangeably since we won’t be focusing on ROS 1 here.

It’s not impossible that, in the future (when ROS 1 has completely disappeared), the name ROS 2 becomes ROS again. If you’ve heard about Angular, it started as AngularJS, after which they released Angular2, and then a few years later, it simply became Angular. I guess that something similar will happen with ROS, although this is only a theory of mine for now.

What is ROS, when should we use it, and why?

Before we start understanding what ROS is, let’s understand why we would need it.

Why ROS?

Let’s start with a big problem that occurs often in robotics.

Imagine that you just got a new project at your job, and you have to develop a robotics application, or you are doing a new research thesis. One very important thing to take into account is that in real life, any project or thesis will have a specific duration, from a few months to a few years.

Now, what will happen?

You start to design the robotics system you need for your project and soon realize that it will take a lot of time to develop the robot because all the existing solutions you found don’t match what you need. After a few weeks, you finalize the specifications, and you start building your robot. A few months in, you’re still developing the basic software for wheel control and navigation. You underestimated how much time it would take you to just get the robot running. After 1 or 2 years, you realize that all you’ve done for now is build a robotics system, and you still haven’t started the core functionality of your application or research. Now is the time to hurry.

You finish the robot as well as you can, make some shortcuts, and publish your paper or present that prototype. In the best-case scenario, you could also share your code with an open-source license so that other people can use it, but probably not directly as it’s just code for your own need, not a complete framework or library with modular components, documentation, and so on.

Then, you move on to a new project, new job, and new research. Somebody else will take your place, read your code, and realize that it doesn’t help them build their application. Hence, they have to start from scratch.

What just happened here is that you reinvented the wheel. The next person will repeat the same circle. And this is much more common than you think. People keep reinventing the wheel over and over again. This is the number one reason why ROS was created: to stop you from reinventing the wheel anytime you need to create a robot. Just like you have open-source frameworks, tools, and environments to develop websites or mobile applications, why not do the same for robotics? This is the philosophy behind ROS: to provide a standard for robotics applications that you can use on any robot.

After you learn ROS, you can spend less time on the basics and focus on the key functionalities you want to add instead. You can program new robots in no time, join existing projects, and easily collaborate with a team.

What is ROS?

ROS is hard to define because it’s not just one thing. And to be honest, I don’t think you can truly understand what it is until you start to understand how to write code with it.

One thing we can start with is what ROS is not.

ROS is not an operating system. It’s a combination of four main parts:

FrameworkSet of toolsPlug-and-play pluginsAn online community

Let’s dive a bit more into each of these parts.

ROS is a framework with plumbing

ROS comes with a set of rules on how to build an application. As we will see in this book, you will need to create packages, and then write programs inside those packages (nodes). There is a specific way to create and write them, as well as create tools to build and use them.

Any framework comes with a specific set of rules. The remarkable thing about this is that after you’ve created a few projects, any new project is going to be easier and faster to set up. Also, as everyone is following the same set of rules, you can more easily work in a team or understand and use the code written by others.

As a direct consequence of using this framework, you get access to what is often referred to as plumbing, which means that the underlying communication between the nodes is managed for you. Imagine that you’re building a house, and the plumbing or electrical system is already done for you. This will save you a lot of development time, and you also don’t need to learn how to do it yourself (and thus, reinvent the wheel).

To sum it up, with ROS, you can easily separate your application into different sub-programs (called nodes). The communication between nodes is handled for you. You can easily test one component, and if this component fails, it will not affect the other running components. ROS is a modular framework.

A set of tools

ROS comes with a set of tools that allow you to develop faster. Among them, you can find command-line tools to build the application, introspection tools to monitor the flow of communication, logging functionalities, plots, and more.

You also get 3D visualization tools to see what your robot is doing, and even a complete simulator using real physics, named Gazebo, so that you can work on a realistic simulation before trying out your robot.

There are quite a lot of available tools, and we’ll discover many of them throughout this book. As an example of how useful it can be, there is one (called bags) that allows you to save communication streams so that they can be replayed later. Let’s say you build a mobile robot, and you need to test the robot outside when it’s raining, then continue to develop the software while taking the rain into account. You probably won’t have rain every day, or you won’t even have access to the robot any time you want. With this tool, you can run the experiment once, save the data, and replay it later to develop your application for a specific set of conditions.

Capabilities – plug-and-play plugins and stacks

This is probably where you will save hundreds of hours. Imagine two common scenarios:

You develop a mobile robot and need the robot to navigate autonomously in a dynamic environment.You develop a six-axis robotic arm and want to create motion planning to perform a smooth movement on all axes.

This looks quite complex and involves understanding and implementing several algorithms, as well as writing well-optimized and efficient code. This is where you would probably have to reinvent the wheel and waste lots of precious time.

For those two scenarios, you can find existing plugins that do the job for you. All you need to do is install the plugins and configure your robot to make it compatible. Of course, this is easier said than done, but the workload can be counted in days/weeks, not months/years. And once again, once you know how to use those plugins, your next project will take much less time.

There are many plugins that you can use. Some are quite simple, while others involve a collection of plugins and are also called frameworks or stacks. Your job as a ROS developer is to glue all those components together, and maybe create new components for functionalities that are not developed yet.

Online community

This is the fourth pillar of ROS, and it’s quite an important one: the community. ROS is an open-source project with a permissive license. I can’t give you any legal advice on licensing, but you can use ROS in a commercial product without having to redistribute your code.

You can find all the ROS code online, as well as the code for the plug-and-play plugins. Everything is easily accessible on GitHub.

The ROS project is also backed by an online community that you can most commonly find in the following areas:

Robotics Stack Exchange (https://robotics.stackexchange.com/): You can use this to ask technical questions. If you know Stack Overflow, as most developers do, well, this is Stack Overflow for robotics.ROS Discourse forums (https://discourse.ros.org/): Here, you can get informed about the latest developments, jobs, community projects, new ideas, and more. I recommend checking this website often to stay up to date with where ROS is going.

When to use ROS

Now that you understand a bit more what ROS is, should you use ROS whenever your project has something 'robotics' in it? In this section, I will give you some hints on when using ROS makes sense, backed by some examples to give you a better idea.

First, if you’re reading this book because you need to learn ROS for your work/university, then the question is easily answered: yes, you will use ROS for your project.

But if you must make the decision yourself, what should you do?

Let’s simplify robotics and say that a robotics system contains three categories of things: actuators, sensors, and controllers.

An actuator is something that creates movement (for example, a motor to rotate a wheel). A sensor will read data from the environment (for example, a camera, laser scan, or temperature sensor). A controller is something that is in between: it takes the data available from one or multiple sensors (input) and, through an algorithm, creates a command for the actuators of the robot (output). In a way, the controller is the brain or one of the brains of the robot.

For very simple applications, when you just have a few sensors and actuators, you might not need ROS.

Here are a few examples where ROS isn’t needed:

You just need to take a picture from a camera when a user presses a button, using a Raspberry Pi board, and send this picture to a web server. There’s no need to use ROS—you can just combine a few Python libraries in a script, and you’re done. Using ROS here would be a good example of over-engineering (unless you do this for learning purposes).You have to use a servo motor to open/close a door when a movement is detected, using an infrared sensor. This is a very simple application that can easily be programmed using a basic microcontroller board—and you can do a quick prototype with a board such as Arduino.You have built a simple robot with two wheels and an infrared sensor, and you want to make the robot follow a line. This is a typical project that’s given to students in engineering school, and a simple algorithm on an Arduino board will do.

Now, let’s consider some examples where ROS is needed:

You have a new mobile robot with two wheels and a laser scan, and you want to read data from the laser scan, map the environment, make the robot move autonomously, and control the two wheels accordingly. On top of that, you want to simulate the robot in 3D with real physical properties. This is when ROS is going to become very handy. Not only will it help you to make all the components work together, but you can also use existing algorithms for path planning (through a ROS plugin) and simulation.You need to create a system that contains a six-axis robotic arm, or even multiple robotic arms working together, along with conveyor belts and mobile robots.Your robotics application (not necessarily just one robot) contains lots of sensors and actuators that you want to develop separately and add them in a modular way.You want to create a hardware driver for a component and make this component easy to use by other robotics developers. By making the component ROS-compatible, anybody who knows ROS can integrate it into their application with low effort.

As you can see from the former examples, ROS is not always needed whenever you need to program hardware or create a robotics system. Of course, you could use it for any application, but it’s like if you were to use a complete web framework (for example, Django) for a single static web page.

With the latter examples, you can see that if your system becomes more complex, if you want to easily collaborate with other developers, or if you realize that one big part of your system can be solved with one of the plug-and-play plugins, ROS may be the solution.

Of course, it takes time to learn it and your first project will take longer to complete, but then, with more experience, you will go much faster.

As an example, it could take less than a week for a senior ROS developer to write custom code for a robotic arm (including robot model, motion planning, and hardware control) and the same for a mobile robot with navigation capabilities (provided that the hardware already has a ROS driver). Less than a week and you get a workingsoftware prototype.

ROS 1 versus ROS 2

To be clear, this book is all about ROS 2, not ROS 1. You will start learning ROS 2 from zero experience. This section is probably the only time I will be talking that much about ROS 1.

A quick story of ROS, and how we got to ROS 2

ROS 1 (originally called ROS) was first developed in 2007. It quickly gained popularity and grew exponentially in the following years.

In 2014, the ROS 2 project was announced. Simply put, ROS 1 was a bit too limited for industrial applications (lack of real-time support, safety, and so on) and was only used in research/education. To solve this problem, the developers decided to make ROS more “industrial friendly,” as well as make it better, thanks to all the lessons learned from the beginning of ROS.

Now, why create ROS 2 and not just continue ROS with some new changes? Well, the changes were too big, and they would have completely broken compatibility with older versions. Thus, it was decided to create a completely new ROS from scratch and name it ROS 2. In 2014, ROS 2 was officially announced, and the development of the project started.

In December 2017, the first ROS 2 distribution was released, which meant that ROS 1 and ROS 2 started to co-exist. At this point, ROS 2 was lacking many core functionalities and plugins, making it unsuitable for serious projects. Most ROS developers were still using ROS 1.

Years passed by and ROS 2 got more development, plugins, and more. Its popularity started to grow.

I would say it was worth it to use ROS 2 (compared to ROS 1) starting from 2022. This is probably more of a personal opinion and some people might disagree, but from 2022 and the release of ROS 2 Humble (more on distributions in Chapter 2), we had access to a long-term release that was stable, with all the major plugins and stacks working correctly, which is what you need to program a robot.

In the meantime, it was announced that ROS 1 would end in May 2025. After this date, ROS 1 would still exist, but it wouldn’t be supported anymore.

2023 was the year with the most significant shift from ROS 1 to ROS 2 among the ROS community. It is now safe to say that ROS 2 is the way to go when developing new ROS applications.

So, if you had previously heard about ROS 1 and ROS 2, now you know that ROS 2 is what you need to learn, and we can say that ROS 1 is a dead project. But is that true?

Is ROS 1 dead already?

In theory, yes, but in practice, it’s (always) a bit different. As you probably know, several companies are using obsolete and legacy technologies. The reason is that updating software to a new version is often quite expensive and can also be risky. That’s why you still see job offers from banking systems requiring skills in Cobol, a programming language from the 1960s that no one uses anymore.

In robotics, things are a bit similar. Some companies have released robots with a specific version of ROS 1, and while the robot is still on the market, the company will not upgrade and still use and maintain the previous version, also called legacy. Thus, the definitive transition in 2025 is going to take a few more years.

Why am I writing this? Simply to let you know that if you happen to get a job in a robotics company that has been using ROS already, you might encounter a few ROS 1 projects, even after ROS 1 is officially finished. However, be assured that all the ROS 2 knowledge you have can easily be ported to ROS 1 as the core concepts are the same.

To conclude, for all new learnings, projects, studies, teaching, and startups, ROS 2 is what you need. I will now close this chapter of ROS 1 and focus on ROS 2. As mentioned previously, I might write ROS or ROS 2 interchangeably as we aren’t targeting ROS 1 here.

Prerequisites for starting with ROS 2

To get started with ROS and this book, there are a few things you need to know.

Knowledge prerequisites

It is best that you have some knowledge of the following:

Linux command line: Since we’ll be using Ubuntu, being familiar with Linux is mandatory. You don’t need to be an expert—you just need to know the basics. Many tools in ROS 2 involve the command line, so knowing how to open a Terminal and write basic commands will help you tremendously.Python programming: The two most common languages for ROS are Python and C++. Python is easier to get started with and allows you to prototype things faster. Hence, this is the language we will use for all detailed explanations. You need to know Python basics, and object-oriented programming (OOP) is a good plus as ROS 2 is heavily using OOP everywhere.Optional: C++ programming. Even if the focus of the book is on Python, I still wanted to include C++ code for everything we do. If you only want to learn Python, you can ignore the C++ code, but of course, if you want to follow C++ instructions, you need C++ basics (better with OOP).

I want to emphasize that it will be much, much easier for you to learn ROS 2 if you have good programming and Linux basics. Learning ROS is already quite challenging (though with this book, the goal is to reduce the learning curve), so if you’re starting ROS, Linux, and Python from scratch, this could be overwhelming.

If you’re reading these lines and you don’t know how to write a Python function or navigate to a directory in the Terminal, then I really recommend that you pause here, take some time to learn Python and Linux basics, and come back to this book. There’s no need to spend hundreds of hours doing this but investing some time to get the basics right will help you finish this ROS 2 book faster.

Hardware and software

You’ll need to have a computer to follow this book. Regarding the specifications, you don’t need anything fancy to get started with ROS 2. If you can open a web browser with a few tabs and have a smooth experience, I would say that your computer is good enough to get started.

Then, later, depending on what you want to do with ROS, you might need a better machine (for example, if you want to simulate multiple robots using lots of sensor and image processing). However, it’s probably better to wait until you need the extra power to upgrade. For now, the most important thing to do is start learning ROS.

For software requirements, I will give you the necessary installation instructions throughout this book. All the software we will be using is free to use and open source.

We will also use Ubuntu 24.04, in which we will run ROS 2. Having Ubuntu installed is a requirement, but I will give you a recap in Chapter 2.

How to follow this book

The book is divided into three parts, including 14 chapters.

Each chapter can be followed individually, although for one chapter, you need the knowledge from all previous chapters.

If you got this book because you just want to get started from scratch, then it’s simple: follow the book in the order it’s been written. I have designed it specifically so that you learn the concepts one step at a time without having to think about what directions you should take.

Then, as you progress, feel free to come back to any chapter to clear up doubts. I encourage you to do that. The first time you learn about a concept, you don’t necessarily grasp all the subtleties. As you continue with this book and use the concept along with other new concepts, you often have ‘epiphany moments’, when everything clicks together.

If you already know some ROS 2 basics (or you’ve already read this book), then feel free to jump to any chapter. If a chapter starts from a code base that we developed in previous chapters, then you will be able to download the code and start from there.

There is a GitHub repository you can use to follow this book: https://github.com/PacktPublishing/ROS-2-from-Scratch. All the code we’ll write is hosted there, so be sure to use this GitHub repository closely while following along. I will explain how to use this repository a bit later in this book.

Summary

In this introductory chapter, we cleared up some of the most common confusion points regarding ROS: its name, what it is and isn’t, when to use it, and why. You also learned more about the different ROS versions (ROS 1 and ROS 2), and you learned what kind of prerequisites you need to get started with ROS 2.

You should now have a better understanding of the big picture, and even if everything still seems a bit confusing, don’t worry too much—it will all make sense when you use the ROS 2 concepts and code with them.

Now, to be able to use ROS 2, we need to install it. This will be the focus of the next chapter and will help you get your environment 100% ready for ROS 2.

2

Installing and Setting Up ROS 2

Before using ROS 2, we need to install it and set it up. Doing this is not as trivial as just downloading and installing a basic program. There are several ROS 2 versions (called distributions), and we need to choose which one is the most appropriate. We also need to pick an Ubuntu version as ROS and Ubuntu distributions are closely linked.

Once you know which ROS/Ubuntu combination you need, you have to install the corresponding Ubuntu operating system (OS). Although being familiar with Linux is a prerequisite for this book, I will still do a recap on how to install Ubuntu on a virtual machine (VM), just in case, so you won’t be lost and can continue with this book.

Then, we will install ROS 2 on Ubuntu, set it up in our environment, and install additional tools that will allow you to have a better development experience.

By the end of this chapter, you will have everything ready on your computer so that you can use ROS 2 and write custom programs.

Even if all the installation steps sound a bit daunting, don’t worry—it’s not that hard, and it gets easier to do with every new installation. To give you an idea, with a stable internet connection, it takes me about 1 hour to install a fresh version of Ubuntu and 20 minutes to install ROS (most of that time is spent waiting for the installation to finish).

In this chapter, we are going to cover the following topics:

Which ROS 2 distribution to chooseInstalling the OS (Ubuntu)Installing ROS 2Setting up the environment for ROS 2Extra tools for ROS 2 development

Which ROS 2 distribution to choose

Before you install ROS 2, it’s important to know which distribution you need to use. To make that decision, you first need to understand a bit more about what ROS 2 distributions are, and what specificities each one has.

What is a ROS 2 distribution?

ROS 2 is a project in continuous development, constantly receiving new features or improvements to existing ones.

A distribution is simply a freeze in the development at some given point to create a stable release. With this, you can be sure that the core packages for one given distribution will not have any breaking changes. Without distributions, it would be impossible to have a stable system, and you would need to update your code constantly.

Every year, a new ROS 2 distribution is released on May 23. This day corresponds to World Turtle Day. As you will be able to observe, all ROS distributions have a turtle as a logo; there’s a mobile robot platform named TurtleBot and even a 2D educational tool named Turtlesim. This is based on a reference to an educational programming language from 1967 named Logo, which included a feature to move some kind of turtle robot on the screen. So, if you were confused about why there are so many turtles everywhere, now you know—and that’s the end of this turtle parenthesis.

You can see all ROS 2 distributions on the ROS 2 documentation releases page: https://docs.ros.org/en/rolling/Releases.html.

You will see one new distribution every year in May. As for the order, there is no number; instead, the names are in alphabetical order. The first official release was named Ardent Apalone, then Bouncy Bolson, and so on. In May 2024, ROS Jazzy Jalisco was released. Following this, you can expect to have ROS K in 2025, ROS L in 2026, and so on. The name of a new release is usually announced 1 year in advance.

Note

ROS distributions contain two names, but it’s common practice just to refer to the first one. So, instead of talking about ROS Jazzy Jalisco, we will talk about ROS Jazzy. We could also write ROS 2 Jazzy to specify that this distribution is for ROS 2, not ROS 1, but this isn’t needed since Jazzy is a name only used for ROS 2, hence ROS Jazzy.

On top of all the displayed distributions, there is another one that exists in parallel: ROS Rolling. This distribution is a bit special and is the distribution where all new developments are made. To make an analogy with Git and versioned systems, it’s just like having a development branch and using this branch to release stable versions once a year. Thus, ROS Rolling is not a stable distribution, and I don’t recommend using it for learning or to release a product. This is a distribution you can use if you want to test brand-new features before they are officially released into the next stable distribution—or if you want to contribute to the ROS code. However, if you’re reading this book, you’re not there yet.

Now that you know what ROS 2 distributions are and how to find them, let’s start to look at the differences between them. This will allow us to choose theright one.

LTS and non-LTS distributions

If you look a bit closer, you’ll see that some distributions are supported for 5 years, others for 1.5 years (this only applies after 2022). You can see this by comparing the release date with the end-of-life (EOL) date. Currently supported distributions also have a green background on the screen, so you can easily spot them.

When a distribution reaches its EOL date, it just means that it will not receive official support and package updates anymore. This doesn’t mean you can’t use it (in fact, lots of companies are still using legacy versions from 5 years ago or more), but you won’t get any updates.

The first official ROS 2 distribution was ROS Ardent, released in December 2017. After that, the first few distributions were still not quite complete, and the development team preferred to release shorter distributions so that the development could go faster.

ROS Humble was the first Long-Term Support (LTS) release supported for 5 years (2022-2027).

ROS Jazzy is also an LTS version, with official support from 2024 to 2029. From this, you can expect that every 2 years (even number: 2024, 2026, 2028, and so on), a new LTS distribution will be released in May and supported for 5 years.

A few LTS distributions can coexist. In 2026, for example, with the release of the ROS L distribution, you will be able to use ROS Humble and ROS Jazzy as well.

Then, you have non-LTS distributions. Those are released on odd years (2023, 2025, 2027, and so on) and are supported for 1.5 years only. Those distributions are released just so you can have access to new development in a somehow stable release without having to wait for 2 years. However, due to the short lifespan of non-LTS distributions and the fact that they will probably be less stable (and less supported), it is best not to use them if your goal is learning, teaching, or using ROS for a commercial application.

With this, you can see that we can discard half of the distributions and now focus only on the LTS ones that are currently supported. Let’s finish this section and choose the distribution that we will use for this book.

How to choose a ROS distribution

What I recommend is to use the latest available LTS distribution. However, I wouldn’t necessarily use an LTS distribution just after it’s been released because it can still contain some bugs and problems. Also, some of the plugins and other community packages you need might not have been ported yet. Generally, if you want to work with a stable system, it’s sometimes best not to stay too close to the new and shiny technology and wait a bit.

For example, ROS Humble was released in May 2022. Right after it was available, I tested it, but to use it in a production environment, I would have had to wait until September or even November, just to be sure everything was working correctly.

So, for this book, we will use ROS Jazzy, which was released in May 2024.

Note

You can learn with one distribution and then start a project with another one. If you have a project or job that requires you to use a different ROS 2 distribution, you can still start to learn with ROS Jazzy. The gap between distributions is very small, especially for the core functionalities. 99% of this book can be applied to any ROS 2 LTS distribution released after 2022.

Installing the OS (Ubuntu)

ROS 2 works on three OSs: Ubuntu, Windows, and macOS. Although Ubuntu and Windows get Tier 1 support, macOS has only Tier 3 support, meaning “best effort,” not “fully tested.” You can learn more about what Tier 1, 2, and 3 mean on the REP 2000, which describes the timeline and target platforms for ROS 2 releases: https://www.ros.org/reps/rep-2000.html.

This means that using macOS for ROS 2 is not necessarily the best choice for learning (if you’re an Apple user). We’re left with Windows or Ubuntu.

From teaching experience, I saw that even if ROS could work well on Windows, it’s not easy to install and use it correctly. Lots of bugs can occur, especially with the 2D and 3D tools. When you’re learning ROS, you want a smooth experience, and you want to spend time learning the features, not fixing the configuration.

Hence, the best overall option is to use Ubuntu. If you don’t have Ubuntu and are using Windows/macOS, you can either install Ubuntu natively as a dual boot on your computer or use a VM (there are a few other options, but I won’t cover those here).

Now that we have selected ROS Jazzy, and we want to run it on Ubuntu, the question is: on which Ubuntu distribution do we install it?

The relationship between ROS 2 and Ubuntu

If you go to the Jazzy release page (https://docs.ros.org/en/rolling/Releases/Release-Jazzy-Jalisco.html), you’ll see that ROS Jazzy is supported on Ubuntu 24.04 (and not any other previous or future Ubuntu distributions).

There is a close relationship between ROS and Ubuntu distributions. This relationship is quite simple: for every new Ubuntu LTS distribution (every 2 years on an even number), there is a new ROS 2 LTS distribution:

Ubuntu 22.04: ROS HumbleUbuntu 24.04: ROS JazzyUbuntu 26.04: ROS L

It’s important to use the correct combination. Thus, before installing ROS Jazzy, the first thing you must do is make sure you have Ubuntu 24.04 installed on your computer. If you happen to have an older version, I strongly encourage you to upgrade or simply install Ubuntu 24.04 from scratch.

Note

If you have to use another Ubuntu distribution because, for example, you’re using a computer from school/work and you can’t change the OS, then use the corresponding ROS distribution. However, I recommend not