AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
state_observer.hpp
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
15#pragma once
16
17#include <chrono>
18
19#include "behaviortree_cpp/loggers/abstract_logger.h"
20#include "rclcpp/rclcpp.hpp"
21
23{
24
31class TreeStateObserver : public BT::StatusChangeLogger
32{
33public:
42 const BT::Tree & tree, const rclcpp::Logger & node_logger,
43 std::chrono::seconds max_logging_interval = std::chrono::seconds(0));
44
48 virtual void flush() override;
49
54 void setLogging(bool active);
55
61 const std::vector<std::string> & getRunningActionHistory() const;
62
67 const std::string & getLastRunningActionName() const;
68
69private:
70 // Creates a bitmask that uniquely identifies a node's state change
71 uint16_t createStateChangeBitmask(BT::NodeStatus prev_status, BT::NodeStatus curr_status);
72
73 virtual void callback(
74 BT::Duration timestamp, const BT::TreeNode & node, BT::NodeStatus prev_status, BT::NodeStatus status) override;
75
76 const rclcpp::Logger logger_;
77 const std::string root_tree_id_;
78 const std::chrono::seconds max_logging_interval_;
79 bool logging_active_{false};
80 std::vector<std::string> running_action_history_;
81 std::string last_running_action_name_;
82 std::map<std::pair<uint16_t, uint16_t>, BT::Duration> last_log_map_;
83};
84
85} // namespace auto_apms_behavior_tree
virtual void flush() override
Reset the internal state variables.
const std::string & getLastRunningActionName() const
Get the name of the last action node that returned BT::NodeStatus::RUNNING.
virtual void callback(BT::Duration timestamp, const BT::TreeNode &node, BT::NodeStatus prev_status, BT::NodeStatus status) override
TreeStateObserver(const BT::Tree &tree, const rclcpp::Logger &node_logger, std::chrono::seconds max_logging_interval=std::chrono::seconds(0))
Constructor.
const std::vector< std::string > & getRunningActionHistory() const
Get all names of action nodes that returned BT::NodeStatus::RUNNING since the last time TreeStateObse...
void setLogging(bool active)
Configure whether the observer should write to the logger.
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30