发布者

  1. #!/usr/bin/env python
  2. #coding=utf-
  3.  
  4. import rospy
  5. from std_msgs.msg import String
  6.  
  7. def talker():
  8.     pub = rospy.Publisher('chatter',String, queue_size=)
  9.     rospy.init_node('talker',anonymous=True)
  10.     rate = rospy.Rate() # 10hz
  11.     while not rospy.is_shutdown():
  12.         hello_str = "超哥 好帅啊 %s" % rospy.get_time()
  13.         rospy.loginfo(hello_str)
  14.         pub.publish(hello_str)
  15.         rate.sleep()
  16.  
  17. if __name__ == '__main__':
  18.     try:
  19.         talker()
  20.     except rospy.ROSInterruptException:
  21.         pass

  • from std_msgs.msg import String

  • 分析:

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

  1. String(data=str)

订阅者:

  1. #!/usr/bin/env python
  2. #coding=utf-
  3.  
  4. import rospy
  5. from std_msgs.msg import String
  6.  
  7. def callback(data):
  8.     rospy.loginfo(rospy.get_caller_id() + '我觉得 %s', data.data)
  9.  
  10. def listener():
  11.  
  12.     # In ROS, nodes are uniquely named. If two nodes with the same
  13.     # name are launched, the previous one is kicked off. The
  14.     # anonymous=True flag means that rospy will choose a unique
  15.     # name for our 'listener' node so that multiple listeners can
  16.     # run simultaneously.
  17. #对上面注释翻译
  18. #在ROS中,节点是唯一命名的。 如果两个节点相同
  19.     #名称被启动,前一个被启动。该
  20.     anonymous = True标志意味着rospy会选择一个独特的
  21.     #我们的'侦听器'节点的名称,以便多个侦听器可以
  22.     #同时运行。
  23.      rospy.init_node('listener', anonymous=True)
  24.  
  25.     rospy.Subscriber('chatter', String, callback)
  26.  
  27.     # spin() simply keeps python from exiting until this node is stopped
  28.     rospy.spin()
  29.  
  30. if __name__ == '__main__':
  31.     listener()

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

输出为:

  1. [INFO] [WallTime: 1526964838.601590] /listener_1299_1526964825697好帅啊 1526964838.6
  2. [INFO] [WallTime: 1526964838.701610] /listener_1299_1526964825697好帅啊 1526964838.7
  3. [INFO] [WallTime: 1526964838.801621] /listener_1299_1526964825697好帅啊 1526964838.8
  4. [INFO] [WallTime: 1526964838.901650] /listener_1299_1526964825697好帅啊 1526964838.9
  5. [INFO] [WallTime: 1526964839.001606] /listener_1299_1526964825697好帅啊 1526964839.0
  6. [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. Ultra-QuickSort(poj 2299归并排序)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=232#problem/A B - Ultra-QuickSort Time Li ...

  2. 人活着系列之芳姐和芳姐的猪(Floyd)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2929 这个题一方面数据水,另一方面就是思维水, ...

  3. PAT 1068 Find More Coins[dp][难]

    1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...

  4. Jquery map()

    <!DOCTYPE html> <html> <head> <style>p { color:red; }</style> <scri ...

  5. 批量生成反色图片,用PHOTOSHOP批处理功能。

    http://zhidao.baidu.com/link?url=Iz46PDPnEITummTEwo2GtUrK6AeAjlidJ7HtCPJ6NYZJbbllRwNg2iBAcNwF2TYjccP ...

  6. Postman使用js获取日期

    在用postman进行接口自动化测试的时候,某个查询接口需要使用到日期参数进行请求: 假设当前日期为2018-05-07 10:30:20 ,需要传的日期为: beginTime:2018-05-01 ...

  7. js绘制圆形时钟

    纯js制作圆形时钟 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  8. c# 日期函数[string.Format----GetDateTimeFormats]格式

    DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Text = dt.ToFileTi ...

  9. OVS中的key解析

    OVS在处理每条流的时候,先根据每条流生产相应的key,然后根据key匹配相应的流表,根据流表中的action操作来处理每条流,本文对key的结构体进行分析,看看对于一条流会提出那些特征信息.对于ke ...

  10. python进程同步,condition例子

    #coding=utf-8import multiprocessing as mpimport time def consumer(cond):    with cond:        print ...