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

You cannot control how often your subscriber callback is called. roscpp will attempt to call your callback once for every message that is received.

If you can change the publish rate, that's the most obvious way to change how often your callback is called, but it will affect every node subscribing to that topic, and how you change the publish rate depends on the node that is publishing. You don't say which node is publishing these messages, so I can't give more specific advice.

If you can't change the publisher (either it affects other nodes or the code doesn't allow it), you can use the topic_tools throttle node to subscribe to the topic and publish it at a slower rate.

Depending on what you're trying to do, you could also implement the callback throttling in your own code; I see two ways to do this:

  • keep a variable that tracks the last time you processed a message, and check that in your callback to figure out when to do the actual message processing.
  • Have a simple callback that only store the most recent message in a global or class variable, and then use a timer to process that data.

There are additional tradeoffs between all of these approaches, but without knowing why you want to subscribe at a slower rater, I can't give more specific advice.

click to hide/show revision 2
No.2 Revision

You cannot control how often your subscriber callback is called. roscpp will attempt to call your callback once for every message that is received.

If you can change the publish rate, that's the most obvious way to change how often your callback is called, but it will affect every node subscribing to that topic, and how you change the publish rate depends on the node that is publishing. You don't say which node is publishing these messages, so I can't give more specific advice.

If you can't change the publisher (either it affects other nodes or the code doesn't allow it), you can use the topic_tools throttlethrottle node to subscribe to the topic and publish it at a slower rate.

Depending on what you're trying to do, you could also implement the callback throttling in your own code; I see two ways to do this:

  • keep a variable that tracks the last time you processed a message, and check that in your callback to figure out when to do the actual message processing.
  • Have a simple callback that only store the most recent message in a global or class variable, and then use a timer to process that data.

There are additional tradeoffs between all of these approaches, but without knowing why you want to subscribe at a slower rater, I can't give more specific advice.