Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/controls/reactive_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ NodeStatus ReactiveSequence::tick()
} // end switch
} //end for

if (success_count == childrenCount())
//if (success_count == childrenCount())
{
resetChildren();
}
Expand Down
117 changes: 117 additions & 0 deletions tests/gtest_skipping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "behaviortree_cpp/basic_types.h"
#include "behaviortree_cpp/bt_factory.h"
#include "test_helper.hpp"
#include "../sample_nodes/dummy_nodes.h"

using namespace BT;

Expand Down Expand Up @@ -114,3 +115,119 @@ TEST(SkippingLogic, ReactiveSingleChild)

tree.tickWhileRunning();
}

enum DeviceType { BATT=1, CONTROLLER=2 };
BT::NodeStatus checkLevel2(BT::TreeNode &self)
{
double percent = self.getInput<double>("percentage").value();
DeviceType devType;
auto res = self.getInput("deviceType", devType);
if(!res) {
throw std::runtime_error(res.error());
}

if(devType == DeviceType::BATT)
{
self.setOutput("isLowBattery", (percent < 25));
}
std::cout << "Device: " << devType << " Level: " << percent << std::endl;
return BT::NodeStatus::SUCCESS;
}

TEST(SkippingLogic, RepeatedSkippingReactiveSequence)
{
BehaviorTreeFactory factory;
std::array<int, 2> counters;
RegisterTestTick(factory, "Test", counters);

//! setting the battery level = 50
const std::string xml_text = R"(

<root BTCPP_format="4" >
<BehaviorTree ID="PowerManagerT">
<ReactiveSequence>
<Script code=" LOW_BATT:=50 "/>
<CheckLevel deviceType="BATT"
percentage="{LOW_BATT}"
isLowBattery="{isLowBattery}"/>
<TestA _skipIf="!isLowBattery"/>
<TestB/>
</ReactiveSequence>
</BehaviorTree>
</root>)";

factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");
factory.registerSimpleCondition("CheckLevel", std::bind(checkLevel2, std::placeholders::_1),
{ BT::InputPort("percentage"),
BT::InputPort("deviceType"),
BT::OutputPort("isLowBattery")});
factory.registerScriptingEnums<DeviceType>();

auto tree = factory.createTreeFromText(xml_text);

BT::NodeStatus status;
try {
int runs = 0;
while(runs < 5)
{
status = tree.tickOnce();
tree.sleep(std::chrono::milliseconds(10));
runs++;
}
} catch (BT::LogicError err) {
std::cout << err.what() << std::endl;
}

ASSERT_EQ(status, NodeStatus::SUCCESS);
ASSERT_EQ(counters[0], 0);
ASSERT_EQ(counters[1], 5);
}

TEST(SkippingLogic, RepeatedNoSkippingReactiveSequence)
{
BehaviorTreeFactory factory;
std::array<int, 2> counters;
RegisterTestTick(factory, "Test", counters);

//! setting the battery level = 10
const std::string xml_text = R"(

<root BTCPP_format="4" >
<BehaviorTree ID="PowerManagerT">
<ReactiveSequence>
<Script code=" LOW_BATT:=10 "/>
<CheckLevel deviceType="BATT"
percentage="{LOW_BATT}"
isLowBattery="{isLowBattery}"/>
<TestA _skipIf="!isLowBattery"/>
<TestB/>
</ReactiveSequence>
</BehaviorTree>
</root>)";

factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");
factory.registerSimpleCondition("CheckLevel", std::bind(checkLevel2, std::placeholders::_1),
{ BT::InputPort("percentage"),
BT::InputPort("deviceType"),
BT::OutputPort("isLowBattery")});
factory.registerScriptingEnums<DeviceType>();

auto tree = factory.createTreeFromText(xml_text);

BT::NodeStatus status;
try {
int runs = 0;
while(runs < 5)
{
status = tree.tickOnce();
tree.sleep(std::chrono::milliseconds(10));
runs++;
}
} catch (BT::LogicError err) {
std::cout << err.what() << std::endl;
}

ASSERT_EQ(status, NodeStatus::SUCCESS);
ASSERT_EQ(counters[0], 5);
ASSERT_EQ(counters[1], 5);
}
1 change: 1 addition & 0 deletions tests/script_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "../sample_nodes/dummy_nodes.h"

#include <lexy/input/string_input.hpp>
#include "test_helper.hpp"

TEST(ParserTest, AnyTypes)
{
Expand Down