【ROS学习-6】了解ROS topics(主题)

1.在上面node的例子基础上再开一个Terminal后运行如下命令:
 rosrun turtlesim turtle_teleop_key
   此时Terminal会有如下提示输出:
  Reading from keyboard
  ---------------------------
  Use arrow keys to move the turtle.
   这时在该Terminal按键盘的方向键,就可以看到node例子里的乌龟在移动。
2.打开一个新的Terminal,运行如下命令就可以看到node之间通过topic通信的关系:
  rosrun rqt_graph rqt_graph

   命令运行后有如下界面:
3.关于rostopic命令
   运行rostopic -h命令可以看到如下帮助信息:
  rostopic is a command-line tool for printing information about ROS Topics.

  Commands:
    rostopic bw display bandwidth used by topic
    rostopic delay display delay of topic from timestamp in header
    rostopic echo print messages to screen
    rostopic find find topics by type
    rostopic hz display publishing rate of topic
    rostopic info print information about active topic
    rostopic list list active topics
    rostopic pub publish data to topic
    rostopic type print topic or field type

    Type rostopic <command> -h for more detailed usage, e.g. 'rostopic echo -h'
4.使用rostopic echo命令查看主题发布的数据
   运行rostopic echo /turtle1/cmd_vel命令,并在可以接收键盘方向键的Terminal操作方向键,可以看到如下返回信息:
  linear:
  x: -2.0
  y: 0.0
  z: 0.0
  angular:
  x: 0.0
  y: 0.0
  z: 0.0
  ---
   相应的刷新rqt_graph界面,更新为如下;
5.使用rostopic list查看当前所有订阅和发布的主题
   运行rostopic list -h可以看到如下帮助信息:
  Usage: rostopic list [/namespace]

  Options:
    -h, --help show this help message and exit
    -b BAGFILE, --bag=BAGFILE
      list topics in .bag file
    -v, --verbose list full details about each topic
    -p list only publishers
    -s list only subscribers
    --host group by host name
   运行rostopic list -v可以看到如下内容:
  Published topics:
  * /turtle1/color_sensor [turtlesim/Color] 1 publisher
  * /turtle1/cmd_vel [geometry_msgs/Twist] 1 publisher
  * /rosout [rosgraph_msgs/Log] 4 publishers
  * /rosout_agg [rosgraph_msgs/Log] 1 publisher
  * /turtle1/pose [turtlesim/Pose] 1 publisher

  Subscribed topics:
  * /turtle1/cmd_vel [geometry_msgs/Twist] 2 subscribers
  * /rosout [rosgraph_msgs/Log] 1 subscriber
  * /statistics [rosgraph_msgs/TopicStatistics] 1 subscriber
6.ROS Messages
   两个通过topic通信的node需要保持相同的消息类型,使用rostopic type可以查看到相应的topic的消息类型,继而通过rosmsg show命令可以查看到该类型的定义:
   运行rostopic type /turtle1/cmd_vel命令会返回如下内容:
  geometry_msgs/Twist
   接下来使用rosmsg show geometry_msgs/Twist命令,可以看到如下返回值:
  geometry_msgs/Vector3 linear
  float64 x
  float64 y
  float64 z
  geometry_msgs/Vector3 angular
  float64 x
  float64 y
  float64 z
7.了解了Topic的类型后继续了解如何通过命令发布Topic,运行rostopic pub -l /turtle1/cmd_vel geometry_msgs/Twist -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.8]'命令,会发布主题为/turtle1/cmd_vel,类型为 geometry_msgs/Twist,参数为'[2.0, 0.0, 0.0]' '[0.0, 0.0, 1.0]'的带参数的主题,其中—是分隔符。
   该命令运行后,会出现如下提示:
  publishing and latching message. Press ctrl-C to terminate
   而相应的界面如下:
8.上面发布的主题只是让乌龟移动了1/4个圆,接下来让乌龟以1Hz的频率不停移动转圈,运行如下命令:
  rostopic pub /turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'
   使用到了-r 1参数,表示执行的频率是1Hz,相应的截图如下:
   此时刷新rqt_graph会有如下界面:
9.接下来使用rostopic hz命令可查看指定Topic的发布频率,如下命令:
  rostopic hz /turtle1/cmd_vel
   此时会看到如下信息不停输出:
  subscribed to [/turtle1/cmd_vel]
  no new messages
  average rate: 1.001
  min: 0.999s max: 0.999s std dev: 0.00000s window: 2
  average rate: 1.000
  min: 0.999s max: 1.000s std dev: 0.00062s window: 3
  average rate: 1.000
  min: 0.999s max: 1.000s std dev: 0.00054s window: 4
10.使用rqt_plot可以滚动刷新发布的主题里的数据根据时移绘制情况,执行 rosrun rqt_plot rqt_plot命令, 有如下界面:
   此时在上面界面里的Topic框里输入/turtle1/pose/x并按回车或按后面的+号,界面会变为如下:
    也可以继续添加其他主题时的数据项变量名,让上面界面里显示更多对比数据。
11.参考网址

评论

此博客中的热门博文

I/O映射之I/O端口

通过Netlink检测网线插拔

使用seq_file