Skip to content

Commit

Permalink
backport changes in 4.x and fix #643
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Aug 23, 2023
1 parent 2fb1175 commit 51f92c4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/controls/reactive_fallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ NodeStatus ReactiveFallback::tick()
switch (child_status)
{
case NodeStatus::RUNNING: {
for (size_t i = index + 1; i < childrenCount(); i++)

// reset the previous children, to make sure that they are in IDLE state
// the next time we tick them
for (size_t i = 0; i < index; i++)
{
haltChild(i);
}
Expand Down
8 changes: 4 additions & 4 deletions src/controls/reactive_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace BT
NodeStatus ReactiveSequence::tick()
{
size_t success_count = 0;
size_t running_count = 0;

for (size_t index = 0; index < childrenCount(); index++)
{
Expand All @@ -27,9 +26,9 @@ NodeStatus ReactiveSequence::tick()
switch (child_status)
{
case NodeStatus::RUNNING: {
running_count++;

for (size_t i = index + 1; i < childrenCount(); i++)
// reset the previous children, to make sure that they are in IDLE state
// the next time we tick them
for (size_t i = 0; i < index; i++)
{
haltChild(i);
}
Expand All @@ -51,6 +50,7 @@ NodeStatus ReactiveSequence::tick()
} // end switch
} //end for


if (success_count == childrenCount())
{
resetChildren();
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(BT_TESTS
gtest_blackboard.cpp
gtest_blackboard_precondition.cpp
gtest_ports.cpp
gtest_reactive.cpp
navigation_test.cpp
gtest_subtree.cpp
gtest_switch.cpp
Expand Down
69 changes: 69 additions & 0 deletions tests/gtest_reactive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <gtest/gtest.h>
#include "behaviortree_cpp_v3/bt_factory.h"
#include "test_helper.hpp"
#include "behaviortree_cpp_v3/loggers/bt_cout_logger.h"

using BT::NodeStatus;
using std::chrono::milliseconds;

class SleepNode : public BT::StatefulActionNode
{
public:

SleepNode(const std::string& name, const BT::NodeConfiguration& config):
StatefulActionNode(name, config) {}

NodeStatus onStart() override {
count_ = 0;
return NodeStatus::RUNNING;
}

NodeStatus onRunning() override {
return ++count_ < 10 ? NodeStatus::RUNNING : NodeStatus::SUCCESS;
}

void onHalted() override {}

static BT::PortsList providedPorts(){
return {};
}

private:
int count_ = 0;
};


TEST(Reactive, TestLogging)
{
using namespace BT;

static const char* reactive_xml_text = R"(
<root>
<BehaviorTree ID="Main">
<ReactiveSequence>
<TestA name="testA"/>
<AlwaysSuccess name="success"/>
<Sleep/>
</ReactiveSequence>
</BehaviorTree>
</root>
)";

BehaviorTreeFactory factory;

factory.registerNodeType<SleepNode>("Sleep");

std::array<int, 1> counters;
RegisterTestTick(factory, "Test", counters);

auto tree = factory.createTreeFromText(reactive_xml_text);
StdCoutLogger logger(tree);

auto ret = tree.tickRootWhileRunning();
ASSERT_EQ(ret, NodeStatus::SUCCESS);

int num_ticks = counters[0];
ASSERT_GE(num_ticks, 10);
}


27 changes: 27 additions & 0 deletions tests/test_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef TEST_HELPER_HPP
#define TEST_HELPER_HPP

#include "behaviortree_cpp_v3/bt_factory.h"

inline BT::NodeStatus TestTick(int* tick_counter)
{
(*tick_counter)++;
return BT::NodeStatus::SUCCESS;
}

template <size_t N> inline
void RegisterTestTick(BT::BehaviorTreeFactory& factory, const std::string& name_prefix,
std::array<int, N>& tick_counters)
{
for(size_t i=0; i<tick_counters.size(); i++)
{
tick_counters[i] = false;
char str[100];
sprintf(str, "%s%c", name_prefix.c_str(), char('A'+i ) );
int* counter_ptr = &(tick_counters[i]);
factory.registerSimpleAction(str, std::bind(&TestTick, counter_ptr));
}
}


#endif // TEST_HELPER_HPP

0 comments on commit 51f92c4

Please sign in to comment.