Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
behavior_tree_engine.hpp
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 #ifndef NAV2_BEHAVIOR_TREE__BEHAVIOR_TREE_ENGINE_HPP_
17 #define NAV2_BEHAVIOR_TREE__BEHAVIOR_TREE_ENGINE_HPP_
18 
19 #include <memory>
20 #include <string>
21 #include <vector>
22 
23 #include "behaviortree_cpp/behavior_tree.h"
24 #include "behaviortree_cpp/bt_factory.h"
25 #include "behaviortree_cpp/loggers/groot2_publisher.h"
26 #include "behaviortree_cpp/xml_parsing.h"
27 
28 #include "nav2_ros_common/lifecycle_node.hpp"
29 
30 namespace nav2_behavior_tree
31 {
32 
37 enum class BtStatus { SUCCEEDED, FAILED, CANCELED };
38 
43 struct BTInfo
44 {
45  std::string main_id;
46  std::vector<std::string> behavior_tree_ids;
47 };
48 
54 {
55 public:
60  explicit BehaviorTreeEngine(
61  const std::vector<std::string> & plugin_libraries,
62  nav2::LifecycleNode::SharedPtr node);
63  virtual ~BehaviorTreeEngine() {}
64 
73  BtStatus run(
74  BT::Tree * tree,
75  std::function<void()> onLoop,
76  std::function<bool()> cancelRequested,
77  std::chrono::milliseconds loopTimeout = std::chrono::milliseconds(10));
78 
85  BT::Tree createTreeFromText(
86  const std::string & xml_string,
87  BT::Blackboard::Ptr blackboard);
88 
95  BT::Tree createTreeFromFile(
96  const std::string & file_path,
97  BT::Blackboard::Ptr blackboard);
98 
104  BTInfo parseTreeInfo(const std::string & filename);
105 
112  BT::Tree createTree(
113  const std::string & tree_id,
114  BT::Blackboard::Ptr blackboard);
115 
121  void addGrootMonitoring(BT::Tree * tree, uint16_t server_port);
122 
126  void resetGrootMonitor();
127 
132  void registerTreeFromFile(const std::string & file_path);
133 
138  void haltAllActions(BT::Tree & tree);
139 
140 protected:
141  // The factory that will be used to dynamically construct the behavior tree
142  BT::BehaviorTreeFactory factory_;
143 
144  // Node handle used to obtain clocks at run time
145  nav2::LifecycleNode::WeakPtr node_;
146 
147  // Groot2 monitor
148  std::unique_ptr<BT::Groot2Publisher> groot_monitor_;
149 };
150 
151 } // namespace nav2_behavior_tree
152 
153 #endif // NAV2_BEHAVIOR_TREE__BEHAVIOR_TREE_ENGINE_HPP_
A class to create and handle behavior trees.
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.
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.
BTInfo parseTreeInfo(const std::string &filename)
Function to parse Behavior Tree information from an XML file.
A struct to hold Behavior Tree ID information.