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

@Weasfas 's answer is correct for setting a particular rate of publishing and subscribing using sleep(). Accroding to your other questions you might need to use a timer because :

  1. You should not do too much things within a subscriber, imagine you receive messages at a rate of 100Hz but you take 1 sec to execute the callback, you will end up loosing data or process old data that has been stored in the callback queue. You can use a global variable to store the message data and in another function you can use the global variable and publish what's needed. About the Rate, yes it will affect both subscribers and publisher (if the publisher is within the callback). Spinning at a define rate means that your callbacks are triggered at this rate (but it will be executed as many times as messages have been stored in the queue while waiting)
  2. You should use the timer to bind it with the publisher function and define the rate at 100 for calling this function that will use the global variable that gets the data from the callback. So you hsould have one callback function to store the messages and another function that is called by the timer that deals with the data and then publish it.