ROS 2 from Scratch: Get started with ROS 2 and create robotics applications with Python and C++
()
Related to ROS 2 from Scratch
Related ebooks
Ultimate Robotics Programming with ROS 2 and Python: Design, Develop, and Implement Intelligent Robotics Applications with Advanced Navigation, Simulation, and Computer Vision for Mobile and Industrial Robots (English Edition) Rating: 0 out of 5 stars0 ratingsRobot Operating System: Mastering Autonomous Systems for Seamless Integration and Control Rating: 0 out of 5 stars0 ratingsROS Robotics By Example Rating: 0 out of 5 stars0 ratingsEffective Robotics Programming with ROS - Third Edition Rating: 0 out of 5 stars0 ratingsHands-On Python for DevOps: Leverage Python's native libraries to streamline your workflow and save time with automation Rating: 0 out of 5 stars0 ratingsArduino: Building Intelligent Systems with Microcontroller Programming Rating: 0 out of 5 stars0 ratingsEmbedded Rust Programming: Building Safe and Efficient Systems Rating: 0 out of 5 stars0 ratingsRust for Embedded Systems Rating: 0 out of 5 stars0 ratingsPython Data Structures and Algorithms Rating: 5 out of 5 stars5/5Open Source Robotics: Advancing Innovation and Collaboration Through Accessible Technology Rating: 0 out of 5 stars0 ratingsProjects with IOTA Rating: 0 out of 5 stars0 ratingsRobots vs Drones Rating: 0 out of 5 stars0 ratingsBuilding an Operating System with Rust: A Practical Guide Rating: 0 out of 5 stars0 ratingsRobotic Mechanical Systems Fundamentals Rating: 0 out of 5 stars0 ratingsBuilding Scalable Web Apps with Node.js and Express Rating: 0 out of 5 stars0 ratingsEmbedded Systems Programming with C++: Real-World Techniques Rating: 0 out of 5 stars0 ratingsIndustrial Robotics: Automation Beyond the PLC: Industrial Automation, #3 Rating: 0 out of 5 stars0 ratingsPython for TensorFlow Pocket Primer: A Quick Guide to Python Libraries for TensorFlow Developers Rating: 0 out of 5 stars0 ratingsUltimate Web API Development with Django REST Framework Rating: 0 out of 5 stars0 ratingsElectronic Engineering: Advancing Robotics Through Circuit Design and System Integration Rating: 0 out of 5 stars0 ratingsMastering the Art of Rust Programming: Unraveling the Secrets of Expert-Level Programming Rating: 0 out of 5 stars0 ratingsRobotics: Understanding Intelligent Systems and Their Impact on Society Rating: 0 out of 5 stars0 ratingsHome Automation with Raspberry Pi: Projects Using Google Home, Amazon Echo, and Other Intelligent Personal Assistants Rating: 0 out of 5 stars0 ratingsRobots in Society - How Automation is Shaping Our Future Rating: 4 out of 5 stars4/5
Robotics For You
Human Compatible: Artificial Intelligence and the Problem of Control Rating: 4 out of 5 stars4/5Life 3.0: Being Human in the Age of Artificial Intelligence Rating: 4 out of 5 stars4/5Some Future Day: How AI Is Going to Change Everything Rating: 0 out of 5 stars0 ratingsDark Aeon: Transhumanism and the War Against Humanity Rating: 5 out of 5 stars5/5Artificial Intelligence Revolution: How AI Will Change our Society, Economy, and Culture Rating: 5 out of 5 stars5/5How to Build an Android: The True Story of Philip K. Dick's Robotic Resurrection Rating: 4 out of 5 stars4/5Possible Minds: Twenty-Five Ways of Looking at AI Rating: 3 out of 5 stars3/5101 Spy Gadgets for the Evil Genius 2/E Rating: 4 out of 5 stars4/5Inside Nvidia: Jensen Huang's Vision for Artificial Intelligence Rating: 0 out of 5 stars0 ratingsHow to Survive a Robot Uprising: Tips on Defending Yourself Against the Coming Rebellion Rating: 3 out of 5 stars3/5How to Walk on Water and Climb up Walls: Animal Movement and the Robots of the Future Rating: 4 out of 5 stars4/5The Official Raspberry Pi Handbook 2024: Astounding projects with Raspberry Pi computers Rating: 0 out of 5 stars0 ratingsArtificial Intelligence: The Complete Beginner’s Guide to the Future of A.I. Rating: 4 out of 5 stars4/5Computer Engineering: Advancing Automation and Intelligent Systems Rating: 0 out of 5 stars0 ratingsChatGPT: The Future of Intelligent Conversation Rating: 4 out of 5 stars4/52062: The World that AI Made Rating: 5 out of 5 stars5/5Arduino: The complete guide to Arduino for beginners, including projects, tips, tricks, and programming! Rating: 4 out of 5 stars4/5Arduino: The ultimate guide to Arduino, including projects, programming tips & tricks, and much more! Rating: 0 out of 5 stars0 ratingsMastering Drones - A Beginner's Guide To Start Making Money With Drones Rating: 2 out of 5 stars2/5Robot Building For Dummies Rating: 3 out of 5 stars3/5Extended Kalman Filter: Advanced Techniques in Dynamic State Estimation for Robotic Systems Rating: 0 out of 5 stars0 ratingsRebooting AI: Building Artificial Intelligence We Can Trust Rating: 4 out of 5 stars4/5Collection of Raspberry Pi Projects Rating: 5 out of 5 stars5/5Pumps vs Furnaces Rating: 0 out of 5 stars0 ratingsThe Official Raspberry Pi Projects Book Volume 4: 200 Pages of Inspiration and Ideas. 55 Projects & Guides Rating: 0 out of 5 stars0 ratings
Reviews for ROS 2 from Scratch
0 ratings0 reviews
Book preview
ROS 2 from Scratch - 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 .
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
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 and click on the Start button.
Tips or important notes
Appear like this.
Get in touch
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, email us at [email protected]
and mention the book title in the subject of your message.
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 would report this to us. Please visit www.packtpub.com/support/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 [email protected]
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 authors.packtpub.com
.
Share Your Thoughts
Once you’ve read ROS 2 from Scratch, we’d love to hear your thoughts! Please click here to go straight to the Amazon review page
for this book and share your feedback.
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
leave_a_review_qr_codehttps://packt.link/free-ebook/9781835881408
Submit your proof of purchase
That’s it! We’ll send your free PDF and other benefits to your email directly
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 2
Chapter 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:
Terminology
What is ROS, when should we use it, and why?
ROS 1 versus ROS 2
Prerequisites for starting with ROS 2
How 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 ROS
ROS 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 on
ROS 1: When talking specifically about the first version of ROS. However, this will be quite rare since the focus here is on ROS 2
ROS 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:
Framework
Set of tools
Plug-and-play plugins
An 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