sequence_star_node.cpp
Go to the documentation of this file.
00001 /* Copyright (C) 2015-2018 Michele Colledanchise -  All Rights Reserved
00002  * Copyright (C) 2018 Davide Faconti -  All Rights Reserved
00003 *
00004 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00005 *   to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00006 *   and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
00007 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00008 *
00009 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00010 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00011 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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         }   // end switch
00093     }       // end while loop
00094 
00095     // The entire while loop completed. This means that all the children returned SUCCESS.
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 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10