t01_build_your_first_tree.cpp
Go to the documentation of this file.
2 
3 //#define MANUAL_STATIC_LINKING
4 
5 #ifdef MANUAL_STATIC_LINKING
6 #include "dummy_nodes.h"
7 #endif
8 
9 using namespace BT;
10 
20 // clang-format off
21 static const char* xml_text = R"(
22 
23  <root main_tree_to_execute = "MainTree" >
24 
25  <BehaviorTree ID="MainTree">
26  <Sequence name="root_sequence">
27  <CheckBattery name="battery_ok"/>
28  <OpenGripper name="open_gripper"/>
29  <ApproachObject name="approach_object"/>
30  <CloseGripper name="close_gripper"/>
31  </Sequence>
32  </BehaviorTree>
33 
34  </root>
35  )";
36 
37 // clang-format on
38 
39 int main()
40 {
41  // We use the BehaviorTreeFactory to register our custom nodes
42  BehaviorTreeFactory factory;
43 
44  /* There are two ways to register nodes:
45  * - statically, i.e. registering all the nodes one by one.
46  * - dynamically, loading the TreeNodes from a shared library (plugin).
47  * */
48 
49 #ifdef MANUAL_STATIC_LINKING
50  // Note: the name used to register should be the same used in the XML.
51  // Note that the same operations could be done using DummyNodes::RegisterNodes(factory)
52 
53  using namespace DummyNodes;
54 
55  // The recommended way to create a Node is through inheritance.
56  // Even if it requires more boilerplate, it allows you to use more functionalities
57  // like ports (we will discuss this in future tutorials).
58  factory.registerNodeType<ApproachObject>("ApproachObject");
59 
60  // Registering a SimpleActionNode using a function pointer.
61  // you may also use C++11 lambdas instead of std::bind
62  factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
63 
64  //You can also create SimpleActionNodes using methods of a class
65  GripperInterface gripper;
66  factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &gripper));
67  factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &gripper));
68 
69 #else
70  // Load dynamically a plugin and register the TreeNodes it contains
71  // it automated the registering step.
72  factory.registerFromPlugin("./libdummy_nodes_dyn.so");
73 #endif
74 
75  // Trees are created at deployment-time (i.e. at run-time, but only once at the beginning).
76  // The currently supported format is XML.
77  // IMPORTANT: when the object "tree" goes out of scope, all the TreeNodes are destroyed
78  auto tree = factory.createTreeFromText(xml_text);
79 
80  // To "execute" a Tree you need to "tick" it.
81  // The tick is propagated to the children based on the logic of the tree.
82  // In this case, the entire sequence is executed, because all the children
83  // of the Sequence return SUCCESS.
84  tree.tickRoot();
85 
86  return 0;
87 }
88 
89 /* Expected output:
90 *
91  [ Battery: OK ]
92  GripperInterface::open
93  ApproachObject: approach_object
94  GripperInterface::close
95 */
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:290
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
NodeStatus tickRoot()
Definition: bt_factory.h:181
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
void registerFromPlugin(const std::string &file_path)
registerFromPlugin load a shared library and execute the function BT_REGISTER_NODES (see macro)...
Definition: bt_factory.cpp:135
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleAction help you register nodes of type SimpleActionNode.
Definition: bt_factory.cpp:111
static const char * xml_text
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleCondition help you register nodes of type SimpleConditionNode.
Definition: bt_factory.cpp:99
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25