rospy基础--订阅与发布

本文介绍了在ROS中使用rospy进行订阅与发布的基础操作。首先通过示例展示了如何创建一个发布者节点和订阅者节点,解释了`rospy.Publisher`和`rospy.init_node`等关键函数的作用。在遇到`Unable to immediately register with master node`错误时,通过启动`roscore`解决。接着分析了`talker`和`listener`代码,包括`callback`函数的订阅机制。最后提到了`rospy.Subscriber`的订阅功能和`rospy.spin()`保持节点运行的重要性。
摘要由CSDN通过智能技术生成

1.按照roswiki上的教程,rospy/Tutorials,我们先把代码粘贴过来运行一下发布者和订阅者的代码

talker代码:

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

运行代码会出现标红

[ERROR] [1585751789.057178]: Unable to immediately register with master node [http://localhost:11311]: master may not be running yet. Will keep trying.

熟悉的11311,是提示我们没有开大网管roscore,我们新开个终端roscore一下,程序得以顺利运行

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值