t02_factory_tree.cpp
Go to the documentation of this file.
3 
4 //#define MANUAL_STATIC_LINKING
5 
6 #ifdef MANUAL_STATIC_LINKING
7 #include "dummy_nodes.h"
8 #endif
9 
10 using namespace BT;
11 
12 // clang-format off
13 const std::string xml_text = R"(
14 
15  <root main_tree_to_execute = "MainTree" >
16 
17  <BehaviorTree ID="MainTree">
18  <Sequence name="root_sequence">
19  <SayHello name="action_hello"/>
20  <OpenGripper name="open_gripper"/>
21  <ApproachObject name="approach_object"/>
22  <CloseGripper name="close_gripper"/>
23  </Sequence>
24  </BehaviorTree>
25 
26  </root>
27  )";
28 
29 // clang-format on
30 
31 int main()
32 {
33  /* In this example we build a tree at run-time.
34  * The tree is defined using an XML (see xml_text).
35  * To achieve this we must first register our TreeNodes into
36  * a BehaviorTreeFactory.
37  */
38  BehaviorTreeFactory factory;
39 
40  /* There are two ways to register nodes:
41  * - statically, including directly DummyNodes.
42  * - dynamically, loading the TreeNodes from a shared library (plugin).
43  * */
44 
45 #ifdef MANUAL_STATIC_LINKING
46  // Note: the name used to register should be the same used in the XML.
47  // Note that the same operations could be done using DummyNodes::RegisterNodes(factory)
48 
49  using namespace DummyNodes;
50 
51  factory.registerSimpleAction("SayHello", std::bind(SayHello));
52  factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
53  factory.registerSimpleCondition("CheckTemperature", std::bind(CheckTemperature));
54 
55  GripperInterface gripper;
56  factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &gripper));
57  factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &gripper));
58 
59  factory.registerNodeType<ApproachObject>("ApproachObject");
60 
61 #else
62  // Load dynamically a plugin and register the TreeNodes it contains
63  factory.registerFromPlugin("./libdummy_nodes.so");
64 #endif
65 
66  // IMPORTANT: when the object tree goes out of scope, all the TreeNodes are destroyed
67  auto tree = buildTreeFromText(factory, xml_text);
68 
69  // The tick is propagated to all the children.
70  // until one of the returns FAILURE or RUNNING.
71  // In this case all of the return SUCCESS
72  tree.root_node->executeTick();
73 
74  return 0;
75 }
int main()
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:98
NodeStatus SayHello()
Definition: test_package.cpp:6
const std::string xml_text
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor)
Register a SimpleConditionNode.
Definition: bt_factory.cpp:74
BT::NodeStatus CheckTemperature()
Definition: dummy_nodes.cpp:24
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor)
Register a SimpleActionNode.
Definition: bt_factory.cpp:85
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:107
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:18
Tree buildTreeFromText(const BehaviorTreeFactory &factory, const std::string &text, const Blackboard::Ptr &blackboard=Blackboard::Ptr())


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53