0
$\begingroup$

Rosanswers logo

I was following the tutorial and faced the following problem: some messages (usually 3) are lost when doing the sequence:

  1. start talker
  2. start listener
  3. stop talker
  4. start talker

For example, an output like this is produced in the talker window:

$ rosrun beginner_tutorials talker 
[ INFO] [1548238652.003693516]: hello world 0
[ INFO] [1548238652.103993652]: hello world 1
[ INFO] [1548238652.203964623]: hello world 2
[ INFO] [1548238652.304064752]: hello world 3
[ INFO] [1548238652.404034941]: hello world 4
...

While in the listener window only latter are shown:

[ INFO] [1548238652.304599291]: I heard: [hello world 3]
[ INFO] [1548238652.404490112]: I heard: [hello world 4]
[ INFO] [1548238652.504314152]: I heard: [hello world 5]
...

There is no such problem with the Python publisher/subscriber, not even a single message was lost.

ROS Kinetic, Ubuntu 16.04.05 x64 (run under VirtualBox with Win7 x64 as a host). No original files were changed, just copy and paste from the tutorials.


Originally posted by volume_8091 on ROS Answers with karma: 27 on 2019-01-23

Post score: 1


Original comments

Comment by curi_ROS on 2019-01-23:
You could try increasing the queue size of the publisher. However, there is no guarantee that all messages will be delivered.

Comment by mali on 2019-01-23:
if you lose only the first three messages each time you start the talker node, the reason might be that: the publisher takes milliseconds to be initialized. try to put some delay like 1 sec before publishing any message and see what will happen.

Comment by volume_8091 on 2019-01-24:
@curi_ROS increasing queue size from 1000 to 10,000 did not give any difference. As only 3 messages were lost, buffer size of 1000 would already be enough to avoid the problem.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Hi!

As you already noticed, this is caused by the time it takes to initialize the Publisher. I wanted to share an alternate solution to the problem, which is using the getNumSubscribers() method of the Publisher class.

This method returns the number of subscribers that are currently connected to the Publisher. In your case, you could add something like this to the talker.cpp code:

while(chatter_pub.getNumSubscribers() == 0)
    loop_rate.sleep();

This would go right before the publish of the message:

chatter_pub.publish(msg);

This way, you won't publish any message until the Publisher/Subscriber connection is created, so you won't lose any message. You can also check the following post for more details.

Cheers,


Originally posted by Alberto E. with karma: 356 on 2019-01-24

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by gvdhoorn on 2019-01-24:
+1 for mentioning getNumSubscribers(), but a nitpick: you write "the Publisher/Subscriber", as if there are only 1 of each. That is rarely the case. There are typically many, in which case waiting for a single one makes little sense.

Same comment about the text in the blogpost you link to.

Comment by gvdhoorn on 2019-01-24:
It would be good to link to / discuss an explanation of when to use which type of interaction, as "lost messages" are almost inherent to something as loosely / completely decoupled as (ROS) pub-sub.

If lost msgs are unacceptable, perhaps services or actions are better suited.

Comment by volume_8091 on 2019-01-24:
Not only did your solution work, but it is also much more reliable and avoids ugly guess-waiting-time question.

Comment by volume_8091 on 2019-01-24:
@Alberto E. As a final note, could you share a piece of advice on how to look up the names of subscribers? This could enable not starting publishing until the very important Subscriber(s) starts listening.

UPD: the video is good, thank you very much

Comment by gvdhoorn on 2019-01-24:\

This could enable not starting publishing until the very important Subscriber(s) starts listening.

and would also remove one of the advantages of pub-sub: decoupling in the space dimension.

See #q203129.

$\endgroup$

Your Answer

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