AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
state_observer.hpp
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
15#pragma once
16
17#include <chrono>
18
19#include "behaviortree_cpp/loggers/abstract_logger.h"
20#include "rclcpp/rclcpp.hpp"
21
23{
24
25class BTStateObserver : public BT::StatusChangeLogger
26{
27public:
28 BTStateObserver(const BT::Tree& tree, const rclcpp::Logger& node_logger,
29 std::chrono::seconds max_logging_rate = std::chrono::seconds(0));
30
31 virtual void flush() override;
32
33 void setLogging(bool active);
34
35 const std::vector<std::string>& getRunningActionHistory() const;
36 const std::string& getLastRunningActionName() const;
37
38private:
39 // Creates a bitmask that uniquely identifies a node's state change
40 uint16_t createStateChangeBitmask(BT::NodeStatus prev_status, BT::NodeStatus curr_status);
41
42 virtual void callback(BT::Duration timestamp, const BT::TreeNode& node, BT::NodeStatus prev_status,
43 BT::NodeStatus status) override;
44
45 const rclcpp::Logger logger_;
46 const std::string root_tree_id_;
47 const std::chrono::seconds max_logging_rate_;
48 bool logging_active_{ false };
49 std::vector<std::string> running_action_history_;
50 std::string last_running_action_name_;
51 std::map<std::pair<uint16_t, uint16_t>, BT::Duration> last_log_map_;
52};
53
54} // 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