15 #include "nav2_util/lifecycle_node.hpp"
21 #include "lifecycle_msgs/msg/state.hpp"
22 #include "nav2_util/node_utils.hpp"
24 using namespace std::chrono_literals;
29 LifecycleNode::LifecycleNode(
30 const std::string & node_name,
31 const std::string & ns,
32 const rclcpp::NodeOptions & options)
36 this->declare_parameter(bond::msg::Constants::DISABLE_HEARTBEAT_TIMEOUT_PARAM,
true);
39 bond::msg::Constants::DISABLE_HEARTBEAT_TIMEOUT_PARAM,
true));
41 nav2_util::declare_parameter_if_not_declared(
42 this,
"bond_heartbeat_period", rclcpp::ParameterValue(0.1));
43 this->get_parameter(
"bond_heartbeat_period", bond_heartbeat_period);
45 bool autostart_node =
false;
46 nav2_util::declare_parameter_if_not_declared(
47 this,
"autostart_node", rclcpp::ParameterValue(
false));
48 this->get_parameter(
"autostart_node", autostart_node);
58 LifecycleNode::~LifecycleNode()
60 RCLCPP_INFO(get_logger(),
"Destroying");
64 if (rcl_preshutdown_cb_handle_) {
65 rclcpp::Context::SharedPtr context = get_node_base_interface()->get_context();
66 context->remove_pre_shutdown_callback(*(rcl_preshutdown_cb_handle_.get()));
67 rcl_preshutdown_cb_handle_.reset();
73 if (bond_heartbeat_period > 0.0) {
74 RCLCPP_INFO(get_logger(),
"Creating bond (%s) to lifecycle manager.", this->get_name());
76 bond_ = std::make_unique<bond::Bond>(
81 bond_->setHeartbeatPeriod(bond_heartbeat_period);
82 bond_->setHeartbeatTimeout(4.0);
89 using lifecycle_msgs::msg::State;
90 autostart_timer_ = this->create_wall_timer(
93 autostart_timer_->cancel();
94 RCLCPP_INFO(get_logger(),
"Auto-starting node: %s", this->get_name());
95 if (configure().
id() != State::PRIMARY_STATE_INACTIVE) {
96 RCLCPP_ERROR(get_logger(),
"Auto-starting node %s failed to configure!", this->get_name());
99 if (activate().
id() != State::PRIMARY_STATE_ACTIVE) {
100 RCLCPP_ERROR(get_logger(),
"Auto-starting node %s failed to activate!", this->get_name());
112 if (get_current_state().
id() ==
113 lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
118 if (get_current_state().
id() ==
119 lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
128 get_logger(),
"Running Nav2 LifecycleNode rcl preshutdown (%s)",
138 rclcpp::Context::SharedPtr context = get_node_base_interface()->get_context();
140 rcl_preshutdown_cb_handle_ = std::make_unique<rclcpp::PreShutdownCallbackHandle>(
141 context->add_pre_shutdown_callback(
148 if (bond_heartbeat_period > 0.0) {
149 RCLCPP_INFO(get_logger(),
"Destroying bond (%s) to lifecycle manager.", this->get_name());
161 "\n\t%s lifecycle node launched. \n"
162 "\tWaiting on external lifecycle transitions to activate\n"
163 "\tSee https://design.ros2.org/articles/node_lifecycle.html for more information.", get_name());
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
void register_rcl_preshutdown_callback()
void autostart()
Automatically configure and active the node.
void printLifecycleNodeNotification()
Print notifications for lifecycle node.
std::shared_ptr< nav2_util::LifecycleNode > shared_from_this()
Get a shared pointer of this.
virtual void on_rcl_preshutdown()
Perform preshutdown activities before our Context is shutdown. Note that this is related to our Conte...
void createBond()
Create bond connection to lifecycle manager.
void destroyBond()
Destroy bond connection to lifecycle manager.