How to display the function of latch? [closed]
I want to show the function of latch in publisher. According to the description of the latch: "When a connection is latched, the last message published is saved and automatically sent to any future subscribers that connect." So I write a small program to show the function of latch. Below is my code, I just did a modification slightly according to the ROS tutorials.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000, true);
ros::Rate loop_rate(100);
int count = 0;
while(chatter_pub.getNumSubscribers() == 0)
loop_rate.sleep();
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
}
But when I run the subscribers, it doesn't show me the last message published.
Do you know why?