|
| 1 | +/* Copyright (C) 2022 Davide Faconti - All Rights Reserved |
| 2 | + * |
| 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/action_node.h" |
| 17 | +#include "behaviortree_cpp/decorators/timer_queue.h" |
| 18 | +#include "behaviortree_cpp/scripting/script_parser.hpp" |
| 19 | + |
| 20 | +namespace BT |
| 21 | +{ |
| 22 | + |
| 23 | +struct TestNodeConfig |
| 24 | +{ |
| 25 | + /// status to return when the action is completed |
| 26 | + NodeStatus return_status = NodeStatus::SUCCESS; |
| 27 | + |
| 28 | + /// script to execute when actions is completed |
| 29 | + std::string post_script; |
| 30 | + |
| 31 | + /// if async_delay > 0, this action become asynchronous and wait this amount of time |
| 32 | + std::chrono::milliseconds async_delay = std::chrono::milliseconds(0); |
| 33 | + |
| 34 | + /// C++ callback to execute at the beginning |
| 35 | + std::function<void()> pre_func; |
| 36 | + |
| 37 | + /// C++ callback to execute at the end |
| 38 | + std::function<void()> post_func; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * @brief The TestNode is a Node that can be configure to: |
| 43 | + * |
| 44 | + * 1. Return a specific status (SUCCESS / FAILURE) |
| 45 | + * 2. Execute a post condition script (unless halted) |
| 46 | + * 3. Either complete immediately (synchronous action), or after a |
| 47 | + * given period of time (asynchronous action) |
| 48 | + * |
| 49 | + * This behavior is changed by the parameters pased with TestNodeConfig. |
| 50 | + * |
| 51 | + * This particular node is created by the factory when TestNodeConfig is |
| 52 | + * added as a substitution rule: |
| 53 | + * |
| 54 | + * TestNodeConfig test_config; |
| 55 | + * // change fields of test_config |
| 56 | + * factory.addSubstitutionRule(pattern, test_config); |
| 57 | + * |
| 58 | + * See tutorial 11 for more details. |
| 59 | + */ |
| 60 | +class TestNode : public BT::StatefulActionNode |
| 61 | +{ |
| 62 | +public: |
| 63 | + TestNode(const std::string& name, const NodeConfig& config) : |
| 64 | + StatefulActionNode(name, config) |
| 65 | + { |
| 66 | + setRegistrationID("TestNode"); |
| 67 | + } |
| 68 | + |
| 69 | + static PortsList providedPorts() |
| 70 | + { |
| 71 | + return {}; |
| 72 | + } |
| 73 | + |
| 74 | + void setConfig(const TestNodeConfig& config); |
| 75 | + |
| 76 | +private: |
| 77 | + |
| 78 | + virtual NodeStatus onStart() override; |
| 79 | + |
| 80 | + virtual NodeStatus onRunning() override; |
| 81 | + |
| 82 | + virtual void onHalted() override; |
| 83 | + |
| 84 | + NodeStatus onCompleted(); |
| 85 | + |
| 86 | + TestNodeConfig _test_config; |
| 87 | + ScriptFunction _executor; |
| 88 | + TimerQueue<> _timer; |
| 89 | + std::atomic_bool _completed; |
| 90 | +}; |
| 91 | + |
| 92 | +} // namespace BT |
0 commit comments