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 BTCPP_format="4" >
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",
63  [&](TreeNode&) { return CheckBattery(); });
64 
65  //You can also create SimpleActionNodes using methods of a class
66  GripperInterface gripper;
67  factory.registerSimpleAction("OpenGripper", [&](TreeNode&) { return gripper.open(); });
68  factory.registerSimpleAction("CloseGripper",
69  [&](TreeNode&) { return gripper.close(); });
70 
71 #else
72  // Load dynamically a plugin and register the TreeNodes it contains
73  // it automated the registering step.
74  factory.registerFromPlugin("../sample_nodes/bin/libdummy_nodes_dyn.so");
75 #endif
76 
77  // Trees are created at deployment-time (i.e. at run-time, but only once at the beginning).
78  // The currently supported format is XML.
79  // IMPORTANT: when the object "tree" goes out of scope, all the TreeNodes are destroyed
80  auto tree = factory.createTreeFromText(xml_text);
81 
82  // To "execute" a Tree you need to "tick" it.
83  // The tick is propagated to the children based on the logic of the tree.
84  // In this case, the entire sequence is executed, because all the children
85  // of the Sequence return SUCCESS.
86  tree.tickWhileRunning();
87 
88  return 0;
89 }
90 
91 /* Expected output:
92 *
93  [ Battery: OK ]
94  GripperInterface::open
95  ApproachObject: approach_object
96  GripperInterface::close
97 */
BT
Definition: ex01_wrap_legacy.cpp:29
DummyNodes::GripperInterface::open
NodeStatus open()
Definition: dummy_nodes.cpp:31
BT::BehaviorTreeFactory::registerFromPlugin
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:192
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
main
int main()
Definition: t01_build_your_first_tree.cpp:39
DummyNodes::CheckBattery
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13
bt_factory.h
DummyNodes
Definition: dummy_nodes.cpp:10
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:610
xml_text
static const char * xml_text
Definition: t01_build_your_first_tree.cpp:21
dummy_nodes.h
DummyNodes::ApproachObject
Definition: dummy_nodes.h:35
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
DummyNodes::GripperInterface
Definition: dummy_nodes.h:17
DummyNodes::GripperInterface::close
NodeStatus close()
Definition: dummy_nodes.cpp:38
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::BehaviorTreeFactory::registerSimpleCondition
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:153
BT::BehaviorTreeFactory::registerSimpleAction
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:166


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17