Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
bt_navigator.cpp
1 // Copyright (c) 2018 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_bt_navigator/bt_navigator.hpp"
16 
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 #include "nav2_util/geometry_utils.hpp"
23 #include "nav2_ros_common/node_utils.hpp"
24 #include "nav2_util/string_utils.hpp"
25 #include "nav2_util/robot_utils.hpp"
26 #include "nav2_behavior_tree/bt_utils.hpp"
27 
28 #include "nav2_behavior_tree/plugins_list.hpp"
29 
30 using nav2::declare_parameter_if_not_declared;
31 
32 namespace nav2_bt_navigator
33 {
34 
35 BtNavigator::BtNavigator(rclcpp::NodeOptions options)
36 : nav2::LifecycleNode("bt_navigator", "",
37  options.automatically_declare_parameters_from_overrides(true)),
38  class_loader_("nav2_core", "nav2_core::NavigatorBase")
39 {
40  RCLCPP_INFO(get_logger(), "Creating");
41 }
42 
44 {
45 }
46 
47 nav2::CallbackReturn
48 BtNavigator::on_configure(const rclcpp_lifecycle::State & state)
49 {
50  RCLCPP_INFO(get_logger(), "Configuring");
51 
52  tf_ = std::make_shared<tf2_ros::Buffer>(get_clock());
53  auto timer_interface = std::make_shared<tf2_ros::CreateTimerROS>(
54  get_node_base_interface(), get_node_timers_interface());
55  tf_->setCreateTimerInterface(timer_interface);
56  tf_->setUsingDedicatedThread(true);
57  tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_, this, true);
58 
59  global_frame_ = this->declare_or_get_parameter("global_frame", std::string("map"));
60  robot_frame_ = this->declare_or_get_parameter("robot_base_frame", std::string("base_link"));
61  transform_tolerance_ = this->declare_or_get_parameter("transform_tolerance", 0.1);
62  odom_topic_ = this->declare_or_get_parameter("odom_topic", std::string("odom"));
63  filter_duration_ = this->declare_or_get_parameter("filter_duration", 0.3);
64 
65  // Libraries to pull plugins (BT Nodes) from
66  std::vector<std::string> plugin_lib_names;
67  plugin_lib_names = nav2_util::split(nav2::details::BT_BUILTIN_PLUGINS, ';');
68 
69  auto user_defined_plugins =
70  this->declare_or_get_parameter("plugin_lib_names", std::vector<std::string>{});
71  // append user_defined_plugins to plugin_lib_names
72  plugin_lib_names.insert(
73  plugin_lib_names.end(), user_defined_plugins.begin(),
74  user_defined_plugins.end());
75 
76  nav2_core::FeedbackUtils feedback_utils;
77  feedback_utils.tf = tf_;
78  feedback_utils.global_frame = global_frame_;
79  feedback_utils.robot_frame = robot_frame_;
80  feedback_utils.transform_tolerance = transform_tolerance_;
81 
82  // Odometry smoother object for getting current speed
83  auto node = shared_from_this();
84  odom_smoother_ = std::make_shared<nav2_util::OdomSmoother>(node, filter_duration_, odom_topic_);
85 
86  // Navigator defaults
87  const std::vector<std::string> default_navigator_ids = {
88  "navigate_to_pose",
89  "navigate_through_poses"
90  };
91  const std::vector<std::string> default_navigator_types = {
92  "nav2_bt_navigator::NavigateToPoseNavigator",
93  "nav2_bt_navigator::NavigateThroughPosesNavigator"
94  };
95 
96  std::vector<std::string> navigator_ids;
97  navigator_ids = this->declare_or_get_parameter("navigators", default_navigator_ids);
98  if (navigator_ids == default_navigator_ids) {
99  for (size_t i = 0; i < default_navigator_ids.size(); ++i) {
100  declare_parameter_if_not_declared(
101  node, default_navigator_ids[i] + ".plugin",
102  rclcpp::ParameterValue(default_navigator_types[i]));
103  }
104  }
105 
106  // Load navigator plugins
107  for (size_t i = 0; i != navigator_ids.size(); i++) {
108  try {
109  std::string navigator_type = nav2::get_plugin_type_param(node, navigator_ids[i]);
110  RCLCPP_INFO(
111  get_logger(), "Creating navigator id %s of type %s",
112  navigator_ids[i].c_str(), navigator_type.c_str());
113  navigators_.push_back(class_loader_.createUniqueInstance(navigator_type));
114  if (!navigators_.back()->on_configure(
115  node, plugin_lib_names, feedback_utils,
116  &plugin_muxer_, odom_smoother_))
117  {
118  return nav2::CallbackReturn::FAILURE;
119  }
120  } catch (const std::exception & ex) {
121  RCLCPP_FATAL(
122  get_logger(), "Failed to create navigator id %s."
123  " Exception: %s", navigator_ids[i].c_str(), ex.what());
124  on_cleanup(state);
125  return nav2::CallbackReturn::FAILURE;
126  }
127  }
128 
129  return nav2::CallbackReturn::SUCCESS;
130 }
131 
132 nav2::CallbackReturn
133 BtNavigator::on_activate(const rclcpp_lifecycle::State & state)
134 {
135  RCLCPP_INFO(get_logger(), "Activating");
136  for (size_t i = 0; i != navigators_.size(); i++) {
137  if (!navigators_[i]->on_activate()) {
138  on_deactivate(state);
139  return nav2::CallbackReturn::FAILURE;
140  }
141  }
142 
143  // create bond connection
144  createBond();
145 
146  return nav2::CallbackReturn::SUCCESS;
147 }
148 
149 nav2::CallbackReturn
150 BtNavigator::on_deactivate(const rclcpp_lifecycle::State & /*state*/)
151 {
152  RCLCPP_INFO(get_logger(), "Deactivating");
153  for (size_t i = 0; i != navigators_.size(); i++) {
154  if (!navigators_[i]->on_deactivate()) {
155  return nav2::CallbackReturn::FAILURE;
156  }
157  }
158 
159  // destroy bond connection
160  destroyBond();
161 
162  return nav2::CallbackReturn::SUCCESS;
163 }
164 
165 nav2::CallbackReturn
166 BtNavigator::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
167 {
168  RCLCPP_INFO(get_logger(), "Cleaning up");
169 
170  // Reset the listener before the buffer
171  tf_listener_.reset();
172  tf_.reset();
173 
174  for (size_t i = 0; i != navigators_.size(); i++) {
175  if (!navigators_[i]->on_cleanup()) {
176  return nav2::CallbackReturn::FAILURE;
177  }
178  }
179 
180  navigators_.clear();
181  RCLCPP_INFO(get_logger(), "Completed Cleaning up");
182  return nav2::CallbackReturn::SUCCESS;
183 }
184 
185 nav2::CallbackReturn
186 BtNavigator::on_shutdown(const rclcpp_lifecycle::State & /*state*/)
187 {
188  RCLCPP_INFO(get_logger(), "Shutting down");
189  return nav2::CallbackReturn::SUCCESS;
190 }
191 
192 } // namespace nav2_bt_navigator
193 
194 #include "rclcpp_components/register_node_macro.hpp"
195 
196 // Register the component with class_loader.
197 // This acts as a sort of entry point, allowing the component to be discoverable when its library
198 // is being loaded into a running process.
199 RCLCPP_COMPONENTS_REGISTER_NODE(nav2_bt_navigator::BtNavigator)
void destroyBond()
Destroy bond connection to lifecycle manager.
nav2::LifecycleNode::SharedPtr shared_from_this()
Get a shared pointer of this.
void createBond()
Create bond connection to lifecycle manager.
ParamType declare_or_get_parameter(const std::string &parameter_name, const ParamType &default_value, const ParameterDescriptor &parameter_descriptor=ParameterDescriptor())
Declares or gets a parameter. If the parameter is already declared, returns its value; otherwise decl...
An action server that uses behavior tree for navigating a robot to its goal position.
nav2::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
nav2::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivates action server.
nav2::CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override
Configures member variables.
BtNavigator(rclcpp::NodeOptions options=rclcpp::NodeOptions())
A constructor for nav2_bt_navigator::BtNavigator class.
nav2::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Resets member variables.
~BtNavigator()
A destructor for nav2_bt_navigator::BtNavigator class.
nav2::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activates action server.
Navigator feedback utilities required to get transforms and reference frames.