|
| 1 | +/* Copyright (C) 2024 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 | +#pragma once |
| 14 | + |
| 15 | +#include "behaviortree_cpp/decorators/skip_unless_updated.h" |
| 16 | + |
| 17 | +namespace BT |
| 18 | +{ |
| 19 | + |
| 20 | +SkipUnlessUpdated::SkipUnlessUpdated(const std::string& name, const NodeConfig& config) |
| 21 | + : DecoratorNode(name, config) |
| 22 | +{ |
| 23 | + const auto entry_str = config.input_ports.at("entry"); |
| 24 | + StringView stripped_key; |
| 25 | + if(isBlackboardPointer(entry_str, &stripped_key)) |
| 26 | + { |
| 27 | + entry_key_ = stripped_key; |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + entry_key_ = entry_str; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +NodeStatus SkipUnlessUpdated::tick() |
| 36 | +{ |
| 37 | + // continue executing an asynchronous child |
| 38 | + if(still_executing_child_) |
| 39 | + { |
| 40 | + auto status = child()->executeTick(); |
| 41 | + still_executing_child_ = (status == NodeStatus::RUNNING); |
| 42 | + return status; |
| 43 | + } |
| 44 | + |
| 45 | + auto entry = config().blackboard->getEntry(entry_key_); |
| 46 | + std::unique_lock lk(entry->entry_mutex); |
| 47 | + auto seq = static_cast<int64_t>(entry->sequence_id); |
| 48 | + if(seq == sequence_id_) |
| 49 | + { |
| 50 | + return NodeStatus::SKIPPED; |
| 51 | + } |
| 52 | + sequence_id_ = seq; |
| 53 | + |
| 54 | + auto status = child()->executeTick(); |
| 55 | + still_executing_child_ = (status == NodeStatus::RUNNING); |
| 56 | + return status; |
| 57 | +} |
| 58 | + |
| 59 | +void SkipUnlessUpdated::halt() |
| 60 | +{ |
| 61 | + still_executing_child_ = false; |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace BT |
0 commit comments