delay_node.cpp
Go to the documentation of this file.
1 /* Contributed by Indraneel on 26/04/2020
2 */
3 
5 
6 namespace BT
7 {
8 DelayNode::DelayNode(const std::string& name, unsigned milliseconds)
9  : DecoratorNode(name, {})
10  , delay_started_(false)
11  , delay_aborted_(false)
12  , msec_(milliseconds)
13  , read_parameter_from_ports_(false)
14 {
15  setRegistrationID("Delay");
16 }
17 
18 DelayNode::DelayNode(const std::string& name, const NodeConfig& config)
19  : DecoratorNode(name, config)
20  , delay_started_(false)
21  , delay_aborted_(false)
22  , msec_(0)
23  , read_parameter_from_ports_(true)
24 {}
25 
27 {
28  delay_started_ = false;
29  timer_.cancelAll();
31 }
32 
34 {
36  {
37  if(!getInput("delay_msec", msec_))
38  {
39  throw RuntimeError("Missing parameter [delay_msec] in DelayNode");
40  }
41  }
42 
43  if(!delay_started_)
44  {
45  delay_complete_ = false;
46  delay_aborted_ = false;
47  delay_started_ = true;
49 
50  timer_id_ = timer_.add(std::chrono::milliseconds(msec_), [this](bool aborted) {
51  std::unique_lock<std::mutex> lk(delay_mutex_);
52  delay_complete_ = (!aborted);
53  if(!aborted)
54  {
56  }
57  });
58  }
59 
60  std::unique_lock<std::mutex> lk(delay_mutex_);
61 
62  if(delay_aborted_)
63  {
64  delay_aborted_ = false;
65  delay_started_ = false;
66  return NodeStatus::FAILURE;
67  }
68  else if(delay_complete_)
69  {
70  const NodeStatus child_status = child()->executeTick();
71  if(isStatusCompleted(child_status))
72  {
73  delay_started_ = false;
74  delay_aborted_ = false;
75  resetChild();
76  }
77  return child_status;
78  }
79  else
80  {
81  return NodeStatus::RUNNING;
82  }
83 }
84 
85 } // namespace BT
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
BT::TimerQueue::add
uint64_t add(std::chrono::milliseconds milliseconds, std::function< void(bool)> handler)
Adds a new timer.
Definition: timer_queue.h:92
BT::DelayNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: delay_node.cpp:33
BT::TreeNode::emitWakeUpSignal
void emitWakeUpSignal()
Notify that the tree should be ticked again()
Definition: tree_node.cpp:420
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:71
BT::DecoratorNode
Definition: decorator_node.h:8
BT::DelayNode::delay_aborted_
bool delay_aborted_
Definition: delay_node.h:62
BT::DelayNode::DelayNode
DelayNode(const std::string &name, unsigned milliseconds)
Definition: delay_node.cpp:8
delay_node.h
BT::NodeStatus::FAILURE
@ FAILURE
BT::TreeNode::setStatus
void setStatus(NodeStatus new_status)
setStatus changes the status of the node. it will throw if you try to change the status to IDLE,...
Definition: tree_node.cpp:154
BT::DelayNode::delay_started_
bool delay_started_
Definition: delay_node.h:60
BT::RuntimeError
Definition: exceptions.h:58
BT::DelayNode::read_parameter_from_ports_
bool read_parameter_from_ports_
Definition: delay_node.h:64
BT::DelayNode::halt
void halt() override
The method used to interrupt the execution of this node.
Definition: delay_node.cpp:26
BT::DelayNode::timer_
TimerQueue timer_
Definition: delay_node.h:55
BT::DecoratorNode::child
const TreeNode * child() const
Definition: decorator_node.cpp:38
BT::NodeStatus::RUNNING
@ RUNNING
BT::DelayNode::timer_id_
uint64_t timer_id_
Definition: delay_node.h:56
BT::DelayNode::msec_
unsigned msec_
Definition: delay_node.h:63
BT::isStatusCompleted
bool isStatusCompleted(const NodeStatus &status)
Definition: basic_types.h:47
BT::DecoratorNode::halt
virtual void halt() override
The method used to interrupt the execution of this node.
Definition: decorator_node.cpp:32
BT::DelayNode::delay_complete_
std::atomic_bool delay_complete_
Definition: delay_node.h:61
BT::NodeConfig
Definition: tree_node.h:73
BT::DecoratorNode::resetChild
void resetChild()
Definition: decorator_node.cpp:53
BT::DelayNode::delay_mutex_
std::mutex delay_mutex_
Definition: delay_node.h:65
BT::TimerQueue::cancelAll
size_t cancelAll()
Cancels all timers.
Definition: timer_queue.h:151
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07