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/retry_node.h"
00015
00016 namespace BT
00017 {
00018 constexpr const char* RetryNode::NUM_ATTEMPTS;
00019
00020 RetryNode::RetryNode(const std::string& name, unsigned int NTries)
00021 : DecoratorNode(name, {{NUM_ATTEMPTS, std::to_string(NTries)}}),
00022 max_attempts_(NTries),
00023 try_index_(0),
00024 read_parameter_from_blackboard_(false)
00025 {
00026 setRegistrationName("RetryUntilSuccesful");
00027 }
00028
00029 RetryNode::RetryNode(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_ATTEMPTS) );
00035 if(!read_parameter_from_blackboard_)
00036 {
00037 if( !getParam(NUM_ATTEMPTS, max_attempts_) )
00038 {
00039 throw std::runtime_error("Missing parameter [num_attempts] in RetryNode");
00040 }
00041 }
00042 }
00043
00044 void RetryNode::halt()
00045 {
00046 try_index_ = 0;
00047 DecoratorNode::halt();
00048 }
00049
00050 NodeStatus RetryNode::tick()
00051 {
00052 if( read_parameter_from_blackboard_ )
00053 {
00054 if( !getParam(NUM_ATTEMPTS, max_attempts_) )
00055 {
00056 throw std::runtime_error("Missing parameter [num_attempts] in RetryNode");
00057 }
00058 }
00059
00060 setStatus(NodeStatus::RUNNING);
00061 NodeStatus child_state = child_node_->executeTick();
00062
00063 switch (child_state)
00064 {
00065 case NodeStatus::SUCCESS:
00066 {
00067 try_index_ = 0;
00068 return (NodeStatus::SUCCESS);
00069 }
00070
00071 case NodeStatus::FAILURE:
00072 {
00073 try_index_++;
00074 if (try_index_ >= max_attempts_)
00075 {
00076 try_index_ = 0;
00077 return (NodeStatus::FAILURE);
00078 }
00079 }
00080 break;
00081
00082 case NodeStatus::RUNNING:
00083 {
00084 return (NodeStatus::RUNNING);
00085 }
00086
00087 default:
00088 {
00089
00090 }
00091 }
00092
00093 return status();
00094 }
00095
00096 }