Nav2 Navigation Stack - humble  humble
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 <set>
21 #include <limits>
22 #include <vector>
23 
24 #include "nav2_util/geometry_utils.hpp"
25 #include "nav2_util/robot_utils.hpp"
26 #include "nav2_behavior_tree/bt_conversions.hpp"
27 
28 namespace nav2_bt_navigator
29 {
30 
31 BtNavigator::BtNavigator(rclcpp::NodeOptions options)
32 : nav2_util::LifecycleNode("bt_navigator", "",
33  options.automatically_declare_parameters_from_overrides(true))
34 {
35  RCLCPP_INFO(get_logger(), "Creating");
36 
37  const std::vector<std::string> plugin_libs = {
38  "nav2_compute_path_to_pose_action_bt_node",
39  "nav2_compute_path_through_poses_action_bt_node",
40  "nav2_compute_and_track_route_bt_node",
41  "nav2_compute_route_bt_node",
42  "nav2_smooth_path_action_bt_node",
43  "nav2_follow_path_action_bt_node",
44  "nav2_spin_action_bt_node",
45  "nav2_wait_action_bt_node",
46  "nav2_assisted_teleop_action_bt_node",
47  "nav2_back_up_action_bt_node",
48  "nav2_drive_on_heading_bt_node",
49  "nav2_clear_costmap_service_bt_node",
50  "nav2_is_stuck_condition_bt_node",
51  "nav2_goal_reached_condition_bt_node",
52  "nav2_initial_pose_received_condition_bt_node",
53  "nav2_goal_updated_condition_bt_node",
54  "nav2_globally_updated_goal_condition_bt_node",
55  "nav2_is_path_valid_condition_bt_node",
56  "nav2_reinitialize_global_localization_service_bt_node",
57  "nav2_rate_controller_bt_node",
58  "nav2_distance_controller_bt_node",
59  "nav2_speed_controller_bt_node",
60  "nav2_truncate_path_action_bt_node",
61  "nav2_truncate_path_local_action_bt_node",
62  "nav2_goal_updater_node_bt_node",
63  "nav2_recovery_node_bt_node",
64  "nav2_pipeline_sequence_bt_node",
65  "nav2_round_robin_node_bt_node",
66  "nav2_transform_available_condition_bt_node",
67  "nav2_time_expired_condition_bt_node",
68  "nav2_path_expiring_timer_condition",
69  "nav2_distance_traveled_condition_bt_node",
70  "nav2_single_trigger_bt_node",
71  "nav2_goal_updated_controller_bt_node",
72  "nav2_is_battery_low_condition_bt_node",
73  "nav2_navigate_through_poses_action_bt_node",
74  "nav2_navigate_to_pose_action_bt_node",
75  "nav2_remove_passed_goals_action_bt_node",
76  "nav2_planner_selector_bt_node",
77  "nav2_controller_selector_bt_node",
78  "nav2_goal_checker_selector_bt_node",
79  "nav2_controller_cancel_bt_node",
80  "nav2_path_longer_on_approach_bt_node",
81  "nav2_wait_cancel_bt_node",
82  "nav2_spin_cancel_bt_node",
83  "nav2_assisted_teleop_cancel_bt_node",
84  "nav2_back_up_cancel_bt_node",
85  "nav2_drive_on_heading_cancel_bt_node",
86  "nav2_is_battery_charging_condition_bt_node"
87  };
88 
89  declare_parameter_if_not_declared(
90  this, "plugin_lib_names", rclcpp::ParameterValue(plugin_libs));
91  declare_parameter_if_not_declared(
92  this, "transform_tolerance", rclcpp::ParameterValue(0.1));
93  declare_parameter_if_not_declared(
94  this, "global_frame", rclcpp::ParameterValue(std::string("map")));
95  declare_parameter_if_not_declared(
96  this, "robot_base_frame", rclcpp::ParameterValue(std::string("base_link")));
97  declare_parameter_if_not_declared(
98  this, "odom_topic", rclcpp::ParameterValue(std::string("odom")));
99 }
100 
102 {
103 }
104 
105 nav2_util::CallbackReturn
106 BtNavigator::on_configure(const rclcpp_lifecycle::State & /*state*/)
107 {
108  RCLCPP_INFO(get_logger(), "Configuring");
109 
110  tf_ = std::make_shared<tf2_ros::Buffer>(get_clock());
111  auto timer_interface = std::make_shared<tf2_ros::CreateTimerROS>(
112  get_node_base_interface(), get_node_timers_interface());
113  tf_->setCreateTimerInterface(timer_interface);
114  tf_->setUsingDedicatedThread(true);
115  tf_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_, this, false);
116 
117  global_frame_ = get_parameter("global_frame").as_string();
118  robot_frame_ = get_parameter("robot_base_frame").as_string();
119  transform_tolerance_ = get_parameter("transform_tolerance").as_double();
120  odom_topic_ = get_parameter("odom_topic").as_string();
121 
122  // Libraries to pull plugins (BT Nodes) from
123  auto plugin_lib_names = get_parameter("plugin_lib_names").as_string_array();
124 
125  pose_navigator_ = std::make_unique<nav2_bt_navigator::NavigateToPoseNavigator>();
126  poses_navigator_ = std::make_unique<nav2_bt_navigator::NavigateThroughPosesNavigator>();
127 
128  nav2_bt_navigator::FeedbackUtils feedback_utils;
129  feedback_utils.tf = tf_;
130  feedback_utils.global_frame = global_frame_;
131  feedback_utils.robot_frame = robot_frame_;
132  feedback_utils.transform_tolerance = transform_tolerance_;
133 
134  // Odometry smoother object for getting current speed
135  odom_smoother_ = std::make_shared<nav2_util::OdomSmoother>(shared_from_this(), 0.3, odom_topic_);
136 
137  if (!pose_navigator_->on_configure(
138  shared_from_this(), plugin_lib_names, feedback_utils, &plugin_muxer_, odom_smoother_))
139  {
140  return nav2_util::CallbackReturn::FAILURE;
141  }
142 
143  if (!poses_navigator_->on_configure(
144  shared_from_this(), plugin_lib_names, feedback_utils, &plugin_muxer_, odom_smoother_))
145  {
146  return nav2_util::CallbackReturn::FAILURE;
147  }
148 
149  return nav2_util::CallbackReturn::SUCCESS;
150 }
151 
152 nav2_util::CallbackReturn
153 BtNavigator::on_activate(const rclcpp_lifecycle::State & /*state*/)
154 {
155  RCLCPP_INFO(get_logger(), "Activating");
156 
157  if (!poses_navigator_->on_activate() || !pose_navigator_->on_activate()) {
158  return nav2_util::CallbackReturn::FAILURE;
159  }
160 
161  // create bond connection
162  createBond();
163 
164  return nav2_util::CallbackReturn::SUCCESS;
165 }
166 
167 nav2_util::CallbackReturn
168 BtNavigator::on_deactivate(const rclcpp_lifecycle::State & /*state*/)
169 {
170  RCLCPP_INFO(get_logger(), "Deactivating");
171 
172  if (!poses_navigator_->on_deactivate() || !pose_navigator_->on_deactivate()) {
173  return nav2_util::CallbackReturn::FAILURE;
174  }
175 
176  // destroy bond connection
177  destroyBond();
178 
179  return nav2_util::CallbackReturn::SUCCESS;
180 }
181 
182 nav2_util::CallbackReturn
183 BtNavigator::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
184 {
185  RCLCPP_INFO(get_logger(), "Cleaning up");
186 
187  // Reset the listener before the buffer
188  tf_listener_.reset();
189  tf_.reset();
190 
191  if (!poses_navigator_->on_cleanup() || !pose_navigator_->on_cleanup()) {
192  return nav2_util::CallbackReturn::FAILURE;
193  }
194 
195  poses_navigator_.reset();
196  pose_navigator_.reset();
197 
198  RCLCPP_INFO(get_logger(), "Completed Cleaning up");
199  return nav2_util::CallbackReturn::SUCCESS;
200 }
201 
202 nav2_util::CallbackReturn
203 BtNavigator::on_shutdown(const rclcpp_lifecycle::State & /*state*/)
204 {
205  RCLCPP_INFO(get_logger(), "Shutting down");
206  return nav2_util::CallbackReturn::SUCCESS;
207 }
208 
209 } // namespace nav2_bt_navigator
210 
211 #include "rclcpp_components/register_node_macro.hpp"
212 
213 // Register the component with class_loader.
214 // This acts as a sort of entry point, allowing the component to be discoverable when its library
215 // is being loaded into a running process.
216 RCLCPP_COMPONENTS_REGISTER_NODE(nav2_bt_navigator::BtNavigator)
An action server that uses behavior tree for navigating a robot to its goal position.
nav2_util::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_util::CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override
Activates action server.
nav2_util::CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override
Resets member variables.
~BtNavigator()
A destructor for nav2_bt_navigator::BtNavigator class.
nav2_util::CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override
Called when in shutdown state.
nav2_util::CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override
Deactivates action server.
std::shared_ptr< nav2_util::LifecycleNode > shared_from_this()
Get a shared pointer of this.
void createBond()
Create bond connection to lifecycle manager.
void destroyBond()
Destroy bond connection to lifecycle manager.
Navigator feedback utilities required to get transforms and reference frames.
Definition: navigator.hpp:38