Automatic node shutdown if topic is no longer receiving messages
Hi,
I have a node that receives camera messages. I want the node to shutdown when the rosbag is done playing. I know I can do this with roslaunch. But I wanted to know if I could do this with code.
I noticed that my code works for some recordings and fails for others.
void callback(const sensor_msgs::ImageConstPtr& msg)
{
if(sub.getNumPublishers() == 0)
{
// run main algo and write results
ros::shutdown();
}
else
{
// process message
}
}
Is there a way to shutdown the node outside the callback? My subscriber is part of a class and the subscriber is initialized with the constructor.
MyClass(ros::NodeHandle& n)
{
sub = n.subscribe<sensor_msgs::Image>("/camera/image_color", 100, &MyClass::callback, this);
}
My node is defined as follows
int main(int argc, char * argv[])
{
ros::init(argc, argv, "calib_node");
ros::NodeHandle n;
MyClass obj(n);
ros::spin();
return 0;
}