parallel_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018 Davide Faconti - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * 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:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * 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,
11 * 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.
12 */
13 
15 
16 namespace BT
17 {
18 
19 constexpr const char* ParallelNode::THRESHOLD_KEY;
20 
21 ParallelNode::ParallelNode(const std::string& name, int threshold)
22  : ControlNode::ControlNode(name, {{THRESHOLD_KEY, std::to_string(threshold)}}),
23  threshold_(threshold),
25 {
26  setRegistrationName("Parallel");
27 }
28 
29 ParallelNode::ParallelNode(const std::string &name,
30  const NodeParameters &params)
31  : ControlNode::ControlNode(name, params),
33 {
36  {
38  {
39  throw std::runtime_error("Missing parameter [threshold] in ParallelNode");
40  }
41  }
42 }
43 
45 {
47  {
49  {
50  throw std::runtime_error("Missing parameter [threshold] in ParallelNode");
51  }
52  }
53 
56  // Vector size initialization. children_count_ could change at runtime if you edit the tree
57  const unsigned children_count = children_nodes_.size();
58 
59  // Routing the tree according to the sequence node's logic:
60  for (unsigned int i = 0; i < children_count; i++)
61  {
62  TreeNode* child_node = children_nodes_[i];
63 
64  NodeStatus child_status = child_node->executeTick();
65 
66  switch (child_status)
67  {
69  child_node->setStatus(NodeStatus::IDLE);
70  // the child goes in idle if it has returned success.
72  {
75  haltChildren(0); // halts all running children. The execution is done.
76  return child_status;
77  }
78  break;
80  child_node->setStatus(NodeStatus::IDLE);
81  // the child goes in idle if it has returned failure.
82  if (++failure_childred_num_ > children_count - threshold_)
83  {
86  haltChildren(0); // halts all running children. The execution is hopeless.
87  return child_status;
88  }
89  break;
91  setStatus(child_status);
92  break;
93  default:
94  break;
95  }
96  }
97  return NodeStatus::RUNNING;
98 }
99 
101 {
105 }
106 
108 {
109  return threshold_;
110 }
111 
112 void ParallelNode::setThresholdM(unsigned int threshold_M)
113 {
114  threshold_ = threshold_M;
115 }
116 
117 }
BT::optional< T > getParam(const std::string &key) const
Definition: tree_node.h:117
unsigned int thresholdM()
const std::string & name() const
Definition: tree_node.cpp:92
unsigned int success_childred_num_
Definition: parallel_node.h:44
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:26
virtual void halt() override
The method used to interrupt the execution of a RUNNING node.
void setRegistrationName(const std::string &registration_name)
registrationName() is set by the BehaviorTreeFactory
Definition: tree_node.cpp:113
void haltChildren(int i)
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
ParallelNode(const std::string &name, int threshold)
static constexpr const char * THRESHOLD_KEY
Definition: parallel_node.h:48
void setThresholdM(unsigned int threshold_M)
virtual void halt() override
The method used to interrupt the execution of a RUNNING node.
bool read_parameter_from_blackboard_
Definition: parallel_node.h:47
unsigned int threshold_
Definition: parallel_node.h:43
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
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
unsigned int failure_childred_num_
Definition: parallel_node.h:45
std::basic_string< CharT, Traits > to_string(basic_string_view< CharT, Traits > v)
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:43


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32