timeout_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018-2019 Davide Faconti, Eurecat - 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 */
14 
15 namespace BT
16 {
17 
18 TimeoutNode::TimeoutNode(const std::string& name, unsigned milliseconds)
19  : DecoratorNode(name, {} ),
20  child_halted_(false),
21  timer_id_(0),
22  msec_(milliseconds),
24  timeout_started_(false)
25 {
26  setRegistrationID("Timeout");
27 }
28 
30  : DecoratorNode(name, config),
31  child_halted_(false),
32  timer_id_(0),
33  msec_(0),
35  timeout_started_(false)
36 {
37 }
38 
40 {
42  {
43  if( !getInput("msec", msec_) )
44  {
45  throw RuntimeError("Missing parameter [msec] in TimeoutNode");
46  }
47  }
48 
49  if ( !timeout_started_ )
50  {
51  timeout_started_ = true;
53  child_halted_ = false;
54 
55  if (msec_ > 0)
56  {
57  timer_id_ = timer_.add(std::chrono::milliseconds(msec_),
58  [this](bool aborted)
59  {
60  std::unique_lock<std::mutex> lk( timeout_mutex_ );
61  if (!aborted && child()->status() == NodeStatus::RUNNING)
62  {
63  child_halted_ = true;
64  child()->halt();
66  }
67  });
68  }
69  }
70 
71  std::unique_lock<std::mutex> lk( timeout_mutex_ );
72 
73  if (child_halted_)
74  {
75  timeout_started_ = false;
76  return NodeStatus::FAILURE;
77  }
78  else
79  {
80  auto child_status = child()->executeTick();
81  if (child_status != NodeStatus::RUNNING)
82  {
83  timeout_started_ = false;
84  timeout_mutex_.unlock();
86  timeout_mutex_.lock();
87  }
88  return child_status;
89  }
90 }
91 
92 }
TimeoutNode(const std::string &name, unsigned milliseconds)
const TreeNode * child() const
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
virtual void halt()=0
bool read_parameter_from_ports_
Definition: timeout_node.h:50
uint64_t timer_id_
Definition: timeout_node.h:47
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
TimerQueue timer_
Definition: timeout_node.h:42
uint64_t add(std::chrono::milliseconds milliseconds, std::function< void(bool)> handler)
Adds a new timer.
Definition: timer_queue.h:85
std::mutex timeout_mutex_
Definition: timeout_node.h:52
std::atomic< bool > child_halted_
Definition: timeout_node.h:46
unsigned msec_
Definition: timeout_node.h:49
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:185
void setRegistrationID(StringView ID)
Definition: tree_node.h:158
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
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
size_t cancel(uint64_t id)
Cancels the specified timer.
Definition: timer_queue.h:107


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05