What can cause spinOnce to stop processing messages?
I'm trying to debug a very rare instance (that happens randomly) in my node when I will be waiting for a specific message and it never gets received. I've created a very rough code example to help illustrate how things are happening. My actual code has a lot more subscribers and classes, but this should get the point across.
Key Points
spinOnce()
is being called inside acreateTimer()
callback- Messages are not being added to subscribers queue. (verified)
- Messages are being published by publisher. (verified)
- After the
TimerCallback()
function completes, messages will get received again.
Publisher Node:
int main(int argc, char** argv)
{
ros::init(argc, argv, "Publisher");
ros::NodeHandle node;
ros::Rate r(1);
publisher = node->advertise<Message_type>(topic_string, 1, true);
int count = 0;
while (ros::ok())
{
count++;
publisher.publish(Message_type(count));
r.sleep();
}
}
Subscriber Node:
bool message_received_ = false;
void ImportantCallback(const Message_type& msg)
{
ROS_INFO_STREAM("count: " << msg.count);
message_received_ = true;
}
void TimerCallback(const ros::TimerEvent &timer_event)
{
// wait for a particular message to be received
while(message_received_ == false)
{
ros::spinOnce();
ros::Duration(0.01).sleep();
}
// do stuff
ROS_INFO("Message received");
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "Subscriber");
ros::NodeHandle node;
ros::NodeHandle pr_node("~");
ros::Rate r(10);
// Other subscribers initialized here
subscriber = node->subscribe(topic_string, 1, ImportantCallback, this);
InitializeSubscribers(node);
plan_timer_ = node->createTimer
(ros::Duration(0.01), &TimerCallback, this);
while (ros::ok())
{
ros::spinOnce();
r.sleep();
}
}
Any ideas are greatly appreciated.