1544 字
8 分钟
ROS在Linux环境下的配置与部署指南
ROS在Linux环境下的配置与部署指南
环境要求
1. 系统要求
- 操作系统: Ubuntu 20.04 LTS (Focal Fossa) 或 Ubuntu 22.04 LTS (Jammy Jellyfish)
- 内核版本: Linux 5.4 或更高版本
- 存储空间: 至少 20GB 可用空间
- 内存: 至少 4GB RAM
- 处理器: 64位 x86 或 ARM 架构
2. ROS版本选择
| ROS版本 | Ubuntu版本 | 支持状态 | 推荐用途 |
|---|---|---|---|
| ROS Noetic | 20.04 | 长期支持(LTS) | 生产环境 |
| ROS2 Humble | 22.04 | 长期支持(LTS) | 新项目开发 |
| ROS2 Foxy | 20.04 | 已结束支持 | 遗留项目 |
安装步骤
步骤1: 系统更新与准备
# 更新软件包列表sudo apt updatesudo apt upgrade -y
# 安装必要工具sudo apt install -y curl wget git build-essentialsudo apt install -y python3-pip python3-venv
# 设置软件源为国内镜像(可选,加速下载)sudo sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.listsudo sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list步骤2: 安装ROS Noetic(Ubuntu 20.04)
2.1 配置软件源
# 添加ROS软件源sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
# 添加GPG密钥sudo apt install -y curlcurl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -2.2 安装ROS
# 更新软件包列表sudo apt update
# 安装完整版ROSsudo apt install -y ros-noetic-desktop-full
# 或者安装基础版# sudo apt install -y ros-noetic-desktop
# 或者安装最小版# sudo apt install -y ros-noetic-ros-base2.3 环境配置
# 初始化rosdepsudo rosdep initrosdep update
# 设置环境变量echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrcsource ~/.bashrc
# 安装构建工具依赖sudo apt install -y python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essentialsudo apt install -y python3-catkin-tools步骤3: 安装ROS2 Humble(Ubuntu 22.04)
3.1 设置语言环境
# 确保语言环境支持UTF-8sudo apt update && sudo apt install -y localessudo locale-gen en_US en_US.UTF-8sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8export LANG=en_US.UTF-83.2 添加ROS2软件源
# 添加GPG密钥sudo apt install -y software-properties-commonsudo add-apt-repository universesudo apt update && sudo apt install -y curlsudo 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/null3.3 安装ROS2
# 更新软件包列表sudo apt update
# 安装ROS2桌面版sudo apt install -y ros-humble-desktop
# 或者安装基础版# sudo apt install -y ros-humble-ros-base
# 安装开发工具sudo apt install -y python3-colcon-common-extensionssudo apt install -y python3-rosdep2
# 初始化rosdepsudo rosdep initrosdep update3.4 环境配置
# 设置环境变量echo "source /opt/ros/humble/setup.bash" >> ~/.bashrcsource ~/.bashrc创建工作空间
创建Catkin工作空间(ROS1)
# 创建工作空间目录mkdir -p ~/catkin_ws/srccd ~/catkin_ws/
# 初始化工作空间catkin_init_workspace src
# 构建工作空间catkin_make
# 配置环境变量echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrcsource ~/.bashrc创建Colcon工作空间(ROS2)
# 创建工作空间目录mkdir -p ~/ros2_ws/srccd ~/ros2_ws
# 构建工作空间colcon build
# 配置环境变量echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrcsource ~/.bashrc基础功能测试
ROS1功能测试
# 启动roscoreroscore &
# 在新的终端中运行小海龟示例# 终端1:运行turtlesim节点rosrun turtlesim turtlesim_node
# 终端2:运行控制节点rosrun turtlesim turtle_teleop_key
# 查看节点信息rosnode listrostopic listROS2功能测试
# 在新的终端中运行小海龟示例# 终端1:运行turtlesim节点ros2 run turtlesim turtlesim_node
# 终端2:运行控制节点ros2 run turtlesim turtle_teleop_key
# 查看节点信息ros2 node listros2 topic list常用工具安装
通用工具
# 安装VS Code(可选)sudo snap install code --classic
# 安装Terminator终端sudo apt install -y terminator
# 安装网络工具sudo apt install -y net-tools nmap
# 安装串口工具sudo apt install -y minicom cutecomROS开发工具
# ROS1工具sudo apt install -y ros-noetic-rviz ros-noetic-rqtsudo apt install -y ros-noetic-rosbridge-serversudo apt install -y ros-noetic-tf2-tools
# ROS2工具sudo apt install -y ros-humble-rviz2 ros-humble-rqtsudo apt install -y ros-humble-rosbridge-serversudo apt install -y ros-humble-nav2-bringup机器人特定配置
安装机器人相关包
# ROS1机器人包sudo apt install -y ros-noetic-turtlebot3sudo apt install -y ros-noetic-moveitsudo apt install -y ros-noetic-gazebo-ros-pkgs
# ROS2机器人包sudo apt install -y ros-humble-turtlebot3sudo apt install -y ros-humble-moveitsudo apt install -y ros-humble-gazebo-ros-pkgs配置TurtleBot3(示例)
# 设置TurtleBot3型号echo "export TURTLEBOT3_MODEL=burger" >> ~/.bashrcsource ~/.bashrc
# 下载TurtleBot3仿真模型sudo apt install -y ros-noetic-turtlebot3-simulations# 或者ROS2sudo apt install -y ros-humble-turtlebot3-simulations网络配置(多机通信)
单机配置
# 查看本机IPhostname -I
# 设置环境变量export ROS_IP=$(hostname -I | awk '{print $1}')export ROS_HOSTNAME=$ROS_IPexport ROS_MASTER_URI=http://$ROS_IP:11311
# 持久化配置echo "export ROS_IP=\$(hostname -I | awk '{print \$1}')" >> ~/.bashrcecho "export ROS_HOSTNAME=\$ROS_IP" >> ~/.bashrcecho "export ROS_MASTER_URI=http://\$ROS_IP:11311" >> ~/.bashrc多机通信配置
# 主控机配置export ROS_MASTER_URI=http://<master_ip>:11311export ROS_IP=<local_ip>
# 从机配置export ROS_MASTER_URI=http://<master_ip>:11311export ROS_IP=<local_ip>Docker容器配置(可选)
创建ROS Docker容器
# 拉取ROS镜像docker pull osrf/ros:noetic-desktop-full# 或者ROS2docker pull osrf/ros:humble-desktop
# 运行Docker容器docker run -it \ --env="DISPLAY" \ --env="QT_X11_NO_MITSHM=1" \ --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \ osrf/ros:noetic-desktop-full \ bash故障排除
常见问题及解决方案
问题1: rosdep初始化失败
# 解决方案:手动下载配置文件sudo mkdir -p /etc/ros/rosdep/sources.list.d/sudo curl -o /etc/ros/rosdep/sources.list.d/20-default.list https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/sources.list.d/20-default.list问题2: 权限不足错误
# 解决方案:修复权限sudo chmod -R 755 /opt/ros/sudo chown -R $USER:$USER ~/catkin_ws/问题3: 编译错误
# 解决方案:清理并重新编译cd ~/catkin_wsrm -rf build develcatkin_make cleancatkin_make问题4: 环境变量未生效
# 解决方案:手动刷新环境source ~/.bashrc# 或者exec bash性能优化
系统优化配置
# 禁用不必要的服务sudo systemctl disable bluetooth.servicesudo systemctl disable cups.service
# 调整交换空间(如果内存不足)sudo fallocate -l 2G /swapfilesudo chmod 600 /swapfilesudo mkswap /swapfilesudo swapon /swapfile
# 添加开机自动挂载echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabROS编译优化
# 使用多核编译catkin_make -j$(nproc)
# 或者使用更快的构建系统catkin build --jobs $(nproc) --limit-status-rate 0.01维护与更新
定期维护命令
# 更新ROS软件包sudo apt updatesudo apt upgrade ros-noetic-* # ROS1# 或者sudo apt upgrade ros-humble-* # ROS2
# 清理无用包sudo apt autoremovesudo apt autoclean
# 清理ROS日志rosclean purge -y备份配置
# 备份工作空间tar -czf ~/ros_backup_$(date +%Y%m%d).tar.gz ~/catkin_ws/
# 备份环境配置cp ~/.bashrc ~/.bashrc.backup_$(date +%Y%m%d)附录
有用的命令速查表
| 命令 | ROS1 | ROS2 | 功能 |
|---|---|---|---|
| 启动核心 | roscore | N/A | 启动ROS主节点 |
| 运行节点 | rosrun | ros2 run | 运行指定节点 |
| 列出节点 | rosnode list | ros2 node list | 显示运行中的节点 |
| 列出话题 | rostopic list | ros2 topic list | 显示活跃话题 |
| 话题信息 | rostopic info | ros2 topic info | 显示话题详情 |
| 消息发布 | rostopic pub | ros2 topic pub | 发布消息到话题 |
| 构建系统 | catkin_make | colcon build | 构建工作空间 |
学习资源
- 官方文档: ros.org
- ROS2文档: docs.ros.org
- 中文社区: roschina.org
- GitHub: ros/rosdistro
- 问答社区: answers.ros.org
ROS在Linux环境下的配置与部署指南
https://liyunyi2005.github.io/posts/ros/