Ask Your Question
0

ros::spinOnce() not functioning properly

asked 2012-07-06 09:14:22 -0600

Nishant gravatar image

updated 2014-01-28 17:12:56 -0600

ngrennan gravatar image

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!

edit retag flag offensive close merge delete

Comments

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 :)

Nishant gravatar imageNishant ( 2012-07-06 09:56:50 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
3

answered 2012-07-06 09:36:15 -0600

PerkinsJames gravatar image

updated 2012-07-06 09:38:01 -0600

I suspect that the issue has to do with you one of two things.

1) Resubscribing to the chatter topic everytime you iterate that loop. Put the

ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

line above your while loop and try your program again.

or 2) Also, someone correct me if I'm wrong, but using spinOnce() only processes the messages that have come in since the last time you called spinOnce. If your publisher of the "chatter" topic is not publishing messages at the rate at which you are calling spinOnce, you will not get 50 messages (thus, not getting 50 printouts in your callback). You are looping so fast there that you will for sure not get 50 callbacks.

edit flag offensive delete link more
4

answered 2012-07-06 09:37:32 -0600

You should only call ros::Subscriber once. Calling it within a loop will create a subscriber, which will then immediately go out of scope and be destroyed. Even if you move the subscribe outside the loop, in your example how many times the callback is called will depend on how often a message is published on chatter.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-07-06 09:14:22 -0600

Seen: 1,850 times

Last updated: Jul 06 '12