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