abstract_logger.h
Go to the documentation of this file.
00001 #ifndef ABSTRACT_LOGGER_H
00002 #define ABSTRACT_LOGGER_H
00003 
00004 #include "behaviortree_cpp/behavior_tree.h"
00005 
00006 namespace BT
00007 {
00008 enum class TimestampType
00009 {
00010     ABSOLUTE,
00011     RELATIVE
00012 };
00013 
00014 typedef std::array<uint8_t, 12> SerializedTransition;
00015 
00016 
00017 class StatusChangeLogger
00018 {
00019   public:
00020     StatusChangeLogger(TreeNode* root_node);
00021     virtual ~StatusChangeLogger() = default;
00022 
00023     virtual void callback(BT::Duration timestamp, const TreeNode& node, NodeStatus prev_status,
00024                           NodeStatus status) = 0;
00025 
00026     virtual void flush() = 0;
00027 
00028     void setEnabled(bool enabled)
00029     {
00030         enabled_ = enabled;
00031     }
00032 
00033     void seTimestampType(TimestampType type)
00034     {
00035         type_ = type;
00036     }
00037 
00038     bool enabled() const
00039     {
00040         return enabled_;
00041     }
00042 
00043     // false by default.
00044     bool showsTransitionToIdle() const
00045     {
00046         return show_transition_to_idle_;
00047     }
00048 
00049     void enableTransitionToIdle(bool enable)
00050     {
00051         show_transition_to_idle_ = enable;
00052     }
00053 
00054   private:
00055     bool enabled_;
00056     bool show_transition_to_idle_;
00057     std::vector<TreeNode::StatusChangeSubscriber> subscribers_;
00058     TimestampType type_;
00059     BT::TimePoint first_timestamp_;
00060 };
00061 
00062 //--------------------------------------------
00063 
00064 inline StatusChangeLogger::StatusChangeLogger(TreeNode* root_node)
00065   : enabled_(true), show_transition_to_idle_(true), type_(TimestampType::ABSOLUTE)
00066 {
00067     first_timestamp_ = std::chrono::high_resolution_clock::now();
00068 
00069     auto subscribeCallback = [this](TimePoint timestamp, const TreeNode& node, NodeStatus prev,
00070                                     NodeStatus status) {
00071         if (enabled_ && (status != NodeStatus::IDLE || show_transition_to_idle_))
00072         {
00073             if (type_ == TimestampType::ABSOLUTE)
00074             {
00075                 this->callback(timestamp.time_since_epoch(), node, prev, status);
00076             }
00077             else
00078             {
00079                 this->callback(timestamp - first_timestamp_, node, prev, status);
00080             }
00081         }
00082     };
00083 
00084     auto visitor = [this, subscribeCallback](TreeNode* node) {
00085         subscribers_.push_back(node->subscribeToStatusChange(std::move(subscribeCallback)));
00086     };
00087 
00088     applyRecursiveVisitor(root_node, visitor);
00089 }
00090 }
00091 
00092 #endif   // ABSTRACT_LOGGER_H


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:08