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  using std::chrono::milliseconds;
64 
65  BehaviorTreeFactory factory;
66  factory.registerSimpleCondition("BatteryOK", std::bind(CheckBattery));
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 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 = factory.createTreeFromText(xml_text);
82 
83  // Here, instead of tree.tickRootWhileRunning(),
84  // we prefer our own loop.
85  std::cout << "--- ticking\n";
86  NodeStatus status = tree.tickRoot();
87  std::cout << "--- status: " << toStr(status) << "\n\n";
88 
89  while(status == NodeStatus::RUNNING)
90  {
91  // Sleep to avoid busy loops.
92  // do NOT use other sleep functions!
93  // Small sleep time is OK, here we use a large one only to
94  // have less messages on the console.
95  tree.sleep(std::chrono::milliseconds(100));
96 
97  std::cout << "--- ticking\n";
98  status = tree.tickRoot();
99  std::cout << "--- status: " << toStr(status) << "\n\n";
100  }
101  }
102  return 0;
103 }
104 
105 /*
106 * Expected output:
107 
108 ------------ BUILDING A NEW TREE ------------
109 --- ticking
110 [ Battery: OK ]
111 Robot says: mission started...
112 --- status: RUNNING
113 
114 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
115 --- ticking
116 --- status: RUNNING
117 
118 --- ticking
119 --- status: RUNNING
120 
121 [ MoveBase: FINISHED ]
122 --- ticking
123 Robot says: mission completed!
124 --- status: SUCCESS
125 
126 
127 ------------ BUILDING A NEW TREE ------------
128 --- ticking
129 [ Battery: OK ]
130 Robot says: mission started...
131 --- status: RUNNING
132 
133 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
134 --- ticking
135 [ Battery: OK ]
136 --- status: RUNNING
137 
138 --- ticking
139 [ Battery: OK ]
140 --- status: RUNNING
141 
142 [ MoveBase: FINISHED ]
143 --- ticking
144 [ Battery: OK ]
145 Robot says: mission completed!
146 --- status: SUCCESS
147 */
BT
Definition: ex01_wrap_legacy.cpp:29
BT::Tree::tickRoot
NodeStatus tickRoot()
tickRoot send the tick signal to the root node. It will propagate through the entire tree.
Definition: bt_factory.h:210
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
xml_text_reactive
static const char * xml_text_reactive
Definition: t04_reactive_sequence.cpp:34
Assert
void Assert(bool condition)
Definition: t04_reactive_sequence.cpp:54
DummyNodes::CheckBattery
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13
bt_factory.h
DummyNodes
Definition: dummy_nodes.cpp:10
MoveBaseAction
Definition: movebase_node.h:43
BT::toStr
std::string toStr(T value)
Definition: basic_types.h:141
dummy_nodes.h
movebase_node.h
main
int main()
Definition: t04_reactive_sequence.cpp:60
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
BT::RuntimeError
Definition: exceptions.h:58
DummyNodes::SaySomething
Definition: dummy_nodes.h:50
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:251
BT::NodeStatus::RUNNING
@ RUNNING
xml_text_sequence
static const char * xml_text_sequence
Definition: t04_reactive_sequence.cpp:18
BT::BehaviorTreeFactory::registerSimpleCondition
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:104
xml_text
static const char * xml_text
Definition: ex01_wrap_legacy.cpp:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19