0

我在一个项目中使用 rospy,但是我不完全理解获取消息的工作原理。我有一架无人机每秒发送一条特定消息,但是当我尝试获取该消息时,程序卡住了(从不打印“a”)。我究竟做错了什么?

while(continue):
        ponto_atual = rospy.wait_for_message('/uav1/control_manager/position_cmd',PositionCommand)
        print("a")
        continuar = comparar(ponto_desejado, ponto_atual)
4

1 回答 1

-1

The way you should handle receiving multiple messages on a topic is via a subscription object. The purpose of wait_for_message it to grab a single message.

In rospy it's very easy to setup. For example a callback for a Bool topic looks like: rospy.Subscriber('/your_topic_name',Bool,callback_function)

Now every time a message is sent on /your_topic_name the function callback_function will be called. A more complete example would look something like this:

def callback_function(msg):
    ponto_atual = msg.data
    rospy.loginfo("I received a message!")
    #Anything else you may want to do here

def run_loop():
    rate = rospy.Rate(10.0) #Run at 10Hz
    while not rospy.is_shutdown():
        self.rate.sleep()
        #Other periodic work you may need to do

if __name__ == '__main__':
    rospy.init_node('my_node')
    rospy.Subscriber('/your_topic_name',Bool,callback_function)
    run_loop()
于 2021-08-10T14:00:34.523 回答