Semi-autonomous drone navigation
Python for robotics
Introduction to ROS2
Legrand Joël
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 1 / 44
Topic
1 Introduction
2 Key concepts
3 ROS2 tools and third party
4 Simulation and robots
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 2 / 44
Objectifs et organisation
Cours objectives
Understanding the key concepts of ROS2
I Nodes, messages, services, . . .
Getting to know the tools and ecosystem around ROS2
I Colcon for building project
I Introspecting and visualization tools
I Simulation tools (Gazebo)
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 3 / 44
ROS is a middleware
Robot Operating System (ROS)
Open-source middleware
(not an OS)
Development environment
I Visualization and
introspection tools
Communication library and
tools
Packaging system
I colcon command
Images from [Link]
Plenty of existing modules
Active community
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 4 / 44
ROS abstract layers
DDS (Data Distribution Service)
I Standard that enable data exchanges using
a publish–subscribe pattern.
I Middleware providing high-performance,
scalable, and secure way for nodes to
exchange data and communicate with each
other.
I Used as the communication layer for
ROS2.
rmw (ROS MiddleWare)
I Middleware providing the underlying
communication infrastructure for ROS2
using DDS
I Abstracts the complexity of DDS
I → developers do not need to worry about
the details of how DDS works.
Images from [Link]
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 5 / 44
ROS abstract layers 2
rcl (ROS Client Library)
I Middleware providing a high-level interface
for building and running robot applications
using rmw.
I Abstract the complexity of rmw
I → developers do not need to worry about
the details of how rmw works.
rclcpp
I C++ implementation of rcl
I Provides a set of C++ classes and
functions for building and running robot
applications using rcl
I Provides a number of features and tools
that make it easier to develop and debug
robot applications.
rclpy (resp. rcljava)
I same as rclpp by in python (resp. java) Images from [Link]
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 6 / 44
What ROS is not
Robot Operating System
Not a (computer) operating system but rather a Meta Operating System
I Officially available on Ubuntu Gnu/Linux
I Experimental support for: macos, MS Windows, Fedora, Gentoo, Debian. . .
Not a programming language
I ROS programs written in C++ and Python.
I Experimental: Java, Lisp, Octave. . .
Not a hard real-time environment
Not a development environment
I Can be used from IDE, text editor and command line
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 7 / 44
History of ROS 1/3
Originally from Stanford University (2006)
I personal project of Keenan Wyrobek and Eric Berger
Slide from Eric and Keenan pitch deck
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 8 / 44
History of ROS 2/3
2008 Investment by Willow Garage
I Fist version in 2009 (ROS Mango Tango)
I ...
I ROS Groovy Galapagos, in 2012
From Willow Garage
TurtleBot launched on 2011
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 9 / 44
History of ROS 3/3
2014 Open Source Robotics Foundation
I ROS Hydro Medusa, in 2013
I ...
I ROS Melodic Morenia, in 2018
I ROS2 realeased on 2017
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 10 / 44
Why ROS ?
Open Source:
I Active community, still evolving and improving
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 11 / 44
Why ROS ?
Open Source:
I Active community, still evolving and improving
Reusability:
I Provides various open-sourced tools and software, making it easy to
contribute, adapt and share
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 11 / 44
Why ROS ?
Open Source:
I Active community, still evolving and improving
Reusability:
I Provides various open-sourced tools and software, making it easy to
contribute, adapt and share
Support for Development Tools:
I Debugging tools, 2D & 3D visualization tools (Rviz)
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 11 / 44
Why ROS ?
Open Source:
I Active community, still evolving and improving
Reusability:
I Provides various open-sourced tools and software, making it easy to
contribute, adapt and share
Support for Development Tools:
I Debugging tools, 2D & 3D visualization tools (Rviz)
Rapid Testing:
I Before testing on physical robots, ROS provide simulators (Gazebo) and
simple way to record and playback sensor data (rosbags)
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 11 / 44
Topic
1 Introduction
2 Key concepts
3 ROS2 tools and third party
4 Simulation and robots
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 12 / 44
Computational graph
The computational graph is a network of nodes (a distributed system) that solves
a problem:
Nodes are processes doing some computation
I nodes are represented with rectangles in the graph
Nodes talk to each others by sending messages
I communication between nodes are represented by arrows and ellypses
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 13 / 44
Nodes
ROS applications consist of a composition of individual nodes
I Nodes perform narrow tasks
I Nodes are are decoupled from other parts of the system
F Individual processes that do not share memory
I Nodes talk to each other using a publish-subscribe or request-response
messaging patterns (see next slide)
Ex: Drones
A node linking ROS and the drone driver
A node linking ROS and the joystick driver
Several nodes that manage basic behavior
A node that manages different behaviors
...
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 14 / 44
Messages
Messages are bits of data sent from one node to another over a topic.
They are written in the ROS Message Description Language (.msg)
Many common messages are available in
[Link]
Example: [Link]
float32 r
float32 g
float32 b
float32 a
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 15 / 44
Custom messages
If you need a custom message
Create a specific package
I Do not add you custom message in the package it is used to prevent
dependecy issues
Use semantically meaningful names
By convention, name this package custom_name_interfaces
By convention, use CamelCase for the name of the interface.
I Ex: “[Link]”
Your message can include any number of
I ROS2 primitive data types
I Or already existing messages
Example: [Link]
string command
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 16 / 44
Publisher and Subscriber
Publishers and Subscribers are key components for communication.
Publisher
Role:
I Sends data (typed messages) to a specific topic.
Characteristics:
I Publishes messages of a specific type.
I Multiple publishers can write to the same topic.
Example (in Python)
pub = Node.create_publisher(String, ’my_topic’)
msg = String()
[Link] = ’Hello, ROS 2!’
[Link](msg)
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 17 / 44
Publisher and Subscriber 2
Subscriber
Role:
I Receives data (typed messages) from a specific topic.
I Trigger a callback functions
Characteristics:
I Subscribes to messages of a specific type.
I Multiple subscribers can read from the same topic.
Example (in Python)
Node.create_subscription(String, ’my_topic’, my_callback)
def my_callback(msg):
print(f"Received: {[Link]}")
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 18 / 44
Timer
Role:
I Create timers to schedule callback functions at specified intervals.
Example (in Python)
def timer_callback():
# Your callback logic here
pass
# Execute this timer_callback() every 1.5 seconds
timer = self.create_timer(1.5, timer_callback)
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 19 / 44
Service
Use Cases
I for synchronous, request-response interactions.
I Commonly used for tasks setting parameters, requesting data, or triggering
actions.
Benefits
I Synchronous communication for real-time control.
I Robust error handling through response messages.
Images from ROS2 documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 20 / 44
Service - .srv interface
Defines the structure of requests and responses.
Enables nodes to communicate seamlessly.
Standard srv interfaces available in
I https:
//[Link]/ros2/common_interfaces/tree/rolling/std_srvs/srv
[Link]
int64 a
int64 b
---
int64 sum
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 21 / 44
Service - Server
Role:
I Listens for incoming requests.
I Provides a service upon request.
Characteristics:
I Offers a specific service with a well-defined interface.
Example (in Python)
from example_interfaces.srv import AddTwoInts
class MinimalService(Node):
def init(self):
super().__init__(’minimal_service’)
[Link] = self.create_service(
AddTwoInts, ’add_two_ints’,
self.add_two_ints_callback)
def add_two_ints_callback(self, request, response):
[Link] = request.a + request.b
return response
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 22 / 44
Service - Client
Role:
I Sends requests to a service server.
I Receives responses from the server.
Characteristics:
I Calls a specific service with the same well-defined interface as the server
Example (in Python)
import sys
from example_interfaces.srv import AddTwoInts
class MinimalClientAsync(Node):
def __init__(self):
super().__init__(’minimal_client_async’)
[Link] = self.create_client(AddTwoInts, ’add_two_ints’)
while not [Link].wait_for_service(timeout_sec=1.0):
self.get_logger().info(’service not available, waiting’)
[Link] = [Link]()
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 23 / 44
Service - Client 2
Example (In Python)
class MinimalClientAsync(Node):
[...]
def send_request(self, a, b):
[Link].a = a
[Link].b = b
[Link] = [Link].call_async([Link])
rclpy.spin_until_future_complete(self, [Link])
return [Link]()
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 24 / 44
Actions
Use Cases
I Actions are suitable for long-running, asynchronous tasks.
F Ex: path planning, robot navigation, . . .
Benefits
I Goal status monitoring and feedback.
F enable clients to send goals and servers to execute and provide steady feedback,
as opposed to services which return a single response.
I Similar to services (client-server) except actions are preemptable (you can
cancel them while executing).
I Robust error handling through result messages.
Images from ROS2 documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 25 / 44
Node Discovery
ROS-DDS abstraction layer offers service that makes it possible for nodes to find
each other and talk to each other
CAUTION: you MUST set your ROS_DOMAIN_ID environment variable if you
are broadcasting messages !
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 26 / 44
Composition
ROS applications consist of a composition of individual “nodes” performing narrow
tasks and are decoupled from other parts of the system. This promotes fault
isolation, faster development, modularity, and code reuse, but it often comes at
the cost of performance.
Node Composition
Efficient intra-process communication (only in C++)
I Sending messages creates copy
I → run multiple nodes un the same process (with shared memory)
Dynamic Composition (only in C++)
I Node can be loaded at runtime
Allows to manage callback order
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 27 / 44
Components (only C++)
A component is a node designed to be used with dynamic composition.
I Subclass of rclcpp::Node
I Built into a shared library
It doesn’t have a main function, as it is not meant to be directly instantiated
Registers itself using macros from the rclcpp_components
I makes the component discoverable when its library is loaded into a running
process
But only for C++. . .
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 28 / 44
Executor
Execution management in ROS 2 is handled by Executors
By default, callback functions for a given subscription are executed
sequentially
ROS2 allows to provides more control over execution management by using
explicitelly the Executor API
Executor uses one or more threads to invoke the callbacks of subscriptions,
timers
From ROS2 documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 29 / 44
Executor 2
All executors can be used with multiple nodes.
Exemple code
rclcpp::executors::StaticSingleThreadedExecutor executor;
executor.add_node(node1);
executor.add_node(node2);
executor.add_node(node3);
[Link]();
In this example, all callbacks will be executed sequetially in a single thread.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 30 / 44
Callback groups
ROS2 allows organizing the callbacks of a node in groups
Subscription and timers are assigned to the default callback group.
There are two types of callback groups
I Mutually exclusive: must not be executed in parallel.
I Reentrant: may be executed in parallel.
Callbacks of different callback groups may always be executed in parallel.
Some groups can be prioritized (subscriptions and timers of a control loop)
over others (standard services)
Example (Callback group)
my_callback_group = MutuallyExclusiveCallbackGroup()
my_subscription = self.create_subscription(
Int32, "/topic", [Link], qos_profile=1,
callback_group=my_callback_group)
CAUTION: Beware of deadlocks (see thread and multiprocessing practical
session) !
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 31 / 44
Launch files
Configuration files for launching and managing multiple nodes and
parameters.
Provides a structured way to start and configure components in a ROS2
system.
Launched with the command:
ros2 launch my_package my_launch
Example (XML file)
<launch>
<node pkg="my_package" type="my_node" name="node_name">
<param name="param_name" value="param_value" />
</node>
...
</launch>
Launch files also exist in Python for more flexibility (but more complex).
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 32 / 44
Topic
1 Introduction
2 Key concepts
3 ROS2 tools and third party
4 Simulation and robots
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 33 / 44
ros2 commandline
ros2 pkg list
I Lists all the packages that are currently installed in your ROS 2 workspace.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 34 / 44
ros2 commandline
ros2 pkg list
I Lists all the packages that are currently installed in your ROS 2 workspace.
ros2 pkg create
I Creates a new ROS 2 package in your workspace.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 34 / 44
ros2 commandline
ros2 pkg list
I Lists all the packages that are currently installed in your ROS 2 workspace.
ros2 pkg create
I Creates a new ROS 2 package in your workspace.
ros2 pkg lint
I Lints one or more packages to ensure that they meet the ROS 2 package
guidelines.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 34 / 44
ros2 commandline
ros2 pkg list
I Lists all the packages that are currently installed in your ROS 2 workspace.
ros2 pkg create
I Creates a new ROS 2 package in your workspace.
ros2 pkg lint
I Lints one or more packages to ensure that they meet the ROS 2 package
guidelines.
ros2 pkg test
I Runs the tests for one or more packages in your ROS 2 workspace.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 34 / 44
ros2 commandline
ros2 pkg list
I Lists all the packages that are currently installed in your ROS 2 workspace.
ros2 pkg create
I Creates a new ROS 2 package in your workspace.
ros2 pkg lint
I Lints one or more packages to ensure that they meet the ROS 2 package
guidelines.
ros2 pkg test
I Runs the tests for one or more packages in your ROS 2 workspace.
ros2 doctor
I checks all aspects of ROS 2, including platform, version, network,
environment, running systems and more
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 34 / 44
Bag files
A binary file format for recording and playing back ROS 2 data.
Allows to record and play back sensor data, messages, and events.
I Example: Record sensor data during a robot’s operation for later analysis.
I Debugging: Replay bag files to identify issues in your robot’s behavior.
I Share bag files to enable others to replicate your experiments.
Content of a Bag File
Timestamped data
I each message in a bag file has a timestamp indicating when it was recorded.
Topics and messages
Metadata
I ROS version, start date, end date, duratino, bag size, . . .
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 35 / 44
Bag files 2
Recording Data
ros2 bag record
I Ex: ros2 bag record -o my_data /camera/image /sensor/scan
I Record only relevant topics to save storage space.
Playing Back Data
ros2 bag play
I Ex: ros2 bag play my_data.bag
I Reproducing past experiments
F Example: Replay a bag file to recreate a robot’s behavior.
I Simulating sensor data
F Example: Test algorithms with recorded sensor data instead of real sensors.
Inspecting Bag Files
Using ros2 bag info
I Ex: ros2 bag info my_data.bag
F Checking available topics and duration
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 36 / 44
Third party tools - RVIZ
RViz (ROS Visualization)
3D visualization software tool for robots, sensors, and algorithms
Enables to see the robot’s perception of its world (real or simulated).
Rviz is NOT a simulation tool
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 37 / 44
Third party tools - Rqt_graph
rqt_graph displays a visual graph of the processes running in ROS and their
connections.
From ROS documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 38 / 44
Third party tools - Rqt_plot
rqt_plot lets you visualize scalar data published to ROS topics.
From ROS documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 39 / 44
Third party tools - Rqt_image_view
Rqt_image_view allow to visualize image/video topics.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 40 / 44
Third party tools - Rqt_bag
rqt_bag is a visualizer that lets you see and run graphically data recorded in bag
files.
From ROS documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 41 / 44
Topic
1 Introduction
2 Key concepts
3 ROS2 tools and third party
4 Simulation and robots
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 42 / 44
Turtle
Turtlesim is a lightweight simulator for learning ROS 2.
Images from ros documentation
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 43 / 44
Simulation with gazebo
Gazebo is a robotics design, development, and simulation suite.
Legrand Joël Semi-autonomous drone navigationPython for roboticsIntroduction to ROS2 44 / 44