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