Skip to content

Commit c999c03

Browse files
author
Davide Faconti
committed
add infinite loop to Repeat and Retry (issue #80)
1 parent 77848cb commit c999c03

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

include/behaviortree_cpp/decorators/repeat_node.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,22 @@ class RepeatNode : public DecoratorNode
3737
{
3838
public:
3939

40-
RepeatNode(const std::string& name, unsigned int NTries);
40+
RepeatNode(const std::string& name, int NTries);
4141

4242
RepeatNode(const std::string& name, const NodeConfiguration& config);
4343

4444
virtual ~RepeatNode() override = default;
4545

4646
static PortsList providedPorts()
4747
{
48-
return { InputPort<unsigned>(NUM_CYCLES, "Repeat a succesful child up to N times") };
48+
return { InputPort<int>(NUM_CYCLES,
49+
"Repeat a succesful child up to N times. "
50+
"Use -1 to create an infinite loop.") };
4951
}
5052

5153
private:
52-
unsigned num_cycles_;
53-
unsigned try_index_;
54+
int num_cycles_;
55+
int try_index_;
5456

5557
bool read_parameter_from_ports_;
5658
static constexpr const char* NUM_CYCLES = "num_cycles";

include/behaviortree_cpp/decorators/retry_node.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ class RetryNode : public DecoratorNode
3737
{
3838
public:
3939

40-
RetryNode(const std::string& name, unsigned int NTries);
40+
RetryNode(const std::string& name, int NTries);
4141

4242
RetryNode(const std::string& name, const NodeConfiguration& config);
4343

4444
virtual ~RetryNode() override = default;
4545

4646
static PortsList providedPorts()
4747
{
48-
return { InputPort<unsigned>(NUM_ATTEMPTS, "Execute again a failing child up to N times") };
48+
return { InputPort<int>(NUM_ATTEMPTS,
49+
"Execute again a failing child up to N times. "
50+
"Use -1 to create an infinite loop.") };
4951
}
5052

5153
virtual void halt() override;
5254

5355
private:
54-
unsigned int max_attempts_;
55-
unsigned int try_index_;
56+
int max_attempts_;
57+
int try_index_;
5658

5759
bool read_parameter_from_ports_;
5860
static constexpr const char* NUM_ATTEMPTS = "num_attempts";

src/decorators/repeat_node.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace BT
1717
{
1818
constexpr const char* RepeatNode::NUM_CYCLES;
1919

20-
RepeatNode::RepeatNode(const std::string& name, unsigned int NTries)
20+
RepeatNode::RepeatNode(const std::string& name, int NTries)
2121
: DecoratorNode(name, {} ),
2222
num_cycles_(NTries),
2323
try_index_(0),
@@ -47,7 +47,7 @@ NodeStatus RepeatNode::tick()
4747

4848
setStatus(NodeStatus::RUNNING);
4949

50-
while (try_index_ < num_cycles_)
50+
while (try_index_ < num_cycles_ || num_cycles_== -1 )
5151
{
5252
NodeStatus child_state = child_node_->executeTick();
5353

src/decorators/retry_node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace BT
1717
{
1818
constexpr const char* RetryNode::NUM_ATTEMPTS;
1919

20-
RetryNode::RetryNode(const std::string& name, unsigned int NTries)
20+
RetryNode::RetryNode(const std::string& name, int NTries)
2121
: DecoratorNode(name, {} ),
2222
max_attempts_(NTries),
2323
try_index_(0),

0 commit comments

Comments
 (0)