zoukankan      html  css  js  c++  java
  • python ros topic demo

    发布者

    #!/usr/bin/env python
    #coding=utf-8
     
    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 = "超哥 好帅啊 %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-8
     
     
    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
     
     

  • 相关阅读:
    【BZOJ 3144】 [Hnoi2013]切糕
    【BZOJ 1095】 [ZJOI2007]Hide 捉迷藏
    【BZOJ 4016】[FJOI2014]最短路径树问题
    【BZOJ 2152】 聪聪可可
    【BZOJ 1095】 [ZJOI2007]Hide 捉迷藏 括号序列
    【BZOJ 3196】 Tyvj 1730 二逼平衡树 分块
    【BZOJ 2038】 [2009国家集训队]小Z的袜子(hose)
    【BZOJ 3196】 Tyvj 1730 二逼平衡树
    接下来的事
    【BZOJ 1189】 [HNOI2007]紧急疏散evacuate
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10278374.html
Copyright © 2011-2022 走看看