|
| 1 | +/* Copyright (C) 2019-2020 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 | +#ifndef SWITCH_NODE_H |
| 14 | +#define SWITCH_NODE_H |
| 15 | + |
| 16 | +#include "behaviortree_cpp_v3/control_node.h" |
| 17 | + |
| 18 | +namespace BT |
| 19 | +{ |
| 20 | +/** |
| 21 | + * @brief The SwitchNode |
| 22 | + * |
| 23 | + */ |
| 24 | +template <size_t NUM_CASES> |
| 25 | +class SwitchNode : public ControlNode |
| 26 | +{ |
| 27 | + public: |
| 28 | + SwitchNode(const std::string& name, const BT::NodeConfiguration& config) |
| 29 | + : ControlNode::ControlNode(name, config ), |
| 30 | + running_child_(-1) |
| 31 | + { |
| 32 | + setRegistrationID("Switch"); |
| 33 | + } |
| 34 | + |
| 35 | + virtual ~SwitchNode() override = default; |
| 36 | + |
| 37 | + void halt() override |
| 38 | + { |
| 39 | + running_child_ = -1; |
| 40 | + ControlNode::halt(); |
| 41 | + } |
| 42 | + |
| 43 | + static PortsList providedPorts() |
| 44 | + { |
| 45 | + PortsList ports; |
| 46 | + ports.insert( BT::InputPort<std::string>("variable") ); |
| 47 | + for(unsigned i=0; i < NUM_CASES; i++) |
| 48 | + { |
| 49 | + char case_str[20]; |
| 50 | + sprintf(case_str, "case_%d", i+1); |
| 51 | + ports.insert( BT::InputPort<std::string>(case_str) ); |
| 52 | + } |
| 53 | + return ports; |
| 54 | + } |
| 55 | + |
| 56 | + private: |
| 57 | + int running_child_; |
| 58 | + virtual BT::NodeStatus tick() override; |
| 59 | +}; |
| 60 | + |
| 61 | +template<size_t NUM_CASES> inline |
| 62 | +NodeStatus SwitchNode<NUM_CASES>::tick() |
| 63 | +{ |
| 64 | + if( childrenCount() != NUM_CASES+1) |
| 65 | + { |
| 66 | + throw LogicError("Wrong number of children in SwitchNode; " |
| 67 | + "must be (num_cases + default)"); |
| 68 | + } |
| 69 | + |
| 70 | + std::string variable; |
| 71 | + std::string value; |
| 72 | + int child_index = NUM_CASES; // default index; |
| 73 | + |
| 74 | + if (getInput("variable", variable)) // no variable? jump to default |
| 75 | + { |
| 76 | + // check each case until you find a match |
| 77 | + for (unsigned index = 0; index < NUM_CASES; ++index) |
| 78 | + { |
| 79 | + char case_str[20]; |
| 80 | + sprintf(case_str, "case_%d", index+1); |
| 81 | + |
| 82 | + if (getInput(case_str, value) && variable == value) |
| 83 | + { |
| 84 | + child_index = index; |
| 85 | + break; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // if another one was running earlier, halt it |
| 91 | + if( running_child_ != -1 && running_child_ != child_index) |
| 92 | + { |
| 93 | + halt(); |
| 94 | + } |
| 95 | + |
| 96 | + NodeStatus ret = children_nodes_[child_index]->executeTick(); |
| 97 | + if( ret == NodeStatus::RUNNING ) |
| 98 | + { |
| 99 | + running_child_ = child_index; |
| 100 | + } |
| 101 | + else{ |
| 102 | + running_child_ = -1; |
| 103 | + } |
| 104 | + |
| 105 | + return ret; |
| 106 | +} |
| 107 | + |
| 108 | +} |
| 109 | + |
| 110 | +#endif // SWITCH_NODE_H |
0 commit comments