ROS2常用的命令

pkg

1
2
3
4
5
6
7
8
9
10
# 列出某个包的所有可执行文件
ros2 pkg executables pkg_name
# 列出所有的包
ros2 pkg list

# 某个包所在路径的前缀
ros2 pkg prefix pkg_name

# 查看包对应的 package.xml 文件
ros2 pkg xml pkg_name

node 命令

ros2 node 不识别kill命令,只有infolist

topic

1
2
3
4
5
6
ros2 topic list                # 查看话题列表
ros2 topic info <topic_name> # 查看话题信息
ros2 topic hz <topic_name> # 查看话题发布频率
ros2 topic bw <topic_name> # 查看话题传输带宽
# 发布话题消息
ros2 topic pub <topic_name> <msg_type> <msg_data>
  • ros2 topic info topic_name

现在必须加/

1
2
3
4
5
6
7
user@robot:~$ ros2 topic info imu
Unknown topic 'imu'

user@robot:~$ ros2 topic info /imu
Type: sensor_msgs/msg/Imu
Publisher count: 1
Subscription count: 1

  • 输出话题的详细信息,包括发布订阅者: ros2 topic info /odom -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Type: nav_msgs/msg/Odometry

Publisher count: 1

Node name: turtlebot3_diff_drive
Node namespace: /
Topic type: nav_msgs/msg/Odometry
Endpoint type: PUBLISHER
GID: 01.0f.ea.0f.41.6a.0c.34.01.00.00.00.00.00.a5.03.00.00.00.00.00.00.00.00
QoS profile:
Reliability: RMW_QOS_POLICY_RELIABILITY_RELIABLE
Durability: RMW_QOS_POLICY_DURABILITY_VOLATILE
Lifespan: 2147483651294967295 nanoseconds
Deadline: 2147483651294967295 nanoseconds
Liveliness: RMW_QOS_POLICY_LIVELINESS_AUTOMATIC
Liveliness lease duration: 2147483651294967295 nanoseconds

Subscription count: 1

Node name: my_node
Node namespace: /
Topic type: nav_msgs/msg/Odometry
Endpoint type: SUBSCRIPTION
GID: 01.0f.ea.0f.0d.6a.5d.f0.01.00.00.00.00.00.13.04.00.00.00.00.00.00.00.00
QoS profile:
Reliability: RMW_QOS_POLICY_RELIABILITY_RELIABLE
Durability: RMW_QOS_POLICY_DURABILITY_VOLATILE
Lifespan: 2147483651294967295 nanoseconds
Deadline: 2147483651294967295 nanoseconds
Liveliness: RMW_QOS_POLICY_LIVELINESS_AUTOMATIC
Liveliness lease duration: 2147483651294967295 nanoseconds
  • ros2 interface show msg_name

显示 Topic 发送的消息定义 ros2 interface show sensor_msgs/msg/Imu

  • ros2 topic find msg_name

获知某个消息类型是谁在用

1
2
user@robot:~$ ros2 topic find sensor_msgs/msg/Imu
/imu

  • 手动发消息到话题
1
ros2 topic pub /chatter std_msgs/msg/String 'data:"123"

interface

  • ros2 interface list

分类显示消息、动作、服务的所有类型

  • ros2 interface package pkg_name
1
2
3
4
5
user@robot:~$ ros2 interface package action_msgs
action_msgs/srv/CancelGoal
action_msgs/msg/GoalInfo
action_msgs/msg/GoalStatus
action_msgs/msg/GoalStatusArray

获知某个消息类型是谁在用,可以这样查

1
2
3
4
5
6
7
8
user@robot:~$ ros2 interface packages | grep pendulum_msgs
pendulum_msgs
user@robot:~$ ros2 interface package pendulum_msgs
pendulum_msgs/msg/RttestResults
pendulum_msgs/msg/JointState
pendulum_msgs/msg/JointCommand

user@robot:~$ ros2 topic find pendulum_msgs/msg/RttestResults

  • 显示消息的成员
1
2
user@robot:~$ ros2 interface proto std_msgs/msg/String
"data: ''"

service

  • ros2 service type

可以看到服务的定义。

  • ros2 service find

找出指定类型的所有服务

  • ros2 service find std_srvs/srv/Empty

  • ros2 interface show

显示服务的定义

  • call命令比ROS1复杂了,需要指明service类型: ros2 service call service_name service_type

roslaunch

ros2专用的launch文件,使用python来创建。当然还可以使用XML 和 YAML文件。

进入创建的包的launch目录,直接启动py文件。

action

  • ros2 action list -t
  • ros2 action info /turtle1/rotate_absolute
  • ros2 interface show turtlesim/action/RotateAbsolute.action
1
2
3
4
5
6
7
8
9
10
nvidia@ubuntu:~$ ros2 interface show turtlesim/action/RotateAbsolute.action

# The desired heading in radians
float32 theta
---
# The angular displacement in radians to the starting position
float32 delta
---
# The remaining rotation in radians
float32 remaining

这三项依次是目标,结果,反馈

  • 发送操作目标: ros2 action send_goal <action_name> <action_type> <values>

比如 ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"

1
2
3
4
5
6
7
8
9
10
nvidia@ubuntu:~$ ros2 action send_goal /turtle1/rotate_absolute turtlesim/action/RotateAbsolute "{theta: 1.57}"
Waiting for an action server to become available...
Sending goal:
theta: 1.57
Goal accepted with ID: 5cd8eb561348477abbb532fc816ce792

Result:
delta: 1.5680029392242432

Goal finished with status: SUCCEEDED

当然结果与目标有一定误差

要查看此目标的反馈,请在上次运行的命令中添加 --feedback,会看到连续的打印输出,将继续收到反馈,直到目标完成。

param

ROS2 中没有一个像ROS 1 那样的集中式参数服务器。==ROS 2 的参数是与每个节点绑定的,每个节点都有自己的参数管理系统==。也就是说 节点私有: 参数与节点相关联,而不是全局共享。节点可以声明、获取和更新自己的参数,也可以通过客户端库(SyncParametersClientAsyncParametersClient)访问和修改其他节点的参数,从而实现参数共享。参数可以在节点运行时动态地获取和更新,方便配置和调试.

ROS 2 提供了参数接口,可以用于在节点之间共享参数,实现参数的动态更新和访问。

  • ros2 param list
  • ros2 param describe
1
2
3
4
5
6
7
8
ros2 param describe /turtlesim background_r
Parameter name: background_r
Type: integer
Description: Red channel of the background color
Constraints:
Min value: 0
Max value: 255
Step: 1
  • ros2 param get node_name parameter_name
1
2
ros2 param get /turtlesim background_r
Integer value is: 69
  • ros2 param set (省略)

  • ros2 run + param

  • ros2 run —ros-args —params-file