ros::spinOnce() not functioning properly
Hello,
So I went through the tutorials for publishing/subscribing for a message over a ROS topic over here
http://ros.org/wiki/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
So, I ran everything as they told me to in the tutorial, and it worked fine. I was able to see the output at 10Hz being published and subscribed to. Then I decided to tinker around with the subscriber code.
In that, I changed
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
// %Tag(SPIN)%
ros::spin();
// %EndTag(SPIN)%
to
int i=0;
while(i<50)
{
// %Tag(SUBSCRIBER)%
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
// %EndTag(SUBSCRIBER)%
/**
* ros::spin() will enter a loop, pumping callbacks. With this version, all
* callbacks will be called from within this thread (the main one). ros::spin()
* will exit when Ctrl-C is pressed, or the node is shutdown by the master.
*/
i++;
// %Tag(SPIN)%
ros::spinOnce();
}
// %EndTag(SPIN)%
So this should print out "hello world xyz" 50 times right? However, it is not printing anything. As soon as I run this, the program runs as if there were no subscription and callback.
The callback function is the same as the one in the tutorial. No change there.
What then am I doing wrong regarding the spinning? Any help is greatly appreciated.
Thanks!
Cool. I put ros::Subscriber outside the while loop, and I added a delay of 0.1 seconds in the loop (my publisher publishes at 10Hz). And now it works fine! Thanks PerkinJames and Dan :)