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

Revision history [back]

click to hide/show revision 1
initial version

Does that mean, that a subscriber in ROS HAS to CONTINUOUSLY listen to the topic being published? What if I don't want to? What if I want to listen to a topic say, only when 'flag' is set to 1?

In short: no, you don't have to, you can (re)subscribe and unsubscribe at any time. Subscription you know (NodeHandle::subscribe(..)), to stop a subscription, use NodeHandle::shutdown(). Now subscribe on flag == 1, then shutdown on flag == 0. Keep in mind that it can take some time for your subscription to be processed (as was already mentioned by @Lorenz in your other question).

Does that mean, that a subscriber in ROS HAS to CONTINUOUSLY listen to the topic being published? What if I don't want to? What if I want to listen to a topic say, only when 'flag' is set to 1?

In short: no, you don't have to, you can (re)subscribe and unsubscribe at any time. Subscription you know (NodeHandle::subscribe(..)), to stop a subscription, use NodeHandle::shutdown(). Now subscribe on flag == 1, then shutdown on flag == 0. Keep in mind that it can take some time for your subscription to be processed (as was already mentioned by @Lorenz in your other question).

Instead of (re|un)subscribing, you could also check for flag == 1 in your callback, and only write to your file if the condition is true.

Does that mean, that a subscriber in ROS HAS to CONTINUOUSLY listen to the topic being published? What if I don't want to? What if I want to listen to a topic say, only when 'flag' is set to 1?

In short: no, you don't have to, you can (re)subscribe and unsubscribe at any time. Subscription you know (NodeHandle::subscribe(..)), to stop a subscription, use NodeHandleNodeHandle::shutdown()Subscriber::shutdown(). Now subscribe on flag == 1, then shutdown on flag == 0. Keep in mind that it can take some time for your subscription to be processed (as was already mentioned by @Lorenz in your other question).

Instead of (re|un)subscribing, you could also check for flag == 1 in your callback, and only write to your file if the condition is true.

Does that mean, that a subscriber in ROS HAS to CONTINUOUSLY listen to the topic being published? What if I don't want to? What if I want to listen to a topic say, only when 'flag' is set to 1?

In short: no, you don't have to, you can (re)subscribe and unsubscribe at any time. Subscription you know (NodeHandle::subscribe(..)), to stop a subscription, use NodeHandleSubscriber::shutdown(). Now subscribe on flag == 1, then shutdown on flag == 0. Keep in mind that it can take some time for your subscription to be processed (as was already mentioned by @Lorenz in your other question).

Instead of (re|un)subscribing, you could also check for flag == 1 in your callback, and only write to your file if the condition is true.


It just bothers me that ROS being such an amazing platform, does not seem to have a simple solution to intermittent/'at will' subscription to a topic!

It does, internally there is no difference between a 'temporary' subscription and a 'for-the-duration-of-the-program' subscription. It's just up to you to manage your subscriptions properly.