python ros topic demo
发布者:
- #!/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的更多相关文章
- 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 ...
- RPi 2B python opencv camera demo example
/************************************************************************************** * RPi 2B pyt ...
- ROS手动编写消息发布器和订阅器topic demo(C++)
1.首先创建 package cd ~/catkin_ws/src catkin_create_pkg topic_demo roscpp rospy std_msgs 2. 编写 msg 文件 cd ...
- python ros 回充demo
#!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String i= def talker(): glo ...
- python ros 回充调用demo
#!/usr/bin/env python #coding=utf- import rospy from std_msgs.msg import String def talker(): pub = ...
- pyhanlp python 脚本的demo补充
java demo https://github.com/hankcs/HanLP/tree/master/src/test/java/com/hankcs/demo github python de ...
- python ros 创建节点订阅robot_pose
建立文件夹hello_rospy,再在该目录下建立子目录src,cd到该src目录,运行如下命令创建工作包 catkin_create_pkg beginner_tutorials std_msgs ...
- python ros 订阅robot_pose获取机器人位置
#!/usr/bin/env python import rospy import tf from tf.transformations import * from std_msgs.msg impo ...
- kafka--通过python操作topic
修改 topic 的分区数 shiyanlou:bin/ $ ./kafka-topics.sh --zookeeper localhost:2181 --alter --topic mySendTo ...
随机推荐
- Ultra-QuickSort(poj 2299归并排序)
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=232#problem/A B - Ultra-QuickSort Time Li ...
- 人活着系列之芳姐和芳姐的猪(Floyd)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2929 这个题一方面数据水,另一方面就是思维水, ...
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
- Jquery map()
<!DOCTYPE html> <html> <head> <style>p { color:red; }</style> <scri ...
- 批量生成反色图片,用PHOTOSHOP批处理功能。
http://zhidao.baidu.com/link?url=Iz46PDPnEITummTEwo2GtUrK6AeAjlidJ7HtCPJ6NYZJbbllRwNg2iBAcNwF2TYjccP ...
- Postman使用js获取日期
在用postman进行接口自动化测试的时候,某个查询接口需要使用到日期参数进行请求: 假设当前日期为2018-05-07 10:30:20 ,需要传的日期为: beginTime:2018-05-01 ...
- js绘制圆形时钟
纯js制作圆形时钟 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- c# 日期函数[string.Format----GetDateTimeFormats]格式
DateTime dt = DateTime.Now;Label1.Text = dt.ToString();//2005-11-5 13:21:25Label2.Text = dt.ToFileTi ...
- OVS中的key解析
OVS在处理每条流的时候,先根据每条流生产相应的key,然后根据key匹配相应的流表,根据流表中的action操作来处理每条流,本文对key的结构体进行分析,看看对于一条流会提出那些特征信息.对于ke ...
- python进程同步,condition例子
#coding=utf-8import multiprocessing as mpimport time def consumer(cond): with cond: print ...