Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "behaviortree_cpp/tree_node.h"
00015 #include <cstring>
00016
00017 namespace BT
00018 {
00019 static uint16_t getUID()
00020 {
00021 static uint16_t uid = 1;
00022 return uid++;
00023 }
00024
00025 TreeNode::TreeNode(const std::string& name, const NodeParameters& parameters)
00026 : not_initialized_(true),
00027 name_(name),
00028 status_(NodeStatus::IDLE),
00029 uid_(getUID()),
00030 parameters_(parameters)
00031
00032 {
00033 }
00034
00035 NodeStatus TreeNode::executeTick()
00036 {
00037 initializeOnce();
00038 const NodeStatus status = tick();
00039 setStatus(status);
00040 return status;
00041 }
00042
00043 void TreeNode::setStatus(NodeStatus new_status)
00044 {
00045 NodeStatus prev_status;
00046 {
00047 std::unique_lock<std::mutex> UniqueLock(state_mutex_);
00048 prev_status = status_;
00049 status_ = new_status;
00050 }
00051 if (prev_status != new_status)
00052 {
00053 state_condition_variable_.notify_all();
00054 state_change_signal_.notify(std::chrono::high_resolution_clock::now(), *this, prev_status,
00055 new_status);
00056 }
00057 }
00058
00059 void TreeNode::setBlackboard(const Blackboard::Ptr& bb)
00060 {
00061 bb_ = bb;
00062 }
00063
00064 const Blackboard::Ptr& TreeNode::blackboard() const
00065 {
00066 if( not_initialized_ )
00067 {
00068 throw std::logic_error("You can NOT access the blackboard in the constructor."
00069 " If you need to access the blackboard before the very first tick(), "
00070 " you should override the virtual method TreeNode::onInit()");
00071 }
00072 return bb_;
00073 }
00074
00075 NodeStatus TreeNode::status() const
00076 {
00077 std::lock_guard<std::mutex> LockGuard(state_mutex_);
00078 return status_;
00079 }
00080
00081 NodeStatus TreeNode::waitValidStatus()
00082 {
00083 std::unique_lock<std::mutex> lk(state_mutex_);
00084
00085 state_condition_variable_.wait(lk, [&]() {
00086 return (status_ == NodeStatus::RUNNING || status_ == NodeStatus::SUCCESS ||
00087 status_ == NodeStatus::FAILURE);
00088 });
00089 return status_;
00090 }
00091
00092 const std::string& TreeNode::name() const
00093 {
00094 return name_;
00095 }
00096
00097 bool TreeNode::isHalted() const
00098 {
00099 return status() == NodeStatus::IDLE;
00100 }
00101
00102 TreeNode::StatusChangeSubscriber
00103 TreeNode::subscribeToStatusChange(TreeNode::StatusChangeCallback callback)
00104 {
00105 return state_change_signal_.subscribe(std::move(callback));
00106 }
00107
00108 uint16_t TreeNode::UID() const
00109 {
00110 return uid_;
00111 }
00112
00113 void TreeNode::setRegistrationName(const std::string& registration_name)
00114 {
00115 registration_name_ = registration_name;
00116 }
00117
00118 void TreeNode::initializeOnce()
00119 {
00120 if( not_initialized_ )
00121 {
00122 not_initialized_ = false;
00123 onInit();
00124 }
00125 }
00126
00127 bool TreeNode::isBlackboardPattern(StringView str)
00128 {
00129 return str.size() >= 4 && str[0] == '$' && str[1] == '{' && str.back() == '}';
00130 }
00131
00132 const std::string& TreeNode::registrationName() const
00133 {
00134 return registration_name_;
00135 }
00136
00137 const NodeParameters& TreeNode::initializationParameters() const
00138 {
00139 return parameters_;
00140 }
00141
00142 }