t04_reactive_sequence.cpp
Go to the documentation of this file.
2 
3 #include "dummy_nodes.h"
4 #include "movebase_node.h"
5 
6 using namespace BT;
7 
16 // clang-format off
17 
18 static const char* xml_text_sequence = R"(
19 
20  <root main_tree_to_execute = "MainTree" >
21 
22  <BehaviorTree ID="MainTree">
23  <Sequence name="root">
24  <BatteryOK/>
25  <SaySomething message="mission started..." />
26  <MoveBase goal="1;2;3"/>
27  <SaySomething message="mission completed!" />
28  </Sequence>
29  </BehaviorTree>
30 
31  </root>
32  )";
33 
34 static const char* xml_text_reactive = R"(
35 
36  <root main_tree_to_execute = "MainTree" >
37 
38  <BehaviorTree ID="MainTree">
39  <ReactiveSequence name="root">
40  <BatteryOK/>
41  <Sequence>
42  <SaySomething message="mission started..." />
43  <MoveBase goal="1;2;3"/>
44  <SaySomething message="mission completed!" />
45  </Sequence>
46  </ReactiveSequence>
47  </BehaviorTree>
48 
49  </root>
50  )";
51 
52 // clang-format on
53 
54 void Assert(bool condition)
55 {
56  if (!condition)
57  throw RuntimeError("this is not what I expected");
58 }
59 
60 int main()
61 {
62  using namespace DummyNodes;
63 
64  BehaviorTreeFactory factory;
65  factory.registerSimpleCondition("BatteryOK", std::bind(CheckBattery));
66  factory.registerNodeType<MoveBaseAction>("MoveBase");
67  factory.registerNodeType<SaySomething>("SaySomething");
68 
69  // Compare the state transitions and messages using either
70  // xml_text_sequence and xml_text_sequence_star
71 
72  // The main difference that you should notice is:
73  // 1) When Sequence is used, BatteryOK is executed at __each__ tick()
74  // 2) When SequenceStar is used, those ConditionNodes are executed only __once__.
75 
77  {
78  std::cout << "\n------------ BUILDING A NEW TREE ------------" << std::endl;
79 
80  auto tree = factory.createTreeFromText(xml_text);
81 
82  NodeStatus status;
83 
84  std::cout << "\n--- 1st executeTick() ---" << std::endl;
85  status = tree.tickRoot();
86  Assert(status == NodeStatus::RUNNING);
87 
88  SleepMS(150);
89  std::cout << "\n--- 2nd executeTick() ---" << std::endl;
90  status = tree.tickRoot();
91  Assert(status == NodeStatus::RUNNING);
92 
93  SleepMS(150);
94  std::cout << "\n--- 3rd executeTick() ---" << std::endl;
95  status = tree.tickRoot();
96  Assert(status == NodeStatus::SUCCESS);
97 
98  std::cout << std::endl;
99  }
100  return 0;
101 }
102 
103 /*
104  Expected output:
105 
106 ------------ BUILDING A NEW TREE ------------
107 
108 --- 1st executeTick() ---
109 [ Battery: OK ]
110 Robot says: "mission started..."
111 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
112 
113 --- 2nd executeTick() ---
114 [ Battery: OK ]
115 [ MoveBase: FINISHED ]
116 
117 --- 3rd executeTick() ---
118 [ Battery: OK ]
119 Robot says: "mission completed!"
120 
121 
122 ------------ BUILDING A NEW TREE ------------
123 
124 --- 1st executeTick() ---
125 [ Battery: OK ]
126 Robot says: "mission started..."
127 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
128 
129 --- 2nd executeTick() ---
130 [ MoveBase: FINISHED ]
131 
132 --- 3rd executeTick() ---
133 Robot says: "mission completed!"
134 
135 */
static const char * xml_text_reactive
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:290
void Assert(bool condition)
void SleepMS(int ms)
Definition: movebase_node.h:12
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
NodeStatus tickRoot()
Definition: bt_factory.h:181
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
int main()
static const char * xml_text_sequence
static const char * xml_text
NodeStatus
Definition: basic_types.h:35
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:99
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13


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