Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
behavior_tree_navigator.hpp
1 // Copyright (c) 2021-2023 Samsung Research America
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 #ifndef NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_
16 #define NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 #include <mutex>
22 #include <chrono>
23 
24 #include "nav2_util/odometry_utils.hpp"
25 #include "nav2_ros_common/tf2_factories.hpp"
26 #include "rclcpp/clock.hpp"
27 #include "rclcpp/logger.hpp"
28 #include "rclcpp/logging.hpp"
29 #include "rclcpp/rate.hpp"
30 #include "nav2_ros_common/lifecycle_node.hpp"
31 #include "pluginlib/class_loader.hpp"
32 #include "nav2_behavior_tree/bt_action_server.hpp"
33 #include "nav2_ros_common/node_utils.hpp"
34 
35 namespace nav2_core
36 {
37 class NavigatorMuxer;
38 
44 {
45  std::string robot_frame;
46  std::string global_frame;
47  double transform_tolerance;
48  nav2::TransformBuffer::SharedPtr tf;
49 };
50 
59 {
60 public:
61  NavigatorBase() = default;
62  virtual ~NavigatorBase() = default;
63 
68  virtual bool on_configure(
69  nav2::LifecycleNode::WeakPtr parent_node,
70  const std::vector<std::string> & plugin_lib_names,
71  const FeedbackUtils & feedback_utils,
72  nav2_core::NavigatorMuxer * plugin_muxer,
73  std::shared_ptr<nav2_util::OdomSmoother> odom_smoother) = 0;
74 
79  virtual bool on_activate() = 0;
80 
85  virtual bool on_deactivate() = 0;
86 
91  virtual bool on_cleanup() = 0;
92 
93  virtual void preempt() = 0;
94 };
95 
102 {
103 public:
108  : current_navigator_(nullptr) {}
109 
115  {
116  std::scoped_lock l(mutex_);
117  return current_navigator_ != nullptr;
118  }
119 
125  {
126  std::scoped_lock l(mutex_);
127  if (current_navigator_ != nullptr) {
128  RCLCPP_ERROR(
129  rclcpp::get_logger("NavigatorMutex"),
130  "Major error! Navigation requested while another navigation"
131  " task is in progress! This likely occurred from an incorrect"
132  "implementation of a navigator plugin.");
133  }
134  current_navigator_ = navigator;
135  }
136 
142  {
143  std::scoped_lock l(mutex_);
144  if (current_navigator_ != navigator) {
145  RCLCPP_ERROR(
146  rclcpp::get_logger("NavigatorMutex"),
147  "Major error! Navigation stopped while another navigation"
148  " task is in progress! This likely occurred from an incorrect"
149  "implementation of a navigator plugin.");
150  } else {
151  current_navigator_ = nullptr;
152  }
153  }
154 
155  void preemptCurrentNavigator()
156  {
157  std::scoped_lock l(mutex_);
158  current_navigator_->preempt();
159  }
160 
167  {
168  std::scoped_lock l(mutex_);
169  return current_navigator_ == navigator;
170  }
171 
172 protected:
173  nav2_core::NavigatorBase * current_navigator_{nullptr};
174  std::mutex mutex_;
175 };
176 
183 template<class ActionT>
185 {
186 public:
187  using Ptr = std::shared_ptr<nav2_core::BehaviorTreeNavigator<ActionT>>;
188 
193  : NavigatorBase()
194  {
195  plugin_muxer_ = nullptr;
196  }
197 
201  virtual ~BehaviorTreeNavigator() = default;
202 
214  nav2::LifecycleNode::WeakPtr parent_node,
215  const std::vector<std::string> & plugin_lib_names,
216  const FeedbackUtils & feedback_utils,
217  nav2_core::NavigatorMuxer * plugin_muxer,
218  std::shared_ptr<nav2_util::OdomSmoother> odom_smoother) final
219  {
220  auto node = parent_node.lock();
221  logger_ = node->get_logger();
222  clock_ = node->get_clock();
223  feedback_utils_ = feedback_utils;
224  plugin_muxer_ = plugin_muxer;
225 
226  // get the default behavior tree for this navigator
227  std::string default_bt_xml_filename = getDefaultBTFilepath(parent_node);
228 
229  const auto bt_tree_directory =
230  nav2::get_package_share_directory("nav2_bt_navigator") + "/behavior_trees";
231  auto search_directories = node->declare_or_get_parameter(
232  "bt_search_directories",
233  std::vector<std::string>{bt_tree_directory, bt_tree_directory + "/subtrees"}
234  );
235 
236  allow_navigator_preemption_ = node->declare_or_get_parameter(
237  "allow_navigator_preemption", false);
238 
239  navigator_preemption_timeout_ = std::chrono::milliseconds(
240  node->declare_or_get_parameter("navigator_preemption_timeout", 500));
241 
242  // Create the Behavior Tree Action Server for this navigator
243  bt_action_server_ =
244  std::make_unique<nav2_behavior_tree::BtActionServer<ActionT, nav2::LifecycleNode>>(
245  node,
246  getName(),
247  plugin_lib_names,
248  default_bt_xml_filename,
249  std::bind(&BehaviorTreeNavigator::onGoalReceived, this, std::placeholders::_1),
250  std::bind(&BehaviorTreeNavigator::onLoop, this),
251  std::bind(&BehaviorTreeNavigator::onPreempt, this, std::placeholders::_1),
252  std::bind(
254  std::placeholders::_1, std::placeholders::_2),
255  search_directories);
256 
257  bool ok = true;
258  if (!bt_action_server_->on_configure()) {
259  ok = false;
260  }
261 
262  BT::Blackboard::Ptr blackboard = bt_action_server_->getBlackboard();
263  blackboard->set("tf_buffer", feedback_utils.tf); // NOLINT
264  blackboard->set("initial_pose_received", false); // NOLINT
265  blackboard->set("number_recoveries", 0); // NOLINT
266  blackboard->set("odom_smoother", odom_smoother); // NOLINT
267 
268  return configure(parent_node, odom_smoother) && ok;
269  }
270 
275  bool on_activate() final
276  {
277  bool ok = true;
278 
279  if (!bt_action_server_->on_activate()) {
280  ok = false;
281  }
282 
283  return activate() && ok;
284  }
285 
290  bool on_deactivate() final
291  {
292  bool ok = true;
293  if (!bt_action_server_->on_deactivate()) {
294  ok = false;
295  }
296 
297  return deactivate() && ok;
298  }
299 
304  bool on_cleanup() final
305  {
306  bool ok = true;
307  if (!bt_action_server_->on_cleanup()) {
308  ok = false;
309  }
310 
311  bt_action_server_.reset();
312 
313  return cleanup() && ok;
314  }
315 
316  virtual std::string getDefaultBTFilepath(nav2::LifecycleNode::WeakPtr node) = 0;
317 
322  virtual std::string getName() = 0;
323 
324  void preempt() final
325  {
326  bt_action_server_->preemptCurrentNavigator();
327  }
328 
329 protected:
333  bool onGoalReceived(typename ActionT::Goal::ConstSharedPtr goal)
334  {
335  if (plugin_muxer_->isNavigating() && !plugin_muxer_->isCurrentNavigator(this)) {
336  // A different navigator is processing; check if cross-navigator preemption is allowed
337  if (!allow_navigator_preemption_) {
338  RCLCPP_ERROR(
339  logger_,
340  "Requested navigation from %s while another navigator is processing,"
341  " rejecting request.", getName().c_str());
342  return false;
343  }
344 
345  RCLCPP_INFO(
346  logger_,
347  "Requested navigation from %s while another navigator is processing,"
348  " stopping current navigator.", getName().c_str());
349  plugin_muxer_->preemptCurrentNavigator();
350 
351  const auto start = std::chrono::steady_clock::now();
352  rclcpp::Rate r(100);
353  while (plugin_muxer_->isNavigating()) {
354  if (std::chrono::steady_clock::now() - start > navigator_preemption_timeout_) {
355  RCLCPP_ERROR(
356  logger_,
357  "Timed out waiting for current navigator to stop before accepting"
358  " goal from %s. Rejecting request.", getName().c_str());
359  return false;
360  }
361  r.sleep();
362  }
363  }
364 
365  bool goal_accepted = goalReceived(goal);
366 
367  if (goal_accepted) {
368  plugin_muxer_->startNavigating(this);
369  }
370 
371  return goal_accepted;
372  }
373 
378  typename ActionT::Result::SharedPtr result,
379  nav2_behavior_tree::BtStatus & final_bt_status)
380  {
381  plugin_muxer_->stopNavigating(this);
382  goalCompleted(result, final_bt_status);
383  }
384 
390  virtual bool goalReceived(typename ActionT::Goal::ConstSharedPtr goal) = 0;
391 
396  virtual void onLoop() = 0;
397 
401  virtual void onPreempt(typename ActionT::Goal::ConstSharedPtr goal) = 0;
402 
407  virtual void goalCompleted(
408  typename ActionT::Result::SharedPtr result,
409  nav2_behavior_tree::BtStatus & final_bt_status) = 0;
410 
414  virtual bool configure(
415  nav2::LifecycleNode::WeakPtr /*node*/,
416  std::shared_ptr<nav2_util::OdomSmoother>/*odom_smoother*/)
417  {
418  return true;
419  }
420 
424  virtual bool cleanup() {return true;}
425 
429  virtual bool activate() {return true;}
430 
434  virtual bool deactivate() {return true;}
435 
436  std::unique_ptr<nav2_behavior_tree::BtActionServer<ActionT, nav2::LifecycleNode>>
437  bt_action_server_;
438  rclcpp::Logger logger_{rclcpp::get_logger("Navigator")};
439  rclcpp::Clock::SharedPtr clock_;
440  FeedbackUtils feedback_utils_;
441  NavigatorMuxer * plugin_muxer_;
442 
443  // True if you want to allow navigator preemption
444  bool allow_navigator_preemption_;
445 
446  // Timeout value while waiting for preemption of a navigator by another one
447  std::chrono::milliseconds navigator_preemption_timeout_;
448 };
449 
450 } // namespace nav2_core
451 
452 #endif // NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_
Navigator interface that acts as a base class for all BT-based Navigator action's plugins All methods...
virtual void goalCompleted(typename ActionT::Result::SharedPtr result, nav2_behavior_tree::BtStatus &final_bt_status)=0
A callback that is called when a the action is completed; Can fill in action result message or indica...
virtual void onPreempt(typename ActionT::Goal::ConstSharedPtr goal)=0
A callback that is called when a preempt is requested.
void onCompletion(typename ActionT::Result::SharedPtr result, nav2_behavior_tree::BtStatus &final_bt_status)
An intermediate completion function to mux navigators.
BehaviorTreeNavigator()
A Navigator constructor.
virtual ~BehaviorTreeNavigator()=default
Virtual destructor.
virtual std::string getName()=0
Get the action name of this navigator to expose.
bool onGoalReceived(typename ActionT::Goal::ConstSharedPtr goal)
An intermediate goal reception function to mux navigators.
bool on_cleanup() final
Cleanup a navigator.
virtual bool deactivate()
Method to deactivate and any threads involved in execution.
virtual void onLoop()=0
A callback that defines execution that happens on one iteration through the BT Can be used to publish...
virtual bool activate()
Method to activate any threads involved in execution.
bool on_deactivate() final
Deactivation of the navigator's backend BT and actions.
virtual bool cleanup()
Method to cleanup resources.
virtual bool configure(nav2::LifecycleNode::WeakPtr, std::shared_ptr< nav2_util::OdomSmoother >)
virtual bool goalReceived(typename ActionT::Goal::ConstSharedPtr goal)=0
A callback to be called when a new goal is received by the BT action server Can be used to check if g...
bool on_activate() final
Activation of the navigator's backend BT and actions.
bool on_configure(nav2::LifecycleNode::WeakPtr parent_node, const std::vector< std::string > &plugin_lib_names, const FeedbackUtils &feedback_utils, nav2_core::NavigatorMuxer *plugin_muxer, std::shared_ptr< nav2_util::OdomSmoother > odom_smoother) final
Configuration to setup the navigator's backend BT and actions.
Navigator interface to allow navigators to be stored in a vector and accessed via pluginlib due to te...
virtual bool on_cleanup()=0
Cleanup a navigator.
virtual bool on_deactivate()=0
Deactivation of the navigator's backend BT and actions.
virtual bool on_activate()=0
Activation of the navigator's backend BT and actions.
virtual bool on_configure(nav2::LifecycleNode::WeakPtr parent_node, const std::vector< std::string > &plugin_lib_names, const FeedbackUtils &feedback_utils, nav2_core::NavigatorMuxer *plugin_muxer, std::shared_ptr< nav2_util::OdomSmoother > odom_smoother)=0
Configuration of the navigator's backend BT and actions.
A class to control the state of the BT navigator by allowing only a single plugin to be processed at ...
void stopNavigating(nav2_core::NavigatorBase *navigator)
Stop navigating with a given navigator.
void startNavigating(nav2_core::NavigatorBase *navigator)
Start navigating with a given navigator.
bool isNavigating()
Get the navigator muxer state.
NavigatorMuxer()
A Navigator Muxer constructor.
bool isCurrentNavigator(nav2_core::NavigatorBase *navigator)
Check if the given navigator is the currently active navigator.
Navigator feedback utilities required to get transforms and reference frames.