From 9933e6869d4555f088b48c50a182a1170eb9fc98 Mon Sep 17 00:00:00 2001 From: Arashdeep Singh <147547278+ArashdeepSinghMaan@users.noreply.github.com> Date: Mon, 3 Nov 2025 21:52:35 +0530 Subject: [PATCH 1/2] Enhance documentation on ROS 2 main function Expanded explanation of the main function and ROS 2 initialization process. Signed-off-by: Arashdeep Singh <147547278+ArashdeepSinghMaan@users.noreply.github.com> --- .../Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst b/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst index b8f0490325a..a588afdd6e8 100644 --- a/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst +++ b/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst @@ -199,7 +199,12 @@ In the bottom of the class is the declaration of the timer, publisher, and count size_t count_; Following the ``MinimalPublisher`` class is ``main``, where the node actually executes. -``rclcpp::init`` initializes ROS 2, and ``rclcpp::spin`` starts processing data from the node, including callbacks from the timer. +``rclcpp::init`` initializes ROS 2, and ``rclcpp::spin`` starts processing data from the node, including callbacks from the timer. The ``main()`` function begins by calling ``rclcpp::init(argc, argv);`` which initializes the ROS 2 client library. This step prepares the system to handle communication and parses any command-line arguments for remapping, parameters, or logging configuration. It is important to call this function before creating any nodes. + +Next, the node is executed using ``rclcpp::spin(std::make_shared());`` which creates a shared instance of the node and enters an event loop that keeps it active. The ``spin()`` function processes all incoming callbacks, including those from timers, subscriptions, and services, and continues running until the node is explicitly shut down (for example, when ``Ctrl+C`` is pressed). + +Finally, ``rclcpp::shutdown();`` is called to stop all ROS 2 activity, clean up allocated resources, and ensure that the program exits gracefully. + .. code-block:: C++ From 932594a497870209474e2c4c6bf3064420aec621 Mon Sep 17 00:00:00 2001 From: Arashdeep Singh <147547278+ArashdeepSinghMaan@users.noreply.github.com> Date: Tue, 4 Nov 2025 19:09:39 +0530 Subject: [PATCH 2/2] Update source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alejandro Hernández Cordero Signed-off-by: Arashdeep Singh <147547278+ArashdeepSinghMaan@users.noreply.github.com> --- .../Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst b/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst index a588afdd6e8..c32395a668e 100644 --- a/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst +++ b/source/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.rst @@ -199,9 +199,12 @@ In the bottom of the class is the declaration of the timer, publisher, and count size_t count_; Following the ``MinimalPublisher`` class is ``main``, where the node actually executes. -``rclcpp::init`` initializes ROS 2, and ``rclcpp::spin`` starts processing data from the node, including callbacks from the timer. The ``main()`` function begins by calling ``rclcpp::init(argc, argv);`` which initializes the ROS 2 client library. This step prepares the system to handle communication and parses any command-line arguments for remapping, parameters, or logging configuration. It is important to call this function before creating any nodes. +``rclcpp::init`` initializes ROS 2, and ``rclcpp::spin`` starts processing data from the node, including callbacks from the timer. The ``main()`` function begins by calling ``rclcpp::init(argc, argv);`` which initializes the ROS 2 client library. +This step prepares the system to handle communication and parses any command-line arguments for remapping, parameters, or logging configuration. +It is important to call this function before creating any nodes. -Next, the node is executed using ``rclcpp::spin(std::make_shared());`` which creates a shared instance of the node and enters an event loop that keeps it active. The ``spin()`` function processes all incoming callbacks, including those from timers, subscriptions, and services, and continues running until the node is explicitly shut down (for example, when ``Ctrl+C`` is pressed). +Next, the node is executed using ``rclcpp::spin(std::make_shared());`` which creates a shared instance of the node and enters an event loop that keeps it active. +The ``spin()`` function processes all incoming callbacks, including those from timers, subscriptions, and services, and continues running until the node is explicitly shut down (for example, when ``Ctrl+C`` is pressed). Finally, ``rclcpp::shutdown();`` is called to stop all ROS 2 activity, clean up allocated resources, and ensure that the program exits gracefully.