timeout_node.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include <atomic>
6 
7 namespace BT
8 {
22 template <typename _Clock = std::chrono::steady_clock,
23  typename _Duration = std::chrono::steady_clock::duration>
24 class TimeoutNode : public DecoratorNode
25 {
26 public:
27  TimeoutNode(const std::string& name, unsigned milliseconds) :
28  DecoratorNode(name, {}),
29  child_halted_(false),
30  timer_id_(0),
31  msec_(milliseconds),
33  timeout_started_(false)
34  {
35  setRegistrationID("Timeout");
36  }
37 
38  TimeoutNode(const std::string& name, const NodeConfiguration& config) :
39  DecoratorNode(name, config),
40  child_halted_(false),
41  timer_id_(0),
42  msec_(0),
44  timeout_started_(false)
45  {}
46 
47  ~TimeoutNode() override
48  {
49  timer_.cancelAll();
50  }
51 
53  {
54  return {InputPort<unsigned>("msec", "After a certain amount of time, "
55  "halt() the child if it is still running.")};
56  }
57 
58 private:
60 
61  virtual BT::NodeStatus tick() override
62  {
64  {
65  if (!getInput("msec", msec_))
66  {
67  throw RuntimeError("Missing parameter [msec] in TimeoutNode");
68  }
69  }
70 
71  if (!timeout_started_)
72  {
73  timeout_started_ = true;
75  child_halted_ = false;
76 
77  if (msec_ > 0)
78  {
79  timer_id_ = timer_.add(std::chrono::milliseconds(msec_), [this](bool aborted) {
80  // Return immediately if the timer was aborted.
81  // This function could be invoked during destruction of this object and
82  // we don't want to access member variables if not needed.
83  if (aborted)
84  {
85  return;
86  }
87  std::unique_lock<std::mutex> lk(timeout_mutex_);
88  if (child()->status() == NodeStatus::RUNNING)
89  {
90  child_halted_ = true;
91  haltChild();
93  }
94  });
95  }
96  }
97 
98  std::unique_lock<std::mutex> lk(timeout_mutex_);
99 
100  if (child_halted_)
101  {
102  timeout_started_ = false;
103  return NodeStatus::FAILURE;
104  }
105  else
106  {
107  const NodeStatus child_status = child()->executeTick();
108  if(StatusCompleted(child_status))
109  {
110  timeout_started_ = false;
111  timeout_mutex_.unlock();
112  timer_.cancel(timer_id_);
113  timeout_mutex_.lock();
114  resetChild();
115  }
116  return child_status;
117  }
118  }
119 
120  std::atomic<bool> child_halted_;
121  uint64_t timer_id_;
122 
123  unsigned msec_;
127 };
128 } // namespace BT
const NodeConfiguration & config() const
Definition: tree_node.cpp:127
~TimeoutNode() override
Definition: timeout_node.h:47
The TimeoutNode will halt() a running child if the latter has been RUNNING for more than a give time...
Definition: timeout_node.h:24
NodeStatus status() const
Definition: tree_node.cpp:84
size_t cancel(uint64_t id)
Cancels the specified timer.
Definition: timer_queue.h:108
static pthread_mutex_t mutex
Definition: minitrace.cpp:61
TimeoutNode(const std::string &name, unsigned milliseconds)
Definition: timeout_node.h:27
bool StatusCompleted(const NodeStatus &status)
Definition: basic_types.h:43
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
void haltChild()
Same as resetChild()
TimerQueue< _Clock, _Duration > timer_
Definition: timeout_node.h:59
std::mutex timeout_mutex_
Definition: timeout_node.h:126
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
TimeoutNode(const std::string &name, const NodeConfiguration &config)
Definition: timeout_node.h:38
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: timeout_node.h:61
const TreeNode * child() const
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:201
bool read_parameter_from_ports_
Definition: timeout_node.h:124
std::atomic< bool > child_halted_
Definition: timeout_node.h:120
uint64_t add(std::chrono::milliseconds milliseconds, std::function< void(bool)> handler)
Adds a new timer.
Definition: timer_queue.h:86
void emitStateChanged()
Definition: tree_node.cpp:193
static PortsList providedPorts()
Definition: timeout_node.h:52
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:63
uint64_t timer_id_
Definition: timeout_node.h:121


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14