diff --git a/common/autoware_node/CMakeLists.txt b/common/autoware_node/CMakeLists.txt index 007167fef5..0d5577f336 100644 --- a/common/autoware_node/CMakeLists.txt +++ b/common/autoware_node/CMakeLists.txt @@ -18,8 +18,7 @@ if(BUILD_TESTING) target_include_directories(${TEST_NAME} PRIVATE src/include) target_link_libraries(${TEST_NAME} ${PROJECT_NAME}) ament_target_dependencies(${TEST_NAME} - rclcpp - rclcpp_lifecycle) + rclcpp) endforeach() endif() diff --git a/common/autoware_node/README.md b/common/autoware_node/README.md index 28e39e54ea..3fe0108183 100644 --- a/common/autoware_node/README.md +++ b/common/autoware_node/README.md @@ -8,16 +8,7 @@ AN is an `autoware.core` package designed to provide a base class for all future nodes in the system. -It also inherits all lifecycle control capabilities of the base -class [LifecycleNode](https://docs.ros2.org/latest/api/rclcpp_lifecycle/classrclcpp__lifecycle_1_1LifecycleNode.html) ## Usage Check the [autoware_test_node](../../demos/autoware_test_node/README.md) package for an example of how to use `autoware::Node`. - -## Design - -### Lifecycle - -AN inherits from ROS 2 [rclcpp_lifecycle::LifecycleNode](https://design.ros2.org/articles/node_lifecycle.html) and has -all the basic functions of it. diff --git a/common/autoware_node/include/autoware/node/node.hpp b/common/autoware_node/include/autoware/node/node.hpp index 4531bf48c7..369e39b6b1 100644 --- a/common/autoware_node/include/autoware/node/node.hpp +++ b/common/autoware_node/include/autoware/node/node.hpp @@ -17,24 +17,19 @@ #include "autoware/node/visibility_control.hpp" -#include +#include #include namespace autoware::node { -using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn; - -class Node : public rclcpp_lifecycle::LifecycleNode +class Node : public rclcpp::Node { public: AUTOWARE_NODE_PUBLIC explicit Node( const std::string & node_name, const std::string & ns = "", const rclcpp::NodeOptions & options = rclcpp::NodeOptions()); - -protected: - CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override; }; } // namespace autoware::node diff --git a/common/autoware_node/package.xml b/common/autoware_node/package.xml index 8a328c0105..19eb4a0273 100644 --- a/common/autoware_node/package.xml +++ b/common/autoware_node/package.xml @@ -4,13 +4,13 @@ autoware_node 0.2.0 Autoware Node is an Autoware.Core package designed to provide a base class for all nodes in the system. - M. Fatih Cırıt + Mete Fatih Cırıt Apache-2.0 ament_cmake_auto autoware_cmake - rclcpp_lifecycle + rclcpp ament_cmake_ros autoware_lint_common diff --git a/common/autoware_node/src/node.cpp b/common/autoware_node/src/node.cpp index 385d3731d0..d02341fcbe 100644 --- a/common/autoware_node/src/node.cpp +++ b/common/autoware_node/src/node.cpp @@ -13,7 +13,7 @@ // limitations under the License. #include -#include +#include #include @@ -21,18 +21,10 @@ namespace autoware::node { Node::Node( const std::string & node_name, const std::string & ns, const rclcpp::NodeOptions & options) -: LifecycleNode(node_name, ns, options) +: rclcpp::Node(node_name, ns, options) { RCLCPP_DEBUG( get_logger(), "Node %s constructor was called.", get_node_base_interface()->get_fully_qualified_name()); } - -CallbackReturn Node::on_shutdown(const rclcpp_lifecycle::State & state) -{ - RCLCPP_DEBUG( - get_logger(), "Node %s shutdown was called with state %s.", - get_node_base_interface()->get_fully_qualified_name(), state.label().c_str()); - return CallbackReturn::SUCCESS; -} } // namespace autoware::node diff --git a/common/autoware_node/test/test_an_init_shutdown.cpp b/common/autoware_node/test/test_an_init_shutdown.cpp index 1224c17c92..01fd5127fe 100644 --- a/common/autoware_node/test/test_an_init_shutdown.cpp +++ b/common/autoware_node/test/test_an_init_shutdown.cpp @@ -15,11 +15,10 @@ #include #include -#include - #include #include +#include class AutowareNodeInitShutdown : public ::testing::Test { @@ -31,30 +30,44 @@ class AutowareNodeInitShutdown : public ::testing::Test rclcpp::NodeOptions node_options_an_; }; +// Helper function to wait until the executor starts spinning with a timeout. +bool wait_for_executor_spinning( + const std::shared_ptr & executor, + std::chrono::milliseconds timeout) +{ + auto start_time = std::chrono::steady_clock::now(); + while (!executor->is_spinning()) { + if (std::chrono::steady_clock::now() - start_time > timeout) { + return false; + } + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + return true; +} + TEST_F(AutowareNodeInitShutdown, NodeInitShutdown) { - autoware::node::Node::SharedPtr autoware_node = - std::make_shared("test_node", "test_ns", node_options_an_); + const std::string node_name = "test_node"; + const std::string node_ns = "test_ns"; + + auto autoware_node = std::make_shared(node_name, node_ns, node_options_an_); auto executor = std::make_shared(); executor->add_node(autoware_node->get_node_base_interface()); - std::thread thread_spin = std::thread([&executor]() { executor->spin(); }); - - ASSERT_EQ( - autoware_node->get_current_state().id(), - lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED); + // Start the executor in a separate thread. + std::thread executor_thread([executor]() { executor->spin(); }); - auto state = autoware_node->shutdown(); + ASSERT_EQ(autoware_node->get_fully_qualified_name(), "/" + node_ns + "/" + node_name); - ASSERT_EQ(state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED); + // Wait until the executor starts spinning (timeout after 2 seconds). + ASSERT_TRUE(wait_for_executor_spinning(executor, std::chrono::milliseconds(2000))) + << "Executor did not start spinning within the expected timeout."; - // wait until executor is spinning - while (!executor->is_spinning()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - executor->cancel(); // make sure cancel is called after spin - if (thread_spin.joinable()) { - thread_spin.join(); + executor->cancel(); + if (executor_thread.joinable()) { + executor_thread.join(); } + + ASSERT_FALSE(executor->is_spinning()); } diff --git a/demos/autoware_test_node/README.md b/demos/autoware_test_node/README.md index 91a07cd710..1d1f5bf856 100644 --- a/demos/autoware_test_node/README.md +++ b/demos/autoware_test_node/README.md @@ -7,47 +7,3 @@ This package contains a simple example of how to use `autoware::Node`. ```bash ros2 launch autoware_test_node autoware_test_node.launch.xml ``` - -### Lifecycle control - -Information on Lifecycle nodes can be found [here](https://design.ros2.org/articles/node_lifecycle.html). - -Output a list of nodes with lifecycle: - -```console -$ ros2 lifecycle nodes -/test_ns1/test_node1 -``` - -Get the current state of a node: - -```console -$ ros2 lifecycle get /test_ns1/test_node1 -unconfigured [1] -``` - -List the available transitions for the node: - -```console -$ ros2 lifecycle list /test_ns1/test_node1 -- configure [1] - Start: unconfigured - Goal: configuring -- shutdown [5] - Start: unconfigured - Goal: shuttingdown -``` - -Shutdown the node: - -```console -$ ros2 lifecycle set /test_ns1/test_node1 shutdown -Transitioning successful -``` - -```console -$ ros2 lifecycle get /test_ns1/test_node1 -finalized [4] -``` - -The node will remain alive in the `finalized` state until it is killed by the user. diff --git a/demos/autoware_test_node/package.xml b/demos/autoware_test_node/package.xml index ccf92468ab..8e573f435c 100644 --- a/demos/autoware_test_node/package.xml +++ b/demos/autoware_test_node/package.xml @@ -4,7 +4,7 @@ autoware_test_node 0.2.0 Test package for Autoware Node. - M. Fatih Cırıt + Mete Fatih Cırıt Apache-2.0 ament_cmake_auto @@ -13,7 +13,6 @@ autoware_node rclcpp rclcpp_components - rclcpp_lifecycle ament_cmake diff --git a/demos/autoware_test_node/src/include/test_node.hpp b/demos/autoware_test_node/src/include/test_node.hpp index 027171d2d8..03d2f31d49 100644 --- a/demos/autoware_test_node/src/include/test_node.hpp +++ b/demos/autoware_test_node/src/include/test_node.hpp @@ -16,7 +16,6 @@ #define TEST_NODE_HPP_ #include -#include namespace autoware::test_node {