timeout_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018-2023 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 
14 
15 namespace BT
16 {
17 
19 {
21  {
22  if(!getInput("msec", msec_))
23  {
24  throw RuntimeError("Missing parameter [msec] in TimeoutNode");
25  }
26  }
27 
28  if(!timeout_started_)
29  {
30  timeout_started_ = true;
32  child_halted_ = false;
33 
34  if(msec_ > 0)
35  {
36  timer_id_ = timer_.add(std::chrono::milliseconds(msec_), [this](bool aborted) {
37  // Return immediately if the timer was aborted.
38  // This function could be invoked during destruction of this object and
39  // we don't want to access member variables if not needed.
40  if(aborted)
41  {
42  return;
43  }
44  std::unique_lock<std::mutex> lk(timeout_mutex_);
45  if(child()->status() == NodeStatus::RUNNING)
46  {
47  child_halted_ = true;
48  haltChild();
50  }
51  });
52  }
53  }
54 
55  std::unique_lock<std::mutex> lk(timeout_mutex_);
56 
57  if(child_halted_)
58  {
59  timeout_started_ = false;
60  return NodeStatus::FAILURE;
61  }
62  else
63  {
64  const NodeStatus child_status = child()->executeTick();
65  if(isStatusCompleted(child_status))
66  {
67  timeout_started_ = false;
68  timeout_mutex_.unlock();
70  timeout_mutex_.lock();
71  resetChild();
72  }
73  return child_status;
74  }
75 }
76 
78 {
79  timeout_started_ = false;
80  timer_.cancelAll();
82 }
83 
84 } // 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::TreeNode::emitWakeUpSignal
void emitWakeUpSignal()
Notify that the tree should be ticked again()
Definition: tree_node.cpp:426
BT::TimeoutNode::read_parameter_from_ports_
bool read_parameter_from_ports_
Definition: timeout_node.h:79
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::haltChild
void haltChild()
Same as resetChild()
Definition: decorator_node.cpp:48
BT::TimeoutNode::timer_id_
uint64_t timer_id_
Definition: timeout_node.h:76
BT::TimeoutNode::timer_
TimerQueue timer_
Definition: timeout_node.h:74
timeout_node.h
BT::TimerQueue::cancel
size_t cancel(uint64_t id)
Cancels the specified timer.
Definition: timer_queue.h:114
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:285
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:160
BT::RuntimeError
Definition: exceptions.h:58
BT::TimeoutNode::timeout_mutex_
std::mutex timeout_mutex_
Definition: timeout_node.h:81
BT::TimeoutNode::timeout_started_
std::atomic_bool timeout_started_
Definition: timeout_node.h:80
BT::DecoratorNode::child
const TreeNode * child() const
Definition: decorator_node.cpp:38
BT::NodeStatus::RUNNING
@ RUNNING
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::TimeoutNode::msec_
unsigned msec_
Definition: timeout_node.h:78
BT::TimeoutNode::halt
void halt() override
The method used to interrupt the execution of this node.
Definition: timeout_node.cpp:77
BT::TimeoutNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: timeout_node.cpp:18
BT::DecoratorNode::resetChild
void resetChild()
Definition: decorator_node.cpp:53
BT::TimeoutNode::child_halted_
std::atomic_bool child_halted_
Definition: timeout_node.h:75
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 Dec 13 2024 03:19:17