rospy subscriber - wait for new message on topic

asked 2019-06-07 06:25:41 -0500

Jan Tromp gravatar image

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

edit retag flag offensive close merge delete

Comments

Update: I have made a function that works, but seems there has to be an easier way to achieve this:

self.camera_data = rospy.wait_for_message("/camera/objects_wrt_robot_frame", ObjectList.ObjectArray)
      while self.old_camera_data == self.camera_data:
            print("data is the same as old data!")
            self.camera_data = rospy.wait_for_message("/camera/objects_wrt_robot_frame", 
            ObjectList.ObjectArray)
            rospy.sleep(0.2)

#do robot stuff

self.old_camera_data = self.camera_data
Jan Tromp gravatar image Jan Tromp  ( 2019-06-07 06:27:25 -0500 )edit

Please post this comment under #q324966 so we can close this one, as it's a duplicate of #q324966.

gvdhoorn gravatar image gvdhoorn  ( 2019-06-07 08:43:20 -0500 )edit