How do I publish exactly one message?
Hi everyone,
I have been working with ROS for sometime now, and quite happily. However, there is one sticky issue that I would like to resolve. I learnt the basics of publishing from the tutorial "Writing a simple publisher and subscriber". This is the gist of what is demonstrated:
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
I have been using the same format for publishing ever since. Obviously, this will keep publishing as long as the program terminates or roscore terminates. I would like to publish a single message, irrespective of message size. In this case, I would like publish one std_msgs::String message. How do I do that?
<edit> (Nov 14, 2012) Upon reading comments from Benoit Larochelle and Lorenz, I would like to make my question more specific. In my case, latched topics does not apply to the current application logic. I would like all current subscribers to get the message but not future subscribers. Also, the subscribers are external packages that listen to standard non-stamped messages. In addition, I cannot use actionlib since the subscribers do not use it as well. One example is to send a twist command (that does not have a timestamp) to a swarm of robots simultaneously. We ofcourse dont want future subscribers to react to an old command (latched) and we do not want to send the command at a specific rate, but only send it once. </edit>
I have tried simple removing the while loop and executing "publish" and "spinOnce()" just one time. This doesn't work, no messages are published.
Can anyone tell me how I can publish a single message?
Thanks Shanker Keshavdas DFKI, Saarbruecken, Germany
I don't think topics were supposed to be used that way. I think there might be hacks possible, e.g. by using subscription callbacks and publish only to a selected set of subscribers. But I doubt that a really robust implementation is possible.