publishers two message on the same topic not working for subscriber
Hi everyone; I am sending two deferent data message publishers on the same topic, and I see both data publisher in output when I use echo. I use two node and I make the subscriber from this publisher I see just the last message publish. Always loses the first message and stay the last. M y code is:
output_modem_publisher_ = nh_.advertise<std_msgs::String>(modem_topic_, 100);
modemin_subscriber_ = nh_.subscribe(modem_in_, 100, &Depth::modeminCallback, this);
void Depth::callPublisher()
{
std::string out_modem2;
std::stringstream monsun2;
monsun2 << 2 << "@" << "d";
out_modem2 = monsun2.str();
std_msgs::String m2;
m2.data = out_modem2;
output_modem_publisher_.publish(m2);
std::string out_modem3;
std::stringstream monsun3;
monsun3 << 3 << "@" << "d" ;
out_modem3 = monsun3.str();
std_msgs::String m3;
m3.data = out_modem3;
output_modem_publisher_.publish(m3);
}
...
...
ros::Rate loop_rate(15);
int count = 0;
while (ros::ok())
{
depth.callPublisher();
ros::spinOnce();
loop_rate.sleep();
++count;
}
The echo results are for the publisher:
rostopic echo depth/modem_topic
data: 2@d
---
data: 3@d
---
data: 2@d
---
data: 3@d
---
... The echo results are for the subscriber :
rostopic echo depth2/modem_in:
data: 3@d
---
data: 3@d
---
data: 3@d
---
...
when I use usleep(ms)
for 10ms I see both data by subscriber. But I don't like to use usleep()
due to make low performance for my program.
the code of subscriber is:
void DepthMonsun::modeminCallback(const std_msgs::StringPtr& modemin_msg)
{
// ROS_INFO(" modeminCallback ");
std::string in_modem;
in_modem = modemin_msg->data;
std::vector<std::string> v;
split(in_modem, v, '@');
//decoder the data that received from modem/out of MONSUN
if (in_modem[0] == '2')
{
std::vector<std::string> v;
split(in_modem, v, '@');
x = 0;
x = atof(v.at(2).c_str());
....
}
else if (in_modem[0] == '3')
{
std::vector<std::string> v;
split(in_modem, v, '@');
y = 0;
y = atof(v.at(2).c_str());
...
}
Any idea for solving this problem...,
thanks
Why do you want to have two publishers, publishing at the same time, for different data ?
Have you checked via
rxgraph
? So if the subscriber is the one not working, could you post its code?I had a mistake in explaining the problem. I have one publisher but for two messages on the same publisher. excuse me.