Nav2 Navigation Stack - jazzy  jazzy
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/xml_parsing.h"
26 
27 #include "rclcpp/rclcpp.hpp"
28 
29 namespace nav2_behavior_tree
30 {
31 
36 enum class BtStatus { SUCCEEDED, FAILED, CANCELED };
37 
43 {
44 public:
49  explicit BehaviorTreeEngine(
50  const std::vector<std::string> & plugin_libraries,
51  rclcpp::Node::SharedPtr node);
52  virtual ~BehaviorTreeEngine() {}
53 
62  BtStatus run(
63  BT::Tree * tree,
64  std::function<void()> onLoop,
65  std::function<bool()> cancelRequested,
66  std::chrono::milliseconds loopTimeout = std::chrono::milliseconds(10));
67 
74  BT::Tree createTreeFromText(
75  const std::string & xml_string,
76  BT::Blackboard::Ptr blackboard);
77 
84  BT::Tree createTreeFromFile(
85  const std::string & file_path,
86  BT::Blackboard::Ptr blackboard);
87 
92  void haltAllActions(BT::Tree & tree);
93 
94 protected:
95  // The factory that will be used to dynamically construct the behavior tree
96  BT::BehaviorTreeFactory factory_;
97 
98  // Clock
99  rclcpp::Clock::SharedPtr clock_;
100 };
101 
102 } // namespace nav2_behavior_tree
103 
104 #endif // NAV2_BEHAVIOR_TREE__BEHAVIOR_TREE_ENGINE_HPP_
A class to create and handle behavior trees.
BehaviorTreeEngine(const std::vector< std::string > &plugin_libraries, rclcpp::Node::SharedPtr node)
A constructor for nav2_behavior_tree::BehaviorTreeEngine.
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.