def talker(self):
rate = rospy.Rate(10)
while not rospy.is_shutdown():
hello_str = "I AM IRON MAN "
self.pub.publish(hello_str[2:4])
self.pub.publish(hello_str[5:9])
self.pub.publish(hello_str[10:13])
rate.sleep()
我尝试了上面的代码,并成功地被订阅者听到。但是当我尝试下面的代码时,订阅者没有收到发布的消息。我想知道为什么上面的代码无效,而下面的代码无效?
def talker(self):
rate = rospy.Rate(10)
hello_str = "I AM IRON MAN "
self.pub.publish(hello_str[2:4])
self.pub.publish(hello_str[5:9])
self.pub.publish(hello_str[10:13])
rate.sleep()
答案 0 :(得分:1)
发布并不需要循环。
此功能仅发送一条消息。
测试:启动您的订阅者,然后启动您的发布者。您应该看到一组数据。
如果不使用无限循环完成脚本。您的节点将完成并关闭。
我们使用睡眠并在无限循环内进行发布,以特定速率发布数据。
希望有帮助!
答案 1 :(得分:0)