Skip to content

Commit 2361662

Browse files
change the behavior of tickOnce to actually loop is wake up signal is… (#522)
* change the behavior of tickOnce to actually loop is wake up signal is received * fix warning
1 parent acdc6e9 commit 2361662

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

include/behaviortree_cpp/bt_factory.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,15 @@ class Tree
157157

158158
~Tree();
159159

160-
/// Tick the root of the tree once.
160+
/// Tick the root of the tree once, even if a node invoked
161+
/// emitWakeUpSignal()
162+
NodeStatus tickExactlyOnce();
163+
164+
/**
165+
* @brief by default, tickOnce() sends a single tick, BUT
166+
* as long as there is at least one node of the tree
167+
* invoking TreeNode::emitWakeUpSignal(), it will be ticked again.
168+
*/
161169
NodeStatus tickOnce();
162170

163171
/// Call tickOnce until the status is different from RUNNING.
@@ -179,7 +187,8 @@ class Tree
179187

180188
enum TickOption
181189
{
182-
ONCE,
190+
EXACTLY_ONCE,
191+
ONCE_UNLESS_WOKEN_UP,
183192
WHILE_RUNNING
184193
};
185194

include/behaviortree_cpp/utils/wakeup_signal.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ namespace BT
1111
class WakeUpSignal
1212
{
1313
public:
14-
/// Return true if the
14+
/// Return true if the timeout was NOT reached and the
15+
/// signal was received.
1516
bool waitFor(std::chrono::system_clock::duration tm)
1617
{
1718
std::unique_lock<std::mutex> lk(mutex_);

src/bt_factory.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,14 @@ Tree::~Tree()
375375
haltTree();
376376
}
377377

378+
NodeStatus Tree::tickExactlyOnce()
379+
{
380+
return tickRoot(EXACTLY_ONCE, {});
381+
}
382+
378383
NodeStatus Tree::tickOnce()
379384
{
380-
return tickRoot(ONCE, {});
385+
return tickRoot(ONCE_UNLESS_WOKEN_UP, {});
381386
}
382387

383388
NodeStatus Tree::tickWhileRunning(std::chrono::milliseconds sleep_time)
@@ -416,19 +421,30 @@ NodeStatus Tree::tickRoot(TickOption opt, std::chrono::milliseconds sleep_time)
416421
{
417422
NodeStatus status = NodeStatus::IDLE;
418423

424+
if (!wake_up_)
425+
{
426+
initialize();
427+
}
428+
429+
if (!rootNode())
430+
{
431+
throw RuntimeError("Empty Tree");
432+
}
433+
419434
while (status == NodeStatus::IDLE ||
420435
(opt == TickOption::WHILE_RUNNING && status == NodeStatus::RUNNING))
421436
{
422-
if (!wake_up_)
423-
{
424-
initialize();
425-
}
437+
status = rootNode()->executeTick();
426438

427-
if (!rootNode())
439+
// Inner loop. The previous tick might have triggered the wake-up
440+
// in this case, unless TickOption::EXACTLY_ONCE, we tick again
441+
while( opt != TickOption::EXACTLY_ONCE &&
442+
status == NodeStatus::RUNNING &&
443+
wake_up_->waitFor(std::chrono::milliseconds(0)) )
428444
{
429-
throw RuntimeError("Empty Tree");
445+
status = rootNode()->executeTick();
430446
}
431-
status = rootNode()->executeTick();
447+
432448
if (status == NodeStatus::SUCCESS || status == NodeStatus::FAILURE)
433449
{
434450
rootNode()->resetStatus();

tools/bt_log_cat.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ int main(int argc, char* argv[])
2424
const size_t length = static_cast<size_t>(ftell(file));
2525
fseek(file, 0L, SEEK_SET);
2626
std::vector<char> buffer(length);
27-
fread(buffer.data(), sizeof(char), length, file);
27+
auto ret = fread(buffer.data(), sizeof(char), length, file);
28+
(void)ret;
2829
fclose(file);
2930

3031
const auto bt_header_size = flatbuffers::ReadScalar<uint32_t>(&buffer[0]);

0 commit comments

Comments
 (0)