首页 > 编程语言 >使用python写ros publisher和subscriber

使用python写ros publisher和subscriber

时间:2023-08-02 11:12:41浏览次数:40  
标签:__ publisher name python listener rospy ros String

publisher

#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

subscriber

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + "I heard %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.
    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()

参考自ROS官网教程:https://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

标签:__,publisher,name,python,listener,rospy,ros,String
From: https://www.cnblogs.com/langgeligelang/p/17600071.html

相关文章

  • [oeasy]python0078_变量部分总结_variable_summary
    删除变量回忆上次内容上次研究了变量的死有生就有死原本的死是在程序退出的时候自动执行的也可以手动给变量执行死刑del  del(a)之后dir()就无法在当前作用域(scope)内观察到这个变量了也就是说a死了......
  • 如何在 Python 中打印对象的属性
    在Python编程语言中,对象是指由类或类型创建的实例。每个对象都有自己的属性,这些属性可以是变量或函数。通常,我们需要打印对象的属性来了解它的状态。本文将介绍如何在Python中打印对象的属性。1.使用dir()函数dir()函数是Python中的一个内置函数,它可以返回一个对象的所有......
  • [转载] 解决Pycharm中右键运行python程序时出现Run ‘pytest‘ in XXX.py
    1、在Pycharm中右键运行python程序时出现Run'pytest'inXXX.py,这是进入了Pytest模式。2、解决办法进入到File-Seetings-Tools-PythonintegratedTools页面,找到Testing下的Defaulttestrunner,把Pytest设置为Unittests就可以了————————————————原文链接:ht......
  • python云计算简单代码举例
    以下是一个简单的Python代码示例,演示如何使用PythonSDK连接到云计算平台(以AWSS3为例),上传文件到云存储桶:importboto3#创建S3客户端s3=boto3.client('s3')#上传文件到S3存储桶defupload_file_to_s3(bucket_name,file_path,object_name):try:#使用S3客......
  • python脚本练习:
    1.两个Excel表格进行比对数据,一个为源,一个作为补全数据上代码:importpandasaspd#读取两个Excel数据source=pd.read_excel('C:/Users/Administrator/Desktop/source.xlsx')source.head()data=pd.read_excel('C:/Users/Administrator/Desktop/data.xlsx')data.head()#重新对......
  • 10个简单但很有用的Python装饰器
    装饰器(Decorators)是Python中一种强大而灵活的功能,用于修改或增强函数或类的行为。装饰器本质上是一个函数,它接受另一个函数或类作为参数,并返回一个新的函数或类。它们通常用于在不修改原始代码的情况下添加额外的功能或功能。装饰器的语法使用@符号,将装饰器应用于目标函数或......
  • 如何用python语言获得淘宝1688京东商品详情数据API 返回值说明
    前言item_get-获得商品详情搜索商品接口,可以通过关键词请求接口拿到商品列表页面的商品标题,商品价格,商品优惠价,商品视频,商品图片,商品sku属性,商品sku属性描述,发货地,库存,商品销量,店铺优惠券,店铺促销信息等页面上有的数据均可以拿到,以上的数据可以用于行业数据分析,商品搬家业务,商品......
  • Python语法API调试,淘宝1688拼多多商品详情测试接口
    Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发。Python解释器易于扩展,可以使用C语言或C++(或者......
  • Python使用 - 多线程
    常见术语及用法 基本使用#定义线程类classMyThread(threading.Thread):def__init__(self):super(MyThread,self).__init__()#或threading.Thread.__init__(self)defrun(self)->None:tid=threading.currentThread().ident......
  • python ImportError: libGL.so.1: cannot open shared object file: No such file or
    前言python报错pythonImportError:libGL.so.1:cannotopensharedobjectfile:Nosuchfileordirectory这个错误通常表示你的Python程序需要使用OpenGL库,但是该库未安装或未正确配置。解决在LinuxUbuntu系统中,安装OpenGL库:sudoapt-getinstalllibgl1-mesa......