Skip to content

Commit 785b8da

Browse files
committed
fix observer
1 parent e781fcd commit 785b8da

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

examples/t10_observer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ int main()
6363
std::cout << uid << " -> " << name << std::endl;
6464
}
6565

66-
// Tick the tree multiple times, until action_B is finally ticked.
67-
// Since we use const reference, we can check this statistic periodically.
68-
const auto& action_B_stats = observer.getStatistics("last_action");
6966

70-
tree.tickOnce();
67+
tree.tickWhileRunning();
7168

69+
// You can access a specific statistic, using is full path or the UID
70+
const auto& last_action_stats = observer.getStatistics("last_action");
71+
assert(last_action_stats.transitions_count > 0);
7272

7373
std::cout << "----------------" << std::endl;
7474
// print all the statistics
7575
for(const auto& [uid, name]: ordered_UID_to_path) {
7676
const auto& stats = observer.getStatistics(uid);
7777

7878
std::cout << "[" << name
79-
<< "] \tT/S/F: " << stats.tick_count
79+
<< "] \tT/S/F: " << stats.transitions_count
8080
<< "/" << stats.success_count
8181
<< "/" << stats.failure_count
8282
<< std::endl;

include/behaviortree_cpp/loggers/bt_observer.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,20 @@ class TreeObserver : public StatusChangeLogger
2222

2323
struct NodeStatistics
2424
{
25+
// Last __valid__ result, either SUCCESS or FAILURE
2526
NodeStatus last_result = NodeStatus::IDLE;
27+
// Last status. Can be any status, including IDLE or SKIPPED
2628
NodeStatus current_status = NodeStatus::IDLE;
29+
30+
// count status transitions, excluding transition to IDLE
31+
unsigned transitions_count = 0;
32+
// count number of transitions to SUCCESS
2733
unsigned success_count = 0;
34+
// count number of transitions to FAILURE
2835
unsigned failure_count = 0;
29-
unsigned tick_count = 0;
36+
// count number of transitions to SKIPPED
37+
unsigned skip_count = 0;
38+
3039
Duration last_timestamp = {};
3140
};
3241

src/loggers/bt_observer.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,29 @@ void TreeObserver::callback(Duration timestamp, const TreeNode& node,
5151
NodeStatus /*prev_status*/, NodeStatus status)
5252
{
5353
auto& statistics = _statistics[node.UID()];
54-
55-
statistics.tick_count++;
5654
statistics.current_status = status;
5755
statistics.last_timestamp = timestamp;
5856

57+
if(status == NodeStatus::IDLE) {
58+
return;
59+
}
60+
61+
statistics.transitions_count++;
62+
5963
if(status == NodeStatus::SUCCESS)
6064
{
61-
statistics.last_result = NodeStatus::SUCCESS;
65+
statistics.last_result = status;
6266
statistics.success_count++;
6367
}
6468
else if(status == NodeStatus::FAILURE)
6569
{
66-
statistics.last_result = NodeStatus::FAILURE;
70+
statistics.last_result = status;
6771
statistics.failure_count++;
6872
}
73+
else if(status == NodeStatus::SKIPPED)
74+
{
75+
statistics.skip_count++;
76+
}
6977
}
7078

7179
const TreeObserver::NodeStatistics& TreeObserver::getStatistics(const std::string &path) const

0 commit comments

Comments
 (0)