为什么使用源码编译安装?

因为ROS2 Humble官方支持的Tier1 Ubuntu版本为22.0420.04(Tier3)只能通过编译安装

并且,ROS2 Foxy已经在2023年停止支持,Humble是主流的LTS(长期支持)版本。

或许你可能会问:为什么不用Docker?
那必然是纯物理环境运行有一些Docker无法提供的优势,比如可以直接使用系统已经配好的一些环境(例如深度学习需要的pytorch、torchvision,都需要用NV提供的版本)、不用进入Docker容器(调试更简单方便)等等~

写在前面

由于之前踩坑看了CSDN的旧教程半天安装不上去(后来发现是安装命令有更新),建议直接使用官方教程内的命令进行安装(本教程中有一些注意点,可以结合着看)。

如果使用本教程中提供的安装命令出现错误,请查看官方教程。

虽然Humble版本现在应该不会有什么变动了……

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

准备工作

在开始之前,需要确认:

http_proxy=http://your_proxy_ip:7890
https_proxy=https://your_proxy_ip:7890
all_proxy=socks5://your_proxy_ip:7890

环境信息

设备:NVIDIA Jetson Xavier NX Developer Kit 8GB

系统:Ubuntu 20.04 / JetPack 5.1.3 / L4T 35.5.0

环境:Python 3.8

开始安装

确保支持UTF-8

如果你的Jetson安装系统之后没有更改过语言设置,那么不需要执行此部分操作,跳过即可。

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

添加ROS2 APT仓库

启用Universe软件源:

sudo apt install software-properties-common
sudo add-apt-repository universe

之后就可以添加软件源了:

# 授权GPG密钥
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
# 添加软件源
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

安装必要软件包

sudo apt update && sudo apt install -y \
  python3-flake8-docstrings \
  python3-pip \
  python3-pytest-cov \
  ros-dev-tools
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
# 仅适用于Ubuntu20.04LTS

下载ROS2源码

mkdir -p ~/ros2_humble/src
cd ~/ros2_humble
vcs import --input https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos src

一般情况下,如果你的代理已经配置好但仍然不能正常下载,可以这样强制指定代理(仅限这条命令):

http_proxy=http://your_proxy_ip:7890 https_proxy=https://your_proxy_ip:7890 vcs import --input https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos src

使用rosdep安装依赖

注意在Jetson平台上强烈建议非必要不执行apt upgrade,很可能会导致环境出现问题,这里注释掉。

# sudo apt upgrade # 注意在Jetson平台上强烈建议不要更新系统软件包,很可能会导致环境出现问题
sudo rosdep init
rosdep update

如果你的rosdep没有正确读取你的代理配置(表现为xxx Timeout),也可以强制指定代理:

http_proxy=http://your_proxy_ip:7890 https_proxy=https://your_proxy_ip:7890 rosdep update

正常的输出类似:

reading in sources list data from /etc/ros/rosdep/sources.list.d
Query rosdistro index https://raw.githubusercontent.com/ros/rosdistro/master/index-v4.yaml
Skip end-of-life distro "ardent"    #跳过了一些已经终止支持的版本
Skip end-of-life distro "bouncy"
Skip end-of-life distro "crystal"
Skip end-of-life distro "dashing"
Skip end-of-life distro "eloquent"
Skip end-of-life distro "foxy"
Skip end-of-life distro "galactic"
Skip end-of-life distro "groovy"
Add distro "humble"                 #增加了当前仍然支持的版本
Skip end-of-life distro "hydro"
Skip end-of-life distro "indigo"
Add distro "iron"
Skip end-of-life distro "jade"
Add distro "jazzy"
Skip end-of-life distro "kinetic"
Skip end-of-life distro "lunar"
Skip end-of-life distro "melodic"
Add distro "noetic"
Add distro "rolling"
updated cache in /home/jetson/.ros/rosdep/sources.cache

安装软件包:

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

这条命令会传递需要安装的软件包调用apt进行安装,一般情况下不需要指定代理。

此过程会消耗较多时间(有非常多软件包需要安装),请耐心等待。

当你看到#All required rosdeps installed successfully就代表所需软件包安装完成,可以继续了。

构建代码

cd ~/ros2_humble/
colcon build --symlink-install

编译过程也需要比较长的时间……毕竟是Tier3系统版本,为了装Humble也只能等一下了~

闲得慌可以开个jtop看CPU旋转4小时

在Xavier NX上最终编译耗时4小时21分钟。

配置环境

echo ". ~/ros2_humble/install/local_setup.bash" >> ~/.bashrc
source ~/.bashrc

接下来就可以正常使用ROS2了,开两个终端来测试一下:

Listener:

ros2 run demo_nodes_py listener

Talker:

ros2 run demo_nodes_cpp talker

测试Listener成功接收Talker的信息,大功告成~