0
$\begingroup$

Rosanswers logo

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!


Originally posted by Nishant on ROS Answers with karma: 143 on 2012-07-06

Post score: 0


Original comments

Comment by Nishant on 2012-07-06:
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 :)

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by PerkinsJames with karma: 506 on 2012-07-06

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by Dan Lazewatsky with karma: 9115 on 2012-07-06

This answer was NOT ACCEPTED on the original site

Post score: 4

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.