Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
behavior_tree_engine.cpp
1 // Copyright (c) 2018 Intel Corporation
2 // Copyright (c) 2020 Florian Gramss
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "nav2_behavior_tree/behavior_tree_engine.hpp"
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "rclcpp/rclcpp.hpp"
23 #include "behaviortree_cpp_v3/utils/shared_library.h"
24 
25 namespace nav2_behavior_tree
26 {
27 
29  const std::vector<std::string> & plugin_libraries)
30 {
31  BT::SharedLibrary loader;
32  for (const auto & p : plugin_libraries) {
33  factory_.registerFromPlugin(loader.getOSName(p));
34  }
35 }
36 
37 BtStatus
39  BT::Tree * tree,
40  std::function<void()> onLoop,
41  std::function<bool()> cancelRequested,
42  std::chrono::milliseconds loopTimeout)
43 {
44  rclcpp::WallRate loopRate(loopTimeout);
45  BT::NodeStatus result = BT::NodeStatus::RUNNING;
46 
47  // Loop until something happens with ROS or the node completes
48  try {
49  while (rclcpp::ok() && result == BT::NodeStatus::RUNNING) {
50  if (cancelRequested()) {
51  tree->rootNode()->halt();
52  return BtStatus::CANCELED;
53  }
54 
55  result = tree->tickRoot();
56 
57  onLoop();
58 
59  if (!loopRate.sleep()) {
60  RCLCPP_WARN(
61  rclcpp::get_logger("BehaviorTreeEngine"),
62  "Behavior Tree tick rate %0.2f was exceeded!",
63  1.0 / (loopRate.period().count() * 1.0e-9));
64  }
65  }
66  } catch (const std::exception & ex) {
67  RCLCPP_ERROR(
68  rclcpp::get_logger("BehaviorTreeEngine"),
69  "Behavior tree threw exception: %s. Exiting with failure.", ex.what());
70  return BtStatus::FAILED;
71  }
72 
73  return (result == BT::NodeStatus::SUCCESS) ? BtStatus::SUCCEEDED : BtStatus::FAILED;
74 }
75 
76 BT::Tree
78  const std::string & xml_string,
79  BT::Blackboard::Ptr blackboard)
80 {
81  return factory_.createTreeFromText(xml_string, blackboard);
82 }
83 
84 BT::Tree
86  const std::string & file_path,
87  BT::Blackboard::Ptr blackboard)
88 {
89  return factory_.createTreeFromFile(file_path, blackboard);
90 }
91 
92 // In order to re-run a Behavior Tree, we must be able to reset all nodes to the initial state
93 void
94 BehaviorTreeEngine::haltAllActions(BT::TreeNode * root_node)
95 {
96  if (!root_node) {
97  return;
98  }
99 
100  // this halt signal should propagate through the entire tree.
101  root_node->halt();
102 
103  // but, just in case...
104  auto visitor = [](BT::TreeNode * node) {
105  if (node->status() == BT::NodeStatus::RUNNING) {
106  node->halt();
107  }
108  };
109  BT::applyRecursiveVisitor(root_node, visitor);
110 }
111 
112 } // namespace nav2_behavior_tree
BehaviorTreeEngine(const std::vector< std::string > &plugin_libraries)
A constructor for nav2_behavior_tree::BehaviorTreeEngine.
BT::Tree createTreeFromFile(const std::string &file_path, BT::Blackboard::Ptr blackboard)
Function to create a BT from an XML file.
void haltAllActions(BT::TreeNode *root_node)
Function to explicitly reset all BT nodes to initial state.
BT::Tree createTreeFromText(const std::string &xml_string, BT::Blackboard::Ptr blackboard)
Function to create a BT from a XML string.
BtStatus run(BT::Tree *tree, std::function< void()> onLoop, std::function< bool()> cancelRequested, std::chrono::milliseconds loopTimeout=std::chrono::milliseconds(10))
Function to execute a BT at a specific rate.