timeout_node.h
Go to the documentation of this file.
1 #ifndef DECORATOR_TIMEOUT_NODE_H
2 #define DECORATOR_TIMEOUT_NODE_H
3 
5 #include <atomic>
6 #include "timer_queue.h"
7 
8 namespace BT
9 {
23 template <typename _Clock = std::chrono::steady_clock, 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 
48  ~TimeoutNode() override
49  {
50  timer_.cancelAll();
51  }
52 
54  {
55  return { InputPort<unsigned>("msec", "After a certain amount of time, "
56  "halt() the child if it is still running.") };
57  }
58 
59  private:
61 
62  virtual BT::NodeStatus tick() override
63  {
65  {
66  if( !getInput("msec", msec_) )
67  {
68  throw RuntimeError("Missing parameter [msec] in TimeoutNode");
69  }
70  }
71 
72  if ( !timeout_started_ )
73  {
74  timeout_started_ = true;
76  child_halted_ = false;
77 
78  if (msec_ > 0)
79  {
80  timer_id_ = timer_.add(std::chrono::milliseconds(msec_),
81  [this](bool aborted)
82  {
83  std::unique_lock<std::mutex> lk( timeout_mutex_ );
84  if (!aborted && child()->status() == NodeStatus::RUNNING)
85  {
86  child_halted_ = true;
87  haltChild();
88  }
89  });
90  }
91  }
92 
93  std::unique_lock<std::mutex> lk( timeout_mutex_ );
94 
95  if (child_halted_)
96  {
97  timeout_started_ = false;
98  return NodeStatus::FAILURE;
99  }
100  else
101  {
102  auto child_status = child()->executeTick();
103  if (child_status != NodeStatus::RUNNING)
104  {
105  timeout_started_ = false;
106  timeout_mutex_.unlock();
107  timer_.cancel(timer_id_);
108  timeout_mutex_.lock();
109  }
110  return child_status;
111  }
112  }
113 
114  std::atomic<bool> child_halted_;
115  uint64_t timer_id_;
116 
117  unsigned msec_;
121 };
122 }
123 
124 #endif // DEADLINE_NODE_H
const TreeNode * child() const
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
~TimeoutNode() override
Definition: timeout_node.h:48
The TimeoutNode will halt() a running child if the latter has been RUNNING for more than a give time...
Definition: timeout_node.h:24
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
void haltChild()
Halt() the child node.
TimerQueue< _Clock, _Duration > timer_
Definition: timeout_node.h:60
std::mutex timeout_mutex_
Definition: timeout_node.h:120
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:192
TimeoutNode(const std::string &name, const NodeConfiguration &config)
Definition: timeout_node.h:38
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:317
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: timeout_node.h:62
void setRegistrationID(StringView ID)
Definition: tree_node.h:163
bool read_parameter_from_ports_
Definition: timeout_node.h:118
std::atomic< bool > child_halted_
Definition: timeout_node.h:114
uint64_t add(std::chrono::milliseconds milliseconds, std::function< void(bool)> handler)
Adds a new timer.
Definition: timer_queue.h:86
static PortsList providedPorts()
Definition: timeout_node.h:53
NodeStatus status() const
Definition: tree_node.cpp:56
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:33
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:40
uint64_t timer_id_
Definition: timeout_node.h:115


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25