Add a condition in a while loop
Hi guys, I was wondering how is it possible to introduce a condition in a standard spin function. More precisely: at the moment the spin function for my code looks like:
ros::Rate loop(frequency);
while(ros::ok()){
//do some computations and publish messages
loop.sleep();
ros::spinOnce();
}
By doing so, the computations and publish messages are executed at every loop. I would now like to introduce the following condition:
do the computation and publish messages only if a new message on the topic A is received.
So the new spin function should look like:
ros::Rate loop(frequency);
while(ros::ok()){
if (new message on topic A is received){
//do some computations and publish messages
}
loop.sleep();
ros::spinOnce();
}
I would like to ask you what is the best way to introduce this condition.