rospy subscriber - wait for new message on topic
Hi all, For my project I'm working with a camera that calculates the position (with relation to a reference frame) of an object and publishes this on the topic camera/object.
I want to make a node that subscribes to this topic and uses the data to move a robot. I want to make this all in an actionlib server.
To make the camera perform its object recognition i have to publish on another topic (camera/ext_commands).
I want my node to publish a command on the camera/ext_commands topic, then wait for the camera calculates the position and publishes it on the camera/object topic.
I have 2 options for doing this but i seem to run in the same problem. Option 1:
self.cam_pub.publish("e_look_for_object")
self.cam_sub = rospy.Subscriber("/camera/objects_wrt_robot_frame", ObjectList.ObjectArray, self.callback)
def callback(data):
#do robot stuff with the data
Option 2:
self.cam_pub.publish("e_look_for_object")
self.pickit_data = rospy.wait_for_message("/camera/objects_wrt_robot_frame", ObjectList.ObjectArray)
# do robot stuff with the data
My problem: When i run the program, the wait_for_message or the subscriber function both return the previous data, i.e. the position of the objects from last scan.
Question: Is there a standard way to tell the node to wait until a new message is published on the topic and then read out this data?
Thanks in advance!
Kind regards, Jan Tromp
Update: I have made a function that works, but seems there has to be an easier way to achieve this:
Please post this comment under #q324966 so we can close this one, as it's a duplicate of #q324966.