有没有一种方法可以让 ros::spinOnce() 从回调队列中调用特定数量的回调?

Is there a method to make ros::spinOnce() call a specific number of callbacks from the callback queue?

我正在用 ROS 编写一个程序,它应该在每个回调函数之后执行某些计算。我的回调函数基本上订阅了一个主题并设置了一个用于执行特定计算的变量的值。我订阅的主题频率为 30 Hz。所以,我的程序中有一个 while 循环,它以 30 Hz 的速率运行。循环有点类似于下面的代码:

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() 类似于 ros::spin() 但不同之处在于它不会阻塞。每次调用 ros::spinOnce() 都会处理 all 自上次调用以来收到的消息,即它会为每条消息调用所有订阅者回调函数。如果您想控制回调函数,那么我会缓冲从回调函数收到的消息,并以您想要的速率和方式处理缓冲的消息。如果回调进行了一些冗长的计算,这也是您应该处理信息的方式。回调函数应该很快。