跳转至

ROS1&2常用命令对照

我在Ubuntu24.04上安装了ROS2 Jazzy,但由于现在存在大量由ROS1开发的机器人项目,学校课程和实验也都使用ROS1进行操作,因此我在Docker中安装了ROS1 Noetic来应对ROS1的需求。

本文将ROS1和ROS2的常用命令对照,方便快速切换使用ROS1和ROS2。

安装ROS 可以采用鱼香ROS(小鱼)的一键安装命令:

Bash
wget http://fishros.com/install -O fishros && . fishros

注意

ROS1&2 命令支持Bash,Zsh,对Fish的适配很差,建议使用Bash。

一个bug

如果docker中的ROS环境中无法开启图形界面(报错是Qt相关的环境问题),只要在本地终端中运行sudo xhost +命令,即可解决。

激活ROS环境

需要在终端中运行

Bash
source /opt/ros/noetic/setup.bash
Bash
source /opt/ros/jazzy/setup.bash

为了方便,可以将上述命令写入.bashrc文件中,以后打开终端就自动激活ROS环境。

启动ROS节点

Bash
1
2
3
4
# 必须先在一个终端中启动master节点
roscore
# 然后在另一个终端中启动其他节点
rosrun package_name node_name
Bash
# 直接启动节点即可
ros2 run package_name node_name

示例:

Bash
rosrun turtlesim turtlesim_node
Bash
ros2 run turtlesim turtlesim_node

查看系统结构

Bash
rqt_graph

查看当前话题

Bash
rostopic list
Bash
ros2 topic list

查看当前服务

Bash
rosservice list 
Bash
ros2 service list

如果要显示类型:

Bash
# 显示指定服务的类型
rosservice type /service_name
Bash
1
2
3
4
# 显示指定服务的类型
ros2 service type /service_name
# 显示所有服务的类型
ros2 service list -t

发布话题

Bash
rostopic pub /topic_name topic_type "message"
Bash
ros2 topic pub /topic_name topic_type "message"

限制发送一次:

Bash
rostopic pub -1 /topic_name topic_type "message"
Bash
ros2 topic pub --once /topic_name opic_type 'message'

按固定频率发送消息:

Bash
rostopic pub -r <频率> /topic_name topic_type "message"
Bash
ros2 topic pub --rate <频率> /topic_name topic_type 'message'

示例:

Bash
# yaml格式(可以缺省)
rostopic pub /turtle1/cmd_vel geometry_msgs/Twist "linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0"
# 数组格式(不能缺省)
rostopic pub -r 1 /turtle1/cmd_vel geometry_msgs/Twist -- '[1.0, 0.0, 0.0]' '[0.0, 0.0, 1.0]'
Bash
# JSON格式(可以缺省)
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 1.0}, angular: {z: 0.5}}" --once

输出话题

Bash
rostopic echo /topic_name
Bash
ros2 topic echo /topic_name

调用服务

Bash
# ROS1不需要指定服务类型,会自动推断
rosservice call /service_name "request"
Bash
# ROS2必须显式指定服务类型,否则会报错
ros2 service call /service_name service_type "request"

示例:

Bash
1
2
3
4
rosservice call /spawn "x: 3.0
y: 3.0
theta: 0.0
name: 'turtle2'"
Bash
ros2 service call /spawn turtlesim/srv/Spawn "{x: 3.0, y: 3.0, theta: 0.0, name: 'turtle2'}"

mimic输入输出重映射

在ROS2中,mimic节点的输入输出话题名称和参数传递方式与ROS1不同,例如要对原本的输入输出 topic 做如下修改:

Text Only
/input/pose ----> /turtle1/pose
/output/cmd_vel ----> /turtle2/cmd_vel
ROS1&2的命令分别是:

Text Only
rosrun turtlesim mimic input:=turtle1 output:=turtle2
Bash
ros2 run turtlesim mimic --ros-args --remap input/pose:=turtle1/pose --remap output/cmd_vel:=turtle2/cmd_vel

查看话题消息类型

Bash
rostopic type /topic_name
Bash
ros2 topic type /topic_name

查看消息接口说明

ROS2 将消息(msg)、服务(srv)和动作(action)统一归类为接口,因此相关命令从 rosmsg 改为 ros2 interface

Bash
rosmsg show /msg_name
Bash
ros2 interface show /msg_name

录制运动过程

Bash
rosbag record -a -O bag_name
# 其中`-a`参数表示记录所有话题,`-O`参数指定输出文件名。
Bash
ros2 bag record -a -o bag_name
# 其中`-a`参数表示记录所有话题,`-o`参数指定输出文件名。

查看录制文件信息

Bash
rosbag info bag_name.bag
Bash
ros2 bag info bag_name.bag

播放录制文件

Bash
rosbag play bag_name.bag
Bash
ros2 bag play bag_name.bag

开启RViz可视化界面

Bash
rviz
Bash
rivz2

创建/编译工具包

ROS1采用catkin,ROS2采用colcon。