ROS2入门教程-topics简介
ROS2入门教程-topics(话题) 简介
说明:
- 介绍ros2 topic(话题)
概念:
- ros2 topic是节点和节点之间进行通讯的桥梁
- 节点可以同时发布和接收话题,节点和话题之间是多对多关系。
rqt_graph命令:
- 新开终端,启动turtlesim
ros2 run turtlesim turtlesim_node
- 新开终端,启动键盘控制
ros2 run turtlesim turtle_teleop_key
- rqt_graph显示话题订阅关系
- 新开终端,执行
rqt_graph
- 上图显示节点turtlesim和节点teleop_turtle是如何通过话题/turtle1/cmd_vel来进行通讯
ros2 topic list命令:
- 列出所有的话题
$ ros2 topic list
/parameter_events
/rosout
/turtle1/cmd_vel
/turtle1/color_sensor
/turtle1/pose
- 列出所有的话题,并显示相应的消息类型
$ ros2 topic list -t
/parameter_events [rcl_interfaces/msg/ParameterEvent]
/rosout [rcl_interfaces/msg/Log]
/turtle1/cmd_vel [geometry_msgs/msg/Twist]
/turtle1/color_sensor [turtlesim/msg/Color]
/turtle1/pose [turtlesim/msg/Pose]
ros2 topic echo命令:
显示话题内容:
ros2 topic echo <topic_name>
例如:
$ ros2 topic echo /turtle1/cmd_vel
linear:
x: 2.0
y: 0.0
z: 0.0
angular:
x: 0.0
y: 0.0
z: 0.0
---
ros2 topic info命令:
- 显示话题相关信息,类型,发布和订阅次数
$ ros2 topic info /turtle1/cmd_vel
Type: geometry_msgs/msg/Twist
Publisher count: 1
Subscriber count: 2
ros2 interface show命令
- 通过ros2 topic list -t来获取话题类型
$ ros2 topic list -t
/parameter_events [rcl_interfaces/msg/ParameterEvent]
/rosout [rcl_interfaces/msg/Log]
/turtle1/cmd_vel [geometry_msgs/msg/Twist]
/turtle1/color_sensor [turtlesim/msg/Color]
/turtle1/pose [turtlesim/msg/Pose]
- ros2 interface show显示消息接口内容
$ ros2 interface show geometry_msgs/msg/Twist
# This expresses velocity in free space broken into its linear and angular parts.
Vector3 linear
Vector3 angular
ros2 topic pub命令:
- 命令格式:
ros2 topic pub <topic_name> <msg_type> '<args>'
- 例如发布速度命令
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}}"
- 效果如下:
- 按一定频率发布
ros2 topic pub --rate 1 /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}}"
- 效果如下:
ros2 topic hz命令
- 显示话题的发布频率:
$ ros2 topic hz /turtle1/pose
average rate: 62.460
min: 0.015s max: 0.017s std dev: 0.00036s window: 64
average rate: 62.493
min: 0.015s max: 0.017s std dev: 0.00036s window: 127
average rate: 62.504
min: 0.015s max: 0.017s std dev: 0.00037s window: 190
average rate: 62.502
min: 0.014s max: 0.017s std dev: 0.00037s window: 253
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号