ROS2-Beginner:3-理解话题

发布时间 2023-06-01 16:52:36作者: gary_123

目标:使用rqt_graph以及命令行工具来查看ros2 topic。

背景

ROS2将复杂的系统分解为许多模块化节点。话题是ROS图的一个重要元素,它充当节点交换消息的总线。

一个节点可以发布数据到任意数量的话题,同时可以订阅任意数量的话题。

话题是在节点之间以及系统的不同部分之间移动数据的主要方式之一。

任务

1、运行以下节点

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

2、rqt_graph

通过本教程,我们会使用rqt_graph来可视化变化的节点,话题以及他们之间的连接。

运行之

rqt_graph

You can also open rqt_graph by opening rqt and selecting Plugins > Introspection > Node Graph.

可以从上面看到节点和话题以及两个action。

graph描述了节点turtlesim以及teleop_turtle之间通过话题如何通信。节点/teleop_turtle节点发布数据到话题/turtle1/cmd_vel,节点/turtlesim通过订阅话题来接收数据。

3、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]

4、ros2 topic echo

查看发布在话题上的数据

ros2 topic echo <topic_name>
ros2 topic echo /turtle1/cmd_vel

5、ros2 topic info

话题是一对多,多对一,多对多的通信。

运行如下来查看

ros2 topic info /turtle1/cmd_vel

6、ros2 接口显示

节点使用消息在话题上发送数据。发布者和订阅者必须发送和接收相同类型的数据。

ros2 topic list -t

使用上面的命令可以查看具体话题的类型。

在包geometry_msgs中有一个消息msg称为Twist。

运行下面的命令来查看消息类型的细节

ros2 interface show geometry_msgs/msg/Twist
# This expresses velocity in free space broken into its linear and angular parts.

    Vector3  linear
            float64 x
            float64 y
            float64 z
    Vector3  angular
            float64 x
            float64 y
            float64 z

7、ros2的话题发布

从命令行窗口中发布数据:

ros2 topic pub <topic_name> <msg_type> '<args>'

完整的命令如下,参数需要时YAML的格式

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}}"

上面空格注意;

下面按照1HZ的频率来发布

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}}"

查看当前turtle1的位姿

ros2 topic echo /turtle1/pose

8、ros2 topic hz

查看数据发布的频率

ros2 topic hz /turtle1/pose

9、清理

使用ctrl +c来清理。

总结

节点发布信息到话题上,允许任意数量的其他节点来订阅和访问该信息,本教程学习了使用rqt_graph以及命令行工具来检查节点之间的连接关系。