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  bool all_skipped = true;
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 to RUNNING state as soon as you find an active child
40  all_skipped &= (child_status == NodeStatus::SKIPPED);
41 
42  switch(child_status)
43  {
44  case NodeStatus::RUNNING: {
45  // reset the previous children, to make sure that they are
46  // in IDLE state the next time we tick them
47  for(size_t i = 0; i < childrenCount(); i++)
48  {
49  if(i != index)
50  {
51  haltChild(i);
52  }
53  }
54  if(running_child_ == -1)
55  {
56  running_child_ = int(index);
57  }
58  else if(throw_if_multiple_running && running_child_ != int(index))
59  {
60  throw LogicError("[ReactiveSequence]: only a single child can return RUNNING.\n"
61  "This throw can be disabled with "
62  "ReactiveSequence::EnableException(false)");
63  }
64  return NodeStatus::RUNNING;
65  }
66 
67  case NodeStatus::FAILURE: {
68  resetChildren();
69  return NodeStatus::FAILURE;
70  }
71  // do nothing if SUCCESS
73  break;
74 
75  case NodeStatus::SKIPPED: {
76  // to allow it to be skipped again, we must reset the node
77  haltChild(index);
78  }
79  break;
80 
81  case NodeStatus::IDLE: {
82  throw LogicError("[", name(), "]: A children should not return IDLE");
83  }
84  } // end switch
85  } //end for
86 
87  resetChildren();
88 
89  // Skip if ALL the nodes have been skipped
90  return all_skipped ? NodeStatus::SKIPPED : NodeStatus::SUCCESS;
91 }
92 
94 {
95  running_child_ = -1;
97 }
98 
99 } // 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:71
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
BT::ControlNode::children_nodes_
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:24
BT::ControlNode::resetChildren
void resetChildren()
Definition: control_node.cpp:38
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: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::ReactiveSequence::halt
void halt() override
Definition: reactive_sequence.cpp:93
BT::NodeStatus::SKIPPED
@ SKIPPED
BT::ReactiveSequence::throw_if_multiple_running
static bool throw_if_multiple_running
Definition: reactive_sequence.h:52
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::ControlNode::haltChild
void haltChild(size_t i)
Definition: control_node.cpp:55
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:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17