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

How can the Subscriber subscribe once every 2 seconds

asked 2016-07-31 20:14:17 -0500

yongqi gravatar image

After I create a Subscriber, it will give me message all the time. But I do not want the Subscriber listens all the time, how to set the Subscriber interval? Thanks

edit retag flag offensive close merge delete


Or how can I set the Publisher interval?

yongqi gravatar image yongqi  ( 2016-07-31 20:16:40 -0500 )edit

I'm having the same dilemma, my challenge is listening to cmd_vel and giving the commands to the motor. I want to do that at 2 Hz. Cmd_vel publishes at up to 17 Hz.

Beshari gravatar image Beshari  ( 2023-06-01 04:53:03 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-07-31 22:06:16 -0500

ahendrix gravatar image

updated 2016-08-01 06:35:30 -0500

Airuno2L gravatar image

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.

edit flag offensive delete link more


Thanks for your reply. I am using python and Turtlebot. The Subscriber:rospy.Subscriber('/moblie_base/sensors/core', SensorState, self.pick_up), Publisher:self.vol_pub = rospy.Publisher('volice', String, queue_size=1) Publisher will publish topic in pick_up, can I set the Subscriber interval?

yongqi gravatar image yongqi  ( 2016-07-31 23:03:05 -0500 )edit

No; the subscribe cannot set the interval. My answer was written with roscpp in mind, but the advice applies to Python too.

ahendrix gravatar image ahendrix  ( 2016-08-01 01:29:08 -0500 )edit

Thanks a lot

yongqi gravatar image yongqi  ( 2016-08-01 02:52:31 -0500 )edit

(I edited the link to topic_tools throttle, it was linked to this page)

Airuno2L gravatar image Airuno2L  ( 2016-08-01 06:36:27 -0500 )edit

Would it also be possible to set up a service that I can use to get a message when I need it? I am solving an optimization problem 2 times a second and even though the messages are published very quickly I want to grab the most recent message just before I solve the next optimization

huckl3b3rry87 gravatar image huckl3b3rry87  ( 2017-08-01 22:31:46 -0500 )edit

I would be worried that if I try to use a global variable that when I am trying to update the next optimization with the "current" variables, that they might be "updated" at the same time. It just seems a little dangerous

huckl3b3rry87 gravatar image huckl3b3rry87  ( 2017-08-01 22:34:36 -0500 )edit

Question Tools

1 follower


Asked: 2016-07-31 20:14:17 -0500

Seen: 10,145 times

Last updated: Aug 01 '16