文章目录

 
    • ① 背景
    • ② 前提
    • ③ 任务
      • Ⅰ 准备
      • Ⅱ rqt_graph
      • Ⅲ ros2 topic 命令
        • ros2 topic list
        • ros2 topic echo
        • ros2 topic info
        • ros2 msg show (eloquent版本 ros2 interface show)
          • dashing 版本
          • eloquent 版本
          • 两个版本都可以
        • ros2 topic pub
        • ros2 topic hz
    • ④ 总结
 

① 背景

  ROS 2将复杂的系统分解为许多模块化 节点(node)。 主题(topic)是ROS图的重要元素,它充当节点(node)交换消息的总线。 节点(node)可以将数据发布到任意数量的主题(topic),并同时具有对任意数量的主题(topic)的订阅。 主题(topic)是数据在节点(node)之间以及因此在系统的不同部分之间移动的重要方式之一。 说面了就是通过topic来通讯,通过node 来发现互相  

② 前提

 
  • 装ros2
  • 配置环境
  • 装小乌龟
 

③ 任务

 

Ⅰ 准备

  启动小乌龟 命令:  
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
 

Ⅱ rqt_graph

  这个是个可是化的工具,命令:  
rqt_graph
  不像ros1 还要rosrun   微信图片_20210104120520   这个是个自检工具  

Ⅲ ros2 topic 命令

 

ros2 topic list

  ros2 topic list 列出所有topic ros2 topic list -t列出topic 顺便带上了消息类型,这个好 下面是帮助文档,  
usage: ros2 topic list [-h] [--spin-time SPIN_TIME] [-t] [-c]
                       [--include-hidden-topics]

Output a list of available topics

optional arguments:
  -h, --help            show this help message and exit
  --spin-time SPIN_TIME
                        Spin time in seconds to wait for discovery (only
                        applies when not using an already running daemon)
  -t, --show-types      Additionally show the topic type
  -c, --count-topics    Only display the number of topics discovered
  --include-hidden-topics
                        Consider hidden topics as well
  微信图片_20210104120543   发现两个隐藏的,rqt_graph 去掉hide的勾选也能看到   微信图片_20210104120601  

ros2 topic echo

  可以查看对应topic上 publish 的数据 命令:  
ros2 topic echo <topic_name>
  小乌龟的例子:  
ros2 topic echo /turtle1/cmd_vel
  这样会直观点   微信图片_20210104120813  

ros2 topic info

  看topic的详细信息,命令:ros2 topic info /turtle1/cmd_vel   微信图片_20210104120855  

ros2 msg show (eloquent版本 ros2 interface show)

  用 ros2 topic list -t看到了cmd_vel 的消息类型 是 geometry_msgs/msg/Twist这个  
dashing 版本
 
  • 我们可以用 ros2 msg show geometry_msgs/msg/Twist
  微信图片_20210104120911  
eloquent 版本
 
  • 我们可以用 ros2 interface show geometry_msgs/msg/Twist.msg
  微信图片_20210104120928  
两个版本都可以
 
  • 也可以通过 ros2 topic echo /turtle1/cmd_vel 来看
  微信图片_20210104120956   可以通过rqt来看   微信图片_20210104121026   也可以去官网查http://docs.ros.org/api/geometry_msgs/html/msg/Twist.html   微信图片_20210104121049  
  • ide 配置好之后可以跳到头文件去看也行
  • 去msg文件看也行 方法有点多
 

ros2 topic pub

  可以通过命令行去发送消息  
ros2 topic pub <topic_name> <msg_type> '<args>'
  我们通过命令下发去控制小乌龟  
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}}'
  选项 --rate 1 或者不加 是以1HZ的速度publish,--once 是只发一次   微信图片_20210104121126  

ros2 topic hz

  实时统计topic的消息频率  
ros2 topic hz topic_name
  微信图片_20210104121153  

④ 总结

这个和ros1 区别不大