【ROS2】Ubuntu 20.04 下源码编译 Humble

官方手册写的很详细。以下步骤针对 Ubuntu 20.04 作必要简化,如果是 22.04 需要重新参照手册来进行安装。

https://docs.ros.org/en/humble/Installation/Alternatives/Ubuntu-Development-Setup.html

安装必要依赖

最后一行是编译源码过程中需要的一些依赖库,缺少会导致各种报错

sudo apt update && sudo apt install -y \
  locales software-properties-common curl \
  libacl1-dev libasio-dev libtinyxml2-dev libx11-dev libxaw7-dev

1. 设置和确保系统使用支持UTF-8编码的区域设置

locale  # check for UTF-8

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

2. 添加 ROS2 apt 仓库

开启 ubuntu universe 仓库

sudo add-apt-repository universe

等同于 Software & Update 中选中 universe 项的图形化操作

官网建议获取 ROS2 APT 配置包最新版本

export ROS_APT_SOURCE_VERSION=$(curl -s https://api.github.com/repos/ros-infrastructure/ros-apt-source/releases/latest | grep -F "tag_name" | awk -F\" '{print $4}')

这个网址有可能访问失败。直接到网站,查看最新版。然后手动设置

export ROS_APT_SOURCE_VERSION=1.1.0

然后下载发行 deb,需要魔法。

curl -L -o /tmp/ros2-apt-source.deb "https://github.com/ros-infrastructure/ros-apt-source/releases/download/${ROS_APT_SOURCE_VERSION}/ros2-apt-source_${ROS_APT_SOURCE_VERSION}.focal_all.deb"

安装该文件

sudo dpkg -i /tmp/ros2-apt-source.deb

打印,说明 OK。该包安装后会自动配置 apt 源。例如cat /etc/apt/sources.list.d/ros2.sources

Selecting previously unselected package ros2-apt-source.
(Reading database ... 326425 files and directories currently installed.)
Preparing to unpack /tmp/ros2-apt-source.deb ...
Unpacking ros2-apt-source (1.1.0~focal) ...
Setting up ros2-apt-source (1.1.0~focal) ...

3. 安装功能包依赖管理工具即其他 ROS 工具

依赖管理工具,可以帮助我们自动拉取所有功能包的 github 仓库,依赖的子功能包仓库也会被处理,所以非常重要。

apt

sudo apt update && sudo apt install -y \
  python3-flake8-docstrings \
  python3-pip \
  python3-pytest-cov \
  ros-dev-tools

python

python3 -m pip install -U \
   flake8-blind-except \
   flake8-builtins \
   flake8-class-newline \
   flake8-comprehensions \
   flake8-deprecated \
   flake8-import-order \
   flake8-quotes \
   "pytest>=5.3" \
   pytest-repeat \
   pytest-rerunfailures \
   empy==3.3.4

# rosidl_generator_c 编译依赖
python3 -m pip install lark

4. 获取 ROS 源码

新建一个文件夹用来存储源码。注意选择好位置。

mkdir -p ~/Dev/ros2_humble/src && cd ~/Dev/ros2_humble

拉取源码。vcs是一个用于管理 ROS 工作空间中的版本控制工具,它能够通过读取.repos文件来自动化地从多个 Git 仓库导入源代码。import命令会将.repos文件中列出的所有包导入到工作空间中,并自动将这些包克隆到指定目录。

vcs import --input https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos src

注意:如果我们需要额外安装安装一些自己算法依赖的功能包,例如 mavros/vrpn,则需要在此步骤进行指定!如果此步骤遗漏了功能包,则需要重新编辑 cat。然后执行 4 后面的步骤。最好的办法是保存该网址内容,形成自己的必须源码清单。原始 repos 文件如下。

repositories:
  ament/ament_cmake:
    type: git
    url: https://github.com/ament/ament_cmake.git
    version: humble
  ament/ament_index:
    type: git
    url: https://github.com/ament/ament_index.git
    version: humble
  ament/ament_lint:
    type: git
    url: https://github.com/ament/ament_lint.git
    version: humble
  ament/ament_package:
    type: git
    url: https://github.com/ament/ament_package.git
    version: humble
  ament/google_benchmark_vendor:
    type: git
    url: https://github.com/ament/google_benchmark_vendor.git
    version: humble
  ament/googletest:
    type: git
    url: https://github.com/ament/googletest.git
    version: humble
  ament/uncrustify_vendor:
    type: git
    url: https://github.com/ament/uncrustify_vendor.git
    version: humble
  eProsima/Fast-CDR:
    type: git
    url: https://github.com/eProsima/Fast-CDR.git
    version: v1.0.24
  eProsima/Fast-DDS:
    type: git
    url: https://github.com/eProsima/Fast-DDS.git
    version: 2.6.x
  eProsima/foonathan_memory_vendor:
    type: git
    url: https://github.com/eProsima/foonathan_memory_vendor.git
    version: master
  eclipse-cyclonedds/cyclonedds:
    type: git
    url: https://github.com/eclipse-cyclonedds/cyclonedds.git
    version: releases/0.10.x
  eclipse-iceoryx/iceoryx:
    type: git
    url: https://github.com/eclipse-iceoryx/iceoryx.git
    version: release_2.0
  gazebo-release/gz_cmake2_vendor:
    type: git
    url: https://github.com/gazebo-release/gz_cmake2_vendor.git
    version: humble
  gazebo-release/gz_math6_vendor:
    type: git
    url: https://github.com/gazebo-release/gz_math6_vendor.git
    version: humble
  osrf/osrf_pycommon:
    type: git
    url: https://github.com/osrf/osrf_pycommon.git
    version: master
  osrf/osrf_testing_tools_cpp:
    type: git
    url: https://github.com/osrf/osrf_testing_tools_cpp.git
    version: humble
  ros-perception/image_common:
    type: git
    url: https://github.com/ros-perception/image_common.git
    version: humble
  ros-perception/laser_geometry:
    type: git
    url: https://github.com/ros-perception/laser_geometry.git
    version: humble
  ros-planning/navigation_msgs:
    type: git
    url: https://github.com/ros-planning/navigation_msgs.git
    version: humble
  ros-tooling/keyboard_handler:
    type: git
    url: https://github.com/ros-tooling/keyboard_handler.git
    version: humble
  ros-tooling/libstatistics_collector:
    type: git
    url: https://github.com/ros-tooling/libstatistics_collector.git
    version: humble
  ros-visualization/interactive_markers:
    type: git
    url: https://github.com/ros-visualization/interactive_markers.git
    version: humble
  ros-visualization/python_qt_binding:
    type: git
    url: https://github.com/ros-visualization/python_qt_binding.git
    version: humble
  ros-visualization/qt_gui_core:
    type: git
    url: https://github.com/ros-visualization/qt_gui_core.git
    version: humble
  ros-visualization/rqt:
    type: git
    url: https://github.com/ros-visualization/rqt.git
    version: humble
  ros-visualization/rqt_action:
    type: git
    url: https://github.com/ros-visualization/rqt_action.git
    version: humble
  ros-visualization/rqt_bag:
    type: git
    url: https://github.com/ros-visualization/rqt_bag.git
    version: humble
  ros-visualization/rqt_console:
    type: git
    url: https://github.com/ros-visualization/rqt_console.git
    version: humble
  ros-visualization/rqt_graph:
    type: git
    url: https://github.com/ros-visualization/rqt_graph.git
    version: humble
  ros-visualization/rqt_msg:
    type: git
    url: https://github.com/ros-visualization/rqt_msg.git
    version: humble
  ros-visualization/rqt_plot:
    type: git
    url: https://github.com/ros-visualization/rqt_plot.git
    version: humble
  ros-visualization/rqt_publisher:
    type: git
    url: https://github.com/ros-visualization/rqt_publisher.git
    version: humble
  ros-visualization/rqt_py_console:
    type: git
    url: https://github.com/ros-visualization/rqt_py_console.git
    version: humble
  ros-visualization/rqt_reconfigure:
    type: git
    url: https://github.com/ros-visualization/rqt_reconfigure.git
    version: humble
  ros-visualization/rqt_service_caller:
    type: git
    url: https://github.com/ros-visualization/rqt_service_caller.git
    version: humble
  ros-visualization/rqt_shell:
    type: git
    url: https://github.com/ros-visualization/rqt_shell.git
    version: humble
  ros-visualization/rqt_srv:
    type: git
    url: https://github.com/ros-visualization/rqt_srv.git
    version: humble
  ros-visualization/rqt_topic:
    type: git
    url: https://github.com/ros-visualization/rqt_topic.git
    version: humble
  ros-visualization/tango_icons_vendor:
    type: git
    url: https://github.com/ros-visualization/tango_icons_vendor.git
    version: humble
  ros/class_loader:
    type: git
    url: https://github.com/ros/class_loader.git
    version: humble
  ros/kdl_parser:
    type: git
    url: https://github.com/ros/kdl_parser.git
    version: humble
  ros/pluginlib:
    type: git
    url: https://github.com/ros/pluginlib.git
    version: humble
  ros/resource_retriever:
    type: git
    url: https://github.com/ros/resource_retriever.git
    version: humble
  ros/robot_state_publisher:
    type: git
    url: https://github.com/ros/robot_state_publisher.git
    version: humble
  ros/ros_environment:
    type: git
    url: https://github.com/ros/ros_environment.git
    version: humble
  ros/ros_tutorials:
    type: git
    url: https://github.com/ros/ros_tutorials.git
    version: humble
  ros/urdfdom:
    type: git
    url: https://github.com/ros/urdfdom.git
    version: humble
  ros/urdfdom_headers:
    type: git
    url: https://github.com/ros/urdfdom_headers.git
    version: humble
  ros2/ament_cmake_ros:
    type: git
    url: https://github.com/ros2/ament_cmake_ros.git
    version: humble
  ros2/common_interfaces:
    type: git
    url: https://github.com/ros2/common_interfaces.git
    version: humble
  ros2/console_bridge_vendor:
    type: git
    url: https://github.com/ros2/console_bridge_vendor.git
    version: humble
  ros2/demos:
    type: git
    url: https://github.com/ros2/demos.git
    version: humble
  ros2/eigen3_cmake_module:
    type: git
    url: https://github.com/ros2/eigen3_cmake_module.git
    version: humble
  ros2/example_interfaces:
    type: git
    url: https://github.com/ros2/example_interfaces.git
    version: humble
  ros2/examples:
    type: git
    url: https://github.com/ros2/examples.git
    version: humble
  ros2/geometry2:
    type: git
    url: https://github.com/ros2/geometry2.git
    version: humble
  ros2/launch:
    type: git
    url: https://github.com/ros2/launch.git
    version: humble
  ros2/launch_ros:
    type: git
    url: https://github.com/ros2/launch_ros.git
    version: humble
  ros2/libyaml_vendor:
    type: git
    url: https://github.com/ros2/libyaml_vendor.git
    version: humble
  ros2/message_filters:
    type: git
    url: https://github.com/ros2/message_filters.git
    version: humble
  ros2/mimick_vendor:
    type: git
    url: https://github.com/ros2/mimick_vendor.git
    version: humble
  ros2/orocos_kdl_vendor:
    type: git
    url: https://github.com/ros2/orocos_kdl_vendor.git
    version: humble
  ros2/performance_test_fixture:
    type: git
    url: https://github.com/ros2/performance_test_fixture.git
    version: humble
  ros2/pybind11_vendor:
    type: git
    url: https://github.com/ros2/pybind11_vendor.git
    version: humble
  ros2/python_cmake_module:
    type: git
    url: https://github.com/ros2/python_cmake_module.git
    version: humble
  ros2/rcl:
    type: git
    url: https://github.com/ros2/rcl.git
    version: humble
  ros2/rcl_interfaces:
    type: git
    url: https://github.com/ros2/rcl_interfaces.git
    version: humble
  ros2/rcl_logging:
    type: git
    url: https://github.com/ros2/rcl_logging.git
    version: humble
  ros2/rclcpp:
    type: git
    url: https://github.com/ros2/rclcpp.git
    version: humble
  ros2/rclpy:
    type: git
    url: https://github.com/ros2/rclpy.git
    version: humble
  ros2/rcpputils:
    type: git
    url: https://github.com/ros2/rcpputils.git
    version: humble
  ros2/rcutils:
    type: git
    url: https://github.com/ros2/rcutils.git
    version: humble
  ros2/realtime_support:
    type: git
    url: https://github.com/ros2/realtime_support.git
    version: humble
  ros2/rmw:
    type: git
    url: https://github.com/ros2/rmw.git
    version: humble
  ros2/rmw_connextdds:
    type: git
    url: https://github.com/ros2/rmw_connextdds.git
    version: humble
  ros2/rmw_cyclonedds:
    type: git
    url: https://github.com/ros2/rmw_cyclonedds.git
    version: humble
  ros2/rmw_dds_common:
    type: git
    url: https://github.com/ros2/rmw_dds_common.git
    version: humble
  ros2/rmw_fastrtps:
    type: git
    url: https://github.com/ros2/rmw_fastrtps.git
    version: humble
  ros2/rmw_implementation:
    type: git
    url: https://github.com/ros2/rmw_implementation.git
    version: humble
  ros2/ros2_tracing:
    type: git
    url: https://github.com/ros2/ros2_tracing.git
    version: humble
  ros2/ros2cli:
    type: git
    url: https://github.com/ros2/ros2cli.git
    version: humble
  ros2/ros2cli_common_extensions:
    type: git
    url: https://github.com/ros2/ros2cli_common_extensions.git
    version: humble
  ros2/ros_testing:
    type: git
    url: https://github.com/ros2/ros_testing.git
    version: humble
  ros2/rosbag2:
    type: git
    url: https://github.com/ros2/rosbag2.git
    version: humble
  ros2/rosidl:
    type: git
    url: https://github.com/ros2/rosidl.git
    version: humble
  ros2/rosidl_dds:
    type: git
    url: https://github.com/ros2/rosidl_dds.git
    version: humble
  ros2/rosidl_defaults:
    type: git
    url: https://github.com/ros2/rosidl_defaults.git
    version: humble
  ros2/rosidl_python:
    type: git
    url: https://github.com/ros2/rosidl_python.git
    version: humble
  ros2/rosidl_runtime_py:
    type: git
    url: https://github.com/ros2/rosidl_runtime_py.git
    version: humble
  ros2/rosidl_typesupport:
    type: git
    url: https://github.com/ros2/rosidl_typesupport.git
    version: humble
  ros2/rosidl_typesupport_fastrtps:
    type: git
    url: https://github.com/ros2/rosidl_typesupport_fastrtps.git
    version: humble
  ros2/rpyutils:
    type: git
    url: https://github.com/ros2/rpyutils.git
    version: humble
  ros2/rviz:
    type: git
    url: https://github.com/ros2/rviz.git
    version: humble
  ros2/spdlog_vendor:
    type: git
    url: https://github.com/ros2/spdlog_vendor.git
    version: humble
  ros2/sros2:
    type: git
    url: https://github.com/ros2/sros2.git
    version: humble
  ros2/system_tests:
    type: git
    url: https://github.com/ros2/system_tests.git
    version: humble
  ros2/test_interface_files:
    type: git
    url: https://github.com/ros2/test_interface_files.git
    version: humble
  ros2/tinyxml2_vendor:
    type: git
    url: https://github.com/ros2/tinyxml2_vendor.git
    version: humble
  ros2/tinyxml_vendor:
    type: git
    url: https://github.com/ros2/tinyxml_vendor.git
    version: humble
  ros2/tlsf:
    type: git
    url: https://github.com/ros2/tlsf.git
    version: humble
  ros2/unique_identifier_msgs:
    type: git
    url: https://github.com/ros2/unique_identifier_msgs.git
    version: humble
  ros2/urdf:
    type: git
    url: https://github.com/ros2/urdf.git
    version: humble
  ros2/yaml_cpp_vendor:
    type: git
    url: https://github.com/ros2/yaml_cpp_vendor.git
    version: humble

此步骤执行完毕,则在 src 目录下出现大量功能包,总共约 200 MB。如果执行失败,可能需要魔法,因为此步骤实际上是多个 git clone 命令。

5. 并行编译整个工作空间

实测以下官方建议并没有什么影响:

编译前不要将其他 ROS 2 版本的环境变量(例如/opt/ros/${ROS_DISTRO}/setup.bash)添加到你的.bashrc中。使用printenv | grep -i ROS检查当前的环境变量,确保没有 ROS 相关的环境被设置。如果输出为空,表示没有 source;如果 source /opt/ros/noetic/setup.bash,那么输出会有很多环境变量,可能导致编译失败。

cd ~/Dev/ros2_humble/ && colcon build --symlink-install --parallel-workers $(nproc)
  • colcon build:这是 ROS 2 推荐的构建工具,用来编译整个工作空间。
  • --symlink-install:此选项表示使用符号链接安装工作空间中的包。这有助于避免在每次修改源代码后重新安装所有包,提高开发效率。

编译成功则打印

Summary: 346 packages finished [6min 30s]

6. 环境变量

与 ROS1 类似,使用 ros2 与 colcon 之类的命令需要刷新环境变量。setup.bash 文件在

source ~/Dev/ros2_humble/install/local_setup.bash

注意如果在此之前 source 了 ros1 的setup.bash 文件会有报错:

ROS_DISTRO was set to ‘noetic’ before. Please make sure that the environment does not mix paths from different distributions.

所以要么 bashrc 中两个版本 ROS 都不要 source。要么启动之前,选择一个 source。

echo "ros: noetic(1)  humble(2) ?"
read choose
case $choose in
  1) source /opt/ros/noetic/setup.bash;;
  2) source ~/Dev/ros2_humble/install/setup.bash;;
esac

7. BUG1-[rviz2]: Stereo is NOT SUPPORTED

为什么 rviz1 启动会提示显卡

rviz
[INFO] [1751899220.081798287]: rviz version 1.14.26
[INFO] [1751899220.081816410]: compiled against Qt version 5.12.8
[INFO] [1751899220.081820311]: compiled against OGRE version 1.9.0 (Ghadamon)
[INFO] [1751899220.083454588]: Forcing OpenGl version 0.
[INFO] [1751899220.491729997]: Stereo is NOT SUPPORTED
[INFO] [1751899220.491757556]: OpenGL device: NVIDIA GeForce RTX 4090/PCIe/SSE2
[INFO] [1751899220.491764919]: OpenGl version: 4.6 (GLSL 4.6).

但是 rviz2 只是打印

rviz2
[INFO] [1751899281.209406548] [rviz2]: Stereo is NOT SUPPORTED
[INFO] [1751899281.209528757] [rviz2]: OpenGl version: 4.6 (GLSL 4.6)
[INFO] [1751899281.262525795] [rviz2]: Stereo is NOT SUPPORTED

从 nvidia-smi 来看,rviz2 应该是使用了 gpu2.

因为是在 Ubuntu 20.04,本身不涉及 wayland 与 x11 选择问题。所以教程所说 QT_QPA_PLATFORM=xcb rviz2 应该没有影响。

可能是 opengl 和 orge 两个依赖版本太旧?可以 rviz2 humble 依旧使用的是 5.12,唯一差别是 orge 依赖的是 1.12.1,而 rviz1 使用的是 1.9。

试着安装高版本

sudo apt -y install ogre-1.12-tools

安装了 1.12,但是依旧打印简略信息。

dpkg -l | grep ogre

ii  libogre-1.12:amd64                           1.12.4+dfsg1-4                       amd64        3D Object-Oriented Graphics Rendering Engine (libraries)
ii  libogre-1.9-dev                              1.9.0+dfsg1-12build1                 amd64        3D Object-Oriented Graphics Rendering Engine (development files)
ii  libogre-1.9.0v5:amd64                        1.9.0+dfsg1-12build1                 amd64        3D Object-Oriented Graphics Rendering Engine (libraries)
ii  ogre-1.12-tools                              1.12.4+dfsg1-4                       amd64        3D Object-Oriented Graphics Rendering Engine (tools)

等待后续增加 display,进一步测试显存占用有没有上升。

上一篇