Skip to content

Commit 51f92c4

Browse files
committed
backport changes in 4.x and fix #643
1 parent 2fb1175 commit 51f92c4

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

src/controls/reactive_fallback.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ NodeStatus ReactiveFallback::tick()
2626
switch (child_status)
2727
{
2828
case NodeStatus::RUNNING: {
29-
for (size_t i = index + 1; i < childrenCount(); i++)
29+
30+
// reset the previous children, to make sure that they are in IDLE state
31+
// the next time we tick them
32+
for (size_t i = 0; i < index; i++)
3033
{
3134
haltChild(i);
3235
}

src/controls/reactive_sequence.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ namespace BT
1717
NodeStatus ReactiveSequence::tick()
1818
{
1919
size_t success_count = 0;
20-
size_t running_count = 0;
2120

2221
for (size_t index = 0; index < childrenCount(); index++)
2322
{
@@ -27,9 +26,9 @@ NodeStatus ReactiveSequence::tick()
2726
switch (child_status)
2827
{
2928
case NodeStatus::RUNNING: {
30-
running_count++;
31-
32-
for (size_t i = index + 1; i < childrenCount(); i++)
29+
// reset the previous children, to make sure that they are in IDLE state
30+
// the next time we tick them
31+
for (size_t i = 0; i < index; i++)
3332
{
3433
haltChild(i);
3534
}
@@ -51,6 +50,7 @@ NodeStatus ReactiveSequence::tick()
5150
} // end switch
5251
} //end for
5352

53+
5454
if (success_count == childrenCount())
5555
{
5656
resetChildren();

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(BT_TESTS
1515
gtest_blackboard.cpp
1616
gtest_blackboard_precondition.cpp
1717
gtest_ports.cpp
18+
gtest_reactive.cpp
1819
navigation_test.cpp
1920
gtest_subtree.cpp
2021
gtest_switch.cpp

tests/gtest_reactive.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <gtest/gtest.h>
2+
#include "behaviortree_cpp_v3/bt_factory.h"
3+
#include "test_helper.hpp"
4+
#include "behaviortree_cpp_v3/loggers/bt_cout_logger.h"
5+
6+
using BT::NodeStatus;
7+
using std::chrono::milliseconds;
8+
9+
class SleepNode : public BT::StatefulActionNode
10+
{
11+
public:
12+
13+
SleepNode(const std::string& name, const BT::NodeConfiguration& config):
14+
StatefulActionNode(name, config) {}
15+
16+
NodeStatus onStart() override {
17+
count_ = 0;
18+
return NodeStatus::RUNNING;
19+
}
20+
21+
NodeStatus onRunning() override {
22+
return ++count_ < 10 ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
23+
}
24+
25+
void onHalted() override {}
26+
27+
static BT::PortsList providedPorts(){
28+
return {};
29+
}
30+
31+
private:
32+
int count_ = 0;
33+
};
34+
35+
36+
TEST(Reactive, TestLogging)
37+
{
38+
using namespace BT;
39+
40+
static const char* reactive_xml_text = R"(
41+
<root>
42+
<BehaviorTree ID="Main">
43+
<ReactiveSequence>
44+
<TestA name="testA"/>
45+
<AlwaysSuccess name="success"/>
46+
<Sleep/>
47+
</ReactiveSequence>
48+
</BehaviorTree>
49+
</root>
50+
)";
51+
52+
BehaviorTreeFactory factory;
53+
54+
factory.registerNodeType<SleepNode>("Sleep");
55+
56+
std::array<int, 1> counters;
57+
RegisterTestTick(factory, "Test", counters);
58+
59+
auto tree = factory.createTreeFromText(reactive_xml_text);
60+
StdCoutLogger logger(tree);
61+
62+
auto ret = tree.tickRootWhileRunning();
63+
ASSERT_EQ(ret, NodeStatus::SUCCESS);
64+
65+
int num_ticks = counters[0];
66+
ASSERT_GE(num_ticks, 10);
67+
}
68+
69+

tests/test_helper.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef TEST_HELPER_HPP
2+
#define TEST_HELPER_HPP
3+
4+
#include "behaviortree_cpp_v3/bt_factory.h"
5+
6+
inline BT::NodeStatus TestTick(int* tick_counter)
7+
{
8+
(*tick_counter)++;
9+
return BT::NodeStatus::SUCCESS;
10+
}
11+
12+
template <size_t N> inline
13+
void RegisterTestTick(BT::BehaviorTreeFactory& factory, const std::string& name_prefix,
14+
std::array<int, N>& tick_counters)
15+
{
16+
for(size_t i=0; i<tick_counters.size(); i++)
17+
{
18+
tick_counters[i] = false;
19+
char str[100];
20+
sprintf(str, "%s%c", name_prefix.c_str(), char('A'+i ) );
21+
int* counter_ptr = &(tick_counters[i]);
22+
factory.registerSimpleAction(str, std::bind(&TestTick, counter_ptr));
23+
}
24+
}
25+
26+
27+
#endif // TEST_HELPER_HPP

0 commit comments

Comments
 (0)