ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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,

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() getNumSubscribers() method of the Publisher 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,

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() 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,