t05_crossdoor.cpp
Go to the documentation of this file.
1 #include "crossdoor_nodes.h"
2 
7 
8 #ifdef ZMQ_FOUND
10 #endif
11 
22 // clang-format off
23 
24 static const char* xml_text = R"(
25 <root main_tree_to_execute = "MainTree">
26  <!--------------------------------------->
27  <BehaviorTree ID="DoorClosed">
28  <Sequence name="door_closed_sequence">
29  <Inverter>
30  <Condition ID="IsDoorOpen"/>
31  </Inverter>
32  <RetryUntilSuccesful num_attempts="4">
33  <OpenDoor/>
34  </RetryUntilSuccesful>
35  <PassThroughDoor/>
36  </Sequence>
37  </BehaviorTree>
38  <!--------------------------------------->
39  <BehaviorTree ID="MainTree">
40  <Sequence>
41  <Fallback name="root_Fallback">
42  <Sequence name="door_open_sequence">
43  <IsDoorOpen/>
44  <PassThroughDoor/>
45  </Sequence>
46  <SubTree ID="DoorClosed"/>
47  <PassThroughWindow/>
48  </Fallback>
49  <CloseDoor/>
50  </Sequence>
51  </BehaviorTree>
52  <!--------------------------------------->
53 </root>
54  )";
55 
56 // clang-format on
57 
58 using namespace BT;
59 
60 int main(int argc, char** argv)
61 {
63 
64  // register all the actions into the factory
65  CrossDoor::RegisterNodes(factory);
66 
67  // Important: when the object tree goes out of scope, all the TreeNodes are destroyed
68  auto tree = factory.createTreeFromText(xml_text);
69 
70  // This logger prints state changes on console
71  StdCoutLogger logger_cout(tree);
72 
73  // This logger saves state changes on file
74  FileLogger logger_file(tree, "bt_trace.fbl");
75 
76  // This logger stores the execution time of each node
77  MinitraceLogger logger_minitrace(tree, "bt_trace.json");
78 
79 #ifdef ZMQ_FOUND
80  // This logger publish status changes using ZeroMQ. Used by Groot
81  PublisherZMQ publisher_zmq(tree);
82 #endif
83 
84  printTreeRecursively(tree.rootNode());
85 
86  const bool LOOP = ( argc == 2 && strcmp( argv[1], "loop") == 0);
87 
88  do
89  {
91  // Keep on ticking until you get either a SUCCESS or FAILURE state
92  while( status == NodeStatus::RUNNING)
93  {
94  status = tree.tickRoot();
95  CrossDoor::SleepMS(1); // optional sleep to avoid "busy loops"
96  }
97  CrossDoor::SleepMS(1000);
98  }
99  while(LOOP);
100 
101  return 0;
102 }
int main(int argc, char **argv)
void SleepMS(int ms)
void RegisterNodes(BT::BehaviorTreeFactory &factory)
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
static const char * xml_text
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
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...


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