0
$\begingroup$

Rosanswers logo

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;
}

Originally posted by ashwath1993 on ROS Answers with karma: 70 on 2022-01-03

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

In your main(), you can replace the ros::spin() with a while loop that does whatever you want it to. In this example, check_for_done() is a function that you provide. If you use this approach, be sure you sleep() on every iteration of the while loop.

ros::Duration d(0.100);
while(ros::ok) {
    if (check_for_done()) {
        ros::shutdown();
    }
    d.sleep();
}

Originally posted by Mike Scheutzow with karma: 4903 on 2022-01-04

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by ashwath1993 on 2022-02-06:
This was sufficient for what I wanted to achieve. Thanks!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.