Python写ROS 订阅与发布程序

1. 编写talker代码

vim ..../src/talker.py

#!/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)
    #调用publisher函数 创建发布节点 ,定义数据类型,
    #queue_size参数是当其接受者接受不够快造成信号堵塞中队列信息的数量限制
    
    rospy.init_node('talker', anonymous=True)
    #启动节点同时为节点命名, 若anoymous为真则节点会自动补充名字,实际名字以talker_322345等表示
    #若为假,则系统不会补充名字,采用用户命名。但是一次只能有一个同名节点,若后面有一个相同listener
    #名字的节点则后面的节点启动会注销前面的相同节点名。
    
    rate = rospy.Rate(10) # 10hz
    #延时的时间变量赋值,通过rate.sleep()实现延时
    
    while not rospy.is_shutdown():
    # 判定开始方式,循环发送,以服务程序跳出为终止点 一般ctrl+c也可
    
        hello_str = "hello world %s" % rospy.get_time()
        # 数据变量的内容 rospy.get_time() 是指ros系统时间,精确到0.01s 
        # 也可以使用 import time  time.strftime('%Y%m%d%H%M%S')
        
        rospy.loginfo(hello_str)
        #在运行的terminal界面info 出信息,可不加,可随意改
        
        pub.publish(hello_str)
        #发布数据 必须发布
        
        rate.sleep()
        #ros中的延时表示,也可用系统的延时 import time  time.sleep(1) 
        #ros系统中的延时应该比ubuntu自带好好,尽量用ros的
if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

2. 编写listener代码

vim ..../src/listener.py

#!/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)
    #回调函数 收到的参数.data是通信的数据 默认通过这样的 def callback(data) 取出data.data数据
    
def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # node 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)
    
    #启动节点同时为节点命名, 若anoymous为真则节点会自动补充名字,实际名字以 listener_322345等表示
    #若为假,则系统不会补充名字,采用用户命名。但是一次只能有一个同名节点,若后面有一个相同listener
    #名字的节点则后面的节点启动会注销前面的相同节点名。
    
    rospy.Subscriber("chatter", String, callback)
    
    #启动订阅,订阅主题,及标准字符串格式,同时调用回调函数,当有数据时调用函数,取出数据
    
    # spin() simply keeps python from exiting until this node is stopped
    
    #循环程序
    rospy.spin()

if __name__ == '__main__':
    listener()
#函数不被用作模块调用

3. 添加权限

chmod +x ..../src/talker.py |chmod +x .../src/listener.py

4. 编译

catkin_make

5. 运行talker

rosrun exam talker.py

6. 运行listener

rosrun exam listener.py

注释:最简单的订阅与发布程序:

  • 订阅

      #!/usr/bin/env python
      import rospy
      from std_msgs.msg import String
      rospy.init_node('talker',anonymous=0)
      pub=rospy.Publiser('chatter',String)
      rate=rospy.Rate(1)
      while not rospy.is_shutdown():
          rospy.loginfo('telll myself')
          pub.publish('hello')
          rate.sleep()
    
  • 发布
    #!/usr/bin/env python
    import rospy
    from std_msgs.msg import String
    rospy.init_node('listener',anonymous=0)
    lis=rospy.Subscriber('chatter',String,callback)
    rospy.spin()
    def callback(data):
    rospy.loginfo(data.data)



原文地址:https://www.cnblogs.com/sea-stream/p/10278403.html