Skip to content
Merged
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
13 changes: 12 additions & 1 deletion include/behaviortree_cpp_v3/controls/reactive_fallback.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,19 @@ class ReactiveFallback : public ControlNode
ReactiveFallback(const std::string& name) : ControlNode(name, {})
{}

/** A ReactiveFallback is not supposed to have more than a single
* anychronous node; if it does an exception is thrown.
* You can disabled that check, if you know what you are doing.
*/
static void EnableException(bool enable);

private:
virtual BT::NodeStatus tick() override;
BT::NodeStatus tick() override;

void halt() override;

int running_child_ = -1;
static bool throw_if_multiple_running;
};

} // namespace BT
14 changes: 13 additions & 1 deletion include/behaviortree_cpp_v3/controls/reactive_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,20 @@ class ReactiveSequence : public ControlNode
ReactiveSequence(const std::string& name) : ControlNode(name, {})
{}

/** A ReactiveSequence is not supposed to have more than a single
* anychronous node; if it does an exception is thrown.
* You can disabled that check, if you know what you are doing.
*/
static void EnableException(bool enable);

private:
virtual BT::NodeStatus tick() override;
BT::NodeStatus tick() override;

void halt() override;

int running_child_ = -1;

static bool throw_if_multiple_running;
};

} // namespace BT
40 changes: 35 additions & 5 deletions src/controls/reactive_fallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@

namespace BT
{

bool ReactiveFallback::throw_if_multiple_running = false;

void ReactiveFallback::EnableException(bool enable)
{
ReactiveFallback::throw_if_multiple_running = enable;
}

NodeStatus ReactiveFallback::tick()
{
size_t failure_count = 0;
if(status() == NodeStatus::IDLE)
{
running_child_ = -1;
}
setStatus(NodeStatus::RUNNING);

for (size_t index = 0; index < childrenCount(); index++)
{
Expand All @@ -26,12 +39,23 @@ NodeStatus ReactiveFallback::tick()
switch (child_status)
{
case NodeStatus::RUNNING: {

// 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++)
// 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 < childrenCount(); i++)
{
haltChild(i);
if(i != index)
{
haltChild(i);
}
}
if(running_child_ == -1)
{
running_child_ = int(index);
}
else if(throw_if_multiple_running && running_child_ != int(index))
{
throw LogicError("[ReactiveFallback]: only a single child can return RUNNING.\n"
"This throw can be disabled with ReactiveFallback::EnableException(false)");
}
return NodeStatus::RUNNING;
}
Expand Down Expand Up @@ -61,4 +85,10 @@ NodeStatus ReactiveFallback::tick()
return NodeStatus::RUNNING;
}

void ReactiveFallback::halt()
{
running_child_ = -1;
ControlNode::halt();
}

} // namespace BT
39 changes: 35 additions & 4 deletions src/controls/reactive_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@

namespace BT
{

bool ReactiveSequence::throw_if_multiple_running = false;

void ReactiveSequence::EnableException(bool enable)
{
ReactiveSequence::throw_if_multiple_running = enable;
}

NodeStatus ReactiveSequence::tick()
{
size_t success_count = 0;
if(status() == NodeStatus::IDLE)
{
running_child_ = -1;
}
setStatus(NodeStatus::RUNNING);

for (size_t index = 0; index < childrenCount(); index++)
{
Expand All @@ -26,11 +39,23 @@ NodeStatus ReactiveSequence::tick()
switch (child_status)
{
case NodeStatus::RUNNING: {
// 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++)
// 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 < childrenCount(); i++)
{
haltChild(i);
if(i != index)
{
haltChild(i);
}
}
if(running_child_ == -1)
{
running_child_ = int(index);
}
else if(throw_if_multiple_running && running_child_ != int(index))
{
throw LogicError("[ReactiveSequence]: only a single child can return RUNNING.\n"
"This throw can be disabled with ReactiveSequence::EnableException(false)");
}
return NodeStatus::RUNNING;
}
Expand Down Expand Up @@ -59,4 +84,10 @@ NodeStatus ReactiveSequence::tick()
return NodeStatus::RUNNING;
}

void ReactiveSequence::halt()
{
running_child_ = -1;
ControlNode::halt();
}

} // namespace BT