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

I'd suspect your first program quickly returns and doesn't run a loop?

In that case, the publisher just hasn't connected yet, when you try to send the message and then your program quits, so it can never resend messages.

You should wait until publisher.getNumSubscribers() > 0, before you publish.

I'd suspect your first program quickly returns and doesn't run a loop?

In that case, the publisher just hasn't connected yet, when you try to send the message and then your program quits, so it can never resend messages.

You should wait until publisher.getNumSubscribers() > 0, before you publish.

Here is some code snippet that I use:

bool waitForSubscribers(ros::Publisher & pub, ros::Duration timeout)
{
    if(pub.getNumSubscribers() > 0)
        return true;
    ros::Time start = ros::Time::now();
    ros::Rate waitTime(0.5);
    while(ros::Time::now() - start < timeout) {
        waitTime.sleep();
        if(pub.getNumSubscribers() > 0)
            break;
    }
    return pub.getNumSubscribers() > 0;
}