最近在给实验室的机器人升级系统,从Ubuntu 20.04换到了22.04 LTS版本,顺便把ROS 1 Noetic也迁移到了ROS 2 Humble。整个过程踩了不少坑,这里把完整流程整理出来,希望能帮到同样需要在新系统上部署ROS的朋友们。
Ubuntu 22.04默认的软件源在国内访问速度不太理想,我强烈建议先换成国内镜像源。打开终端执行以下命令:
bash复制sudo sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list
sudo apt update && sudo apt upgrade -y
接下来需要安装一些基础依赖,这些是后续ROS安装的必备组件:
bash复制sudo apt install -y curl git python3-pip build-essential
特别提醒:Ubuntu 22.04默认的Python版本是3.10,而ROS 2 Humble也是基于Python 3.10设计的,这点比ROS Noetic要友好很多,不需要再折腾Python版本兼容问题了。
官方提供了多种安装方式,我推荐使用Debian包安装,这是最稳定便捷的方法。首先添加ROS 2的APT源:
bash复制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://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
更新软件包索引并安装完整版ROS:
bash复制sudo apt update
sudo apt install ros-humble-desktop-full
安装完成后,记得配置环境变量。我习惯把它加到.bashrc里,这样每次打开终端都能自动加载:
bash复制echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc
验证安装是否成功可以运行一个简单的demo:
bash复制ros2 run demo_nodes_cpp talker
在另一个终端运行:
bash复制ros2 run demo_nodes_py listener
如果能看到talker发送的消息被listener接收打印出来,说明ROS 2环境已经配置正确。
实验室用的Intel Realsense D435i深度相机,在Ubuntu 22.04上需要重新编译安装驱动。这里有几个关键点需要注意,特别是内核模块的兼容性问题。
首先安装编译依赖:
bash复制sudo apt-get install -y libssl-dev libusb-1.0-0-dev pkg-config libgtk-3-dev
sudo apt-get install -y libglfw3-dev libgl1-mesa-dev libglu1-mesa-dev
克隆最新的librealsense源码并编译:
bash复制git clone https://github.com/IntelRealSense/librealsense.git
cd librealsense
mkdir build && cd build
cmake .. -DBUILD_EXAMPLES=true -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
sudo make install
这里有个大坑:Ubuntu 22.04使用的是Linux 5.15内核,需要打补丁才能正常使用Realsense。执行以下命令:
bash复制./scripts/patch-realsense-ubuntu-lts.sh
安装完成后插上相机,运行测试工具验证:
bash复制realsense-viewer
如果能看到相机的实时画面,说明驱动安装成功。
ROS 2版本的realsense-ros功能包安装方式和ROS 1有些不同。首先创建一个工作空间:
bash复制mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
克隆必要的功能包:
bash复制git clone https://github.com/IntelRealSense/realsense-ros.git -b ros2-development
git clone https://github.com/ros2/ddynamic_reconfigure.git
安装依赖并编译:
bash复制cd ~/ros2_ws
rosdep install -i --from-path src --rosdistro humble -y
colcon build
编译完成后记得source环境:
bash复制echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc
启动相机节点测试:
bash复制ros2 launch realsense2_camera rs_launch.py
可以用以下命令查看发布的topic:
bash复制ros2 topic list
实验室有三台搭载Realsense的机器人,需要构建分布式视觉网络。ROS 2的通信机制相比ROS 1有了很大改进,特别是DDS的引入让多机通信更加灵活。
首先确保所有机器在同一个局域网内。我建议给每台机器设置静态IP,避免DHCP导致的IP变化问题。
在主机上(假设IP为192.168.1.100)执行:
bash复制export ROS_DOMAIN_ID=42
在所有从机上执行同样的命令设置相同的DOMAIN_ID。这个数字可以任意选择,但必须保证所有机器相同。
ROS 2不再需要像ROS 1那样设置MASTER_URI,这是DDS带来的一个重要改进。但是需要确保网络配置正确:
bash复制export ROS_LOCALHOST_ONLY=0
多台Realsense同时传输图像数据会占用大量带宽,我们需要对话题进行优化。主要有以下几种方法:
bash复制ros2 launch realsense2_camera rs_launch.py depth_width:=640 depth_height:=480 depth_fps:=15 color_width:=640 color_height:=480 color_fps:=15
bash复制ros2 run image_transport republish raw in:=/camera/color/image_raw out:=/camera/color/image_compressed compressed
创建qos_override.yaml文件:
yaml复制/camera/color/image_raw:
reliability: best_effort
durability: volatile
depth: 10
然后启动时加载:
bash复制ros2 launch realsense2_camera rs_launch.py qos_override:=/path/to/qos_override.yaml
多台Realsense同时运行时,需要解决节点命名冲突问题。ROS 2提供了几种解决方案:
bash复制ros2 launch realsense2_camera rs_launch.py camera_name:=robot1/camera
bash复制ros2 launch realsense2_camera rs_launch.py camera_name:=camera_robot1
python复制from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode
container = ComposableNodeContainer(
name='my_container',
namespace='',
package='rclcpp_components',
executable='component_container',
composable_node_descriptions=[
ComposableNode(
package='realsense2_camera',
plugin='realsense2_camera::RealSenseNodeFactory',
name='camera1',
parameters=[{'serial_no': '123456'}]
),
ComposableNode(
package='realsense2_camera',
plugin='realsense2_camera::RealSenseNodeFactory',
name='camera2',
parameters=[{'serial_no': '654321'}]
),
],
output='screen',
)
在部署过程中遇到了不少问题,这里总结几个典型问题的解决方法。
有时候插上Realsense后系统无法识别,可以尝试以下步骤:
bash复制lsusb | grep Intel
应该能看到类似"Intel Corp. RealSense..."的设备。
bash复制sudo modprobe -r uvcvideo
sudo modprobe uvcvideo
bash复制sudo chmod 666 /dev/bus/usb/*/*
当发现点云数据有缺失或异常时,可以:
bash复制ros2 param set /camera/stereo_module emitter_enabled 3
bash复制realsense-viewer
在工具中进行动态校准
在多机通信中如果发现数据延迟严重,可以:
bash复制ping 192.168.1.100
bash复制ros2 topic bw /camera/color/image_raw
考虑使用有线网络替代无线网络
调整DDS配置,比如使用FastRTPS替代默认的CycloneDDS:
bash复制export RMW_IMPLEMENTATION=rmw_fastrtps_cpp