Nav2 Navigation Stack - rolling  main
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 #include "tinyxml2.h" //NOLINT
22 
23 #include "rclcpp/rclcpp.hpp"
24 #include "behaviortree_cpp/json_export.h"
25 #include "behaviortree_cpp/utils/shared_library.h"
26 #include "nav2_behavior_tree/json_utils.hpp"
27 #include "nav2_behavior_tree/utils/loop_rate.hpp"
28 
29 namespace nav2_behavior_tree
30 {
31 
33  const std::vector<std::string> & plugin_libraries,
34  const nav2::LifecycleNode::SharedPtr node)
35 {
36  BT::SharedLibrary loader;
37  for (const auto & p : plugin_libraries) {
38  factory_.registerFromPlugin(loader.getOSName(p));
39  }
40 
41  // clock for throttled debug log
42  clock_ = node->get_clock();
43 
44  // FIXME: the next two line are needed for back-compatibility with BT.CPP 3.8.x
45  // Note that the can be removed, once we migrate from BT.CPP 4.5.x to 4.6+
46  BT::ReactiveSequence::EnableException(false);
47  BT::ReactiveFallback::EnableException(false);
48 }
49 
50 BtStatus
52  BT::Tree * tree,
53  std::function<void()> onLoop,
54  std::function<bool()> cancelRequested,
55  std::chrono::milliseconds loopTimeout)
56 {
57  nav2_behavior_tree::LoopRate loopRate(loopTimeout, tree);
58  BT::NodeStatus result = BT::NodeStatus::RUNNING;
59 
60  // Loop until something happens with ROS or the node completes
61  try {
62  while (rclcpp::ok() && result == BT::NodeStatus::RUNNING) {
63  if (cancelRequested()) {
64  tree->haltTree();
65  return BtStatus::CANCELED;
66  }
67 
68  result = tree->tickOnce();
69 
70  onLoop();
71 
72  if (!loopRate.sleep()) {
73  RCLCPP_DEBUG_THROTTLE(
74  rclcpp::get_logger("BehaviorTreeEngine"),
75  *clock_, 1000,
76  "Behavior Tree tick rate %0.2f was exceeded!",
77  1.0 / (loopRate.period().count() * 1.0e-9));
78  }
79  }
80  } catch (const std::exception & ex) {
81  RCLCPP_ERROR(
82  rclcpp::get_logger("BehaviorTreeEngine"),
83  "Behavior tree threw exception: %s. Exiting with failure.", ex.what());
84  return BtStatus::FAILED;
85  }
86 
87  return (result == BT::NodeStatus::SUCCESS) ? BtStatus::SUCCEEDED : BtStatus::FAILED;
88 }
89 
90 BT::Tree
92  const std::string & xml_string,
93  BT::Blackboard::Ptr blackboard)
94 {
95  return factory_.createTreeFromText(xml_string, blackboard);
96 }
97 
98 BT::Tree
100  const std::string & file_path,
101  BT::Blackboard::Ptr blackboard)
102 {
103  return factory_.createTreeFromFile(file_path, blackboard);
104 }
105 
107  const std::string & bt_file)
108 {
109  if(bt_file.empty()) {
110  RCLCPP_ERROR(rclcpp::get_logger("BehaviorTreeEngine"),
111  "Error: Empty BT file passed to extractBehaviorTreeID");
112  return "";
113  }
114  tinyxml2::XMLDocument doc;
115  if (doc.LoadFile(bt_file.c_str()) != tinyxml2::XML_SUCCESS) {
116  RCLCPP_ERROR(rclcpp::get_logger("BehaviorTreeEngine"), "Error: Could not open or parse file %s",
117  bt_file.c_str());
118  return "";
119  }
120  tinyxml2::XMLElement * rootElement = doc.RootElement();
121  if (!rootElement) {
122  RCLCPP_ERROR(rclcpp::get_logger("BehaviorTreeEngine"), "Error: Root element not found in %s",
123  bt_file.c_str());
124  return "";
125  }
126  tinyxml2::XMLElement * btElement = rootElement->FirstChildElement("BehaviorTree");
127  if (!btElement) {
128  RCLCPP_ERROR(rclcpp::get_logger("BehaviorTreeEngine"),
129  "Error: <BehaviorTree> element not found in %s", bt_file.c_str());
130  return "";
131  }
132  const char * idValue = btElement->Attribute("ID");
133  if (idValue) {
134  return std::string(idValue);
135  } else {
136  RCLCPP_ERROR(rclcpp::get_logger("BehaviorTreeEngine"),
137  "Error: ID attribute not found on <BehaviorTree> element in %s",
138  bt_file.c_str());
139  return "";
140  }
141 }
142 
143 BT::Tree
145  const std::string & tree_id,
146  BT::Blackboard::Ptr blackboard)
147 {
148  return factory_.createTree(tree_id, blackboard);
149 }
150 
153  const std::string & file_path)
154 {
155  factory_.registerBehaviorTreeFromFile(file_path);
156 }
157 
158 void
160  BT::Tree * tree,
161  uint16_t server_port)
162 {
163  // This logger publish status changes using Groot2
164  groot_monitor_ = std::make_unique<BT::Groot2Publisher>(*tree, server_port);
165 
166  // Register common types JSON definitions
167  BT::RegisterJsonDefinition<builtin_interfaces::msg::Time>();
168  BT::RegisterJsonDefinition<std_msgs::msg::Header>();
169 }
170 
171 void
173 {
174  if (groot_monitor_) {
175  groot_monitor_.reset();
176  }
177 }
178 
179 // In order to re-run a Behavior Tree, we must be able to reset all nodes to the initial state
180 void
182 {
183  // this halt signal should propagate through the entire tree.
184  tree.haltTree();
185 }
186 
187 } // namespace nav2_behavior_tree
BT::Tree createTree(const std::string &tree_id, BT::Blackboard::Ptr blackboard)
Function to create a BT from a BehaviorTree ID.
BehaviorTreeEngine(const std::vector< std::string > &plugin_libraries, nav2::LifecycleNode::SharedPtr node)
A constructor for nav2_behavior_tree::BehaviorTreeEngine.
void addGrootMonitoring(BT::Tree *tree, uint16_t server_port)
Add Groot2 monitor to publish BT status changes.
BT::Tree createTreeFromFile(const std::string &file_path, BT::Blackboard::Ptr blackboard)
Function to create a BT from an XML file.
void registerTreeFromFile(const std::string &file_path)
Function to register a BT from an XML file.
BT::Tree createTreeFromText(const std::string &xml_string, BT::Blackboard::Ptr blackboard)
Function to create a BT from a XML string.
void haltAllActions(BT::Tree &tree)
Function to explicitly reset all BT nodes to initial state.
std::string extractBehaviorTreeID(const std::string &file_or_id)
Extract BehaviorTree ID from BT file path or BT ID.
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.