Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "behaviortree_cpp/controls/parallel_node.h"
00015
00016 namespace BT
00017 {
00018
00019 constexpr const char* ParallelNode::THRESHOLD_KEY;
00020
00021 ParallelNode::ParallelNode(const std::string& name, int threshold)
00022 : ControlNode::ControlNode(name, {{THRESHOLD_KEY, std::to_string(threshold)}}),
00023 threshold_(threshold),
00024 read_parameter_from_blackboard_(false)
00025 {
00026 setRegistrationName("Parallel");
00027 }
00028
00029 ParallelNode::ParallelNode(const std::string &name,
00030 const NodeParameters ¶ms)
00031 : ControlNode::ControlNode(name, params),
00032 read_parameter_from_blackboard_(false)
00033 {
00034 read_parameter_from_blackboard_ = isBlackboardPattern( params.at(THRESHOLD_KEY) );
00035 if(!read_parameter_from_blackboard_)
00036 {
00037 if( !getParam(THRESHOLD_KEY, threshold_) )
00038 {
00039 throw std::runtime_error("Missing parameter [threshold] in ParallelNode");
00040 }
00041 }
00042 }
00043
00044 NodeStatus ParallelNode::tick()
00045 {
00046 if(read_parameter_from_blackboard_)
00047 {
00048 if( !getParam(THRESHOLD_KEY, threshold_) )
00049 {
00050 throw std::runtime_error("Missing parameter [threshold] in ParallelNode");
00051 }
00052 }
00053
00054 success_childred_num_ = 0;
00055 failure_childred_num_ = 0;
00056
00057 const unsigned children_count = children_nodes_.size();
00058
00059
00060 for (unsigned int i = 0; i < children_count; i++)
00061 {
00062 TreeNode* child_node = children_nodes_[i];
00063
00064 NodeStatus child_status = child_node->executeTick();
00065
00066 switch (child_status)
00067 {
00068 case NodeStatus::SUCCESS:
00069 child_node->setStatus(NodeStatus::IDLE);
00070
00071 if (++success_childred_num_ == threshold_)
00072 {
00073 success_childred_num_ = 0;
00074 failure_childred_num_ = 0;
00075 haltChildren(0);
00076 return child_status;
00077 }
00078 break;
00079 case NodeStatus::FAILURE:
00080 child_node->setStatus(NodeStatus::IDLE);
00081
00082 if (++failure_childred_num_ > children_count - threshold_)
00083 {
00084 success_childred_num_ = 0;
00085 failure_childred_num_ = 0;
00086 haltChildren(0);
00087 return child_status;
00088 }
00089 break;
00090 case NodeStatus::RUNNING:
00091 setStatus(child_status);
00092 break;
00093 default:
00094 break;
00095 }
00096 }
00097 return NodeStatus::RUNNING;
00098 }
00099
00100 void ParallelNode::halt()
00101 {
00102 success_childred_num_ = 0;
00103 failure_childred_num_ = 0;
00104 ControlNode::halt();
00105 }
00106
00107 unsigned int ParallelNode::thresholdM()
00108 {
00109 return threshold_;
00110 }
00111
00112 void ParallelNode::setThresholdM(unsigned int threshold_M)
00113 {
00114 threshold_ = threshold_M;
00115 }
00116
00117 }