-
Notifications
You must be signed in to change notification settings - Fork 717
/
Copy pathnavigation_test.cpp
320 lines (262 loc) · 7.93 KB
/
navigation_test.cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "behaviortree_cpp/xml_parsing.h"
#include "behaviortree_cpp/blackboard.h"
#include <gtest/gtest.h>
using namespace BT;
// clang-format off
static const char* xml_text = R"(
<root BTCPP_format="4" main_tree_to_execute="BehaviorTree">
<BehaviorTree ID="BehaviorTree">
<Fallback name="root">
<ReactiveSequence name="navigation_subtree">
<Inverter>
<Condition ID="IsStuck"/>
</Inverter>
<SequenceWithMemory name="navigate">
<Action ID="ComputePathToPose"/>
<Action ID="FollowPath"/>
</SequenceWithMemory>
</ReactiveSequence>
<SequenceWithMemory name="stuck_recovery">
<Condition ID="IsStuck"/>
<Action ID="BackUpAndSpin"/>
</SequenceWithMemory>
</Fallback>
</BehaviorTree>
</root>
)";
// clang-format on
using Milliseconds = std::chrono::milliseconds;
inline std::chrono::high_resolution_clock::time_point Now()
{
return std::chrono::high_resolution_clock::now();
}
//--------------------------------------------
class TestNode
{
public:
TestNode(const std::string& name) : _expected_result(true), _tick_count(0), _name(name)
{}
void setExpectedResult(bool will_succeed)
{
_expected_result = will_succeed;
}
NodeStatus expectedResult() const
{
return _expected_result ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
}
void resetTickCount()
{
_tick_count = 0;
}
int tickCount() const
{
return _tick_count;
}
NodeStatus tickImpl()
{
std::cout << _name << ": " << (_expected_result ? "true" : "false") << std::endl;
_tick_count++;
return expectedResult();
}
private:
bool _expected_result;
int _tick_count;
std::string _name;
};
class IsStuck : public ConditionNode, public TestNode
{
public:
IsStuck(const std::string& name) : ConditionNode(name, {}), TestNode(name)
{}
NodeStatus tick() override
{
return tickImpl();
}
};
class BackUpAndSpin : public SyncActionNode, public TestNode
{
public:
BackUpAndSpin(const std::string& name) : SyncActionNode(name, {}), TestNode(name)
{}
NodeStatus tick() override
{
return tickImpl();
}
};
class ComputePathToPose : public SyncActionNode, public TestNode
{
public:
ComputePathToPose(const std::string& name) : SyncActionNode(name, {}), TestNode(name)
{}
NodeStatus tick() override
{
return tickImpl();
}
};
class FollowPath : public ActionNodeBase, public TestNode
{
std::chrono::high_resolution_clock::time_point _initial_time;
public:
FollowPath(const std::string& name)
: ActionNodeBase(name, {}), TestNode(name), _halted(false)
{}
NodeStatus tick() override
{
if(status() == NodeStatus::IDLE)
{
setStatus(NodeStatus::RUNNING);
_halted = false;
std::cout << "FollowPath::started" << std::endl;
_initial_time = Now();
}
// Yield for 1 second
while(Now() < _initial_time + Milliseconds(600) || _halted)
{
return NodeStatus::RUNNING;
}
if(_halted)
{
return NodeStatus::IDLE;
}
return tickImpl();
}
void halt() override
{
std::cout << "FollowPath::halt" << std::endl;
_halted = true;
}
bool wasHalted() const
{
return _halted;
}
private:
bool _halted;
};
//-------------------------------------
template <typename Original, typename Casted>
void TryDynamicCastPtr(Original* ptr, Casted*& destination)
{
if(dynamic_cast<Casted*>(ptr))
{
destination = dynamic_cast<Casted*>(ptr);
}
}
/****************TESTS START HERE***************************/
TEST(Navigationtest, MoveBaseRecovery)
{
BehaviorTreeFactory factory;
factory.registerNodeType<IsStuck>("IsStuck");
factory.registerNodeType<BackUpAndSpin>("BackUpAndSpin");
factory.registerNodeType<ComputePathToPose>("ComputePathToPose");
factory.registerNodeType<FollowPath>("FollowPath");
auto tree = factory.createTreeFromText(xml_text);
// Need to retrieve the node pointers with dynamic cast
// In a normal application you would NEVER want to do such a thing.
IsStuck* first_stuck_node = nullptr;
IsStuck* second_stuck_node = nullptr;
BackUpAndSpin* back_spin_node = nullptr;
ComputePathToPose* compute_node = nullptr;
FollowPath* follow_node = nullptr;
for(auto& subtree : tree.subtrees)
{
for(auto& node : subtree->nodes)
{
auto ptr = node.get();
if(!first_stuck_node)
{
TryDynamicCastPtr(ptr, first_stuck_node);
}
else
{
TryDynamicCastPtr(ptr, second_stuck_node);
}
TryDynamicCastPtr(ptr, back_spin_node);
TryDynamicCastPtr(ptr, follow_node);
TryDynamicCastPtr(ptr, compute_node);
}
}
ASSERT_TRUE(first_stuck_node);
ASSERT_TRUE(second_stuck_node);
ASSERT_TRUE(back_spin_node);
ASSERT_TRUE(compute_node);
ASSERT_TRUE(follow_node);
std::cout << "-----------------------" << std::endl;
// First case: not stuck, everything fine.
NodeStatus status = NodeStatus::IDLE;
first_stuck_node->setExpectedResult(false);
while(status == NodeStatus::IDLE || status == NodeStatus::RUNNING)
{
status = tree.tickWhileRunning();
std::this_thread::sleep_for(Milliseconds(100));
}
// SUCCESS expected
ASSERT_EQ(status, NodeStatus::SUCCESS);
// IsStuck on the left branch must run several times
ASSERT_GE(first_stuck_node->tickCount(), 6);
// Never take the right branch (recovery)
ASSERT_EQ(second_stuck_node->tickCount(), 0);
ASSERT_EQ(back_spin_node->tickCount(), 0);
ASSERT_EQ(compute_node->tickCount(), 1);
ASSERT_EQ(follow_node->tickCount(), 1);
ASSERT_FALSE(follow_node->wasHalted());
std::cout << "-----------------------" << std::endl;
// Second case: get stuck after a while
// Initialize everything first
first_stuck_node->resetTickCount();
second_stuck_node->resetTickCount();
compute_node->resetTickCount();
follow_node->resetTickCount();
back_spin_node->resetTickCount();
status = NodeStatus::IDLE;
int cycle = 0;
while(status == NodeStatus::IDLE || status == NodeStatus::RUNNING)
{
// At the fifth cycle get stuck
if(++cycle == 2)
{
first_stuck_node->setExpectedResult(true);
second_stuck_node->setExpectedResult(true);
}
status = tree.tickWhileRunning();
std::this_thread::sleep_for(Milliseconds(100));
}
// SUCCESS expected
ASSERT_EQ(status, NodeStatus::SUCCESS);
// First IsStuck must run several times
ASSERT_GE(first_stuck_node->tickCount(), 2);
// Second IsStuck probably only once
ASSERT_EQ(second_stuck_node->tickCount(), 1);
ASSERT_EQ(back_spin_node->tickCount(), 1);
// compute done once and follow started but halted
ASSERT_EQ(compute_node->tickCount(), 1);
ASSERT_EQ(follow_node->tickCount(), 0); // started but never completed
ASSERT_TRUE(follow_node->wasHalted());
ASSERT_EQ(compute_node->status(), NodeStatus::IDLE);
ASSERT_EQ(follow_node->status(), NodeStatus::IDLE);
ASSERT_EQ(back_spin_node->status(), NodeStatus::IDLE);
std::cout << "-----------------------" << std::endl;
// Third case: execute again
// Initialize everything first
first_stuck_node->resetTickCount();
second_stuck_node->resetTickCount();
compute_node->resetTickCount();
follow_node->resetTickCount();
back_spin_node->resetTickCount();
status = NodeStatus::IDLE;
first_stuck_node->setExpectedResult(false);
second_stuck_node->setExpectedResult(false);
while(status == NodeStatus::IDLE || status == NodeStatus::RUNNING)
{
status = tree.tickWhileRunning();
std::this_thread::sleep_for(Milliseconds(100));
}
// SUCCESS expected
ASSERT_EQ(status, NodeStatus::SUCCESS);
ASSERT_GE(first_stuck_node->tickCount(), 6);
ASSERT_EQ(second_stuck_node->tickCount(), 0);
ASSERT_EQ(back_spin_node->tickCount(), 0);
ASSERT_EQ(compute_node->status(), NodeStatus::IDLE);
ASSERT_EQ(follow_node->status(), NodeStatus::IDLE);
ASSERT_EQ(back_spin_node->status(), NodeStatus::IDLE);
ASSERT_FALSE(follow_node->wasHalted());
}