reactive_sequence.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 success_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("[ReactiveSequence]: only a single child can return RUNNING.\n"
58  "This throw can be disabled with ReactiveSequence::EnableException(false)");
59  }
60  return NodeStatus::RUNNING;
61  }
62 
63  case NodeStatus::FAILURE: {
64  resetChildren();
65  return NodeStatus::FAILURE;
66  }
67  case NodeStatus::SUCCESS: {
68  success_count++;
69  }
70  break;
71 
72  case NodeStatus::IDLE: {
73  throw LogicError("A child node must never return IDLE");
74  }
75  } // end switch
76  } //end for
77 
78 
79  if (success_count == childrenCount())
80  {
81  resetChildren();
82  return NodeStatus::SUCCESS;
83  }
84  return NodeStatus::RUNNING;
85 }
86 
88 {
89  running_child_ = -1;
91 }
92 
93 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ControlNode::childrenCount
size_t childrenCount() const
Definition: control_node.cpp:27
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
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
reactive_sequence.h
BT::ReactiveSequence::EnableException
static void EnableException(bool enable)
Definition: reactive_sequence.cpp:20
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::ReactiveSequence::halt
void halt() override
Definition: reactive_sequence.cpp:87
BT::ReactiveSequence::throw_if_multiple_running
static bool throw_if_multiple_running
Definition: reactive_sequence.h:52
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::ControlNode::haltChild
void haltChild(size_t i)
Definition: control_node.cpp:54
BT::NodeStatus::RUNNING
@ RUNNING
BT::ReactiveSequence::running_child_
int running_child_
Definition: reactive_sequence.h:50
BT::NodeStatus::IDLE
@ IDLE
BT::ReactiveSequence::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: reactive_sequence.cpp:25
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35


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