BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
delay_node.h
1/* Copyright (C) 2018-2025 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/decorator_node.h"
16#include "behaviortree_cpp/utils/timer_queue.h"
17
18#include <atomic>
19
20namespace BT
21{
22/**
23 * @brief The delay node will introduce a delay and then tick the
24 * child returning the status of the child as it is upon completion
25 * The delay is in milliseconds and it is passed using the port "delay_msec".
26 *
27 * During the delay the node changes status to RUNNING
28 *
29 * Example:
30 *
31 * <Delay delay_msec="5000">
32 * <KeepYourBreath/>
33 * </Delay>
34 */
35class DelayNode : public DecoratorNode
36{
37public:
38 DelayNode(const std::string& name, unsigned milliseconds);
39
40 DelayNode(const std::string& name, const NodeConfig& config);
41
42 ~DelayNode() override
43 {
44 halt();
45 }
46
47 DelayNode(const DelayNode&) = delete;
48 DelayNode& operator=(const DelayNode&) = delete;
49 DelayNode(DelayNode&&) = delete;
50 DelayNode& operator=(DelayNode&&) = delete;
51
52 static PortsList providedPorts()
53 {
54 return { InputPort<unsigned>("delay_msec", "Tick the child after a few "
55 "milliseconds") };
56 }
57
58 void halt() override;
59
60private:
61 TimerQueue<> timer_;
62 uint64_t timer_id_;
63
64 virtual BT::NodeStatus tick() override;
65
66 bool delay_started_ = false;
67 std::atomic_bool delay_complete_ = false;
68 bool delay_aborted_ = false;
69 unsigned msec_;
70 bool read_parameter_from_ports_;
71 std::mutex delay_mutex_;
72};
73
74} // namespace BT
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The delay node will introduce a delay and then tick the child returning the status of the child as it...
Definition: delay_node.h:36
void halt() override
The method used to interrupt the execution of this node.
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105