Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "behaviortree_cpp/decorators/repeat_node.h"
00015
00016 namespace BT
00017 {
00018 constexpr const char* RepeatNode::NUM_CYCLES;
00019
00020 RepeatNode::RepeatNode(const std::string& name, unsigned int NTries)
00021 : DecoratorNode(name, {{NUM_CYCLES, std::to_string(NTries)}}),
00022 num_cycles_(NTries),
00023 try_index_(0),
00024 read_parameter_from_blackboard_(false)
00025 {
00026 setRegistrationName("Repeat");
00027 }
00028
00029 RepeatNode::RepeatNode(const std::string& name, const NodeParameters& params)
00030 : DecoratorNode(name, params),
00031 try_index_(0),
00032 read_parameter_from_blackboard_(false)
00033 {
00034 read_parameter_from_blackboard_ = isBlackboardPattern( params.at(NUM_CYCLES) );
00035 if(!read_parameter_from_blackboard_)
00036 {
00037 if( !getParam(NUM_CYCLES, num_cycles_) )
00038 {
00039 throw std::runtime_error("Missing parameter [num_cycles] in RepeatNode");
00040 }
00041 }
00042 }
00043
00044 NodeStatus RepeatNode::tick()
00045 {
00046 if( read_parameter_from_blackboard_ )
00047 {
00048 if( !getParam(NUM_CYCLES, num_cycles_) )
00049 {
00050 throw std::runtime_error("Missing parameter [num_cycles] in RepeatNode");
00051 }
00052 }
00053
00054 setStatus(NodeStatus::RUNNING);
00055 NodeStatus child_state = child_node_->executeTick();
00056
00057 switch (child_state)
00058 {
00059 case NodeStatus::SUCCESS:
00060 {
00061 try_index_++;
00062 if (try_index_ >= num_cycles_)
00063 {
00064 try_index_ = 0;
00065 return (NodeStatus::SUCCESS);
00066 }
00067 }
00068 break;
00069
00070 case NodeStatus::FAILURE:
00071 {
00072 try_index_ = 0;
00073 return (NodeStatus::FAILURE);
00074 }
00075
00076 case NodeStatus::RUNNING:
00077 {
00078 return (NodeStatus::RUNNING);
00079 }
00080
00081 default:
00082 {
00083
00084 }
00085 }
00086 return status();
00087 }
00088
00089 void RepeatNode::halt()
00090 {
00091 try_index_ = 0;
00092 DecoratorNode::halt();
00093 }
00094
00095 }