在 ubuntu 18.04 arm(docker 容器)中安装 ROS 环境。本文所使用的 ROS 安装命令实测可用于 ubuntu 18.04 amd64 的设备。

1. 简述

由于本人对 ROS 并不是特别了解,这里就不引入 ROS 的背景介绍了。

ROS 的安装主要基于官网的教程,先选择你当前使用的 ubuntu 版本对应的 ROS,然后根据具体版本的 ROS 官方教程来安装就可以了。

先打开 ROS 官网:cn - ROS Wiki

选择 “安装”,会进入如下页面,这里会提示你不同版本的 ROS 以及推荐安装的 ubuntu 系统。

image.png

因为我当前使用的是 ubuntu18.04,所以选择 ROS Melodic Morenia 这一发行版。点进去后可以看到这里列出来了支持的操作系统和对应的架构版本,我们当前使用的 ubuntu 18.04 对应的发行版本代号是 Bionic,这个版本的 ROS 是支持 arm64 架构的。

image.png

点击 ubuntu 链接,就会跳转到 ubuntu 18.04 上安装 ROS 的教程了:cn/melodic/Installation/Ubuntu - ROS Wiki

跟着教程一步一来,本文主要记录遇到过的坑。

2. 安装 ROS

2.1. 配置软件源

官网给出了两个软件源的命令,一个是官方的源,另外一个是国内清华源

bash
1
2
3
4
# 官方源
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
# 清华源
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'

这里推荐直接使用国内的清华源。执行这个命令之后就会遇到第一个错误,环境中缺少 lsb_release 命令。

plaintext
1
2
root@ubuntu-linux-22-04-02-desktop:/work# lsb_release -sc
bash: lsb_release: command not found

需要 apt 安装 lsb-release 包,执行 apt-get install lsb-release 命令即可。安装了之后就可以使用 lsb_release 命令了,命令的作用是显示当前发行版的名称,前文已经提到了 ubuntu 18.04 是 bionic。

plaintext
1
2
root@ubuntu-linux-22-04-02-desktop:/work# lsb_release -sc
bionic

安装了该软件包后,重新执行上述添加软件源的命令。注意如果你刚刚已经执行了命令,则会得到一个有问题的软件源文件,需要删掉文件,再重新添加

bash
1
2
3
4
# 先删除原本的文件(因为命令不存在导致源有问题)
rm -rf /etc/apt/sources.list.d/ros-latest.list
# 重新添加清华源
sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'

随后还需要执行命令添加一下密钥(不管是清华源还是官方源都是这个命令)

bash
1
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654

随后更新一下软件源

bash
1
sudo apt-get -y update

2.2. 安装 ROS 的不同版本的命令

现在软件源已经更新好了,可以根据自己的需要安装不同版本的 ros 了。官网给出了下面四种不同的命令,这里我直接选了第一个安装了完整版。

桌面完整版(推荐): : 包含 ROS、rqtrviz、机器人通用库、2D/3D 模拟器、导航以及 2D/3D 感知包。或 单击这里

plaintext
1
sudo apt install ros-melodic-desktop-full

桌面版: 包含 ROS,rqtrviz 和机器人通用库,或 单击这里

plaintext
1
sudo apt install ros-melodic-desktop

ROS - 基础包: 包含 ROS 包,构建和通信库。没有图形界面工具。或 单击这里

plaintext
1
sudo apt install ros-melodic-ros-base

单独的包: 你也可以安装某个指定的 ROS 软件包(使用软件包名称替换掉下面的 PACKAGE):

plaintext
1
sudo apt install ros-melodic-PACKAGE

如:

plaintext
1
sudo apt install ros-melodic-slam-gmapping

完整版需要安装的东西很多,耐心等待一下。

image.png

在安装的途中,会弹出两次让你选择 tzdata 的弹窗,直接选择 GMT+8 或者 Asia/Shanghai 就可以了。

image.png

2.3. ROS 初始化

随后依照官网的教程,使用 sudo rosdep init 命令来初始化 ros 系统。

注意,rosdep 命令是不会跟随上面的完整桌面版包一起安装的,需要我们手动安装一下。

bash
1
sudo apt install python-rosdep

第一次执行 rosdep init 的时候,遇到了网络问题(http 错误),通过 apt install iputils-ping 安装了 ping 命令,发现 raw.githubusercontent.com 域名走了 127.0.0.1,延迟低于 1ms,肯定是不对的。

最开始我以为它是走了错误的代理,但是 ping 了 github.com 是没有问题的,所以问题在 DNS 解析上。解决方案采用了最粗暴的方式,直接将 raw.githubusercontent.com 域名的 IP 写入了 /etc/hosts 文件,避免修改系统的 DNS 设置。

plaintext
1
185.199.109.133 raw.githubusercontent.com

修改了之后,ping raw.githubusercontent.com 正常了

image.png

随后就能正常执行 rosdep 初始化命令了。命令执行输出如下。

plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
root@ubuntu-linux-22-04-02-desktop:/# rosdep init
Wrote /etc/ros/rosdep/sources.list.d/20-default.list
Recommended: please run

rosdep update

root@ubuntu-linux-22-04-02-desktop:/# rosdep update
reading in sources list data from /etc/ros/rosdep/sources.list.d
Warning: running 'rosdep update' as root is not recommended.
You should run 'sudo rosdep fix-permissions' and invoke 'rosdep update' again without sudo.
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml
Hit https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml
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"
Skip end-of-life 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 /root/.ros/rosdep/sources.cache

2.4. 设置环境

如果你用的是 bash,执行如下命令

bash
1
2
echo "source /opt/ros/melodic/setup.bash" >> ~/.bashrc
source ~/.bashrc

如果是 zsh,执行如下命令

bash
1
2
echo "source /opt/ros/melodic/setup.zsh" >> ~/.zshrc
source ~/.zshrc

如果你不想让 ROS 环境始终生效,可以使用如下命令暂时在当前 bash 生效。

bash
1
source /opt/ros/melodic/setup.bash

注意:如果你不只安装了一个 ROS 发行版, ~/.bashrc 只能使用你需要的 ROS 版本的 setup.bash 脚本。

2.5. ROS 工具依赖

rosinstall 是一个常用的命令行工具,用于下载众多 ROS 包的 source tree。

可以通过下面的命令安装这个工具。

bash
1
2
3
4
sudo apt-get install -y \
python-rosinstall \
python-rosinstall-generator \
python-wstool build-essential

3. 确认安装是否成功

3.1. roscore 命令

到这里,我们的 ROS 安装就完成了,可以使用 roscore 命令来确定 ros 是否成功安装,出现如下输出即为安装成功。

plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
root@ubuntu-linux-22-04-02-desktop:/# roscore
... logging to /root/.ros/log/cfad45ce-e696-11ef-bdf0-001c42a84d93/roslaunch-ubuntu-linux-22-04-02-desktop-522.log
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://ubuntu-linux-22-04-02-desktop:38385/
ros_comm version 1.14.13


SUMMARY
========

PARAMETERS
* /rosdistro: melodic
* /rosversion: 1.14.13

NODES

auto-starting new master
process[master]: started with pid [536]
ROS_MASTER_URI=http://ubuntu-linux-22-04-02-desktop:11311/

setting /run_id to cfad45ce-e696-11ef-bdf0-001c42a84d93
process[rosout-1]: started with pid [547]
started core service [/rosout]

image.png

3.2. 可能遇到的问题

执行 roscore 命令的时候可能遇到如下问题,报错的重点是 Permission denied 缺少权限

plaintext
1
2
3
4
5
6
7
king@ubuntu:~/slam/pkg$ roscore
Traceback (most recent call last):
File "/opt/ros/melodic/lib/python2.7/dist-packages/roslaunch/__init__.py", line 290, in main
write_pid_file(options.pid_fn, options.core, options.port)
File "/opt/ros/melodic/lib/python2.7/dist-packages/roslaunch/__init__.py", line 112, in write_pid_file
with open(pid_fn, "w") as f:
IOError: [Errno 13] Permission denied: '/home/king/.ros/roscore-11311.pid'

这个权限缺少并不是因为没有加 sudo 导致的,因为这个目录就是我们当前子用户的目录。

解决方案是修改这个目录的权限

bash
1
sudo chmod -R 777 /home/king/.ros/

随后就可以正常启动 roscore 了

plaintext
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
king@ubuntu:~/slam/pkg$ roscore
WARNING: cannot create log directory [/home/king/.ros/log/4322bfb0-f665-11ef-8ac7-000c29839929]. Please set ROS_LOG_DIR to a writable location.
Checking log directory for disk usage. This may take a while.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://ubuntu:40095/
ros_comm version 1.14.13


SUMMARY
========

PARAMETERS
* /rosdistro: melodic
* /rosversion: 1.14.13

NODES

auto-starting new master
process[master]: started with pid [111860]
ROS_MASTER_URI=http://ubuntu:11311/

setting /run_id to 4322bfb0-f665-11ef-8ac7-000c29839929
process[rosout-1]: started with pid [111878]
started core service [/rosout]

4. The end

至此,ROS 安装完毕啦!