ros_tutorials
  • 简介
  • 1 安装并配置ROS环境
    • 1.1 安装ROS
    • 1.2 管理环境变量
    • 1.3 创建ROS工作空间
  • 2 ROS文件系统介绍
    • 2.1 快速了解文件系统概念
    • 2.2 文件系统工具
    • 2.3 回顾
  • 3 创建ROS程序包(Packages)
    • 3.1 catkin程序包由什么组成?
    • 3.2 在catkin工作空间中的程序包
    • 3.3 创建一个catkin程序包
    • 3.4 编译程序包
    • 3.5 程序包依赖关系
    • 3.6 自定义你的程序包
  • 4 编译ROS程序包(packages)
    • 4.1 使用 catkin_make
    • 4.2 编译你的程序包Package
  • 5 理解ROS节点(nodes)
    • 5.1 图(Graph)概念
    • 5.2 节点(Nodes)
    • 5.3 客户端库(Client Libraries)
    • 5.4 roscore
    • 5.5 使用rosnode
    • 5.6 使用rosrun
    • 5.7 回顾
  • 6 理解ROS话题(topics)
    • 6.1 开始
    • 6.2 ros topics
    • 6.3 ROS Messages
    • 6.4 继续学习 rostopic
    • 6.5 使用 rqt_plot
  • 7 理解ROS服务和参数
    • 7.1 ROS Services
    • 7.2 使用rosservice
    • 7.3 使用 rosparam
  • 8 使用 rqt_console 和 roslaunch
    • 8.1 预先安装rqt和turtlesim程序包
    • 8.2 使用rqt_console和rqt_logger_level
  • 9 使用rosed编辑ROS中的文件
    • 9.1 使用 rosed
    • 9.2 使用Tab键补全文件名
    • 9.3编辑器
  • 10 创建ROS消息和ROS服务
    • 10.1 消息(msg)和服务(srv)介绍
    • 10.2 使用 msg
    • 10.3 使用 srv
    • 10.4 msg和srv都需要的步骤
    • 10.5 获得帮助
    • 10.6 回顾
  • 11 编写简单的Publisher和Subscriber(C++)
    • 11.1 编写Publisher Node
    • 11.2 编写Subscriber Node
    • 11.3 编译nodes
  • 12 编写简单的Publisher和Subscriber(Python)
    • 12.1 编写Publisher Node
    • 12.2 编写Subscriber Node
    • 12.3 编译nodes
  • 13 测试消息Publisher和Subscriber
    • 13.1 启动发布器(Publisher)
    • 13.2 启动订阅器(Subscriber)
  • 14 编写简单的Service和Client (C++)
    • 14.1 编写Service节点
    • 14.2 编写Client节点
    • 14.3 编译nodes
  • 15 编写简单的Service和Client (Python)
    • 15.1 编写Service Node
    • 15.2 编写Client节点
    • 15.3 编译nodes
    • 15.4 试试看
  • 16 测试简单的Service和Client
    • 16.1 运行Service
    • 16.2 运行Client
    • 16.3 关于Service和Client节点的更多例子
  • 17 录制与回放数据
    • 17.1 录制数据(通过创建一个bag文件)
    • 17.2 检查并回放bag文件
    • 17.3 录制数据子集
    • 17.4 rosbag record/play 命令的局限性
  • 18 roswtf入门
    • 18.1 安装检查
    • 18.2 运行时检查(在有ROS节点运行时)
    • 18.3 错误报告
  • 19 探索ROS维基
    • 19.1 基础
    • 19.2 高级
  • 20 接下来做什么
    • 20.1 使用模拟器
    • 20.2 使用 RViz
    • 20.3 理解 TF
    • 20.4 更进一步
Powered by GitBook
On this page
  • 12.2.1 The Code
  • 12.2.2 The Code Explained
  1. 12 编写简单的Publisher和Subscriber(Python)

12.2 编写Subscriber Node

12.2.1 The Code

将listener.py文件下载到脚本目录中

$ roscd beginner_tutorials/scripts/
$ wget https://raw.github.com/ros/ros_tutorials/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py

文件内容看起来为:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # node are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber("chatter", String, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

不要忘记给该节点增加执行权限

$ chmod +x listener.py

12.2.2 The Code Explained

listener.py的代码与talker.py类似,只是我们引入了一个新的基于回调的订阅消息机制。

rospy.init_node('listener', anonymous=True)

rospy.Subscriber("chatter", String, callback)

# spin() simply keeps python from exiting until this node is stopped
rospy.spin()

这声明你的节点订阅了类型为std_msgs.msgs.String的chatter话题。当收到新消息时,回调将作为第一个参数与消息一起调用。

我们也改变了对rospy.init_node()的调用。我们添加了anonymous = True关键字参数。 ROS要求每个节点都有唯一的名称。如果一个名字相同的节点出现,它会颠覆之前的节点。这样可以很容易地将故障节点从网络中踢出。 anonymous = True标志告诉rospy为节点生成一个唯一的名称,以便您可以轻松地运行多个listener.py节点。

最后加上,rospy.spin()只是让你的节点退出,直到节点关闭。与roscpp不同,rospy.spin()不影响订阅者回调函数,因为这些函数有自己的线程。

Previous12.1 编写Publisher NodeNext12.3 编译nodes

Last updated 6 years ago