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 // Only stop the timer thread here; do NOT call halt(). During tree
45 // destruction the child node may already be gone: nodes are owned by a
46 // flat std::vector<TreeNode::Ptr> whose element-destruction order is
47 // unspecified (libstdc++ tears down front-to-back, libc++ back-to-front),
48 // so the child can outlive or predecease this decorator. halt() ->
49 // DecoratorNode::halt() -> resetChild() dereferences that child and
50 // segfaults on libc++ (macOS). This mirrors TimeoutNode's destructor.
51 timer_.cancelAll();
52 }
53
54 DelayNode(const DelayNode&) = delete;
55 DelayNode& operator=(const DelayNode&) = delete;
56 DelayNode(DelayNode&&) = delete;
57 DelayNode& operator=(DelayNode&&) = delete;
58
59 static PortsList providedPorts()
60 {
61 return { InputPort<unsigned>("delay_msec", "Tick the child after a few "
62 "milliseconds") };
63 }
64
65 void halt() override;
66
67private:
68 TimerQueue<> timer_;
69 uint64_t timer_id_;
70
71 virtual BT::NodeStatus tick() override;
72
73 bool delay_started_ = false;
74 std::atomic_bool delay_complete_ = false;
75 bool delay_aborted_ = false;
76 unsigned msec_;
77 bool read_parameter_from_ports_;
78 std::mutex delay_mutex_;
79};
80
81} // 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