ROS2常用指令(附 ROS1 & ROS2 对照表)

一、参考资料

ROS2 常用命令超详细总结(附 ROS1 & ROS2 对照表)-CSDN博客

ros2 常用命令 - 洛苏 - 博客园

ROS 命令行界面 - Programming Multiple Robots with ROS 2

二、ROS2常用指令

ROS 2 apt源

bash 复制代码
sudo apt update && sudo apt install curl gnupg2 lsb-release
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

环境变量

bash 复制代码
# === 添加 ROS2 环境设置 ===
# 方法1: 直接 source 安装目录的 setup.bash
source /opt/ros/humble/setup.bash

# 方法2: 或者使用环境变量(如果知道安装路径)
export ROS_DISTRO=humble
export ROS_PYTHON_VERSION=3
export ROS_VERSION=2

export PATH=/opt/ros/humble/bin:$PATH
export LD_LIBRARY_PATH=/opt/ros/humble/lib:$LD_LIBRARY_PATH
export PYTHONPATH=/opt/ros/humble/lib/python3.8/site-packages:$PYTHONPATH
export ROS_PACKAGE_PATH=/opt/ros/humble/share
export CMAKE_PREFIX_PATH=/opt/ros/humble:$CMAKE_PREFIX_PATH
export ROS_LOG_DIR=/root/.ros/log

ros2版本

bash 复制代码
# 查看系统中有哪些 ROS2 版本
ls -d /opt/ros/*/ 2>/dev/null

# 查找 ros2 命令的位置
which ros2

# 查看 ros2 的详细路径
type ros2

# 查看 ROS2 版本
ros2 version

pkg相关

bash 复制代码
# 创建功能包
ros2 pkg create <pkg_name> \
	--build-type ament_cmake \
	--dependencies rclcpp std_msgs

# 查看已安装包
ros2 pkg list

# 查看包内可执行文件
ros2 pkg executables <package>

# 启用DEBUG日志
ros2 run pkg node --ros-args --log-level DEBUG

node相关

bash 复制代码
# 查看节点列表
ros2 node list

# 查看节点详情
ros2 node info <node_name>

# 运行node节点
ros2 run <package> <executable_name>

topic相关

bash 复制代码
# 列出话题
ros2 topic list

# 查看话题列表(含类型)
ros2 topic list -t

# 查看话题信息
ros2 topic info </topic>

# 查看话题类型
ros2 topic type </topic>

# 按类型查找话题
ros2 topic find <msg_type>
# 如:ros2 topic find geometry_msgs/msg/Twist
# 如:ros2 topic find -c geometry_msgs/msg/Twist # 加 -c 只看数量

# 打印话题消息
ros2 topic echo </topic>

# 查看话题频率
ros2 topic hz </topic>

# 查看话题带宽
ros2 topic bw </topic>

# 查看消息延迟
ros2 topic delay </topic>

# 给话题发布消息
ros2 topic pub <topic_name> <message_type> '<message_content>'

# 常用参数
# 默认:1 Hz 持续发布;
# --once / -1:只发布一次;
# --rate 5 / -r 5:5 Hz 持续发布。

# 如:ros2 topic pub --once /cmd_vel geometry_msgs/msg/Twist \
"{linear: {x: 0.3, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}"
# 如:ros2 topic pub --once /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"

interface相关

bash 复制代码
# 列出所有接口
ros2 interface list

# 接口分三类:
# msg/ 消息(Messages)
# srv/ 服务(Services)
# action/ 动作(Actions)

# 查看某接口包内的类型
ros2 interface package <message_type>
# 如:ros2 interface package geometry_msgs

# 列出有哪些接口包
ros2 interface packages

# 查看某接口定义
ros2 interface show <message_type>
# 如:ros2 interface show geometry_msgs/msg/Twist

# 生成消息模板
ros2 interface proto <message_type>

lifecycle 生命周期节点

bash 复制代码
ros2 lifecycle nodes                       # 所有 LCN
ros2 lifecycle list </lifecycle_node>     # 下一个可用状态
ros2 lifecycle list </lifecycle_node> -a  # 所有可能转换
ros2 lifecycle get  </lifecycle_node>     # 当前状态
ros2 lifecycle set  </lifecycle_node> configure
ros2 lifecycle set  </lifecycle_node> activate
ros2 lifecycle set  </lifecycle_node> deactivate
ros2 lifecycle set  </lifecycle_node> cleanup
ros2 lifecycle set  </lifecycle_node> shutdown

rosdep相关

bash 复制代码
sudo rosdep init
rosdep update

# 安装依赖包
rosdep install -i --from-path src --rosdistro jazzy -y

# 参数说明:
# -i:忽略缺失的依赖项(即使某些依赖未找到也不报错)
# --from-path src:从src目录下的包中查找依赖
# --rosdistro jazzy:指定ROS发行版为Jazzy
# -y:自动回答yes,无需确认

参数 / 服务 / 动作 / bag包

param相关

bash 复制代码
# 列出所有节点参数
ros2 param list

# 读取参数
ros2 param get </node> <param_name>

# 修改参数
ros2 param set </node> <param_name> value

# 导出为 YAML
ros2 param dump /demo_node
ros2 param dump /demo_node --output-dir ./config

# 用 YAML 启动节点
ros2 run demo_cpp_pkg demo_node \
  --ros-args --params-file ./config/demo_node.yaml

service 相关

bash 复制代码
# 列出所有服务
ros2 service list  
ros2 service list -t # 带类型

# 查看某服务类型
ros2 service type <service_name>

# 按类型找服务
ros2 service find <type_name>

# call service
ros2 service call <service_name> <service_type> <arguments>
# 如:ros2 service call /spawn turtlesim/srv/Spawn "{x: 2.0, y: 2.0, theta: 0.2, name: 't1'}"

action 相关

bash 复制代码
ros2 action list                           # 动作列表
ros2 action list -t                        # 带类型
ros2 action info /navigate_to_pose         # 查看动作详情
 
ros2 action send_goal /navigate_to_pose \
  nav2_msgs/action/NavigateToPose \
  '{pose: { ... }}' --feedback

bag包

bash 复制代码
# 查看信息
ros2 bag info <bag_name>

ros2 bag play <bag_name>                     # 播放
ros2 bag play <bag_name> -r 2                # 2 倍速
ros2 bag play <bag_name> -l                  # 循环播放
ros2 bag play <bag_name> --topics /cmd_vel   # 只播某些话题

ros2 bag record /cmd_vel                   # 录制单话题
ros2 bag record -o log1 /cmd_vel /odom     # 录多个
ros2 bag record -a                         # 录所有话题

Launch 启动文件

bash 复制代码
ros2 launch <package> <file.launch.py>

可用 --show-args 查看参数。

三、ROS1 vs ROS2 常用指令对照表

话题相关

关键差异 :ROS2 消息类型写法多了 /msg,比如 geometry_msgs/msg/Twist

功能 ROS1 ROS2
查看所有话题 rostopic list ros2 topic list
查看话题内容 rostopic echo /topic ros2 topic echo /topic
查看话题信息 rostopic info /topic ros2 topic info /topic
查看话题类型 rostopic type /topic ros2 topic type /topic
按类型查话题 无直接命令 ros2 topic find <msg_type>
发布消息 rostopic pub /cmd_vel geometry_msgs/Twist ... ros2 topic pub /cmd_vel geometry_msgs/msg/Twist ...
查看频率 rostopic hz /topic ros2 topic hz /topic
查看带宽 rostopic bw /topic ros2 topic bw /topic

节点相关

功能 ROS1 ROS2
节点列表 rosnode list ros2 node list
节点信息 rosnode info /node ros2 node info /node

运行节点 & Launch

场景 ROS1 ROS2
运行 C++ 节点 rosrun pkg node ros2 run pkg executable
启动 launch roslaunch pkg xxx.launch ros2 launch pkg xxx.launch.py
核心进程 需要 roscore 没有 roscore,基于 DDS 自动发现

服务相关

功能 ROS1 ROS2
服务列表 rosservice list ros2 service list
服务类型 rosservice type /srv ros2 service type /srv
调用服务 rosservice call /srv ... ros2 service call /srv type '{...}'

参数相关

功能 ROS1 ROS2
参数列表 rosparam list ros2 param list
获取参数 rosparam get /xxx ros2 param get /node param
设置参数 rosparam set /xxx value ros2 param set /node param value
导出参数 rosparam dump file.yaml ros2 param dump /node --output-dir ...

工作空间与构建(C++)

项目 ROS1 ROS2
构建工具 catkin_make / catkin build colcon build
常用工作空间名 catkin_ws ros2_ws
源环境脚本 devel/setup.bash **install/setup.bash**
典型用法 source **devel**/setup.bash source **install**/setup.bash
相关推荐
阿豪只会阿巴2 天前
【多喝热水系列】从零开始的ROS2之旅——Day5
c++·笔记·python·ubuntu·ros2
维度攻城狮5 天前
ros2参数通信案例
开发语言·windows·python·ros2·参数通信
zylyehuo7 天前
Ubuntu22.04(ROS2 humble)小车仿真环境搭建
ros2·导航
阿豪只会阿巴7 天前
【多喝热水系列】从零开始的ROS2之旅——Day3
linux·笔记·ubuntu·ros2
阿豪只会阿巴7 天前
【多喝热水系列】从零开始的ROS2之旅——Day4
c++·笔记·python·ros2
撬动未来的支点7 天前
【ROS2速通】资料,笔记攻略
ros2
社会零时工8 天前
【ROS2】海康相机ROS2设备服务节点开发
linux·c++·相机·ros2
曾小蛙9 天前
【ROS2+深度相机】奥比中光Gemini 335L的简单使用
机器人·ros2·奥比中光·双目相机·gemini 335l·orbbec sdk·orbbecsdk_ros2
yuyuyue24910 天前
ego-planner-ros2核心算法解析
机器人·ros2