reactive_fallback.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2020 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 */
12 
14 
15 namespace BT
16 {
17 
19 
21 {
23 }
24 
26 {
27  size_t failure_count = 0;
28  if(status() == NodeStatus::IDLE)
29  {
30  running_child_ = -1;
31  }
33 
34  for (size_t index = 0; index < childrenCount(); index++)
35  {
36  TreeNode* current_child_node = children_nodes_[index];
37  const NodeStatus child_status = current_child_node->executeTick();
38 
39  switch (child_status)
40  {
41  case NodeStatus::RUNNING: {
42  // reset the previous children, to make sure that they are
43  // in IDLE state the next time we tick them
44  for (size_t i = 0; i < childrenCount(); i++)
45  {
46  if(i != index)
47  {
48  haltChild(i);
49  }
50  }
51  if(running_child_ == -1)
52  {
53  running_child_ = int(index);
54  }
55  else if(throw_if_multiple_running && running_child_ != int(index))
56  {
57  throw LogicError("[ReactiveFallback]: only a single child can return RUNNING.\n"
58  "This throw can be disabled with ReactiveFallback::EnableException(false)");
59  }
60  return NodeStatus::RUNNING;
61  }
62 
63  case NodeStatus::FAILURE: {
64  failure_count++;
65  }
66  break;
67 
68  case NodeStatus::SUCCESS: {
69  resetChildren();
70  return NodeStatus::SUCCESS;
71  }
72 
73  case NodeStatus::IDLE: {
74  throw LogicError("A child node must never return IDLE");
75  }
76  } // end switch
77  } //end for
78 
79  if (failure_count == childrenCount())
80  {
81  resetChildren();
82  return NodeStatus::FAILURE;
83  }
84 
85  return NodeStatus::RUNNING;
86 }
87 
89 {
90  running_child_ = -1;
92 }
93 
94 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ControlNode::childrenCount
size_t childrenCount() const
Definition: control_node.cpp:27
BT::ReactiveFallback::halt
void halt() override
Definition: reactive_fallback.cpp:88
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
BT::ReactiveFallback::throw_if_multiple_running
static bool throw_if_multiple_running
Definition: reactive_fallback.h:51
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:55
BT::ControlNode::children_nodes_
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:24
BT::ControlNode::resetChildren
void resetChildren()
Definition: control_node.cpp:37
BT::LogicError
Definition: exceptions.h:45
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:84
BT::NodeStatus::FAILURE
@ FAILURE
BT::TreeNode::setStatus
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:63
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::ControlNode::haltChild
void haltChild(size_t i)
Definition: control_node.cpp:54
BT::NodeStatus::RUNNING
@ RUNNING
BT::ReactiveFallback::EnableException
static void EnableException(bool enable)
Definition: reactive_fallback.cpp:20
BT::NodeStatus::IDLE
@ IDLE
BT::ReactiveFallback::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: reactive_fallback.cpp:25
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
reactive_fallback.h
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35
BT::ReactiveFallback::running_child_
int running_child_
Definition: reactive_fallback.h:50


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19