Repository navigation
Expand file tree
/
Copy patht04_reactive_sequence.cpp
More file actions
106 lines (82 loc) · 2.67 KB
/
Copy patht04_reactive_sequence.cpp
File metadata and controls
106 lines (82 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "dummy_nodes.h"
#include "movebase_node.h"
#include "behaviortree_cpp/bt_factory.h"
using namespace BT;
/** This tutorial will teach you:
*
* - The difference between Sequence and ReactiveSequence
*
* - How to create an asynchronous ActionNode.
*/
// clang-format off
namespace
{
const char* xml_text_sequence = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="MainTree">
<Sequence name="root">
<BatteryOK/>
<SaySomething message="mission started..." />
<MoveBase goal="1;2;3"/>
<SaySomething message="mission completed!" />
</Sequence>
</BehaviorTree>
</root>
)";
const char* xml_text_reactive = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="MainTree">
<ReactiveSequence name="root">
<BatteryOK/>
<Sequence>
<SaySomething message="mission started..." />
<MoveBase goal="1;2;3"/>
<SaySomething message="mission completed!" />
</Sequence>
</ReactiveSequence>
</BehaviorTree>
</root>
)";
} // namespace
// clang-format on
using namespace DummyNodes;
int main()
{
BehaviorTreeFactory factory;
factory.registerSimpleCondition("BatteryOK", std::bind(CheckBattery));
factory.registerNodeType<MoveBaseAction>("MoveBase");
factory.registerNodeType<SaySomething>("SaySomething");
// Compare the state transitions and messages using either
// xml_text_sequence and xml_text_reactive.
// The main difference that you should notice is:
// 1) When Sequence is used, the ConditionNode is executed only __once__ because it returns SUCCESS.
// 2) When ReactiveSequence is used, BatteryOK is executed at __each__ tick()
for(auto& xml_text : { xml_text_sequence, xml_text_reactive })
{
std::cout << "\n------------ BUILDING A NEW TREE ------------\n\n";
auto tree = factory.createTreeFromText(xml_text);
NodeStatus status = NodeStatus::IDLE;
#if 0
// Tick the root until we receive either SUCCESS or RUNNING
// same as: tree.tickRoot(Tree::WHILE_RUNNING)
std::cout << "--- ticking\n";
status = tree.tickWhileRunning();
std::cout << "--- status: " << toStr(status) << "\n\n";
#else
// If we need to run code between one tick() and the next,
// we can implement our own while loop
while(status != NodeStatus::SUCCESS)
{
std::cout << "--- ticking\n";
status = tree.tickOnce();
std::cout << "--- status: " << toStr(status) << "\n\n";
// if still running, add some wait time
if(status == NodeStatus::RUNNING)
{
tree.sleep(std::chrono::milliseconds(100));
}
}
#endif
}
return 0;
}