Nav2 Navigation Stack - kilted  kilted
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/json_export.h"
24 #include "behaviortree_cpp/utils/shared_library.h"
25 #include "nav2_behavior_tree/json_utils.hpp"
26 #include "nav2_behavior_tree/utils/loop_rate.hpp"
27 
28 namespace nav2_behavior_tree
29 {
30 
32  const std::vector<std::string> & plugin_libraries, rclcpp::Node::SharedPtr node)
33 {
34  BT::SharedLibrary loader;
35  for (const auto & p : plugin_libraries) {
36  factory_.registerFromPlugin(loader.getOSName(p));
37  }
38 
39  // clock for throttled debug log
40  clock_ = node->get_clock();
41 
42  // FIXME: the next two line are needed for back-compatibility with BT.CPP 3.8.x
43  // Note that the can be removed, once we migrate from BT.CPP 4.5.x to 4.6+
44  BT::ReactiveSequence::EnableException(false);
45  BT::ReactiveFallback::EnableException(false);
46 }
47 
48 BtStatus
50  BT::Tree * tree,
51  std::function<void()> onLoop,
52  std::function<bool()> cancelRequested,
53  std::chrono::milliseconds loopTimeout)
54 {
55  nav2_behavior_tree::LoopRate loopRate(loopTimeout, tree);
56  BT::NodeStatus result = BT::NodeStatus::RUNNING;
57 
58  // Loop until something happens with ROS or the node completes
59  try {
60  while (rclcpp::ok() && result == BT::NodeStatus::RUNNING) {
61  if (cancelRequested()) {
62  tree->haltTree();
63  return BtStatus::CANCELED;
64  }
65 
66  result = tree->tickOnce();
67 
68  onLoop();
69 
70  if (!loopRate.sleep()) {
71  RCLCPP_DEBUG_THROTTLE(
72  rclcpp::get_logger("BehaviorTreeEngine"),
73  *clock_, 1000,
74  "Behavior Tree tick rate %0.2f was exceeded!",
75  1.0 / (loopRate.period().count() * 1.0e-9));
76  }
77  }
78  } catch (const std::exception & ex) {
79  RCLCPP_ERROR(
80  rclcpp::get_logger("BehaviorTreeEngine"),
81  "Behavior tree threw exception: %s. Exiting with failure.", ex.what());
82  return BtStatus::FAILED;
83  }
84 
85  return (result == BT::NodeStatus::SUCCESS) ? BtStatus::SUCCEEDED : BtStatus::FAILED;
86 }
87 
88 BT::Tree
90  const std::string & xml_string,
91  BT::Blackboard::Ptr blackboard)
92 {
93  return factory_.createTreeFromText(xml_string, blackboard);
94 }
95 
96 BT::Tree
98  const std::string & file_path,
99  BT::Blackboard::Ptr blackboard)
100 {
101  return factory_.createTreeFromFile(file_path, blackboard);
102 }
103 
104 void
106  BT::Tree * tree,
107  uint16_t server_port)
108 {
109  // This logger publish status changes using Groot2
110  groot_monitor_ = std::make_unique<BT::Groot2Publisher>(*tree, server_port);
111 
112  // Register common types JSON definitions
113  BT::RegisterJsonDefinition<builtin_interfaces::msg::Time>();
114  BT::RegisterJsonDefinition<std_msgs::msg::Header>();
115 }
116 
117 void
119 {
120  if (groot_monitor_) {
121  groot_monitor_.reset();
122  }
123 }
124 
125 // In order to re-run a Behavior Tree, we must be able to reset all nodes to the initial state
126 void
128 {
129  // this halt signal should propagate through the entire tree.
130  tree.haltTree();
131 }
132 
133 } // namespace nav2_behavior_tree
BehaviorTreeEngine(const std::vector< std::string > &plugin_libraries, rclcpp::Node::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.
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.
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.