Nav2 Navigation Stack - rolling  main
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 
44 {
45 public:
50  explicit BehaviorTreeEngine(
51  const std::vector<std::string> & plugin_libraries,
52  nav2::LifecycleNode::SharedPtr node);
53  virtual ~BehaviorTreeEngine() {}
54 
63  BtStatus run(
64  BT::Tree * tree,
65  std::function<void()> onLoop,
66  std::function<bool()> cancelRequested,
67  std::chrono::milliseconds loopTimeout = std::chrono::milliseconds(10));
68 
75  BT::Tree createTreeFromText(
76  const std::string & xml_string,
77  BT::Blackboard::Ptr blackboard);
78 
85  BT::Tree createTreeFromFile(
86  const std::string & file_path,
87  BT::Blackboard::Ptr blackboard);
88 
94  void addGrootMonitoring(BT::Tree * tree, uint16_t server_port);
95 
99  void resetGrootMonitor();
100 
105  void haltAllActions(BT::Tree & tree);
106 
107 protected:
108  // The factory that will be used to dynamically construct the behavior tree
109  BT::BehaviorTreeFactory factory_;
110 
111  // Clock
112  rclcpp::Clock::SharedPtr clock_;
113 
114  // Groot2 monitor
115  std::unique_ptr<BT::Groot2Publisher> groot_monitor_;
116 };
117 
118 } // namespace nav2_behavior_tree
119 
120 #endif // NAV2_BEHAVIOR_TREE__BEHAVIOR_TREE_ENGINE_HPP_
A class to create and handle behavior trees.
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.