Stop ros::spin()
Hi,
I would like to create a subscriber and publisher program that send messages and wait for an answer. The idea is to publish then listen the node. When an answer is received, it will stop listen the node and continue to execute the program.
Actually, I successfully write the subscriber and publisher, but i'm not able to handle the ros::spin. I don't know how to stop it or pause it, despite looking some forums.
Here's my run function :
//PUBLISH
ROS_INFO("%s", message.data.c_str());
publisher.publish(message);
ros::spin();
Here's my callback function :
void callback_listener(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("Received");
if (msg == "ok") // something like this
//here I should stop the ros::spin()
}
Help would be appreciated.
Thanks
Try to use
ros::spinOnce
in your codeAlready used it, but not working.
To support @kosmastsk comment you need to know that
ros::spin
directive is a blocking function, hence it will block all your execution at themain
process. You may useros::spinOnce
that already implement a type of wait/synchronization for processing messages.If that does not fit your set up, just change the
main
process of your program with aThat's not really what I'm looking for. I don't use any "while" because it's a GUI with Qt5. ros::AsyncSpinner spinner(0) seems doing the behavior that i'm looking for. But when I write it in main, I get some errors about threads. So, i'm a little bit lost.
If you are dealing with a Qt5 project you do not need to write that in the main process where you instantiate the main Qt Window, you should have a Node running in another thread providing ROS communications with the Qt application, In that case you will have a "while"
ros::ok()
running in his own thread node, while the Qt5 application will be running in the main process. Nevertheless either you use a custom callback queue to decide when to dequeue the message and resume execution or perform await
in the Qt thread to only resume the execution whenstart
is called or thewait
finishes, I am still do not know why you want to block ROS execution but there is another probable approach that you can use and that is waitForMessage which will wait until a message is received and drops connection.