发布者

#!/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. html09

    1.Jquery的常用方法1)选择器2)操作节点以下的 obj 都是指 jQuery对象 1.操作样式 obj.css() :不加参数是获取节点的css样式 obj.css({"属性&quo ...

  2. linux命令:文件搜索命令

    ---恢复内容开始--- 文件搜索命令:which 命令名称:which 命令所在路径:/usr/bin/which 执行权限:所有用户 语法:which  [命令名称] 功能描述:显示系统命令所在目 ...

  3. mysql日志详解

    日志分类: 一.错误日志. 1.在配置文件中的配置是:log-error="DESKTOP-igoodful.err",查看参数的键值对:show variables like ' ...

  4. 2:1 Strus2架构

    一: 二: 表示:当以/login或者login.do表示的请求过来,就使用class指定的LoginAction类来处理,处理完了返回一个结果字符串,若果结果字符串是"fail" ...

  5. VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)

    上一节讲了标签控件Tab Control以后,常用控件的内容就全部讲完了,当然并没有包括所有控件,主要是一些很常用很重要的控件.本节开始鸡啄米将为大家讲解菜单的概念及使用. 菜单简介 菜单在界面设计中 ...

  6. UVALive - 7261 Xiongnu's Land

    思路: 先二分下界,再二分上届. #include <bits/stdc++.h> using namespace std; #define MP make_pair #define PB ...

  7. linux常用命令:vmstat 命令

    vmstat 是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存.进程.CPU活动进行监控.他是对系统的整体 情况进行统计,不足之处是无法对某个进程进 ...

  8. Linux基础命令---find

    file 判断指定文件的文件类型,它依据文件内容判断,并不依据扩展名.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法     ...

  9. 解读 Q_D, Q_Q 指针

    见 qglog.h文件定义: #define Q_D(Class) Class##Private * const d = d_func()    #define Q_Q(Class) Class * ...

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

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