Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0abbd60

Browse files
author
Davide Faconti
committed
Adding NodeParameters to ParallelNode
1 parent 947b649 commit 0abbd60

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

include/behaviortree_cpp/controls/parallel_node.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,17 @@ namespace BT
2121
class ParallelNode : public ControlNode
2222
{
2323
public:
24-
// Constructor
25-
ParallelNode(const std::string& name, int threshold_M);
24+
25+
ParallelNode(const std::string& name, int threshold);
26+
27+
ParallelNode(const std::string& name, const NodeParameters& params);
28+
29+
static const NodeParameters& requiredNodeParameters()
30+
{
31+
static NodeParameters params = {{THRESHOLD_KEY, "1"}};
32+
return params;
33+
}
34+
2635
~ParallelNode() = default;
2736

2837
virtual void halt() override;
@@ -31,10 +40,13 @@ class ParallelNode : public ControlNode
3140
void setThresholdM(unsigned int threshold_M);
3241

3342
private:
34-
unsigned int threshold_M_;
43+
unsigned int threshold_;
3544
unsigned int success_childred_num_;
3645
unsigned int failure_childred_num_;
3746

47+
bool refresh_parameter_;
48+
static constexpr const char* THRESHOLD_KEY = "threshold";
49+
3850
virtual BT::NodeStatus tick() override;
3951
};
4052
}

include/behaviortree_cpp/tree_node.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class TreeNode
140140
try
141141
{
142142
// check if it follows this ${pattern}, if it does, search inside the blackboard
143-
if (bb_ && str.size() >= 4 && str[0] == '$' && str[1] == '{' && str.back() == '}')
143+
if ( bb_ && isBlackboardPattern(str))
144144
{
145145
const std::string stripped_key(&str[2], str.size() - 3);
146146
bool found = bb_->get(stripped_key, destination);
@@ -182,6 +182,9 @@ class TreeNode
182182
const NodeParameters parameters_;
183183

184184
Blackboard::Ptr bb_;
185+
186+
protected:
187+
static bool isBlackboardPattern(const std::string& str );
185188
};
186189
}
187190

src/controls/parallel_node.cpp

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,41 @@
1313

1414
#include "behaviortree_cpp/controls/parallel_node.h"
1515

16-
BT::ParallelNode::ParallelNode(const std::string& name, int threshold_M)
17-
: ControlNode::ControlNode(name, NodeParameters()), threshold_M_(threshold_M)
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),
24+
refresh_parameter_(false)
1825
{
1926
}
2027

21-
BT::NodeStatus BT::ParallelNode::tick()
28+
ParallelNode::ParallelNode(const std::string &name,
29+
const NodeParameters &params)
30+
: ControlNode::ControlNode(name, params),
31+
threshold_(1),
32+
refresh_parameter_(false)
33+
{
34+
auto param = getParam<unsigned>( THRESHOLD_KEY );
35+
if( !param )
36+
{
37+
throw std::runtime_error("Missing parameter [threshold] in ParallelNode");
38+
}
39+
refresh_parameter_ = isBlackboardPattern( params.begin()->second );
40+
}
41+
42+
NodeStatus ParallelNode::tick()
2243
{
44+
if( refresh_parameter_ )
45+
{
46+
// Read it at every tick. Since it points to the blackboard,
47+
// it may change dynamically
48+
getParam(THRESHOLD_KEY, threshold_);
49+
}
50+
2351
success_childred_num_ = 0;
2452
failure_childred_num_ = 0;
2553
// Vector size initialization. children_count_ could change at runtime if you edit the tree
@@ -35,9 +63,9 @@ BT::NodeStatus BT::ParallelNode::tick()
3563
switch (child_status)
3664
{
3765
case NodeStatus::SUCCESS:
38-
child_node->setStatus(
39-
NodeStatus::IDLE); // the child goes in idle if it has returned success.
40-
if (++success_childred_num_ == threshold_M_)
66+
child_node->setStatus(NodeStatus::IDLE);
67+
// the child goes in idle if it has returned success.
68+
if (++success_childred_num_ == threshold_)
4169
{
4270
success_childred_num_ = 0;
4371
failure_childred_num_ = 0;
@@ -46,9 +74,9 @@ BT::NodeStatus BT::ParallelNode::tick()
4674
}
4775
break;
4876
case NodeStatus::FAILURE:
49-
child_node->setStatus(
50-
NodeStatus::IDLE); // the child goes in idle if it has returned failure.
51-
if (++failure_childred_num_ > children_count - threshold_M_)
77+
child_node->setStatus(NodeStatus::IDLE);
78+
// the child goes in idle if it has returned failure.
79+
if (++failure_childred_num_ > children_count - threshold_)
5280
{
5381
success_childred_num_ = 0;
5482
failure_childred_num_ = 0;
@@ -66,19 +94,21 @@ BT::NodeStatus BT::ParallelNode::tick()
6694
return NodeStatus::RUNNING;
6795
}
6896

69-
void BT::ParallelNode::halt()
97+
void ParallelNode::halt()
7098
{
7199
success_childred_num_ = 0;
72100
failure_childred_num_ = 0;
73-
BT::ControlNode::halt();
101+
ControlNode::halt();
74102
}
75103

76-
unsigned int BT::ParallelNode::thresholdM()
104+
unsigned int ParallelNode::thresholdM()
77105
{
78-
return threshold_M_;
106+
return threshold_;
79107
}
80108

81-
void BT::ParallelNode::setThresholdM(unsigned int threshold_M)
109+
void ParallelNode::setThresholdM(unsigned int threshold_M)
82110
{
83-
threshold_M_ = threshold_M;
111+
threshold_ = threshold_M;
112+
}
113+
84114
}

src/tree_node.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ void TreeNode::setRegistrationName(const std::string& registration_name)
103103
registration_name_ = registration_name;
104104
}
105105

106+
bool TreeNode::isBlackboardPattern(const std::string &str)
107+
{
108+
return str.size() >= 4 && str[0] == '$' && str[1] == '{' && str.back() == '}';
109+
}
110+
106111
const std::string& TreeNode::registrationName() const
107112
{
108113
return registration_name_;

0 commit comments

Comments
 (0)