|
| 1 | +/* Copyright (C) 2018 Davide Faconti - All Rights Reserved |
| 2 | +* |
| 3 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 4 | +* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 5 | +* 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: |
| 6 | +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 7 | +* |
| 8 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | +* 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, |
| 10 | +* 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. |
| 11 | +*/ |
| 12 | + |
| 13 | +#include "behaviortree_cpp/controls/priority_fallback.h" |
| 14 | + |
| 15 | +namespace BT |
| 16 | +{ |
| 17 | + |
| 18 | +constexpr const char* PriorityFallbackNode::CHILDREN_PRIORITY; |
| 19 | + |
| 20 | +PriorityFallbackNode::PriorityFallbackNode(const std::string &name, |
| 21 | + const NodeParameters ¶ms) |
| 22 | + : ControlNode(name, params) |
| 23 | + , refresh_parameter_(false) |
| 24 | +{ |
| 25 | + if( !getParam(CHILDREN_PRIORITY, children_order_) ) |
| 26 | + { |
| 27 | + throw std::runtime_error("Missing parameter [children_priority]" |
| 28 | + " in PrioritySequenceNode"); |
| 29 | + } |
| 30 | + refresh_parameter_ = isBlackboardPattern( params.at(CHILDREN_PRIORITY) ); |
| 31 | +} |
| 32 | + |
| 33 | +NodeStatus PriorityFallbackNode::tick() |
| 34 | +{ |
| 35 | + const unsigned children_count = children_nodes_.size(); |
| 36 | + |
| 37 | + if( refresh_parameter_ ) |
| 38 | + { |
| 39 | + if( !getParam(CHILDREN_PRIORITY, children_order_) ) |
| 40 | + { |
| 41 | + throw std::runtime_error("Problem parsing vector [children_priority] in PrioritySequenceNode"); |
| 42 | + } |
| 43 | + } |
| 44 | + if( children_order_.size() != children_count) |
| 45 | + { |
| 46 | + throw std::runtime_error("Size mismatch in [children_priority], at PrioritySequenceNode"); |
| 47 | + } |
| 48 | + |
| 49 | + setStatus(NodeStatus::RUNNING); |
| 50 | + |
| 51 | + for (unsigned i = 0; i < children_count; i++) |
| 52 | + { |
| 53 | + int index = children_order_[i]; |
| 54 | + if( index < 0) |
| 55 | + { |
| 56 | + continue; |
| 57 | + } |
| 58 | + TreeNode* child_node = children_nodes_[index]; |
| 59 | + const NodeStatus child_status = child_node->executeTick(); |
| 60 | + |
| 61 | + switch (child_status) |
| 62 | + { |
| 63 | + case NodeStatus::RUNNING: |
| 64 | + { |
| 65 | + return child_status; |
| 66 | + } |
| 67 | + case NodeStatus::SUCCESS: |
| 68 | + { |
| 69 | + for (unsigned t = 0; t <= i; t++) |
| 70 | + { |
| 71 | + int idx = children_order_[t]; |
| 72 | + children_nodes_[ idx ]->setStatus(NodeStatus::IDLE); |
| 73 | + } |
| 74 | + for (unsigned t = i+1; t < children_count; t++) |
| 75 | + { |
| 76 | + int idx = children_order_[t]; |
| 77 | + if (children_nodes_[idx]->status() == NodeStatus::RUNNING) |
| 78 | + { |
| 79 | + children_nodes_[idx]->halt(); |
| 80 | + } |
| 81 | + } |
| 82 | + return child_status; |
| 83 | + } |
| 84 | + case NodeStatus::FAILURE: |
| 85 | + { |
| 86 | + // continue; |
| 87 | + } |
| 88 | + break; |
| 89 | + |
| 90 | + case NodeStatus::IDLE: |
| 91 | + { |
| 92 | + throw std::runtime_error("This is not supposed to happen"); |
| 93 | + } |
| 94 | + } // end switch |
| 95 | + } // end for loop |
| 96 | + |
| 97 | + for (auto& ch : children_nodes_) |
| 98 | + { |
| 99 | + ch->setStatus(NodeStatus::IDLE); |
| 100 | + } |
| 101 | + return NodeStatus::FAILURE; |
| 102 | +} |
| 103 | + |
| 104 | +} |
0 commit comments