I am writing a program in ROS that should perform certain computations after each callback function. My callback function basically subscribes to a topic and sets the value of a variable which is used to perform a specific computation. The topic that I am subscribing to has a frequency of 30 Hz. So, I have a while loop in my program that runs at the rate of 30 Hz. The loop is somewhat similar to the following code:
while (ros::ok())
{
ros::spinOnce(); //this should set a certain variable "a"
perform_computation(); //this performs computation on the variable "a"
looprate.sleep(); //this runs at 30 Hz
}
ros::spinOnce() is similar to ros::spin() but with the difference that it does not block. Each call to ros::spinOnce() will process all received messages since the last call, i.e. it will call all subscriber callback functions for each of the messages. If you want to take control over the callback functions, then I'd buffer the messages received from the callback function and process the buffered messages in the rate and way you want. This is also the way you should process information if the callback does some lengthy computation. Callbacks function should be very fast.