发布者

#!/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. 循环结构 while,do while

    while:先判断条件表达式是否成立,成立则执行循环体,不成立则不执行. 格式:while(条件表达式){ 执行语句(控制循环次数): } 例如: int x=1; while(x<3/*条件表 ...

  2. [LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  3. [LeetCode] 394. Decode String_Medium tag: stack 666

    Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where ...

  4. [LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS

    Given a string S and a character C, return an array of integers representing the shortest distance f ...

  5. python处理图片验证码

    WebDriver中实现对特定的Web区域截图方法 import pytesseract from PIL import Image image=Image.open('new.jpg') vcode ...

  6. 数据结构线性表的动态分配顺序存储结构算法c语言具体实现和算法时间复杂度分析

    #include<stdio.h>#include<stdlib.h>//线性表的动态分配顺序存储结构#define LIST_INIT_SIZE 100//线性表存储空间的初 ...

  7. http 同步异步请求

    在用户交互模式下,当你改变表单中某个组件的值时, 譬如你填写名字.修改性别.选择爱好的时候,浏览器和服 务器至今没有发生任何交互,只有当你点击submit的时候, 浏览器才会把你的参数,也就是form ...

  8. Summary: Stack Overflow Error

    What is a stack overflow error? Parameters and local variables are allocated on the stack (with refe ...

  9. Object-C-Foundation-NSDate

    NSDate 表达日期表达时间的方法 NSDate *now=[NSDate date]; 获得当前日期 NSDate *tomrrow=[now dateByAddingTimeInterval:2 ...

  10. 安装SQL2008时显示:RebootRequiredCheck 检查是否需要挂起计算机重新启动。

    解决方法: a.在开始->运行中输入regeditb.删除HKEY_CURRENT_USER\Software\Microsoft\Microsoft SQL Serverc.删除HKEY_LO ...