ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
In your case, I guess the problem is that ROS needs some time to register at the core and to establish all subscriber connections. When you just publish one single message, chances are good that it gets lost because the subscriber is not connected yet. A quick fix would be to add a sleep right after creation of the publisher.
If you really want to make sure that clients are connected, you can use the version of advertise which takes one parameter of type AdvertiseOptions
see here.
In AdvertiseOptions
, you can set a connect callback that gets called whenever a subscriber connects.
Another possiblility is to use the method getNumSubscribers
of the Publisher
class returned by the advertise call. Just wait for at least one subscriber. You can do something like:
ros::Rate poll_rate(100);
while(chatter_pub.getNumSubscribers() == 0)
poll_rate.sleep();