AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
state_observer.cpp
Go to the documentation of this file.
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
17#include <chrono>
18
20{
21
22BTStateObserver::BTStateObserver(const BT::Tree& tree, const rclcpp::Logger& node_logger,
23 std::chrono::seconds max_logging_rate)
24 : StatusChangeLogger{ tree.rootNode() }
25 , logger_{ node_logger }
26 , root_tree_id_{ tree.subtrees[0]->tree_ID }
27 , max_logging_rate_{ max_logging_rate }
28{
29}
30
32{
33 running_action_history_.clear();
34}
35
37{
38 logging_active_ = active;
39}
40
41const std::vector<std::string>& BTStateObserver::getRunningActionHistory() const
42{
43 return running_action_history_;
44}
45
47{
48 return last_running_action_name_;
49}
50
51uint16_t BTStateObserver::createStateChangeBitmask(BT::NodeStatus prev_status, BT::NodeStatus curr_status)
52{
53 return static_cast<uint16_t>(prev_status) << 8 | static_cast<uint16_t>(curr_status);
54}
55
56void BTStateObserver::callback(BT::Duration timestamp, const BT::TreeNode& node, BT::NodeStatus prev_status,
57 BT::NodeStatus status)
58{
59 if (node.type() == BT::NodeType::ACTION && status == BT::NodeStatus::RUNNING)
60 {
61 auto name = node.registrationName() == node.name() ? node.name() : node.registrationName() + ": " + node.name();
62 running_action_history_.push_back(name);
63 last_running_action_name_ = name;
64 }
65
70 if (!logging_active_)
71 return;
72 const auto key = std::make_pair(node.UID(), createStateChangeBitmask(prev_status, status));
73 const bool is_first_log = last_log_map_.count(key) == 0;
74
75 if (is_first_log || timestamp - last_log_map_[key] > max_logging_rate_)
76 {
77 if (node.registrationName() == node.name())
78 {
79 RCLCPP_INFO(logger_, "[%s] %s '%s' -- %s -> %s", root_tree_id_.c_str(), BT::toStr(node.type()).c_str(),
80 node.name().c_str(), BT::toStr(prev_status, true).c_str(), BT::toStr(status, true).c_str());
81 }
82 else
83 {
84 RCLCPP_INFO(logger_, "[%s] %s '%s: %s' -- %s -> %s", root_tree_id_.c_str(), BT::toStr(node.type()).c_str(),
85 node.registrationName().c_str(), node.name().c_str(), BT::toStr(prev_status, true).c_str(),
86 BT::toStr(status, true).c_str());
87 }
88 last_log_map_[key] = timestamp;
89 }
90}
91
92} // namespace auto_apms_behavior_tree
const std::string & getLastRunningActionName() const
BTStateObserver(const BT::Tree &tree, const rclcpp::Logger &node_logger, std::chrono::seconds max_logging_rate=std::chrono::seconds(0))
const std::vector< std::string > & getRunningActionHistory() const