为什么ROS Publisher无法无循环运行?

时间:2019-05-07 03:34:05

标签: python-3.x ros

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()

2 个答案:

答案 0 :(得分:1)

发布并不需要循环。

此功能仅发送一条消息。

测试:启动您的订阅者,然后启动您的发布者。您应该看到一组数据。


如果不使用无限循环完成脚本。您的节点将完成并关闭。

我们使用睡眠并在无限循环内进行发布,以特定速率发布数据。

希望有帮助!

答案 1 :(得分:0)

真的不需要循环。

您可以调用spinOnce并使其继续运行。

然后在每个订阅者回叫时,发布订阅者内容。

无论何时有回叫,都会激活一些回叫。这样发布商也可以工作。

这也将起作用。请注意,对于时间相关的控制信号或运行中的PiD或非线性控制器,最好将其放置在时间循环中。

这些是修复时间池和中断服务路由的典型示例。

有关详细信息,您可以尝试查找一些中文中学教科书。全部都在那里

enter image description here