ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to properly shut down a node

asked 2016-03-19 05:42:37 -0600

I have a code where I am checking for shutdown interrupt in a statement like so:

         int main()
          {
              bool  running(false);
              //running is set to true by a condition statement somewhere here
             for(; running || ros::ok() || !viewer->wasStopped() ;)
              {
               // ...             ...                          ...                  ...
               //do something
              }
          ros::shutdown();
          return 0;
       }

However, during runtime, when I do Ctrl + C, I get the usual warning ^C[ WARN] [1458383451.748234940]: Shutdown Signal Received. Shutting down ROS! but the node never shuts down.

Could I be missing something?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-03-19 09:30:49 -0600

Wolf gravatar image

Crlt+C will be handled by the ros node logic internal and causes ros::ok() to no longer return true but false . After ros:ok() is not true any more there is no need to call ros::shutdown()

I think your code may look like this

 int main()
  {
      bool  running(false);
      //running is set to true by a condition statement somewhere here

      // exit the main loop 
      //              if running flag is set false, 
      //          or if ros::ok() is not ok anymore (likely we received Crlt+C), 
      //          or if the view was stopped
     for(; running && ros::ok() && !viewer->wasStopped() ;)
      {
       // ...             ...                          ...                  ...
       //do something
      }

  if ( ros::ok() )
  {
      // we exited the main loop because of false running value or the viewer was stopped,
      // no Crlt+C received, i. e. we have to ros::shutdown() explizitly
      ros::shutdown();
  }
  else
  {
      // nothing to be done here, ros::ok() no longer true, ros::shutdown not needed anymore
  }
  return 0;

}

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-19 05:42:37 -0600

Seen: 14,656 times

Last updated: Mar 19 '16