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

ros2.Crystal when Using rclcpp::executors::MultiThreadedExecutor::spin, if I use ctrl+c it will not shutdown the script

asked 2019-03-05 20:14:31 -0500

Obeseturtle gravatar image

updated 2019-03-06 23:40:47 -0500

I used the following link to create the MultiThreadedExecutor code I have written bellow.

The issue I am having is that when using ctrl + c, I receive a

[INFO] [rclcpp]: signal_handler(signal_value=2)

message but, the thread still hangs. I do not quite understand how the ROS2 signal handler works. Is there a way I can catch the [INFO] [rclcpp]: signal_handler(signal_value=2) message to exit out of the spin and close the program?

using rclcpp::executors::MultiThreadedExecutor;
using builtin_interfaces::msg::Time;

std::shared_ptr<rclcpp::Node> master_node;

int value=0;


class test{

public:
    static void topic_callback(test_msgs::msg::Test::SharedPtr msg)
  {
    std::cout  <<" Subscriber Received [" << msg->data << "]" << std::endl;
    value = msg->data;
  }

};

int main()
{

  rclcpp::shutdown();
  test *t;
  t = new test();
  rclcpp::init(0, nullptr);

  MultiThreadedExecutor executor;
  master_node = rclcpp::Node::make_shared("MasterNode");
  executor.add_node(master_node); 


  rclcpp::Publisher<test_msgs::msg::Test2>::SharedPtr publisher_;
  rclcpp::Subscription<test_msgs::msg::Test>::SharedPtr subscription_;


  subscription_ = master_node->create_subscription<test_msgs::msg::Test>("Test",t->topic_callback);  
  publisher_ =  master_node->create_publisher<test_msgs::msg::Test2>("Test2");
  rclcpp::Clock ros_clock(RCL_ROS_TIME);
  Time ros_now = ros_clock.now();
  std::thread executor_thread(std::bind(&MultiThreadedExecutor::spin, &executor));
  executor_thread.detach();


while(rclcpp::is_initialized)
  {
    auto message = test_msgs::msg::Test();
    message.header.stamp.nanosec = ros_now.nanosec;
    publisher_->publish(message);
    usleep(3000000);
  } 
    rclcpp::shutdown();

}
edit retag flag offensive close merge delete

Comments

Btw, why do you start with rclcpp::shutdown()?

William gravatar image William  ( 2019-03-06 20:06:40 -0500 )edit
1

Why are you detaching the thread? Why not join it at the end of the main loop. spin() will exit when SIGINT is received automatically.

William gravatar image William  ( 2019-03-06 20:20:47 -0500 )edit

The main thread has to be independent to the child thread that is used for the subscriber callback. The main thread is used to make heavy calculations and is constantly jumping to other classes. If i use join, the main thread gets stuck in the callback.

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 20:51:28 -0500 )edit

The reason I start with rclcpp::shutdown() is because the node took a while before shutting down. Sometimes when using ros2 node list, I would have multiple instances of the same node. I researched this issue and in one of the discussions it was mentioned to better have a shutdown in the beginning.

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 20:54:43 -0500 )edit

I will try to find the discussion and link to it.

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 20:55:08 -0500 )edit

If you are literally using while(rclcpp:ok) you are missing the parenthesis again to actually call the function (same as in the original snippet with while(rclcpp::is_initialized)).

Dirk Thomas gravatar image Dirk Thomas  ( 2019-03-06 23:21:35 -0500 )edit

I understand now. I have tried using while(rclcpp::ok()) and the example program shuts down. The segmentation fault I am experiencing in my main program is being caused by something else. The rclcpp shutdown is being properly called. Thank you for the patients you have showed.

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 23:35:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-03-06 18:42:36 -0500

Dirk Thomas gravatar image

updated 2019-03-06 19:24:03 -0500

The condition rclcpp::is_initialized seems wrong. It doesn't actually call the function.

Please try rclcpp::ok() instead.

edit flag offensive delete link more

Comments

Those functions are the same: http://docs.ros2.org/crystal/api/rclc...

William gravatar image William  ( 2019-03-06 18:47:12 -0500 )edit

But rclcpp::ok() is the "right" thing to use going forward.

William gravatar image William  ( 2019-03-06 18:51:28 -0500 )edit

Thank you, I will make sure to use rclcpp::ok() from now on. The underline problem still exist though. The SIGINT is being captured by ROS2 executor. I wanted to catch it using my main program and some how tell the executor to shutdown. I tried executor.cancle(); with no avail.

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 19:42:46 -0500 )edit

What happens is that the main thread shuts down but, the ROS2 executor is still running causing a segmentation error. Is there a proper way to access the rclcpp::signal_handler and exit out of the executor spin? I apologize for the inconvenience.

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 19:44:53 -0500 )edit

The spin should stop automatically if the signal handler is being run (which it appears to be since you are getting the log message).

William gravatar image William  ( 2019-03-06 20:06:12 -0500 )edit

You can register a callback for "on shutdown" rather than installing your own signal handler: http://docs.ros2.org/crystal/api/rclc...

William gravatar image William  ( 2019-03-06 20:06:20 -0500 )edit

Thank you for your reply. As you mentioned, the signal handler is being run and the subscriber callback thread is being shutdown. The While loop will not exit though, meaning that the main thread is active. If I understood you correctly, the "on shutdown" should be used to end the main thread?

Obeseturtle gravatar image Obeseturtle  ( 2019-03-06 20:14:36 -0500 )edit

You said there is a segmentation fault? You should update your question with the crash details. You only need the on_shutdown if you need to do something special for SIGINT, that's not the case here.

William gravatar image William  ( 2019-03-06 20:20:06 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-03-05 20:14:31 -0500

Seen: 2,446 times

Last updated: Mar 06 '19