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

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

Comments

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

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-05 03:40:10 -0500

hank880907 gravatar image

updated 2023-07-06 07:07:05 -0500

if you compile your executable with a main function and rclcpp::spin(), It makes your executable, well, an executable. This will result in a binary file that you can execute directly.

RCLCPP_COMPONENTS_REGISTER_NODE macro generates the required code for registering the node to a node factory that is used by the class loader. This is exactly how plugins work, which means you are basically making your node a plugin. In order to use it, you need to compile your node as a shared library. This means there will be another program that can load your node from the shared library and execute it.

By using rclcpp_components_register_node in CMakeLists.txt, it essentially generates a cpp that instantiates your node and does rcp::spin. The generated cpp file uses the class loader to load the shared library, instead of compiling it statically against the source code. One of the advantages is that it minimized the boilerplate that makes a node an executable.

I would prefer to use RCLCPP_COMPONENTS_REGISTER_NODE because it allows you to have the option of loading multiple nodes to a single process (just like nodelet!) which enables zero-copy data transport.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2021-02-15 06:55:32 -0500

Seen: 2,338 times

Last updated: Jul 06 '23