Lately there came some new examples on how to use ros2 publisher/subscriptions with c++.
They suggest inheriting from a node and implementing your publisher in this inherited class. The reason given in the comments is:
We do not recommend this style anymore, because composition of multiple nodes in the same executable is not possible
I’ve read quite a lot of the rclcpp code and as far as I can see there is no reason why I couldn’t have multiple nodes with the “classic” version. Could you explain this to me?
Thank you for raising this point. We’ll change the code comment to make it more clear. You are correct in that you can have multiple nodes in the same executable without inheriting from Node.
The main goal of subclassing Node is to push all functionality into the Node subclass, so that the main()
function in a standalone executable has approximately no code.
Then, you will be able to both run your node in a standalone executable, or inside a generic “launcher” executable, much like nodelets in “classic” ROS. We’ll be creating minimal examples of this use case very shortly.
The idea of making Node subclasses even in cases like minimal_publisher
is more to get in the mindset of being able to write code can compile to a shared-object that works in both generic standalone executables and generic multi-node launcher processes, where the performance benefits of intra-process communication for large data streams (e.g., vision pipelines) could be significant.