When to use rclcpp::spin or RCLCPP_COMPONENTS_REGISTER_NODE?

asked 2021-02-15 06:59:19 -0500

HANnie gravatar image

When should i use rclcpp::spin() (or one of its variations) and when should I use RCLCPP_COMPONENTS_REGISTER_NODE()?

The context is that I am following the action server tutorial. The code in the tutorial uses RCLCPP_COMPONENTS_REGISTER_NODE to be able to run the node. But the tutorial ends with a link to some other examples. In one of those examples it uses rclcpp::spin. What are the differences between these ways of starting a node?

The action server tutorial: https://index.ros.org/doc/ros2/Tutori...

The examples: https://github.com/ros2/examples/tree...

The specific example with rclcpp::spin: https://github.com/ros2/examples/blob...

edit retag flag offensive close merge delete


Waiting for an answer..

Veera Ragav gravatar image Veera Ragav  ( 2022-03-23 18:52:08 -0500 )edit

Also would like to read the answer to this question

FelipeGdM gravatar image FelipeGdM  ( 2022-06-23 14:16:15 -0500 )edit

I am struggling with an issue related to this... so far what I have understood:

  • In the action server tutorial the code is declared as a library (in CMakeLists) and then rclcpp_components_register_node is used to convert it into a executable node.The CPP code for this contains no main function, just the declaration of a class.
  • In other nodes or tutoriales, in CMakeLists the node is built using "add_executable", and then the CPP code will have a main function, where the rclcpp::spin() is executed.

On the other hand, I'm not sure about the pros and cons of these two different ways of compiling the node.

leander gravatar image leander  ( 2022-07-26 03:57:17 -0500 )edit