15 #ifndef NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_
16 #define NAV2_CORE__BEHAVIOR_TREE_NAVIGATOR_HPP_
24 #include "nav2_util/odometry_utils.hpp"
25 #include "nav2_ros_common/tf2_factories.hpp"
26 #include "rclcpp/rclcpp.hpp"
27 #include "nav2_ros_common/lifecycle_node.hpp"
28 #include "pluginlib/class_loader.hpp"
29 #include "nav2_behavior_tree/bt_action_server.hpp"
30 #include "nav2_ros_common/node_utils.hpp"
42 std::string robot_frame;
43 std::string global_frame;
44 double transform_tolerance;
45 nav2::TransformBuffer::SharedPtr tf;
66 nav2::LifecycleNode::WeakPtr parent_node,
67 const std::vector<std::string> & plugin_lib_names,
70 std::shared_ptr<nav2_util::OdomSmoother> odom_smoother) = 0;
90 virtual void preempt() = 0;
105 : current_navigator_(nullptr) {}
113 std::scoped_lock l(mutex_);
114 return current_navigator_ !=
nullptr;
123 std::scoped_lock l(mutex_);
124 if (current_navigator_ !=
nullptr) {
126 rclcpp::get_logger(
"NavigatorMutex"),
127 "Major error! Navigation requested while another navigation"
128 " task is in progress! This likely occurred from an incorrect"
129 "implementation of a navigator plugin.");
131 current_navigator_ = navigator;
140 std::scoped_lock l(mutex_);
141 if (current_navigator_ != navigator) {
143 rclcpp::get_logger(
"NavigatorMutex"),
144 "Major error! Navigation stopped while another navigation"
145 " task is in progress! This likely occurred from an incorrect"
146 "implementation of a navigator plugin.");
148 current_navigator_ =
nullptr;
152 void preemptCurrentNavigator()
154 std::scoped_lock l(mutex_);
155 current_navigator_->preempt();
165 std::scoped_lock l(mutex_);
166 return current_navigator_ == navigator;
180 template<
class ActionT>
184 using Ptr = std::shared_ptr<nav2_core::BehaviorTreeNavigator<ActionT>>;
192 plugin_muxer_ =
nullptr;
211 nav2::LifecycleNode::WeakPtr parent_node,
212 const std::vector<std::string> & plugin_lib_names,
215 std::shared_ptr<nav2_util::OdomSmoother> odom_smoother)
final
217 auto node = parent_node.lock();
218 logger_ = node->get_logger();
219 clock_ = node->get_clock();
220 feedback_utils_ = feedback_utils;
221 plugin_muxer_ = plugin_muxer;
224 std::string default_bt_xml_filename = getDefaultBTFilepath(parent_node);
226 auto search_directories = node->declare_or_get_parameter(
227 "bt_search_directories",
228 std::vector<std::string>{nav2::get_package_share_directory(
229 "nav2_bt_navigator") +
"/behavior_trees"}
232 allow_navigator_preemption_ = node->declare_or_get_parameter(
233 "allow_navigator_preemption",
false);
235 navigator_preemption_timeout_ = std::chrono::milliseconds(
236 node->declare_or_get_parameter(
"navigator_preemption_timeout", 500));
240 std::make_unique<nav2_behavior_tree::BtActionServer<ActionT, nav2::LifecycleNode>>(
244 default_bt_xml_filename,
250 std::placeholders::_1, std::placeholders::_2),
254 if (!bt_action_server_->on_configure()) {
258 BT::Blackboard::Ptr blackboard = bt_action_server_->getBlackboard();
259 blackboard->set(
"tf_buffer", feedback_utils.tf);
260 blackboard->set(
"initial_pose_received",
false);
261 blackboard->set(
"number_recoveries", 0);
262 blackboard->set(
"odom_smoother", odom_smoother);
264 return configure(parent_node, odom_smoother) && ok;
275 if (!bt_action_server_->on_activate()) {
289 if (!bt_action_server_->on_deactivate()) {
303 if (!bt_action_server_->on_cleanup()) {
307 bt_action_server_.reset();
312 virtual std::string getDefaultBTFilepath(nav2::LifecycleNode::WeakPtr node) = 0;
322 bt_action_server_->preemptCurrentNavigator();
333 if (!allow_navigator_preemption_) {
336 "Requested navigation from %s while another navigator is processing,"
337 " rejecting request.",
getName().c_str());
343 "Requested navigation from %s while another navigator is processing,"
344 " stopping current navigator.",
getName().c_str());
345 plugin_muxer_->preemptCurrentNavigator();
347 const auto start = std::chrono::steady_clock::now();
350 if (std::chrono::steady_clock::now() - start > navigator_preemption_timeout_) {
353 "Timed out waiting for current navigator to stop before accepting"
354 " goal from %s. Rejecting request.",
getName().c_str());
367 return goal_accepted;
374 typename ActionT::Result::SharedPtr result,
375 nav2_behavior_tree::BtStatus & final_bt_status)
386 virtual bool goalReceived(
typename ActionT::Goal::ConstSharedPtr goal) = 0;
397 virtual void onPreempt(
typename ActionT::Goal::ConstSharedPtr goal) = 0;
404 typename ActionT::Result::SharedPtr result,
405 nav2_behavior_tree::BtStatus & final_bt_status) = 0;
411 nav2::LifecycleNode::WeakPtr ,
412 std::shared_ptr<nav2_util::OdomSmoother>)
432 std::unique_ptr<nav2_behavior_tree::BtActionServer<ActionT, nav2::LifecycleNode>>
434 rclcpp::Logger logger_{rclcpp::get_logger(
"Navigator")};
435 rclcpp::Clock::SharedPtr clock_;
436 FeedbackUtils feedback_utils_;
437 NavigatorMuxer * plugin_muxer_;
440 bool allow_navigator_preemption_;
443 std::chrono::milliseconds navigator_preemption_timeout_;
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.