Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
lifecycle_node.cpp
1 // Copyright (c) 2019 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "nav2_util/lifecycle_node.hpp"
16 
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 #include "lifecycle_msgs/msg/state.hpp"
22 
23 namespace nav2_util
24 {
25 
27  const std::string & node_name,
28  const std::string & ns,
29  const rclcpp::NodeOptions & options)
30 : rclcpp_lifecycle::LifecycleNode(node_name, ns, options)
31 {
32  // server side never times out from lifecycle manager
33  this->declare_parameter(bond::msg::Constants::DISABLE_HEARTBEAT_TIMEOUT_PARAM, true);
34  this->set_parameter(
35  rclcpp::Parameter(
36  bond::msg::Constants::DISABLE_HEARTBEAT_TIMEOUT_PARAM, true));
37 
39 
41 }
42 
43 LifecycleNode::~LifecycleNode()
44 {
45  RCLCPP_INFO(get_logger(), "Destroying");
46 
47  runCleanups();
48 
49  if (rcl_preshutdown_cb_handle_) {
50  rclcpp::Context::SharedPtr context = get_node_base_interface()->get_context();
51  context->remove_pre_shutdown_callback(*(rcl_preshutdown_cb_handle_.get()));
52  rcl_preshutdown_cb_handle_.reset();
53  }
54 }
55 
57 {
58  RCLCPP_INFO(get_logger(), "Creating bond (%s) to lifecycle manager.", this->get_name());
59 
60  bond_ = std::make_unique<bond::Bond>(
61  std::string("bond"),
62  this->get_name(),
64 
65  bond_->setHeartbeatPeriod(0.10);
66  bond_->setHeartbeatTimeout(4.0);
67  bond_->start();
68 }
69 
71 {
72  /*
73  * In case this lifecycle node wasn't properly shut down, do it here.
74  * We will give the user some ability to clean up properly here, but it's
75  * best effort; i.e. we aren't trying to account for all possible states.
76  */
77  if (get_current_state().id() ==
78  lifecycle_msgs::msg::State::PRIMARY_STATE_ACTIVE)
79  {
80  this->deactivate();
81  }
82 
83  if (get_current_state().id() ==
84  lifecycle_msgs::msg::State::PRIMARY_STATE_INACTIVE)
85  {
86  this->cleanup();
87  }
88 }
89 
91 {
92  RCLCPP_INFO(
93  get_logger(), "Running Nav2 LifecycleNode rcl preshutdown (%s)",
94  this->get_name());
95 
96  runCleanups();
97 
98  destroyBond();
99 }
100 
102 {
103  rclcpp::Context::SharedPtr context = get_node_base_interface()->get_context();
104 
105  rcl_preshutdown_cb_handle_ = std::make_unique<rclcpp::PreShutdownCallbackHandle>(
106  context->add_pre_shutdown_callback(
107  std::bind(&LifecycleNode::on_rcl_preshutdown, this))
108  );
109 }
110 
112 {
113  RCLCPP_INFO(get_logger(), "Destroying bond (%s) to lifecycle manager.", this->get_name());
114 
115  if (bond_) {
116  bond_.reset();
117  }
118 }
119 
121 {
122  RCLCPP_INFO(
123  get_logger(),
124  "\n\t%s lifecycle node launched. \n"
125  "\tWaiting on external lifecycle transitions to activate\n"
126  "\tSee https://design.ros2.org/articles/node_lifecycle.html for more information.", get_name());
127 }
128 
129 } // namespace nav2_util
A lifecycle node wrapper to enable common Nav2 needs such as manipulating parameters.
LifecycleNode(const std::string &node_name, const std::string &ns="", const rclcpp::NodeOptions &options=rclcpp::NodeOptions())
A lifecycle node constructor.
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.