ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

subscribing and publishing

asked 2012-07-03 12:12:52 -0500

Rydel gravatar image

updated 2012-07-03 12:16:46 -0500

I have an application where 2 nodes are talking to each other over a certain topic, based on where the nodes are at in the program flow they either don't care about the topic or they do. My question is something that the tutorials didn't help with, is there a command that can check a topic on demand? or check a specific piece of a topic on demand?


works great for publishing on demand, is there something similar for subscribing?

edit retag flag offensive close merge delete



I am not sure but you may keep the track of subscribed topic by checking out the last time you received the data. For example if it was 10 seconds ago , then you would think that a problem might happen to the publishing node ? or you may use diagnostic_msgs to understand what is happening

cagatay gravatar image cagatay  ( 2012-07-03 12:17:54 -0500 )edit

yea your comment doesn't really help me. I guess what I'm asking is when a node is listening to a topic how would i have that node extract the most recent data from that topic and use it?

Rydel gravatar image Rydel  ( 2012-07-03 12:23:36 -0500 )edit

You could just always go to your callback function when you receive a message, which will be the most recent one, and have a class variable you check at the beginning of the callback function to see if you should do anything with that data. Elsewhere in your code you can set the class variable.

Thomas D gravatar image Thomas D  ( 2012-07-03 12:45:50 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2012-07-03 16:19:00 -0500

joq gravatar image

As Thomas suggested: run your callback all the time, and ignore messages (or parts of messages) you don't care about.

edit flag offensive delete link more


For anyone else who may see this in the future, I found ros::spinOnce was what I was looking for but be careful if your message isn't being published very often.

Rydel gravatar image Rydel  ( 2012-07-06 07:04:21 -0500 )edit

You should either run ros::spin() or call spinOnce() periodically at some appropriate rate.

joq gravatar image joq  ( 2012-07-06 07:08:28 -0500 )edit

right, or if your looking for a particular piece of the message to change like a status bit or something, put ros::spinOnce() in a while loop having the status bit break the loop

Rydel gravatar image Rydel  ( 2012-07-06 07:22:33 -0500 )edit

answered 2012-08-15 04:51:28 -0500

sergey_alexandrov gravatar image

Some publishers are written the way that if there are no subscribers, then they do not publish and also avoid unneeded processing. To my knowledge, Kinect node works this way. It does not compute point clouds from the depth images coming from the sensor unless there are subscribers on the "points" topic. Therefore if you just need to get a point cloud once in a while, subscribing and ignoring messages will incur a lot of superfluous computations.

What may be useful in such cases is ros::topic::waitForMessage(). It waits for a single message to arrive on a topic, possibly with a timeout.

edit flag offensive delete link more

Question Tools


Asked: 2012-07-03 12:12:52 -0500

Seen: 1,605 times

Last updated: Aug 15 '12