t05_crossdoor.cpp
Go to the documentation of this file.
1 #include "crossdoor_nodes.h"
2 
7 
8 #ifdef ZMQ_FOUND
10 #endif
11 
21 // clang-format off
22 
23 static const char* xml_text = R"(
24 <root main_tree_to_execute = "MainTree">
25  <!--------------------------------------->
26  <BehaviorTree ID="DoorClosed">
27  <Sequence name="door_closed_sequence">
28  <Inverter>
29  <Condition ID="IsDoorOpen"/>
30  </Inverter>
31  <RetryUntilSuccessful num_attempts="4">
32  <OpenDoor/>
33  </RetryUntilSuccessful>
34  <PassThroughDoor/>
35  </Sequence>
36  </BehaviorTree>
37  <!--------------------------------------->
38  <BehaviorTree ID="MainTree">
39  <Sequence>
40  <Fallback name="root_Fallback">
41  <Sequence name="door_open_sequence">
42  <IsDoorOpen/>
43  <PassThroughDoor/>
44  </Sequence>
45  <SubTree ID="DoorClosed"/>
46  <PassThroughWindow/>
47  </Fallback>
48  <CloseDoor/>
49  </Sequence>
50  </BehaviorTree>
51  <!--------------------------------------->
52 </root>
53  )";
54 
55 // clang-format on
56 
57 using namespace BT;
58 
59 int main(int argc, char** argv)
60 {
62 
63  // Register our custom nodes into the factory.
64  // Any default nodes provided by the BT library (such as Fallback) are registered by
65  // default in the BehaviorTreeFactory.
66  CrossDoor::RegisterNodes(factory);
67 
68  // Important: when the object tree goes out of scope, all the TreeNodes are destroyed
69  auto tree = factory.createTreeFromText(xml_text);
70 
71  // This logger prints state changes on console
72  StdCoutLogger logger_cout(tree);
73 
74  // This logger saves state changes on file
75  FileLogger logger_file(tree, "bt_trace.fbl");
76 
77  // This logger stores the execution time of each node
78  MinitraceLogger logger_minitrace(tree, "bt_trace.json");
79 
80 #ifdef ZMQ_FOUND
81  // This logger publish status changes using ZeroMQ. Used by Groot
82  PublisherZMQ publisher_zmq(tree);
83 #endif
84 
85  printTreeRecursively(tree.rootNode());
86 
87  const bool LOOP = (argc == 2 && strcmp(argv[1], "loop") == 0);
88 
89  NodeStatus status = tree.tickRoot();
90  while(LOOP || status == NodeStatus::RUNNING)
91  {
92  tree.sleep(std::chrono::milliseconds(10));
93  status = tree.tickRoot();
94  }
95 
96  return 0;
97 }
int main(int argc, char **argv)
void RegisterNodes(BT::BehaviorTreeFactory &factory)
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
static const char * xml_text
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
NodeStatus
Definition: basic_types.h:35
AddStdCoutLoggerToTree. Give the root node of a tree, a simple callback is subscribed to any status c...


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14