How can I make consecutive waitForMessage wait for the consecutive messages?
To my surprise, the following simple program
#include "ros/ros.h"
#include "std_msgs/Int32.h"
int main(int argc, char** argv)
{
ros::init(argc, argv, "test");
ros::NodeHandle nh;
std_msgs::Int32::ConstPtr ret;
ret = ros::topic::waitForMessage<std_msgs::Int32>("/topic");
ROS_INFO("%d", ret->data);
ret = ros::topic::waitForMessage<std_msgs::Int32>("/topic");
ROS_INFO("%d", ret->data);
ret = ros::topic::waitForMessage<std_msgs::Int32>("/topic");
ROS_INFO("%d", ret->data);
ros::spin();
return 0;
}
Prints 3 0
s when I run a single rostopic pub /topic std_msgs/Int32 "data: 0"
.
One workaround is to add a ros::Duration(1).sleep()
between the waits but that doesn't work for my situation.
How can I have waitForMessage
actually wait for 3 publishes, provided that I don't know the interval between publishes?