Ubuntu20.04下ROS2 Humble安装避坑指南:从清华源加速到环境变量配置
Ubuntu 20.04下ROS2 Humble高效安装与深度配置指南1. 系统环境准备与优化在Ubuntu 20.04上安装ROS2 Humble需要先确保系统环境配置正确。许多安装失败案例都源于基础环境未正确设置特别是locale和软件源配置。关键环境检查项# 检查当前locale设置 locale # 若未显示en_US.UTF-8执行以下命令 sudo apt update sudo apt install locales sudo locale-gen en_US en_US.UTF-8 sudo update-locale LC_ALLen_US.UTF-8 LANGen_US.UTF-8 export LANGen_US.UTF-8对于国内开发者使用清华源可显著提升下载速度。编辑/etc/apt/sources.list文件替换为以下内容deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse更新软件包缓存sudo apt update sudo apt upgrade -y2. ROS2 Humble核心安装流程2.1 添加ROS2软件源与密钥传统安装方式直接从ROS官方源下载但在国内网络环境下可能速度较慢。推荐使用镜像源加速# 安装必要工具 sudo apt install curl gnupg lsb-release # 添加清华提供的ROS2 GPG密钥 sudo curl -sSL https://mirrors.tuna.tsinghua.edu.cn/rosdistro/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg # 添加ROS2软件源 echo deb [arch$(dpkg --print-architecture) signed-by/usr/share/keyrings/ros-archive-keyring.gpg] https://mirrors.tuna.tsinghua.edu.cn/ros2/ubuntu $(source /etc/os-release echo $UBUNTU_CODENAME) main | sudo tee /etc/apt/sources.list.d/ros2.list /dev/null2.2 安装桌面完整版ROS2对于大多数开发者推荐安装桌面完整版包含GUI工具、教程和示例sudo apt update sudo apt install ros-humble-desktop安装时间根据网络状况不同通常需要10-30分钟。安装完成后可以立即验证基础功能# 在新终端中运行发布者 ros2 run demo_nodes_cpp talker # 在另一个终端中运行订阅者 ros2 run demo_nodes_py listener3. 开发环境深度配置3.1 环境变量持久化配置为避免每次打开终端都需要手动source将环境变量加入bashrcecho source /opt/ros/humble/setup.bash ~/.bashrc source ~/.bashrc对于使用zsh的用户echo source /opt/ros/humble/setup.zsh ~/.zshrc source ~/.zshrc3.2 必备开发工具安装ROS2开发需要以下工具链sudo apt install python3-colcon-common-extensions \ python3-rosdep \ python3-vcstool \ build-essential初始化rosdep并更新sudo rosdep init rosdep update3.3 工作空间创建与管理推荐的项目目录结构~/ros2_ws/ src/ # 源代码 build/ # 编译中间文件 install/ # 安装目录 log/ # 编译日志创建基础工作空间mkdir -p ~/ros2_ws/src cd ~/ros2_ws colcon build4. 常见问题解决方案4.1 依赖项安装失败处理当出现依赖问题时使用rosdep自动安装cd ~/ros2_ws rosdep install --from-paths src --ignore-src -y --skip-keys fastcdr rti-connext-dds-6.0.1 urdfdom_headers4.2 编译错误排查常见编译错误及解决方法错误类型可能原因解决方案CMake Error缺少开发包sudo apt install lib -devImportErrorPython包缺失pip install链接错误库路径问题检查LD_LIBRARY_PATH4.3 多机通信配置如需多机通信需统一设置ROS_DOMAIN_IDexport ROS_DOMAIN_ID相同数字 # 持久化配置 echo export ROS_DOMAIN_ID相同数字 ~/.bashrc5. 高级配置与性能优化5.1 更换DDS中间件ROS2支持多种DDS实现默认使用Fast DDS。如需更换为Cyclone DDSsudo apt install ros-humble-rmw-cyclonedds-cpp export RMW_IMPLEMENTATIONrmw_cyclonedds_cpp各DDS实现性能对比特性Fast DDSCyclone DDSConnext延迟中低高CPU占用高中低内存使用高低中5.2 编译优化参数在colcon build时添加优化参数colcon build --cmake-args -DCMAKE_BUILD_TYPERelease对于多核CPU启用并行编译colcon build --parallel-workers 86. 开发工具链集成6.1 VS Code配置安装ROS扩展后创建.vscode/settings.json{ ros.distro: humble, python.analysis.extraPaths: [ /opt/ros/humble/lib/python3.8/site-packages ] }6.2 调试配置创建launch.json用于调试{ version: 0.2.0, configurations: [ { name: ROS: Launch, type: ros, request: launch, target: path/to/launch/file.launch.py } ] }7. 实际项目应用示例以创建简单的发布-订阅节点为例创建功能包cd ~/ros2_ws/src ros2 pkg create --build-type ament_python py_pubsub编写发布者节点publisher_member_function.pyimport rclpy from rclpy.node import Node from std_msgs.msg import String class MinimalPublisher(Node): def __init__(self): super().__init__(minimal_publisher) self.publisher_ self.create_publisher(String, topic, 10) timer_period 0.5 self.timer self.create_timer(timer_period, self.timer_callback) self.i 0 def timer_callback(self): msg String() msg.data Hello World: %d % self.i self.publisher_.publish(msg) self.get_logger().info(Publishing: %s % msg.data) self.i 1 def main(argsNone): rclpy.init(argsargs) minimal_publisher MinimalPublisher() rclpy.spin(minimal_publisher) minimal_publisher.destroy_node() rclpy.shutdown() if __name__ __main__: main()修改setup.py添加入口点entry_points{ console_scripts: [ talker py_pubsub.publisher_member_function:main, ], }编译并运行cd ~/ros2_ws colcon build --packages-select py_pubsub source install/setup.bash ros2 run py_pubsub talker