Ask Your Question

Proper Shutdown handling of node

asked 2018-05-18 02:43:36 -0500

mherrmann gravatar image

updated 2018-05-18 04:09:42 -0500

I have problems shutting down my nodes correctly. Se the following minimal example:

int  main(int argc, char **argv)
    ros::init(argc, argv, "mec_sensor_node");
    ros::NodeHandle _oNH;
    while (ros::ok()) {}
    return 0;

If I kill the node (CTRL+C or rosnode kill) the node quits immediately without giving me the chance to do any cleanup. So my question is, how can I achieve this functionality? Using signals is a problem, because I need a function callback. So I thought killing the node makes ros::ok() to fail, such that the node finishes, but that isn't the case, as can be seen via the ROS_WARN command. Any odeas?


    int  main(int argc, char **argv)
        ros::init(argc, argv, "mec_sensor_node");
        ros::NodeHandle _oNH;
        ros::Rate _oLoopRate(10);
        while (ros::ok()) 
        return 0;
edit retag flag offensive close merge delete


Why is it an issue to have a function callback ?

Delb gravatar image Delb  ( 2018-05-18 03:31:06 -0500 )edit
while (ros::ok()) {}

This may be a minimal example, but just to note: without a sleep or ros::Rate in there, this is consuming 100% of your cpu, even when doing nothing.

I also don't see a ros::spinOnce(), so ROS has no way of processing events. That may be the cause of your issues.

gvdhoorn gravatar image gvdhoorn  ( 2018-05-18 03:36:37 -0500 )edit

@Delb: Probably it may not be an issue, but I couldn't make it to pass a pointer to a signal_handler function. Could you provide a minimal example?

mherrmann gravatar image mherrmann  ( 2018-05-18 04:03:24 -0500 )edit

@gvdhoorn: I know about the resource problem, just deleted the content to reduce the code, but do you think that this regards my problem directly? However I edited the minimal example, but have the same problem.

mherrmann gravatar image mherrmann  ( 2018-05-18 04:07:07 -0500 )edit

I would have provided a similar example as @gvdhoorn 's link I think this is what you are looking for, and as he said if ros::ok() returns false the connection to the master are dropped (see #q253661 )

Delb gravatar image Delb  ( 2018-05-18 05:03:08 -0500 )edit

@gvdhoorn: Now I see, I couldn't use the ros logging system. Printing with cout shows that ros works as intended. Thank you very much for your help.

mherrmann gravatar image mherrmann  ( 2018-05-18 05:16:15 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-05-18 04:18:08 -0500

gvdhoorn gravatar image

updated 2018-05-18 05:25:04 -0500

ros::spinOnce() should be inside your while loop. That function allows ROS time to process incoming events.

Note that logging after ros::ok() returns false is not at all guaranteed to work iirc. A printf(..) might be better.

Btw see #q27655 for what I believe is a duplicate.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2018-05-18 02:43:36 -0500

Seen: 306 times

Last updated: May 18 '18