BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
timeout_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 TimeoutNode will halt() a running child if
24 * the latter has been RUNNING longer than a given time.
25 * The timeout is in milliseconds and it is passed using the port "msec".
26 *
27 * If timeout is reached, the node returns FAILURE.
28 *
29 * Example:
30 *
31 * <Timeout msec="5000">
32 * <KeepYourBreath/>
33 * </Timeout>
34 */
35
36class TimeoutNode : public DecoratorNode
37{
38public:
39 TimeoutNode(const std::string& name, unsigned milliseconds)
40 : DecoratorNode(name, {})
41 , child_halted_(false)
42 , timer_id_(0)
43 , msec_(milliseconds)
44 , read_parameter_from_ports_(false)
45 , timeout_started_(false)
46 {
47 setRegistrationID("Timeout");
48 }
49
50 TimeoutNode(const std::string& name, const NodeConfig& config)
51 : DecoratorNode(name, config)
52 , child_halted_(false)
53 , timer_id_(0)
54 , msec_(0)
55 , read_parameter_from_ports_(true)
56 , timeout_started_(false)
57 {}
58
59 ~TimeoutNode() override
60 {
61 timer_.cancelAll();
62 }
63
64 TimeoutNode(const TimeoutNode&) = delete;
65 TimeoutNode& operator=(const TimeoutNode&) = delete;
66 TimeoutNode(TimeoutNode&&) = delete;
67 TimeoutNode& operator=(TimeoutNode&&) = delete;
68
69 static PortsList providedPorts()
70 {
71 return { InputPort<unsigned>("msec", "After a certain amount of time, "
72 "halt() the child if it is still running.") };
73 }
74
75private:
76 virtual BT::NodeStatus tick() override;
77
78 void halt() override;
79
80 TimerQueue<> timer_;
81 std::atomic_bool child_halted_ = false;
82 uint64_t timer_id_;
83
84 unsigned msec_;
85 bool read_parameter_from_ports_;
86 std::atomic_bool timeout_started_ = false;
87 std::mutex timeout_mutex_;
88};
89
90} // namespace BT
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The TimeoutNode will halt() a running child if the latter has been RUNNING longer than a given time....
Definition: timeout_node.h:37
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105