发布者

#!/usr/bin/env python
#coding=utf- import rospy
from std_msgs.msg import String def talker():
    pub = rospy.Publisher('chatter',String, queue_size=)
    rospy.init_node('talker',anonymous=True)
    rate = rospy.Rate() # 10hz
    while not rospy.is_shutdown():
        hello_str = "超哥 好帅啊 %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep() if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

  • from std_msgs.msg import String

  • 分析:

    • 导入python的标准字符处理库
    • String是一个函数,可以另外方式赋值
msg = String()
msg.data = str

String(data=str)

订阅者:

#!/usr/bin/env python
#coding=utf- import rospy
from std_msgs.msg import String def callback(data):
    rospy.loginfo(rospy.get_caller_id() + '我觉得 %s', data.data) def listener():     # In ROS, nodes are uniquely named. If two nodes with the same
    # name 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.
#对上面注释翻译
#在ROS中,节点是唯一命名的。 如果两个节点相同
    #名称被启动,前一个被启动。该
    #anonymous = True标志意味着rospy会选择一个独特的
    #我们的'侦听器'节点的名称,以便多个侦听器可以
    #同时运行。
     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()

先执行发布者,再执行订阅者(python xxx.py)

输出为:

[INFO] [WallTime: 1526964838.601590] /listener_1299_1526964825697好帅啊 1526964838.6
[INFO] [WallTime: 1526964838.701610] /listener_1299_1526964825697好帅啊 1526964838.7
[INFO] [WallTime: 1526964838.801621] /listener_1299_1526964825697好帅啊 1526964838.8
[INFO] [WallTime: 1526964838.901650] /listener_1299_1526964825697好帅啊 1526964838.9
[INFO] [WallTime: 1526964839.001606] /listener_1299_1526964825697好帅啊 1526964839.0
[INFO] [WallTime: 1526964839.101618] /listener_1299_1526964825697好帅啊 1526964839.1

python ros topic demo的更多相关文章

  1. ROS 进阶学习笔记(13) - Combine Subscriber and Publisher in Python, ROS

    Combine Subscriber and Publisher in Python, ROS This article will describe an example of Combining S ...

  2. RPi 2B python opencv camera demo example

    /************************************************************************************** * RPi 2B pyt ...

  3. ROS手动编写消息发布器和订阅器topic demo(C++)

    1.首先创建 package cd ~/catkin_ws/src catkin_create_pkg topic_demo roscpp rospy std_msgs 2. 编写 msg 文件 cd ...

  4. python ros 回充demo

    #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String i= def talker(): glo ...

  5. python ros 回充调用demo

    #!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker(): pub = ...

  6. pyhanlp python 脚本的demo补充

    java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...

  7. python ros 创建节点订阅robot_pose

    建立文件夹hello_rospy,再在该目录下建立子目录src,cd到该src目录,运行如下命令创建工作包 catkin_create_pkg beginner_tutorials std_msgs ...

  8. python ros 订阅robot_pose获取机器人位置

    #!/usr/bin/env python import rospy import tf from tf.transformations import * from std_msgs.msg impo ...

  9. kafka--通过python操作topic

    修改 topic 的分区数 shiyanlou:bin/ $ ./kafka-topics.sh --zookeeper localhost:2181 --alter --topic mySendTo ...

随机推荐

  1. [py]python面向对象的str getattr特殊方法

    本文旨在说清楚 类中的 def init def str def getattr 这三个方法怎么用的. 定制输入实例名时输出内容 def __str__会定制输出实例名时候的输出 class Chai ...

  2. 置换检验(Permutation Test)学习[转载]

    转自:https://www.cnblogs.com/bnuvincent/p/6813785.html http://www.bioinfo-scrounger.com/archives/564 1 ...

  3. &nbsp

    这个东西在 html 里是空格占位符,普通的空格在 html 里如果连续的多个可能被认为只有一个,而这个东西你写几个就能占几个空格位.

  4. c#实现图片二值化例子(黑白效果)

    C#将图片2值化示例代码,原图及二值化后的图片如下: 原图: 二值化后的图像: 实现代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 ...

  5. Leetcode: Pow(x, n) and Summary: 负数补码总结

    Implement pow(x, n). Analysis:  Time Complexity: O(LogN) Iterative code: refer to https://discuss.le ...

  6. 你真的了解[super ]关键字吗?

    前言 此篇文章是看了阮老师的es6教程,看到super关键字的时候觉得有必要总结梳理一下,原文还是参考 ECMAScript 6入门. 正文 super 这个关键字,既可以当作函数使用,也可以当作对象 ...

  7. php7安装memchced扩展

    tar -zxvf memcached-3.0.3.tgz cd memcached-3.0.3 phpize yum install libmemcached libmemcached-dev ./ ...

  8. JSON—序列化

    表单数据的序列化   用SerializeArray()将有效控件序列化为JSON对象数组? 包含name和value两个属性 SerializeArray()检测一组表单元素中的有效控件? 1.没有 ...

  9. tomcat 的最大连接数设置

    前提说明为了确保服务不会被过多的http长连接压垮,我们需要对tomcat设定个最大连接数,超过这个连接数的请求会拒绝,让其负载到其它机器.达到保护自己的同时起到连接数负载均衡的作用. 动手去做一开始 ...

  10. CPU负载过高异常排查实践与总结

    昨天下午突然收到运维邮件报警,显示数据平台服务器cpu利用率达到了98.94%,而且最近一段时间一直持续在70%以上,看起来像是硬件资源到瓶颈需要扩容了,但仔细思考就会发现咱们的业务系统并不是一个高并 ...