timeout_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018 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 */
13 
14 namespace BT
15 {
16 TimeoutNode::TimeoutNode(const std::string& name, unsigned milliseconds)
17  : DecoratorNode(name, {}), child_halted_(false), msec_(milliseconds),
19 {
20  setRegistrationName("Timeout");
21 }
22 
23 TimeoutNode::TimeoutNode(const std::string& name, const BT::NodeParameters& params)
24  : DecoratorNode(name, params), child_halted_(false), msec_(0)
25 {
28  {
29  if( !getParam("msec", msec_) )
30  {
31  throw std::runtime_error("Missing parameter [msec] in TimeoutNode");
32  }
33  }
34 }
35 
37 {
39  {
40  if( !getParam("msec", msec_) )
41  {
42  throw std::runtime_error("Missing parameter [msec] in TimeoutNode");
43  }
44  }
45 
46  if (status() == NodeStatus::IDLE)
47  {
49  child_halted_ = false;
50 
51  if (msec_ > 0)
52  {
53  timer_id_ = timer().add(std::chrono::milliseconds(msec_), [this](bool aborted) {
54  if (!aborted && child()->status() == NodeStatus::RUNNING)
55  {
56  child()->halt();
57  child_halted_ = true;
58  }
59  });
60  }
61  }
62 
63  if (child_halted_)
64  {
66  }
67  else
68  {
69  auto child_status = child()->executeTick();
70  if (child_status != NodeStatus::RUNNING)
71  {
73  }
74  setStatus(child_status);
75  }
76 
77  return status();
78 }
79 
80 }
BT::optional< T > getParam(const std::string &key) const
Definition: tree_node.h:117
TimeoutNode(const std::string &name, unsigned milliseconds)
static TimerQueue & timer()
Definition: timeout_node.h:24
const TreeNode * child() const
const std::string & name() const
Definition: tree_node.cpp:92
virtual void halt()=0
The method used to interrupt the execution of a RUNNING node.
uint64_t timer_id_
Definition: timeout_node.h:33
void setRegistrationName(const std::string &registration_name)
registrationName() is set by the BehaviorTreeFactory
Definition: tree_node.cpp:113
uint64_t add(std::chrono::milliseconds milliseconds, std::function< void(bool)> handler)
Adds a new timer.
Definition: timer_queue.h:85
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
std::atomic< bool > child_halted_
Definition: timeout_node.h:32
unsigned msec_
Definition: timeout_node.h:35
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
NodeStatus status() const
Definition: tree_node.cpp:75
NodeStatus
Definition: basic_types.h:28
static bool isBlackboardPattern(StringView str)
Definition: tree_node.cpp:127
virtual BT::NodeStatus executeTick()
The method that will be executed to invoke tick(); and setStatus();.
Definition: tree_node.cpp:35
bool read_parameter_from_blackboard_
Definition: timeout_node.h:36
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43
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 Feb 2 2019 04:01:53