t05_crossdoor.cpp
Go to the documentation of this file.
1 #include "crossdoor_nodes.h"
7 
8 #ifdef ZMQ_FOUND
10 #endif
11 
12 // clang-format off
13 
14 const std::string xml_text = R"(
15 <root main_tree_to_execute = "MainTree">
16  <!--------------------------------------->
17  <BehaviorTree ID="DoorClosed">
18  <Sequence name="door_closed_sequence">
19  <Inverter>
20  <Condition ID="IsDoorOpen"/>
21  </Inverter>
22  <RetryUntilSuccesful num_attempts="4">
23  <OpenDoor/>
24  </RetryUntilSuccesful>
25  <PassThroughDoor/>
26  </Sequence>
27  </BehaviorTree>
28  <!--------------------------------------->
29  <BehaviorTree ID="MainTree">
30  <Fallback name="root_Fallback">
31  <Sequence name="door_open_sequence">
32  <IsDoorOpen/>
33  <PassThroughDoor/>
34  </Sequence>
35  <SubTree ID="DoorClosed"/>
36  <PassThroughWindow/>
37  </Fallback>
38  </BehaviorTree>
39  <!--------------------------------------->
40 </root>
41  )";
42 
43 // clang-format on
44 
45 using namespace BT;
46 
47 int main()
48 {
50 
51  // The state of the door is read/written using these keys of the blackboard.
52  auto blackboard = Blackboard::create<BlackboardLocal>();
53  blackboard->set("door_open", false);
54  blackboard->set("door_locked", true);
55 
56  // register all the actions into the factory
57  CrossDoor::RegisterNodes(factory);
58 
59  // Important: when the object tree goes out of scope, all the TreeNodes are destroyed
60  auto tree = buildTreeFromText(factory, xml_text, blackboard);
61 
62  // Create some loggers
63  StdCoutLogger logger_cout(tree.root_node);
64  MinitraceLogger logger_minitrace(tree.root_node, "bt_trace.json");
65  FileLogger logger_file(tree.root_node, "bt_trace.fbl");
66 #ifdef ZMQ_FOUND
67  PublisherZMQ publisher_zmq(tree.root_node);
68 #endif
69 
70  printTreeRecursively(tree.root_node);
71 
72  //while (1)
73  {
75  // Keep on ticking until you get either a SUCCESS or FAILURE state
76  while( status == NodeStatus::RUNNING)
77  {
78  status = tree.root_node->executeTick();
79  CrossDoor::SleepMS(1); // optional sleep to avoid "busy loops"
80  }
81  CrossDoor::SleepMS(2000);
82  }
83  return 0;
84 }
int main()
void SleepMS(int ms)
void printTreeRecursively(const TreeNode *root_node)
void RegisterNodes(BT::BehaviorTreeFactory &factory)
NodeStatus
Definition: basic_types.h:28
const std::string xml_text
AddStdCoutLoggerToTree. Give the root node of a tree, a simple callback is subscribed to any status c...
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 Sun Feb 3 2019 03:14:32