ROS2 – Setup
Installation
Unlike ROS1, ROS2 can be installed on all major platforms such as Linux, Windows and MacOS. Here I will focus on installing ROS2 on an Ubuntu 22.04 system. For other platforms, the installation setups are detailed on the official ROS2 documentation page.
Setup the locale settings
$ sudo apt update && sudo apt install locales
$ sudo locale-gen en_US en_US.UTF-8
$ sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
$ export LANG=en_US.UTF-8
Setup the ROS2 sources
$ sudo apt install software-properties-common
$ sudo add-apt-repository universe
$ sudo apt update && sudo apt install curl -y
$ sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
Install the ROS2 packages
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install ros-humble-desktop
Setup the ROS2 environment
The following command should be run on every new shell to have access to the ROS2 commands.
$ source /opt/ros/humble/setup.bash
However, this command can be added to the shell startup script for it to automatically run every time a new terminal window is created.
$ echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
Test the ROS2 installation
To test the ROS2 installation, we will run a simple publisher - subscriber example.
In a terminal, execute the following:
$ ros2 run demo_nodes_py listener
In another terminal, execute the following:
$ source /opt/ros/humble/setup.bash
$ ros2 run demo_nodes_cpp talker
We should be able to see the talker node saying that it’s Publishing messages and the listener node saying it heard those messages. If this is working, we can be sure that both the C++ and Python ROS2 APIs are working properly.
Packages
In ROS2, a package can be regarded as an organizational unit, allowing us to install our code or share it with others, allowing them to build and use it easily. To create packages in ROS 2, we can use ament as its build system and colcon as its build tool. We can also create a package using either CMake or Python, which are officially supported, though other build types do exist, but we will focus on amend and colcon.
Workspaces
In ROS2, a workspace is a directory containing packages. A build system uses a workspace to go through packages to build them for the project. A workspace can contain multiple packages, separated into individual folders. Packages can also belong to different build types in the same workspace (CMake, Python, etc.). However, nesting of packages is not allowed.
Build system
A build tool operates on a set of packages. It determines the dependency graph and invokes the specific build system for each package in topological order. The build tool itself should know how to set up the environment for the build system and invoke it. The existing ROS build tools are catkin_make, catkin_make_isolated, catkin_tools, colcon, and ament_tools. It controls the compilation and testing of multiple ROS2 packages together. ROS2 uses ament_tools and now colcon as the build tool.
The build system on the other hand operates on a single package. Examples are Make, CMake, ament_cmake, ament_python, Python setuptools, or Autotools. A CMake package is e.g. built by invoking these steps: cmake, make, make install. ROS2 uses ament_cmake and ament_python as the build systems.
Generally, in ROS, each package contains a manifest file (package.xml). This manifest file contains essential metadata about the package, including its dependencies on other packages. This manifest is required for the meta-build tool to function.
In the projects that follow, we will use python as our programming language, ament as our build system and colcon as our build tool.