Skip to content
Draft
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ list(APPEND BT_SOURCE
src/controls/sequence_node.cpp
src/controls/sequence_with_memory_node.cpp
src/controls/switch_node.cpp
src/controls/try_catch_node.cpp
src/controls/while_do_else_node.cpp

src/loggers/bt_cout_logger.cpp
Expand Down
1 change: 1 addition & 0 deletions include/behaviortree_cpp/behavior_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "behaviortree_cpp/controls/sequence_node.h"
#include "behaviortree_cpp/controls/sequence_with_memory_node.h"
#include "behaviortree_cpp/controls/switch_node.h"
#include "behaviortree_cpp/controls/try_catch_node.h"
#include "behaviortree_cpp/controls/if_then_else_node.h"
#include "behaviortree_cpp/controls/while_do_else_node.h"

Expand Down
69 changes: 69 additions & 0 deletions include/behaviortree_cpp/controls/try_catch_node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

#include "behaviortree_cpp/control_node.h"

namespace BT
{
/**
* @brief The TryCatch node executes children 1..N-1 as a Sequence ("try" block).
*
* If all children in the try-block succeed, this node returns SUCCESS.
*
* If any child in the try-block fails, the last child N is executed as a
* "catch" (cleanup) action, and this node returns FAILURE regardless of
* the catch child's result.
*
* - If a try-child returns RUNNING, this node returns RUNNING.
* - If a try-child returns SUCCESS, continue to the next try-child.
* - If a try-child returns FAILURE, enter catch mode and tick the last child.
* - If the catch child returns RUNNING, this node returns RUNNING.
* - When the catch child finishes (SUCCESS or FAILURE), this node returns FAILURE.
* - SKIPPED try-children are skipped over (not treated as failure).
*
* Port "catch_on_halt" (default false): if true, the catch child is also
* executed when the TryCatch node is halted while the try-block is RUNNING.
*
* Requires at least 2 children.
*/
class TryCatchNode : public ControlNode
{
public:
TryCatchNode(const std::string& name, const NodeConfig& config);

~TryCatchNode() override = default;

TryCatchNode(const TryCatchNode&) = delete;
TryCatchNode& operator=(const TryCatchNode&) = delete;
TryCatchNode(TryCatchNode&&) = delete;
TryCatchNode& operator=(TryCatchNode&&) = delete;

static PortsList providedPorts()
{
return { InputPort<bool>("catch_on_halt", false,
"If true, execute the catch child when "
"the node is halted during the try-block") };
}

void halt() override;

private:
size_t current_child_idx_ = 0;
size_t skipped_count_ = 0;
bool in_catch_ = false;

BT::NodeStatus tick() override;
};

} // namespace BT
1 change: 1 addition & 0 deletions src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ BehaviorTreeFactory::BehaviorTreeFactory() : _p(new PImpl)
registerNodeType<ReactiveFallback>("ReactiveFallback");
registerNodeType<IfThenElseNode>("IfThenElse");
registerNodeType<WhileDoElseNode>("WhileDoElse");
registerNodeType<TryCatchNode>("TryCatch");

registerNodeType<InverterNode>("Inverter");

Expand Down
134 changes: 134 additions & 0 deletions src/controls/try_catch_node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* Copyright (C) 2018-2025 Davide Faconti, Eurecat - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "behaviortree_cpp/controls/try_catch_node.h"

namespace BT
{
TryCatchNode::TryCatchNode(const std::string& name, const NodeConfig& config)
: ControlNode::ControlNode(name, config)
{
setRegistrationID("TryCatch");
}

void TryCatchNode::halt()
{
bool catch_on_halt = false;
getInput("catch_on_halt", catch_on_halt);

// If catch_on_halt is enabled and we were in the try-block (not already in catch),
// execute the catch child synchronously before halting.
if(catch_on_halt && !in_catch_ && isStatusActive(status()) &&
children_nodes_.size() >= 2)
{
// Halt all try-block children first
for(size_t i = 0; i < children_nodes_.size() - 1; i++)
{
haltChild(i);
}

// Tick the catch child. If it returns RUNNING, halt it too
// (best-effort cleanup during halt).
TreeNode* catch_child = children_nodes_.back();
const NodeStatus catch_status = catch_child->executeTick();
if(catch_status == NodeStatus::RUNNING)
{
haltChild(children_nodes_.size() - 1);
}
}

current_child_idx_ = 0;
skipped_count_ = 0;
in_catch_ = false;
ControlNode::halt();
}

NodeStatus TryCatchNode::tick()
{
const size_t children_count = children_nodes_.size();

if(children_count < 2)
{
throw LogicError("[", name(), "]: TryCatch requires at least 2 children");
}

if(!isStatusActive(status()))
{
skipped_count_ = 0;
in_catch_ = false;
}

setStatus(NodeStatus::RUNNING);

const size_t try_count = children_count - 1;

// If we are in catch mode, tick the last child (cleanup)
if(in_catch_)
{
TreeNode* catch_child = children_nodes_.back();
const NodeStatus catch_status = catch_child->executeTick();

if(catch_status == NodeStatus::RUNNING)
{
return NodeStatus::RUNNING;
}

// Catch child finished (SUCCESS or FAILURE): return FAILURE
resetChildren();
current_child_idx_ = 0;
in_catch_ = false;
return NodeStatus::FAILURE;
}

// Try-block: execute children 0..N-2 as a Sequence
while(current_child_idx_ < try_count)
{
TreeNode* current_child_node = children_nodes_[current_child_idx_];
const NodeStatus child_status = current_child_node->executeTick();

switch(child_status)
{
case NodeStatus::RUNNING: {
return NodeStatus::RUNNING;
}
case NodeStatus::FAILURE: {
// Enter catch mode: halt try-block children, then tick catch child
resetChildren();
current_child_idx_ = 0;
in_catch_ = true;
return tick(); // re-enter to tick the catch child
}
case NodeStatus::SUCCESS: {
current_child_idx_++;
}
break;
case NodeStatus::SKIPPED: {
current_child_idx_++;
skipped_count_++;
}
break;
case NodeStatus::IDLE: {
throw LogicError("[", name(), "]: A child should not return IDLE");
}
}
}

// All try-children completed successfully (or were skipped)
const bool all_skipped = (skipped_count_ == try_count);
resetChildren();
current_child_idx_ = 0;
skipped_count_ = 0;

return all_skipped ? NodeStatus::SKIPPED : NodeStatus::SUCCESS;
}

} // namespace BT
5 changes: 5 additions & 0 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,11 @@ void VerifyXML(const std::string& xml_text,
ThrowError(line_number, std::string("The node '") + registered_name +
"' must have 1 or more children");
}
if(registered_name == "TryCatch" && children_count < 2)
{
ThrowError(line_number, std::string("The node 'TryCatch' must have "
"at least 2 children"));
}
if(registered_name == "ReactiveSequence")
{
size_t async_count = 0;
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(BT_TESTS
gtest_subtree.cpp
gtest_switch.cpp
gtest_tree.cpp
gtest_try_catch.cpp
gtest_updates.cpp
gtest_wakeup.cpp
gtest_interface.cpp
Expand Down
Loading
Loading