rospy subscriber - wait for new data, then read the data

asked 2019-06-07 06:19:08 -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

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. [..] 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?

so taking a step back: the standard communication pattern for your type of interactions is not topics (ie: asynchronous communication), but either services or actions.

It's certainly possible to use topics here (as actions are essentially just a nr of topics used in a similar way), but you may want to revisit your current design to base it on a pattern that is more suited for it.

gvdhoorn gravatar imagegvdhoorn ( 2019-06-07 06:22:49 -0500 )edit

I am implementing this in an action, but in the execute_callback I need to read the message from the camera, since that's where my data is.

Jan Tromp gravatar imageJan Tromp ( 2019-06-07 06:32:21 -0500 )edit

I was referring to the interaction with the camera. Not your own node.

Topics are not typically used for communication in which it's crucial to receive each and every message, or for interactions that depend on particular messages to be processed in a particular order. Primarily because there is no guarantee that will ever happen.

And I believe you're experiencing that first hand.

gvdhoorn gravatar imagegvdhoorn ( 2019-06-07 08:42:53 -0500 )edit