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


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05