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/sequence_star_node.h"
00015
00016 namespace BT
00017 {
00018
00019 constexpr const char* SequenceStarNode::RESET_PARAM;
00020
00021 SequenceStarNode::SequenceStarNode(const std::string& name, bool reset_on_failure)
00022 : ControlNode::ControlNode(name, {{RESET_PARAM, std::to_string(reset_on_failure)}})
00023 , current_child_idx_(0)
00024 , reset_on_failure_(reset_on_failure)
00025 , read_parameter_from_blackboard_(false)
00026 {
00027 setRegistrationName("SequenceStar");
00028 }
00029
00030 SequenceStarNode::SequenceStarNode(const std::string& name, const NodeParameters& params)
00031 : ControlNode::ControlNode(name, params), current_child_idx_(0),
00032 read_parameter_from_blackboard_(false)
00033 {
00034 read_parameter_from_blackboard_ = isBlackboardPattern( params.at(RESET_PARAM) );
00035 if(!read_parameter_from_blackboard_)
00036 {
00037 if( !getParam(RESET_PARAM, reset_on_failure_) )
00038 {
00039 throw std::runtime_error("Missing parameter [reset_on_failure] in SequenceStarNode");
00040 }
00041 }
00042 }
00043
00044 NodeStatus SequenceStarNode::tick()
00045 {
00046 if(read_parameter_from_blackboard_)
00047 {
00048 if( !getParam(RESET_PARAM, reset_on_failure_) )
00049 {
00050 throw std::runtime_error("Missing parameter [reset_on_failure] in SequenceStarNode");
00051 }
00052 }
00053
00054 const unsigned children_count = children_nodes_.size();
00055
00056 setStatus(NodeStatus::RUNNING);
00057
00058 while (current_child_idx_ < children_count)
00059 {
00060 TreeNode* current_child_node = children_nodes_[current_child_idx_];
00061 const NodeStatus child_status = current_child_node->executeTick();
00062
00063 switch (child_status)
00064 {
00065 case NodeStatus::RUNNING:
00066 {
00067 return child_status;
00068 }
00069 case NodeStatus::FAILURE:
00070 {
00071 if (reset_on_failure_)
00072 {
00073 haltChildren(0);
00074 current_child_idx_ = 0;
00075 }
00076 else
00077 {
00078 haltChildren(current_child_idx_);
00079 }
00080 return child_status;
00081 }
00082 case NodeStatus::SUCCESS:
00083 {
00084 current_child_idx_++;
00085 }
00086 break;
00087
00088 case NodeStatus::IDLE:
00089 {
00090 throw std::runtime_error("This is not supposed to happen");
00091 }
00092 }
00093 }
00094
00095
00096 if (current_child_idx_ == children_count)
00097 {
00098 haltChildren(0);
00099 current_child_idx_ = 0;
00100 }
00101 return NodeStatus::SUCCESS;
00102 }
00103
00104 void SequenceStarNode::halt()
00105 {
00106 current_child_idx_ = 0;
00107 ControlNode::halt();
00108 }
00109 }