Skip to content

Commit 1750138

Browse files
author
Davide Faconti
committed
Read at every tick the parameter if Blackboard is used
1 parent 0abbd60 commit 1750138

File tree

7 files changed

+73
-29
lines changed

7 files changed

+73
-29
lines changed

include/behaviortree_cpp/controls/sequence_star_node.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ class SequenceStarNode : public ControlNode
4646

4747
static const NodeParameters& requiredNodeParameters()
4848
{
49-
static NodeParameters params = {{"reset_on_failure", "true"}};
49+
static NodeParameters params = {{RESET_PARAM, "true"}};
5050
return params;
5151
}
5252

5353
private:
5454
unsigned int current_child_idx_;
5555
bool reset_on_failure_;
5656

57+
bool refresh_parameter_;
58+
static constexpr const char* RESET_PARAM = "reset_on_failure";
59+
5760
virtual BT::NodeStatus tick() override;
5861
};
5962
}

include/behaviortree_cpp/decorators/repeat_node.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class RepeatNode : public DecoratorNode
3535
}
3636

3737
private:
38-
unsigned NTries_;
39-
unsigned TryIndx_;
38+
unsigned num_cycles_;
39+
unsigned try_index_;
4040

41+
bool refresh_parameter_;
4142
static constexpr const char* NUM_CYCLES = "num_cycles";
43+
4244
virtual BT::NodeStatus tick() override;
4345
};
4446
}

include/behaviortree_cpp/decorators/retry_node.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class RetryNode : public DecoratorNode
3535
}
3636

3737
private:
38-
unsigned int NTries_;
39-
unsigned int TryIndx_;
38+
unsigned int max_attempts_;
39+
unsigned int try_index_;
4040

41+
bool refresh_parameter_;
4142
static constexpr const char* NUM_ATTEMPTS = "num_attempts";
43+
4244
virtual BT::NodeStatus tick() override;
4345
};
4446
}

src/controls/parallel_node.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ ParallelNode::ParallelNode(const std::string& name, int threshold)
2828
ParallelNode::ParallelNode(const std::string &name,
2929
const NodeParameters &params)
3030
: ControlNode::ControlNode(name, params),
31-
threshold_(1),
3231
refresh_parameter_(false)
3332
{
34-
auto param = getParam<unsigned>( THRESHOLD_KEY );
35-
if( !param )
33+
if( !getParam(THRESHOLD_KEY, threshold_) )
3634
{
3735
throw std::runtime_error("Missing parameter [threshold] in ParallelNode");
3836
}

src/controls/sequence_star_node.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,37 @@
1515

1616
namespace BT
1717
{
18+
19+
constexpr const char* SequenceStarNode::RESET_PARAM;
20+
1821
SequenceStarNode::SequenceStarNode(const std::string& name, bool reset_on_failure)
19-
: ControlNode::ControlNode(name, SequenceStarNode::requiredNodeParameters())
22+
: ControlNode::ControlNode(name, {{RESET_PARAM, std::to_string(reset_on_failure)}})
2023
, current_child_idx_(0)
2124
, reset_on_failure_(reset_on_failure)
25+
, refresh_parameter_(false)
2226
{
2327
}
2428

2529
SequenceStarNode::SequenceStarNode(const std::string& name, const NodeParameters& params)
26-
: ControlNode::ControlNode(name, params), current_child_idx_(0), reset_on_failure_(true)
30+
: ControlNode::ControlNode(name, params), current_child_idx_(0),
31+
refresh_parameter_(false)
2732
{
28-
getParam<bool>("reset_on_failure", reset_on_failure_);
33+
if( !getParam(RESET_PARAM, reset_on_failure_) )
34+
{
35+
throw std::runtime_error("Missing parameter [reset_on_failure] in SequenceStarNode");
36+
}
37+
refresh_parameter_ = isBlackboardPattern( params.begin()->second );
2938
}
3039

3140
NodeStatus SequenceStarNode::tick()
3241
{
33-
// Vector size initialization. children_count_ could change at runtime if you edit the tree
42+
if( refresh_parameter_)
43+
{
44+
// Read it at every tick. Since it points to the blackboard,
45+
// it may change dynamically
46+
getParam(RESET_PARAM, reset_on_failure_);
47+
}
48+
3449
const unsigned children_count = children_nodes_.size();
3550

3651
setStatus(NodeStatus::RUNNING);

src/decorators/repeat_node.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,43 @@ namespace BT
1818
constexpr const char* RepeatNode::NUM_CYCLES;
1919

2020
RepeatNode::RepeatNode(const std::string& name, unsigned int NTries)
21-
: DecoratorNode(name, {{NUM_CYCLES, std::to_string(NTries)}}), NTries_(NTries), TryIndx_(0)
21+
: DecoratorNode(name, {{NUM_CYCLES, std::to_string(NTries)}}),
22+
num_cycles_(NTries),
23+
try_index_(0),
24+
refresh_parameter_(false)
2225
{
2326
}
2427

2528
RepeatNode::RepeatNode(const std::string& name, const NodeParameters& params)
26-
: DecoratorNode(name, params), NTries_(1), TryIndx_(0)
29+
: DecoratorNode(name, params),
30+
try_index_(0),
31+
refresh_parameter_(false)
2732
{
28-
auto param = getParam<int>(NUM_CYCLES);
29-
if (param)
33+
if( !getParam(NUM_CYCLES, num_cycles_) )
3034
{
31-
NTries_ = param.value();
35+
throw std::runtime_error("Missing parameter [num_cycles] in RepeatNode");
3236
}
37+
refresh_parameter_ = isBlackboardPattern( params.begin()->second );
3338
}
3439

3540
NodeStatus RepeatNode::tick()
3641
{
42+
if( refresh_parameter_ )
43+
{
44+
// Read it at every tick. Since it points to the blackboard,
45+
// it may change dynamically
46+
getParam(RepeatNode::NUM_CYCLES, num_cycles_);
47+
}
48+
3749
setStatus(NodeStatus::RUNNING);
3850
NodeStatus child_state = child_node_->executeTick();
3951

4052
switch (child_state)
4153
{
4254
case NodeStatus::SUCCESS:
4355
{
44-
TryIndx_++;
45-
if (TryIndx_ >= NTries_)
56+
try_index_++;
57+
if (try_index_ >= num_cycles_)
4658
{
4759
setStatus(NodeStatus::SUCCESS);
4860
child_node_->setStatus(NodeStatus::IDLE);
@@ -52,7 +64,7 @@ NodeStatus RepeatNode::tick()
5264

5365
case NodeStatus::FAILURE:
5466
{
55-
TryIndx_ = 0;
67+
try_index_ = 0;
5668
setStatus(NodeStatus::FAILURE);
5769
child_node_->setStatus(NodeStatus::IDLE);
5870
}

src/decorators/retry_node.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,53 @@ namespace BT
1818
constexpr const char* RetryNode::NUM_ATTEMPTS;
1919

2020
RetryNode::RetryNode(const std::string& name, unsigned int NTries)
21-
: DecoratorNode(name, {{NUM_ATTEMPTS, std::to_string(NTries)}}), NTries_(NTries), TryIndx_(0)
21+
: DecoratorNode(name, {{NUM_ATTEMPTS, std::to_string(NTries)}}),
22+
max_attempts_(NTries),
23+
try_index_(0),
24+
refresh_parameter_(false)
2225
{
2326
}
2427

2528
RetryNode::RetryNode(const std::string& name, const NodeParameters& params)
26-
: DecoratorNode(name, params), NTries_(1), TryIndx_(0)
29+
: DecoratorNode(name, params),
30+
try_index_(0),
31+
refresh_parameter_(false)
2732
{
28-
auto param = getParam<int>(NUM_ATTEMPTS);
29-
if (param)
33+
if( !getParam(NUM_ATTEMPTS, max_attempts_) )
3034
{
31-
NTries_ = param.value();
35+
throw std::runtime_error("Missing parameter [num_attempts] in RetryNode");
3236
}
37+
refresh_parameter_ = isBlackboardPattern( params.begin()->second );
3338
}
3439

3540
NodeStatus RetryNode::tick()
3641
{
42+
if( refresh_parameter_ )
43+
{
44+
// Read it at every tick. Since it points to the blackboard,
45+
// it may change dynamically
46+
getParam(NUM_ATTEMPTS, max_attempts_);
47+
}
48+
3749
setStatus(NodeStatus::RUNNING);
3850
NodeStatus child_state = child_node_->executeTick();
3951

4052
switch (child_state)
4153
{
4254
case NodeStatus::SUCCESS:
4355
{
44-
TryIndx_ = 0;
56+
try_index_ = 0;
4557
setStatus(NodeStatus::SUCCESS);
4658
child_node_->setStatus(NodeStatus::IDLE);
4759
}
4860
break;
4961

5062
case NodeStatus::FAILURE:
5163
{
52-
TryIndx_++;
53-
if (TryIndx_ >= NTries_)
64+
try_index_++;
65+
if (try_index_ >= max_attempts_)
5466
{
55-
TryIndx_ = 0;
67+
try_index_ = 0;
5668
setStatus(NodeStatus::FAILURE);
5769
child_node_->setStatus(NodeStatus::IDLE);
5870
}

0 commit comments

Comments
 (0)