Skip to content

Wiki

ROS2Introduction

About 45 min read

Existing English translations were reused. 8 current source blocks remain in Simplified Chinese, without triggering paid translation.

1.ROS2 Introduction

The Robot Operating System (ROS) is a set of software libraries and tools that help you build robot applications. From drivers to state-of-the-art algorithms, and with powerful developer tools, ROS has what you need for your next robotics project. And it's all open source.

The Robot Operating System (ROS) is a set of software libraries and tools that help you build robot applications. From drivers to state-of-the-art algorithms, and with powerful developer tools, ROS has what you need for your next robotics project. And it's all open source.

2.ROS2 framework

3.ROS2Reference materials

3.1.Official information

Because there are too few ROS2 reference documents, many tutorials still need to be checked on the ROS2 official website:

https://www.ros.org/

3.2.Recommended video materials and accompanying documentation

The following materials and videos are highly recommended.

https://www.bilibili.com/video/BV1VB4y137ys

Zhao Xuzuo original companion materials:

4.Knowledge reserves needed to learn ROS2

  1. What knowledge is needed?

    1. Basic Linux operations and Shell scripting language

      Basic Linux tutorial

    https://www.runoob.com/linux/linux-tutorial.html

    1. Basic usage of CMake

      Linux C++ compilation environment configuration

    1. Basic Git operations

      Vinci Robotics Team Git introductory tutorial

    1. C/C++ language

      Vinci Robotics Team Electronics Control Group introductory guide

      Vinci Robotics Team C/C++ materials

    1. Python3 language

      Vinci Robotics Team Python3 tutorial

    https://www.runoob.com/python3/python3-tutorial.html

    1. XML language

    https://www.runoob.com/xml/xml-tutorial.html

    1. YAML language

    http://www.noobyard.com/article/p-saghdsms-mn.html

5.InstallROS2

For ROS1 installation, seeROS Robot Operating System Tutorial

** Kubuntu 24.04 LTS is recommended ** ** It is recommended to learn the ROS2 Jazzy version; this tutorial will be based on ROS2 Jazzy **

5.1.Binary package installationROS2(using Kubuntu Jammy 22.04 to install ROS Humble as an example)

Note:

As of December 2024, it is strongly recommended that if you want to use ROS2, you must use ROS Humble and not ROS Jazzy. There are some differences, especially with Gazebo, for which there are almost no tutorials.

For Ubuntu Jammy 22.04 (LTS), please install ROS Humble (LTS). Installation guide website:https://docs.ros.org/en/humble/Installation/Ubuntu-Install-Debians.html

For Ubuntu Noble 24.04 (LTS), please install ROS Jazzy (LTS). Installation guide website:https://docs.ros.org/en/jazzy/Installation/Ubuntu-Install-Debians.html

Distributions supported by each ROS1 and ROS2 version:https://www.ros.org/reps/rep-2000.html

5.1.1.Go to the ROS official website

Be sure to install the LTS version of ROS2

5.1.2.安装ROS2前期准备工作

Install the LTS version of ROS2 Click to install the ROS2 binary using the Debian package management tool

According to the ROS2 official tutorial, enter the following command in the terminal

Still enter the following command to start the Ubuntu Universe Repo

5.1.3.Change source (domestic source)

This is the official source, which downloads the ROS2 binary file from a foreign server. It has network requirements, so we need to change it to a domestic source.

Search Baidu for the Beijing Foreign Studies University mirror source (personally tested, the fastest source in northern China)

Find ROS2, then check sudo and https, and select the correct Linux distribution. Before using this command, you may encounter insufficient permissions issues; you can first read below on how to run all commands with root privileges.

First enter sudo passwd to set the root password (something that must be done on Ubuntu-based distributions) (distributions such as Debian and Fedora already set it during installation, so there is no need to run this)

sudo passwd

su -

Then enter under administrator privileges

Copy the input line by line and run it

上面下载key的时候如果卡住了,那就是github又抽风了,很正常的问题,可以选择科学*上网(科学机场教程)解决,如果还解决不了,可以通过下方教程修改hosts。

https://blog.csdn.net/qq_40584960/article/details/117963644?sharetype=blog&shareId=117963644&sharerefer=APP&sharesource=qq_33274985&sharefrom=link

After successfully downloading the key, continue with the next steps.

Enter exit to leave root mode

exit

5.1.4.Install via aptROS2

Type everything in the red box below into the terminal.

Domestic mirror downloads are still very fast.

5.1.5.ROS2Basic environment variable configuration

Copy this line

Enter the following command

sudo apt-get install vim
sudo vim ~/.bashrc

Add this line at the very bottom (vim will not use it, please search Baidu yourself)

Refresh the environment variables of the current terminal below

echo 'export ROSDISTRO_INDEX_URL=https://mirrors.bfsu.edu.cn/rosdistro/index-v4.yaml' >> ~/.bashrc
source ~/.bashrc

5.1.6.Test ROS2

Please scroll down to the **测试ROS2** section

5.2.Install from source codeROS2(More difficult; for Ubuntu, use the simple method above.)

5.2.1.Fedora or Rocky

https://www.ros.org/

Go to the official website and select the corresponding version; for example, here I select ROS Jazzy.

Then select to compile from source, and find the corresponding distribution, for example, mine is Fedora.

Then just follow the official website tutorial.

  1. Settings area

Make sure your locale supports UTF-8. If you are in a minimal environment (such as a Docker container), the locale may be something minimal like C. We tested with the following settings. However, if you are using another locale that supports UTF-8, it should be fine.

locale  # check for UTF-8

sudo dnf install langpacks-en glibc-langpack-en
export LANG=en_US.UTF-8

locale  # verify settings
  1. Enable the required repository

Fedora requires no additional repository enablement, whereas RHEL does.

  1. Install development tools
sudo dnf install -y \
  cmake \
  gcc-c++ \
  git \
  make \
  patch \
  python3-colcon-common-extensions \
  python3-mypy \
  python3-pip \
  python3-pydocstyle \
  python3-pytest \
  python3-pytest-cov \
  python3-pytest-mock \
  python3-pytest-repeat \
  python3-pytest-rerunfailures \
  python3-pytest-runner \
  python3-rosdep \
  python3-setuptools \
  python3-vcstool \
  wget

# install some pip packages needed for testing and

# not available as RPMs
python3 -m pip install -U --user \
  flake8-blind-except==0.1.1 \
  flake8-class-newline \
  flake8-deprecated
  1. Build ROS2

    1. Get ROS2 source code

    Find a folder to store ROS2. It is recommended to find one in the /home partition, then open a terminal in this folder.

    mkdir -p ./ros2_jazzy/src
    cd ./ros2_jazzy
    vcs import --input https://raw.githubusercontent.com/ros2/ros2/jazzy/ros2.repos src

    1. Update system

    The ROS 2 package is built on a frequently updated Red Hat family system. It is always recommended to ensure your system is up to date before installing new packages.

    sudo dnf upgrade
    1. Initialize and replace rosdep source

    https://mirrors.bfsu.edu.cn/help/rosdistro/

    
    # 手动模拟 rosdep init
    sudo mkdir -p /etc/ros/rosdep/sources.list.d/
    sudo curl -o /etc/ros/rosdep/sources.list.d/20-default.list -L https://mirrors.bfsu.edu.cn/github-raw/ros/rosdistro/master/rosdep/sources.list.d/20-default.list
    
    # 加入环境
    echo 'export ROSDISTRO_INDEX_URL=https://mirrors.bfsu.edu.cn/rosdistro/index-v4.yaml' >> ~/.bashrc
    
    # 加载.bashrc
    source ~/.bashrc
    
    # 更新rosdep
    rosdep update

    1. Install dependencies via rosdep
    export ROS_PYTHON_VERSION=3
    
    rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers"

    If the above error occurs, use pip3 to install the package.

    sudo pip3 install flake8-docstrings

    Then, use the following command to ignore itflake8-docstrings.

    rosdep install --from-paths src --ignore-src -y \
    --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers python3-flake8-docstrings"

    Displaying success is sufficient

    1. Configure environment (not needed for non-Red Hat family distributions)

    If you are on a Red Hat family system, the following configuration is required

    You can use the following commands to temporarily define these macros.

    export RPM_PACKAGE_NAME=qt_gui_cpp   # 根据包名调整(如 ros_<package>)
    export RPM_PACKAGE_VERSION=1.0.0
    export RPM_PACKAGE_RELEASE=1
    export RPM_ARCH=$(uname -m)
    Variable name Example value Purpose
    RPM_PACKAGE_NAME qt_gui_cpp Define RPM package name
    RPM_PACKAGE_VERSION 1.0.0 Define RPM package version
    RPM_PACKAGE_RELEASE 1 Define RPM package release number
    RPM_ARCH x86_64 or arm64 Define target system architecture

    1. Use colcon to build the source code
    colcon build --symlink-install

    Wait for the build to complete; below is what it looks like when the build is complete and error-free

  2. Configure basic environment

Find the folder below and copy the path, for example mine is~/UserFloder/Applications/ros2_jazzy

Use vim to modify Bash's environment

vim ~/.bashrc

Use the Insert key for input.

After adding these two lines, press ESC, then:wqsave and exit.


# 刷新当前环境变量
source ~/.bashrc
  1. Test ROS2

Please scroll down to the **测试ROS2** section

5.2.2.Debian

https://www.ros.org/

Go to the official website and select the corresponding version; for example, here I select ROS Jazzy.

Then choose to compile from source, and find the corresponding distribution, for example, mine is Debian.

Next, start from the point shown in the figure below and follow the official tutorial step by step, but there are a few points to pay attention to, which I will list here:

  1. Settings area
locale  # check for UTF-8

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

locale  # verify settings
  1. Change source

The red part is what Ubuntu needs to do; we are Debian, so we do not need to do it.

For the blue part, let's not use the official source; the official one is an overseas source, so we'll replace it with a domestic source.

First, switch to a mirror source:

https://mirrors.bfsu.edu.cn/help/ros2/

For example, I am using Debian 12:

sudo apt install curl gnupg2
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key  -o /usr/share/keyrings/ros-archive-keyring.gpg

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://mirrors.bfsu.edu.cn/ros2/ubuntu bookworm main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
sudo apt update

  1. After successfully changing the source as shown above, install the ROS tools:

sudo apt update && sudo apt install -y \
  python3-flake8-blind-except \
  python3-flake8-class-newline \
  python3-flake8-deprecated \
  python3-mypy \
  python3-pip \
  python3-pytest \
  python3-pytest-cov \
  python3-pytest-mock \
  python3-pytest-repeat \
  python3-pytest-rerunfailures \
  python3-pytest-runner \
  python3-pytest-timeout \
  ros-dev-tools
  1. Download ROS2 source code

Find a folder to store ROS2; it is recommended to find one in the /home partition, then open a terminal in this folder.

mkdir -p ./ros2_jazzy/src
cd ./ros2_jazzy
vcs import --input https://raw.githubusercontent.com/ros2/ros2/jazzy/ros2.repos src

  1. Before installing dependencies, we need to first initialize rosdep and change its source:

The red part in the figure, do not follow this; this is the official source and will be very, very slow. It is recommended to use the BFSU repository.

https://mirrors.bfsu.edu.cn/help/rosdistro/


# 手动模拟 rosdep init
sudo mkdir -p /etc/ros/rosdep/sources.list.d/
sudo curl -o /etc/ros/rosdep/sources.list.d/20-default.list -L https://mirrors.bfsu.edu.cn/github-raw/ros/rosdistro/master/rosdep/sources.list.d/20-default.list

# 加入环境
echo 'export ROSDISTRO_INDEX_URL=https://mirrors.bfsu.edu.cn/rosdistro/index-v4.yaml' >> ~/.bashrc

# 加载.bashrc
source ~/.bashrc

# 更新rosdep
rosdep update

If the red box section above appears, the source change was successful. If you do not change the source, the process of installing dependencies below may be extremely slow (I have not actually tried it, just guessing), unless you use a VPN.

sudo apt upgrade

export ROS_PYTHON_VERSION=3

rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers"

  1. CompileROS2

In the ros_jazzy directory, open a terminal and run the following command.

colcon build --symlink-install

colcon build also has the following commands, such as --packages-skip and --packages-select, used to skip and compile a single package.

Wait for the build to complete. Below is what it looks like when the build is complete and error-free.

  1. Configure the base environment

Find the folder below and copy the path, for example mine is~/UserFloder/Applications/ros2_jazzy

Use vim to modify Bash's environment

vim ~/.bashrc

Use the Insert key for input.


# 配置ROS2环境
export ROSDISTRO_INDEX_URL=https://mirrors.bfsu.edu.cn/rosdistro/index-v4.yaml
source ~/UserFloder/Applcations/ros2_jazzy/install/setup.bash

After adding these two lines, press ESC, then:wqsave and exit.


# 刷新当前环境变量
source ~/.bashrc
  1. TestROS2

Please scroll down to the **测试ROS2** section

5.3.Docker installation ROS2

Docker Tutorial

6.ROS2Advanced Installation (Optional)

The above only installs the most basic ROS2 package and configures the most basic ROS2 environment.

After installing the above content, you can choose to proceed with the following advanced configuration ROS2 environment.

也可以不安装进阶得东西,学到啥再安装啥就可以,当然也有想一劳永逸的同学,所以下面也会列出来最常用的ros2包和环境配置供大家一次性安装。

https://index.ros.org/?search_packages=true

6.1.Install advanced packages

Below are some packagesROS2 that come with navigation algorithms and simulation environments, etc. These packages are listed below.

If you are using Ubuntu:

All of the following packages can be installed with apt, no manual compilation required!!!

If you are not using Ubuntu:

The gray ones are packages that were installed in the basic tutorial above,

Yellow indicates packages that need to be compiled manually,

Blue indicates packages that not only require manual compilation but also require manually modifying some source code.

ROS的包(非Ubuntu的发行版基本都需要手动编译)
软件包/库名称
ros-humble-desktop
ros-humble-xacro
ros-humble-joint-state-publisher
ros-humble-joint-state-publisher-gui
ros-humble-gazebo-ros
ros-humble-gazebo-ros-pkgs
ros-humble-ros-gz
ros-humble-diagnostic-updater
ros-humble-teleop-twist-keyboard
ros-humble-navigation2
ros-humble-nav2-bringup
ros-humble-slam-toolbox
ros-humble-cartographer
ros-humble-cartographer-ros
ros-humble-asio-cmake-module
ros-humble-serial-driver
ros-humble-pcl-ros
ros-humble-pointcloud-to-laserscan
ros-humble-vision-opencv
非ROS包(基本都可以apt,dnf安装)
软件包/库名称
libpcl-dev
libeigen3-dev
libpcap-dev
libboost-dev
libogre-1.12-dev

However, the above packages can depend on other packages. The repository below contains repository links for all packages: (read on to see how to use them)

https://github.com/ros/rosdistro/tree/master

6.1.1.APT installation method (strongly recommended for Ubuntu)
sudo apt update && sudo apt install \
    ros-humble-desktop \
    ros-dev-tools \
    ros-humble-xacro \
    ros-humble-joint-state-publisher \
    ros-humble-joint-state-publisher-gui \
    ros-humble-ros-gz \
    ros-humble-gazebo-ros \
    ros-humble-gazebo-ros-pkgs \
    ros-humble-imu-tools \
    ros-humble-diagnostic-updater \
    ros-humble-teleop-twist-keyboard \
    ros-humble-navigation2 \
    ros-humble-nav2-bringup \
    ros-humble-slam-toolbox \
    ros-humble-cartographer \
    ros-humble-cartographer-ros \
    ros-humble-asio-cmake-module \
    ros-humble-serial-driver \
    ros-humble-pcl-ros \
    ros-humble-vision-opencv \
    ros-humble-pointcloud-to-laserscan \
    libpcl-dev \
    libeigen3-dev \
    libpcap-dev \
    python3-colcon-common-extensions
6.1.2.Source code method (non-Ubuntu)

Take Debian 12 as an example.

6.1.2.1.Yellow packet

Take the installation of the three packages ros-jazzy-xacro, ros-jazzy-joint-state-publisher, and ros-jazzy-joint-state-publisher-gui as an example; the operation is the same for other packages.

https://index.ros.org/?search_packages=true

Go to the website above, select the corresponding version in the figure below, for example, I am using JAZZY, then search for xacro in the search box, joint_state_publisher, joint_state_publisher_gui

For example, xacro

Find the xacro for the corresponding version, then the repository address is the checkout URI and the branch is the vcs version. You can use git clone to clone them one by one, but this is an extremely troublesome task, so we choose the vcs command provided to us by ROS2 to download the source code of the packages.

First, navigate to your ros2_jazzy file and open a terminal

and create a .repos file, with any filename you like, for example mine is called my_ros2_jazzy_packages.repos

Then edit it with VSCode, because .repos uses the YAML language, and VSCode can recognize syntax correctness:

Write the information just queried:

repositories:
  xacro:
    type: git
    url: https://github.com/ros/xacro.git
    version: ros2

Find other packages in the same way:

The above figure shows that joint_state_publisher and joint_state_publisher_gui are actually one repository, so we only need to clone one repository.

Then the vcs version, that is, the Git branch, is ros2.

repositories:
  xacro:
    type: git
    url: https://github.com/ros/xacro.git
    version: ros2
  joint_state_publisher:
    type: git
    url: https://github.com/ros/joint_state_publisher.git
    version: ros2

Repeat the same procedure for the other packages.

But let me first use these two packages as an example.

Terminal in the ros2_jazzy directory:

vcs import src < my_ros2_jazzy_packages.repos

As can be seen in the figure below, the cloning was successful

Next, add the supplementary dependencies:

rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers"

The text shown in the figure below indicates success.

Then recompile and install:

colcon build --symlink-install

# 或仅编译特定包
colcon build --packages-select xacro joint_state_publisher joint_state_publisher_gui

Repeat the same procedure for the other packages.

Test:


# 重新加载环境
source ~/.bashrc

# 敲命令
xacro

As shown in the figure, a xacro error prompt means success—this indicates that the installation was successful!

Repeat the same procedure for the other packages.

I also organized the files for the Humble and Jazzy repos, finding them one by one from the official website and distribution.yaml. You can clone them and directly use what I've organized. (But it's possible that the official ROS2 for some repository branches will be modified)

https://github.com/tungchiahui/ros-docker/tree/main/repos

6.1.2.2.blue bag

The general approach for the blue package is the same as for the yellow one, except that after downloading the source code for the blue package, you also need to modify files in the source code before compiling.

As mentioned in vision_opencv's cv_bridge: see the CV_Bridge in the OpenCV section below.

6.1.3.Frequently Asked Questions
  1. libogre-dev找不到

For Gazebo's rendering engine, in Debian 12, it cannot be found using libogre-dev; it can only be found using libogre-1.12-dev.

  1. nav2_mppi_controller里有个被当成空指针了,警告成错误了。

Solve by adding it in the CMakeLists of nav2_mppi_controlleradd_compile_options(-Wno-error=null-dereference)That is sufficient.

  1. nav2_system_tests has a warning about overlapping memory.

The solution is to add the following lines to the CMakeLists in nav2_system_tests.

if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
    add_compile_options(-Wno-restrict)
endif()
  1. Some libraries are not available in the latest version of Fedora, but are available in older versions.

https://src.fedoraproject.org/projects/rpms/%2A

sudo dnf install python3-pykdl --releasever=41
sudo dnf install orocos-kdl-devel --releasever=41
  1. Some Python libraries cannot be installed on the latest version of the system because the Python version is too high.

You can install this library using pip3

pip3 install flake8-docstrings

However, for subsequent compilation, this library needs to be added to the dependencies ignored by rosdep. For example, in the case of python3-flake8-docstrings, it is the following line:

rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers"

# 改为
rosdep install --from-paths src --ignore-src -y --skip-keys "fastcdr rti-connext-dds-6.0.1 urdfdom_headers python3-flake8-docstrings"

6.2.Environment Advancements

6.2.1.APT method

# rosdep换源
export ROSDISTRO_INDEX_URL=https://mirrors.bfsu.edu.cn/rosdistro/index-v4.yaml

# 加载ROS2环境
source /opt/ros/jazzy/setup.bash

# 配置ROS2的分布式
export ROS_DOMAIN_ID=6

# 配置新版Gazebo的资源(这里先注释,等你学到gazebo再打开)
#export GZ_SIM_RESOURCE_PATH=$GZ_SIM_RESOURCE_PATH:/home/tungchiahui/UserFloder/MySource/ROS_WS/gazebo_models:/home/tungchiahui/UserFloder/MySource/ROS_WS/ign_models

Note that the Gazebo resource package may have different macro names in different ROS2 versions, for example, in ROS2Humble it is IGN_GAZEBO_RESOURCE_PATH, while in ROS2Jazzy it is GZ_SIM_RESOURCE_PATH. (Future ROS2 versions are estimated to all use GZ_SIM_RESOURCE_PATH) (See the Gazebo tutorial below for details)

6.2.2.Source code method

与apt方式相同,但是需要把加载ROS2环境的那个setup.bash路径设置为你的ros2安装路径。


# 加载ROS2环境
source ~/UserFloder/Applcations/ros2_jazzy/install/setup.bash

7.TestROS2

7.1.Example 1

Open two terminals and enter the commands shown in the two red boxes below in each.

If the following program pops up, the test is successful.

Pressing Ctrl+C on the terminal can terminate the program.

7.2.Example 2

As a long-standing ROS test project, Turtlesim must be brought out for a spin!

Likewise, open two terminals and enter the following commands in each.

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

Click the red part below with the mouse, then use the arrow keys on the numeric keypad to control the turtle's movement. If there is no numeric keypad, you can try using keys such as GBVCDERT to control the little turtle.

Similarly, pressing Ctrl+C at the terminal can terminate the program.

8.Frequently Asked Questions

8.1.Rviz2 flickering

If a computer with 2K+ resolution has display issues after installing rviz2, you can add the following content to .bashrc to prevent Qt from scaling.

export QT_AUTO_SCREEN_SCALE_FACTOR=0
export QT_SCREEN_SCALE_FACTORS=[1.0]