|
| 1 | +/* Copyright (C) 2023 Davide Faconti - All Rights Reserved |
| 2 | +* Copyright (C) 2022 Gaël Écorchard, Czech Institute of Informatics, Robotics, and Cybernetics (ciirc) <gael.ecorchard@cvut.cz> |
| 3 | +* |
| 4 | +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 5 | +* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 6 | +* 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: |
| 7 | +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 8 | +* |
| 9 | +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 10 | +* 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, |
| 11 | +* 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. |
| 12 | +*/ |
| 13 | + |
| 14 | +#pragma once |
| 15 | + |
| 16 | +#include "behaviortree_cpp/decorator_node.h" |
| 17 | + |
| 18 | + |
| 19 | +namespace BT { |
| 20 | + |
| 21 | +/** |
| 22 | + * @brief The RunOnceNode is used when you want to execute the child |
| 23 | + * only once. |
| 24 | + * If the child is asynchronous, we will tick until either SUCCESS or FAILURE is |
| 25 | + * returned. |
| 26 | + * |
| 27 | + * After that first execution, you can set value of the port "then_skip" to: |
| 28 | + * |
| 29 | + * - if TRUE (default), the node will be skipped in the future. |
| 30 | + * - if FALSE, return synchronously the same status returned by the child, forever. |
| 31 | + */ |
| 32 | +class RunOnceNode : public DecoratorNode |
| 33 | +{ |
| 34 | + public: |
| 35 | + RunOnceNode(const std::string& name, const NodeConfig& config) : |
| 36 | + DecoratorNode(name, config) |
| 37 | + { |
| 38 | + setRegistrationID("RunOnce"); |
| 39 | + } |
| 40 | + |
| 41 | + static PortsList providedPorts() |
| 42 | + { |
| 43 | + return {InputPort<bool>( |
| 44 | + "then_skip", true, |
| 45 | + "If true, skip after the first execution, " |
| 46 | + "otherwise return the same NodeStatus returned once bu the child.")}; |
| 47 | + } |
| 48 | + |
| 49 | + private: |
| 50 | + |
| 51 | + virtual BT::NodeStatus tick() override; |
| 52 | + |
| 53 | + bool already_ticked_ = false; |
| 54 | + NodeStatus returned_status_ = NodeStatus::IDLE; |
| 55 | +}; |
| 56 | + |
| 57 | +//------------ implementation ---------------------------- |
| 58 | + |
| 59 | +inline NodeStatus RunOnceNode::tick() |
| 60 | +{ |
| 61 | + bool skip = true; |
| 62 | + if(auto const res = getInput<bool>("then_skip")) |
| 63 | + { |
| 64 | + skip = res.value(); |
| 65 | + } |
| 66 | + |
| 67 | + if (already_ticked_) |
| 68 | + { |
| 69 | + return skip ? NodeStatus::SKIPPED : returned_status_; |
| 70 | + } |
| 71 | + |
| 72 | + setStatus(NodeStatus::RUNNING); |
| 73 | + auto const status = child_node_->executeTick(); |
| 74 | + |
| 75 | + if(isStatusCompleted(status) ) { |
| 76 | + already_ticked_ = true; |
| 77 | + returned_status_ = status; |
| 78 | + } |
| 79 | + return status; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace BT |
0 commit comments