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 }
33 running_action_history_.clear();
38 logging_active_ = active;
43 return running_action_history_;
48 return last_running_action_name_;
51uint16_t BTStateObserver::createStateChangeBitmask(BT::NodeStatus prev_status, BT::NodeStatus curr_status)
53 return static_cast<uint16_t
>(prev_status) << 8 |
static_cast<uint16_t
>(curr_status);
56void BTStateObserver::callback(BT::Duration timestamp,
const BT::TreeNode& node, BT::NodeStatus prev_status,
57 BT::NodeStatus status)
59 if (node.type() == BT::NodeType::ACTION && status == BT::NodeStatus::RUNNING)
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;
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;
75 if (is_first_log || timestamp - last_log_map_[key] > max_logging_rate_)
77 if (node.registrationName() == node.name())
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());
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());
88 last_log_map_[key] = timestamp;
virtual void flush() override
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
void setLogging(bool active)