Docker + ROS2 开发环境搭建指南

概述

本文介绍了如何使用 Docker 容器化技术搭建 ROS2 开发环境,包括官方镜像资源、相关学习资源、实践案例以及问题及解决方案等内容。通过容器化部署,可以实现环境隔离、快速部署和跨平台兼容,大大提升 ROS2 开发效率。

本文重点在于 Docker 和 ROS2 结合使用这一场景,着重说明遇到的几个案例及相关的配置文件,对于 Docker 本身的使用指南建议查阅 Docker 使用指南,关于 Docker 的安装和配置请查看其中的 相应章节

关于在 Windows 上搭建 ROS 环境的建议:

如果你想搭建过程比较简单,那么建议你使用 WSL,WSL 支持安装多个 Ubuntu 版本,包括 20.04、22.04、24.04等,这对于安装不同 ROS 版本是非常友好的。

如果你相比简单性,更看重可移植性(比如你换了个电脑)和移植的快速性,那么建议你结合 WSL 使用 Docker。

至于安装 VMware 虚拟机 或 VirtualBox 虚拟机,这是过时的方法,这种方式启动速度慢,GUI 也较卡,且无法使用硬件加速,更无法使用 GPU 进行深度学习和训练等。

运行环境 vs 开发环境:

运行环境:

  • 仅 Docker 配置文件(由 Dockerfile、docker-compose.yaml、.env 等文件定义)
  • 通常精简化,只包含运行时依赖
  • 专注于应用程序的运行和部署

开发环境:

  • Dev Container + Docker 配置文件的组合
  • 包含开发工具、调试器、代码编辑器插件等
  • 提供完整的开发体验

本文后续的实践案例中,既包括开发环境,也包括运行环境。

ROS 官方镜像资源

ROS 提供了不同用途的官方 Docker 镜像:

生产环境镜像:ros - Official Image - 精简版本,适合部署开发环境镜像:osrf/ros Tags - 包含 GUI 工具

其他相关资源:

镜像规范:REP 2001 - ROS 2 Variants - 官方镜像约定源码参考:Dockerfile 源码 - 如ros:humble-ros-base

学习资源

ROS2 官方文档

  • 在 Docker 中运行 ROS2 节点 - 多容器部署指南
  • VSCode + Docker 开发环境配置 - 集成开发环境

Docker 官方文档

  • Docker 官方文档 - 完整的 Docker 使用指南
  • Ubuntu 安装 Docker - Linux 环境安装教程

下面将罗列一些实践案例。

官方示例:Talker/Listener

这是 ROS2 官方教程的经典示例,适合学习和测试。

参考文档: ROS2 官方容器化指南

docker-compose.yaml

1 2 3 4 5 6 7 8 9

services: talker: build: . command: ros2 run demo_nodes_cpp talker listener: build: . command: ros2 run demo_nodes_cpp listener depends_on:

  • talker

Dockerfile

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Select the base image, such as humble or jazzy

FROM ros:humble env SHELL=/bin/bash RUN apt update && apt install ros-humble-demo-nodes-cpp -y

********************************************************

* Anything else you want to do like clean up goes here *

********************************************************

Use my profile repository to set up the environment

git clone https://github.com/wsxq2/profile.git ~/.MyProfile && cd ~/.MyProfile && ./deploy.sh $HTTP_PROXY_HOST

[Optional] Set the default user. Omit if you want to keep the default as root.

ROS1/ROS2 桥接

⚠️ 注意: ROS1 Bridge 已逐步被弃用,仅在早期 ROS2 版本(如 Galactic)中支持,Humble 及以后版本不再提供支持。

主要限制:

  • 要求系统同时安装 ROS1 和 ROS2 环境
  • 配置复杂,兼容性问题较多 **建议:**新项目直接使用 ROS2,避免使用桥接

docker-compose.yaml

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

services: ros1: build: context: . dockerfile: ros1.Dockerfile networks:

  • rosnetwork command: rosrun roscpp_tutorials talker ros2: build: context: . dockerfile: ros2.Dockerfile networks:
  • rosnetwork environment:
  • ROS_DOMAIN_ID=1 command: ros2 run demo_nodes_cpp listener bridge: build: context: . dockerfile: bridge.Dockerfile command: ros2 run ros1_bridge dynamic_bridge environment:
  • ROS_MASTER_URI=http://172.19.0.1:11311
  • ROS_DOMAIN_ID=1 networks:
  • rosnetwork networks: rosnetwork:

ros1.Dockerfile

1 2 3 4 5 6 7 8 9

ARG ROS_DISTRO=noetic FROM ros:$ROS_DISTRO

安装ROS包

RUN apt-get update && apt-get install -y
ros-{ROS_DISTRO}-common-tutorials &&
rm -rf /var/lib/apt/lists/*

ros2.Dockerfile

1 2 3 4 5 6 7 8

ARG ROS_DISTRO=humble FROM ros:${ROS_DISTRO}

安装ROS包

RUN apt-get update && apt-get install -y
ros-${ROS_DISTRO}-demo-nodes-cpp &&
rm -rf /var/lib/apt/lists/*

bridge.Dockerfile

1 2 3 4 5

FROM ros:galactic-ros1-bridge

设置环境变量

ENV ROS_HOSTNAME=bridge ENV ROS_MASTER_URI=http://ros1:11311

使用总结

在同一主机上,Docker 容器 ros1 + ros2 + bridge 的组合能成功,但其中的 ros1 如果使用主机的 ros1 则会失败。

由于 bridge 只是 ROS2 发展前期的过渡产品,仅前几个 ROS 版本支持(humble 就已经不支持了),它要求较高,系统中必须同时安装 ROS1 和 ROS2 的环境(如 bridge 容器中就是如此),所以现在尽量不要使用它

RQT 图形化开发环境

用于开发 RQT 插件和图形化调试工具:

特点:

  • 基于 osrf/ros:humble-desktop

镜像 - 集成完整的 GUI 开发环境

  • 支持 X11 转发

VNC方式可参考:docker-ros2-desktop-vnc

Dockerfile

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 37 38 39 40 41 42 43 44 45

FROM osrf/ros:humble-desktop

Set proxy host and port

ARG HTTP_PROXY_HOST=host.docker.internal ARG HTTP_PROXY_PORT=7890

Replace with your proxy host and port or comment out if not needed

ENV http_proxy=http://HTTP_PROXY_PORT ENV https_proxy=$http_proxy

Set the timezone to Shanghai

RUN echo ‘Asia/Shanghai’ > /etc/timezone && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

Update the apt sources to use Tsinghua University’s mirror

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak RUN < /etc/apt/sources.list deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse EOF

Add ROS 2 apt repository

RUN apt-get install curl gnupg2 -y && curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg RUN mv /etc/apt/sources.list.d/ros2.sources /etc/apt/sources.list.d/ros2.sources.bak && echo “deb [arch=$(dpkg —print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu jammy main” | tee /etc/apt/sources.list.d/ros2.list > /dev/null

安装 rqt 及 Python 依赖

RUN apt-get update &&
apt-get install -y python3-pip ros-humble-rqt unzip &&
rm -rf /var/lib/apt/lists/*

安装最新版本的 clangd

RUN CLANGD_VERSION=(curl -s https://api.github.com/repos/clangd/clangd/releases/latest | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/') && \ curl -L -o /tmp/clangd-linux.zip "https://github.com/clangd/clangd/releases/download/{CLANGD_VERSION}/clangd-linux-${CLANGD_VERSION}.zip” &&
unzip /tmp/clangd-linux.zip -d /tmp/ &&
find /tmp -name “clangd” -type f -executable -exec cp {} /usr/local/bin/clangd ; &&
chmod +x /usr/local/bin/clangd &&
rm -rf /tmp/clangd-linux.zip /tmp/clangd_*

手动模拟 rosdep init

RUN mkdir -p /etc/ros/rosdep/sources.list.d/ && curl -o /etc/ros/rosdep/sources.list.d/20-default.list -L https://mirrors.tuna.tsinghua.edu.cn/github-raw/ros/rosdistro/master/rosdep/sources.list.d/20-default.list

set rosdep mirror to Tsinghua University

ENV ROSDISTRO_INDEX_URL=https://mirrors.tuna.tsinghua.edu.cn/rosdistro/index-v4.yaml

docker-compose.yaml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

services: ros2-gui: build: . container_name: ros2-gui ports:

  • “7400-7600:7400-7600/udp” # ROS2 DDS 互通端口 volumes:
  • .:/workspace - /tmp/.X11-unix:/tmp/.X11-unix # 挂载 X11 socket(Linux 下) environment:
  • DISPLAY=host.docker.internal:0.0 # Windows 下 X11,或根据你的 X server 设置调整
  • ROS_DOMAIN_ID=30
  • ROS_LOCALHOST_ONLY=0 - FASTRTPS_DEFAULT_PROFILES_FILE=/workspace/ros2_config.xml networks:
  • rosnet tty: true stdin_open: true working_dir: /workspace networks: rosnet: external: true

devcontainer.json

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

{ “name”: “ROS2 Humble rqt GUI”, “dockerComposeFile”: ”../docker-compose.yml”, “service”: “ros2-gui”, “workspaceFolder”: “/workspace”, “customizations”: { “vscode”: { “settings”: { “terminal.integrated.shell.linux”: “/bin/bash”, “clangd.path”: “/usr/local/bin/clangd” }, “extensions”: [ “donjayamanne.python-extension-pack”, “ms-ros.ros”, “llvm-vs-code-extensions.vscode-clangd”, “seanwu.vscode-qt-for-python” ] } }, “postCreateCommand”: “rosdep update && rosdep install —from-paths src —ignore-src -r -y” }

Cartographer SLAM

APT 安装方式(推荐)

Cartographer 是 Google 开源的 2D/3D SLAM 解决方案。在 ROS2 中可通过包管理器一键安装:

1

sudo apt install ros-$ROS_DISTRO-cartographer-ros

特点:

  • 依赖包较多,安装耗时较长
  • 是生产环境推荐的安装方式
  • 官方仓库:ros2/cartographer_ros

这种方式下,Docker 相关配置就比较简单了。后续相关内容参考自:husarion/cartographer-docker

docker-compose.yaml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

services: cartographer: build: . volumes:

  • ./config/diffbot_lds_2d.lua:/pr2.lua command: > ros2 run cartographer_ros cartographer_node -configuration_directory / -configuration_basename pr2.lua cartographer-occ: build: . command: > ros2 run cartographer_ros cartographer_occupancy_grid_node -resolution 0.05 -publish_period_sec 1.0

其中的 ./config/diffbot_lds_2d.lua

是 cartographer 的核心配置文件,由于它和具体的使用场景相关,需要手动调节,这里就不展开了。

Dockerfile

1 2 3 4 5 6 7 8 9 10

FROM ros:humble ARG ROS_DISTRO=humble RUN apt update &&
apt install -y ros-$ROS_DISTRO-cartographer-ros && \

clean to make the image smaller

apt autoremove -y &&
apt clean &&
rm -rf /var/lib/apt/lists/*

源码编译方式

适用于需要自定义功能的场景,例如添加全局重定位功能。

参考资源:

Dockerfile

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

Select the base image, such as humble or jazzy

FROM ros:humble

Replace with your username

ARG USERNAME=wsxq2 ARG HTTP_PROXY_HOST=wsxq2 ARG USER_UID=1000 ARG USER_GID=$USER_UID

Set proxy host and port

ARG HTTP_PROXY_HOST=192.168.56.200 ARG HTTP_PROXY_PORT=7890

Replace with your proxy host and port or comment out if not needed

ENV http_proxy=http://HTTP_PROXY_PORT ENV https_proxy=$http_proxy

Set the timezone to Shanghai

RUN echo ‘Asia/Shanghai’ > /etc/timezone && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

This allows sudo commands to inherit the proxy environment variables

RUN sed -i ‘/Defaults\s*env_reset/a Defaults env_keep = “http_proxy https_proxy ftp_proxy no_proxy DISPLAY XAUTHORITY”’ /etc/sudoers

Update the apt sources to use Tsinghua University’s mirror

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak RUN < /etc/apt/sources.list deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse deb http://security.ubuntu.com/ubuntu/ jammy-security main restricted universe multiverse EOF

Add ROS 2 apt repository

RUN apt-get install curl gnupg2 -y && curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg RUN mv /etc/apt/sources.list.d/ros2.sources /etc/apt/sources.list.d/ros2.sources.bak && echo “deb [arch=$(dpkg —print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu jammy main” | tee /etc/apt/sources.list.d/ros2.list > /dev/null

Update the apt package index.

RUN apt-get update

Install basic utilities and tools. Use while loop to ensure it retries on failure

RUN /bin/bash -c ‘while true; do if apt-get install -y python3-pip command-not-found vim x11-apps; then break; fi; done’

Delete user if it exists in container (e.g Ubuntu Noble: ubuntu)

RUN if id -u USER_UID` ; fi

Create the user

RUN groupadd —gid USERNAME
&& useradd —uid USER_GID -m $USERNAME \

[Optional] Add sudo support. Omit if you don’t need to install software after connecting.

&& apt-get update
&& apt-get install -y sudo
&& echo USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/USERNAME
&& chmod 0440 /etc/sudoers.d/$USERNAME ENV SHELL /bin/bash

********************************************************

* Anything else you want to do like clean up goes here *

********************************************************

Use my profile repository to set up the environment

git clone https://github.com/wsxq2/profile.git ~/.MyProfile && cd ~/.MyProfile && ./deploy.sh $HTTP_PROXY_HOST

[Optional] Set the default user. Omit if you want to keep the default as root.

USER $USERNAME CMD [“/bin/bash”]

devcontainer.json

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 37 38 39 40 41 42 43

{ “name”: “ROS 2 Development Container”, “privileged”: true, “remoteUser”: “wsxq2”, “build”: { “dockerfile”: “Dockerfile”, “args”: { “USERNAME”: “wsxq2” } }, “workspaceFolder”: “/home/ws”, “workspaceMount”: “source={env:DISPLAY}” ], “mounts”: [ //“source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind,consistency=cached”, //“source=/dev/dri,target=/dev/dri,type=bind,consistency=cached” ], “postCreateCommand”: “rosdep update && rosdep install —from-paths src —ignore-src -y && sudo chown -R $(whoami) /home/ws/” }

在 Docker 中运行 RTAB-Map

RTAB-Map 是一个综合能力很强的 SLAM 算法,它使用的核心传感器是 RGBD 相机或者立体相机或者激光雷达,除此之外,它也可以融合其他传感器的数据。如果使用的是 3D 传感器(如 RGBD 相机、立体相机、3D 激光雷达)RTAB-Map 能实现 6 自由度(XYZ 和 RPY)的建图,得到三维模型,如果使用的是 2D 传感器,则仅能实现 3 自由度(XY 和 Y)的建图,得到平面地图。下面是一些重要的链接:

需要注意的是,RTAB-Map 需要用到 image_transport,这个包实现了灵活的图像/视频传输,即可以方便地发布压缩的和未压缩的图像,压缩的通常带有/compressed

的后缀。图像包括 RGB 图像和深度图像,压缩方式主要包括 JPG 和 PNG。它本身只提供 raw 格式,即未压缩格式,通过插件来提供压缩格式,主要有以下插件:

  • compressed_image_transport:用于压缩 RGB 图像。
  • compressed-depth-image-transport:用于压缩深度图像。
  • theora_image_transport:用于压缩视频流。

这些插件可以通过以下命令一键安装:

1

apt-get install ros-humble-image-transport-plugins

以上命令对于编译运行 RTAB-Map 是必须的。下面罗列一些关于 image_transport 的一些重要链接,以备忘:

下面将说明如何在 Docker 中的 ROS2 中运行 RTABMAP。

事实上,由于 RTABMAP 本身对 ROS2 就有良好的支持,且相关接口和 ROS1 中一致,更重要的是,官方提供了 Docker 镜像,所以要在 Docker 中运行是非常容易的。

Dockerfile

1 2 3

FROM introlab3it/rtabmap_ros:humble RUN apt-get update && apt-get install ros-humble-image-transport-plugins -y && rm -rf /var/lib/apt/lists/

docker-compose.yaml

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 37 38 39 40 41 42

version: ‘3.8’ services: rtabmap: image: wsxq2/rtabmap_ros:humble container_name: rtabmap_mapping privileged: true stdin_open: true tty: true network_mode: host ipc: host environment:

  • DISPLAY
  • QT_X11_NO_MITSHM=1
  • ROS_HOME=/tmp/.ros
  • OMP_WAIT_POLICY=passive volumes:
  • ~/.ros:/tmp/.ros
  • /tmp/.X11-unix:/tmp/.X11-unix command: > ros2 launch rtabmap_demos robot_mapping_demo.launch.py rtabmap_viz:=true rviz:=true bag_player: image: introlab3it/rtabmap_ros:humble container_name: rtabmap_bag_player stdin_open: true tty: true network_mode: host ipc: host environment:
  • OMP_WAIT_POLICY=passive
  • ROS_HOME=/tmp/.ros volumes:
  • ~/.ros:/tmp/.ros
  • /tmp/.X11-unix:/tmp/.X11-unix
  • ./demo_mapping_bag:/tmp/demo_mapping_bag command: > /bin/bash -c “ros2 bag play /tmp/demo_mapping_bag/demo_mapping.db3 —clock” depends_on:
  • rtabmap

使用方法

使用前,需要安装并配置 Docker

完成后,从 Google Drive 下载 bag 文件并解压,解压后要保证目录结构如下所示:

1 2 3 4 5 6 7

./ ├── demo_mapping_bag/ │ ├── demo_mapping.db3 │ └── metadata.yaml ├── docker-compose.yaml ├── Dockerfile └── README.md

然后执行以下命令即可:

1

docker compose up

尝试使用单目相机+3D激光雷达建图

这种使用方式下,就不能使用 rgbd_sync 节点了,相反,要使用 rgb_sync 节点。但实测发现,rgb_sync 节点有一些 bug,需要手动调整一下代码。

FAST-LIVO2 编译与运行及开发环境

这部分说明了四种情形:ROS1 Noetic、ROS1 Noetic + Docker、ROS2 Humble、ROS2 Humble + Docker。第一、三种情形仅描述了如何在已有的 ROS1 和 ROS2 环境中编译并运行 FAST-LIVO2,而第二、四种则在此基础上利用 Docker 搭建了一致的开发环境。

FAST-LIVO2 官方支持的是 ROS1 ,包括 Melodic、Noetic 这两个版本。FAST-LIVO2 中主要有两个组件,FAST-LIVO2 本身和 rpg_vikit,官方的 GitHub 链接如下:

下面列一些网友复现和分析:

  • Fast-LIVO2复现及论文阅读 – Kwan Wai-Pang – Personal Blog
  • 文档预览 - Gitee.com
  • 成功复现Fast-LIVO2到M300RTK(定位建图完全依赖Fast-LIVO2) · Issue #248 · hku-mars/FAST-LIVO2
  • Mid-360复现效果展示+复现过程分享 · Issue #119 · hku-mars/FAST-LIVO2

对于 ROS2,有网友做了相应的移植,且有两个版本可供参考:

其中对于第 2 个版本,由于没有公开 rpg_vikit 的源码,所以无法使用,因此使用第 1 个版本。其对应的 rpg_vikit 源码为 https://github.com/integralrobotics/rpg_vikit。

虽然我们的目标是在 ROS2 humble 中使用,但对于官方 ROS1 版本的运行和测试也有一定的价值,比如:假如实际使用时,在 ROS2 humble 中遇到了问题,找不到解决思路时,可以回到 ROS1 版本中测试看该问题是否同样存在,以排除移植到 ROS2 中改动代码造成的影响。因此,下面先说 ROS1 的情况,再说 ROS2 中的情况。

ROS1 Noetic

如果你有该环境(无论是使用 WSL 还是 Linux主机),那么使用起来会非常简单,按照官方教程走即可,不过有个地方需要注意:在 Noetic 中,Sophus 编译不通过,报错:so2.cpp:32:26: error: lvalue required as left operand of assignment

,这时需要手动改下代码,改动如下所示:

1 2 3 4 5 6 7 8 9 10

namespace Sophus { SO2::SO2() {

  • unit_complex_.real() = 1.;
  • unit_complex_.imag() = 0.;
  • unit_complex_.real(1.);
  • unit_complex_.imag(0.); }

另见:Sophus compiling error at commit a621ff · Issue #339 · hku-mars/FAST-LIVO2

此外,PCL、Eigen、OpenCV 这些依赖可以直接使用 apt 安装:

1

apt install libpcl-dev libeigen3-dev libopencv-dev -y

ROS1 Noetic + Docker

ROS1 noetic 是 FAST-LIVO2 官方支持的版本,本次实践目录结构如下:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

. ├── .devcontainer/ │ └── devcontainer.json ├── .vscode/ ├── build/ ├── data/ # bag 数据文件 ├── devel/ ├── docker/ # docker 相关文件 │ ├── .dockerignore │ ├── .env │ ├── Dockerfile │ └── docker-compose.yml ├── src/ # FAST-LIVO2 源码文件 │ ├── FAST-LIVO2/ │ ├── rpg_vikit/ └── README.md

详见 https://github.com/wsxq2/fast-livo2-in-docker/tree/noetic

ROS2 Humble

这部分主要参考自 https://github.com/integralrobotics/FAST-LIVO2 中的 README.md 说明,但细节上有所完善和优化。

类似地,这里假设你有 ROS2 Humble 的环境,则可以按照以下步骤进行:

执行以下命令安装依赖并下载源码:

1 2 3 4 5 6

sudo apt install libpcl-dev libeigen3-dev libopencv-dev -y sudo apt install ros-humble-sophus -y # 直接使用 apt 中提供的版本,而非手动编译特定版本并安装到系统 sudo apt install ros-humble-image-transport-plugins -y # 不安装这个会编译失败 cd fast_ws/src # 如果没有请自行创建此目录 git clone https://github.com/integralrobotics/FAST-LIVO2 git clone https://github.com/integralrobotics/rpg_vikit

参考 https://github.com/Livox-SDK/livox_ros_driver2 安装 livox_ros_driver2,这是移植到 ROS2 后新增的包。

为什么 ROS2 要新增此包呢?ROS1 中本来也是依赖此包的,后来去掉了,添加了真正依赖的

CustomMsg.h

CustomPoint.h

,从而减少依赖,降低编译难度,详见 [Enh] remove dependency on livox_ros_driver. · hku-mars/FAST-LIVO2@1666b09

此时目录结构如下:

1 2 3 4 5

./ └── src/ ├── FAST-LIVO2/ ├── livox_ros_driver2/ └── rpg_vikit/

编译时执行以下命令:

1 2

cd src/livox_ros_driver2
./build.sh humble

这里由于 livox_ros_driver2 的设计问题,编译时建议这样编译,否则可能编译不通过。

运行前需要转换 bag 格式,将原本的 ROS1 格式转换为 ROS2 格式:

1 2

pip install rosbags rosbags-convert —src Retail_Street.bag —dst Retail_Street

转换完成后需要修改消息类型,在 Retail_Street/metadata.yaml

中做出以下修改:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

rosbag2_bagfile_information: compression_format: ” compression_mode: ” custom_data: {} duration: nanoseconds: 135470252209 files:

  • duration: nanoseconds: 135470252209 message_count: 30157 path: Retail_Street.db3 … topic_metadata: name: /livox/lidar offered_qos_profiles: ” serialization_format: cdr
  • type: livox_ros_driver/msg/CustomMsg
  • type: livox_ros_driver2/msg/CustomMsg type_description_hash: RIHS01_94041b4794f52c1d81def2989107fc898a62dacb7a39d5dbe80d4b55e538bf6d

编译完成后在不同的 Terminal 中分别运行:

1 2

ros2 launch fast_livo mapping_aviz.launch.py use_rviz:=True ros2 bag play -p —clock Retail_Street # 启动后处于暂停状态,前者准备就绪后即可使用空格键开始 play

ROS2 Humble + Docker

ROS2 humble 不是 FAST-LIVO2 官方支持的版本,使用的是 网友移植的版本。目录结构和 前面 基本相同。

总体来说,这部分综合了 ROS1 Noetic + Docker 和 ROS2 Humble 的内容。

详见 https://github.com/wsxq2/fast-livo2-in-docker

官方使用的硬件分析

官方使用的硬件:xuankuzcr/LIV_handhold.

  • 激光雷达:购买 Livox 傲览 Avia 激光探测测距仪 | DJI 大疆商城、 Specs - Avia 傲览激光雷达 - Livox、 Livox Avia 用户手册中文.pdf。对比项:Helios 32线 70° FOV

  • 类型:半固态(推测)。

  • 特点:

  • 点云密度高:由于采用了非重复扫描,当停留0.1s时,点云密度相当于32线激光雷达,场景覆盖率约45%,停留在同一位置0.8s以上基本可以获取100%的信息(场景覆盖率约100%)。

  • 测量距离较远:高亮度(100klx)时低反射率(10%)的情况下依然有190m的测距能力(Helios 110m),最远可达450m(Helios最远150m)。

  • 垂直FOV较大: 70.4 77.2°(选型的Helios为36070°,为了获取较大垂直视角,它采用了不均匀分布,所以它的点云密度比一般的32线还低)。 - 精度相对较低:20m处为2cm(Helios为1cm)

  • 盲区较大:1m内不可测量(Helios为0.2m),1~3m点云可能畸变。

  • 支持时间同步(Helios也支持)

  • 内置IMU BMI088。从而对于IMU无需额外时间同步。

  • 相机:Hikrobot_MV-CA013-21UMUC.pdf。

  • 类型:工业面阵相机

  • 定位:快速成像,飞拍

  • 特点:

  • 支持硬触发、软触发,从而可用于时间同步

  • 130万像素

  • 高帧率:210fps@1280*1024

  • IMU:内置于激光雷达,数据手册:BMI088 Datasheet。

  • 频率:200HZ

  • 支持时间同步

遇到过的问题

X11 方式访问 RVIZ 经常发生错乱现象?

环境说明:Windows11 + Docker Desktop(使用 WSL2) + ROS humble desktop 镜像

尝试了以下步骤:

  • 更新 vcxsrv:marchaesen/vcxsrv: Windows X-server based on the xorg git sources (like xming or cygwin’s xwin), but compiled with Visual Studio 2012 Community Edition.
  • 更新显卡驱动:Drivers and Support for Processors and Graphics

然后目前暂未发现错乱现象,疑似解决了。

其他一些尝试:

  • GPU 性能测试: apt install mesa-utils

然后执行glxgears

命令即可,显示的 FPS 目前好像在 60 左右,基本够用。apt install glmark2

然后glmark2

  • GpuTest:使用方法可参见:Ubuntu 22.04 LTS RTX 2060 6G 显卡 GPU测试 甜甜圈 geeks3d GpuTest-CSDN博客

  • 付费版 xming(听说不错),但看了下价格,需要 97 元,太贵了,就放弃了。

  • 一个非常重要的问答,有个回答者还讲了相关原因:How to troubleshoot OpenGL on Ubuntu under Windows 10 (WSL) - Super User

  • ROS RVIZ 问题常见解决方案(即“不要使用 vcxsrv 中 Native OpenGl 选项”),和我的问题不同,但相关:xorg - how to check and confirm a right opengl version with vcxsrv (for using ros2 rviz2) - Ask Ubuntu

  • VirtualGL:尝试后报错,暂未找到解决方法,后续如果又出现此问题时,可再次尝试解决

  • TurboVNC+VirtualGL:实现服务器的多用户图形化访问与硬件加速 | 一颗栗子球

  • User’s Guide for VirtualGL 3.1.3

  • Releases · VirtualGL/virtualgl

  • 尝试执行 vglrun -display 192.168.3.107:0.0 glxgears

时报错:Error: couldn’t get an RGB, Double-buffered visual。参考了以下两个链接均未成功: