Skip to content

Commit ce6503a

Browse files
committed
parallel node fix
1 parent a363bdc commit ce6503a

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

include/behaviortree_cpp_v3/controls/parallel_node.h

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,50 @@
2020
namespace BT
2121
{
2222

23+
/**
24+
* @brief The ParallelNode execute all its children
25+
* __concurrently__, but not in separate threads!
26+
*
27+
* Even if this may look similar to ReactiveSequence,
28+
* this Control Node is the only one that may have
29+
* multiple children in the RUNNING state at the same time.
30+
*
31+
* The Node is completed either when the THRESHOLD_SUCCESS
32+
* or THRESHOLD_FAILURE number is reached (both configured using ports).
33+
*
34+
* If any of the threahold is reached, and other childen are still running,
35+
* they will be halted.
36+
*
37+
* Note that threshold indexes work as in Python:
38+
* https://www.i2tutorials.com/what-are-negative-indexes-and-why-are-they-used/
39+
*
40+
* Therefore -1 is equivalent to the number of children.
41+
*/
2342
class ParallelNode : public ControlNode
2443
{
2544
public:
2645

27-
ParallelNode(const std::string& name, unsigned success_threshold,
28-
unsigned failure_threshold = 1);
46+
ParallelNode(const std::string& name, int success_threshold,
47+
int failure_threshold = 1);
2948

3049
ParallelNode(const std::string& name, const NodeConfiguration& config);
3150

3251
static PortsList providedPorts()
3352
{
34-
return { InputPort<unsigned>(THRESHOLD_SUCCESS, "number of childen which need to succeed to trigger a SUCCESS" ),
35-
InputPort<unsigned>(THRESHOLD_FAILURE, 1, "number of childen which need to fail to trigger a FAILURE" ) };
53+
return { InputPort<int>(THRESHOLD_SUCCESS,
54+
"number of childen which need to succeed to trigger a SUCCESS" ),
55+
InputPort<int>(THRESHOLD_FAILURE, 1,
56+
"number of childen which need to fail to trigger a FAILURE" ) };
3657
}
3758

3859
~ParallelNode() = default;
3960

4061
virtual void halt() override;
4162

42-
unsigned int thresholdM();
43-
unsigned int thresholdFM();
44-
void setThresholdM(int threshold_M);
45-
void setThresholdFM(int threshold_M);
63+
size_t successThreshold() const;
64+
size_t failureThreshold() const;
65+
void setSuccessThreshold(int threshold_M);
66+
void setFailureThreshold(int threshold_M);
4667

4768
private:
4869
int success_threshold_;

src/controls/parallel_node.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ namespace BT
2222
constexpr const char* ParallelNode::THRESHOLD_FAILURE;
2323
constexpr const char* ParallelNode::THRESHOLD_SUCCESS;
2424

25-
ParallelNode::ParallelNode(const std::string& name, unsigned success_threshold,
26-
unsigned failure_threshold)
25+
ParallelNode::ParallelNode(const std::string& name, int success_threshold,
26+
int failure_threshold)
2727
: ControlNode::ControlNode(name, {} ),
2828
success_threshold_(success_threshold),
2929
failure_threshold_(failure_threshold),
@@ -61,12 +61,12 @@ NodeStatus ParallelNode::tick()
6161

6262
const size_t children_count = children_nodes_.size();
6363

64-
if( children_count < thresholdM())
64+
if( children_count < successThreshold())
6565
{
6666
throw LogicError("Number of children is less than threshold. Can never succeed.");
6767
}
6868

69-
if( children_count < thresholdFM())
69+
if( children_count < failureThreshold())
7070
{
7171
throw LogicError("Number of children is less than threshold. Can never fail.");
7272
}
@@ -97,7 +97,7 @@ NodeStatus ParallelNode::tick()
9797
}
9898
success_childred_num++;
9999

100-
if (success_childred_num == thresholdM())
100+
if (success_childred_num == successThreshold())
101101
{
102102
skip_list_.clear();
103103
haltChildren();
@@ -115,8 +115,8 @@ NodeStatus ParallelNode::tick()
115115

116116
// It fails if it is not possible to succeed anymore or if
117117
// number of failures are equal to failure_threshold_
118-
if ((failure_childred_num > children_count - thresholdM())
119-
|| (failure_childred_num == thresholdFM()))
118+
if ((failure_childred_num > children_count - successThreshold())
119+
|| (failure_childred_num == failureThreshold()))
120120
{
121121
skip_list_.clear();
122122
haltChildren();
@@ -145,26 +145,26 @@ void ParallelNode::halt()
145145
ControlNode::halt();
146146
}
147147

148-
unsigned int ParallelNode::thresholdM()
148+
size_t ParallelNode::successThreshold() const
149149
{
150150
return success_threshold_ < 0
151-
? std::max(children_nodes_.size() + success_threshold_ + 1, static_cast<std::size_t>(0))
151+
? std::max(children_nodes_.size() + success_threshold_ + 1, size_t(0))
152152
: success_threshold_;
153153
}
154154

155-
unsigned int ParallelNode::thresholdFM()
155+
size_t ParallelNode::failureThreshold() const
156156
{
157157
return failure_threshold_ < 0
158-
? std::max(children_nodes_.size() + failure_threshold_ + 1, static_cast<std::size_t>(0))
158+
? std::max(children_nodes_.size() + failure_threshold_ + 1, size_t(0))
159159
: failure_threshold_;
160160
}
161161

162-
void ParallelNode::setThresholdM(int threshold_M)
162+
void ParallelNode::setSuccessThreshold(int threshold_M)
163163
{
164164
success_threshold_ = threshold_M;
165165
}
166166

167-
void ParallelNode::setThresholdFM(int threshold_M)
167+
void ParallelNode::setFailureThreshold(int threshold_M)
168168
{
169169
failure_threshold_ = threshold_M;
170170
}

0 commit comments

Comments
 (0)