15 #include "nav2_behavior_tree/plugins/control/persistent_sequence.hpp"
16 #include "behaviortree_cpp/action_node.h"
17 #include "behaviortree_cpp/bt_factory.h"
19 namespace nav2_behavior_tree
22 PersistentSequenceNode::PersistentSequenceNode(
23 const std::string & name,
24 const BT::NodeConfiguration & conf)
25 : BT::ControlNode::ControlNode(name, conf) {}
27 BT::NodeStatus PersistentSequenceNode::tick()
29 const int children_count = children_nodes_.size();
31 int current_child_idx;
32 if (!getInput(
"current_child_idx", current_child_idx)) {
33 throw BT::RuntimeError(
34 "Missing required input [current_child_idx] in PersistentSequenceNode. "
35 "Set via <Script code=\"current_child_idx := 0\" />");
38 setStatus(BT::NodeStatus::RUNNING);
40 while (current_child_idx < children_count) {
41 TreeNode * current_child_node = children_nodes_[current_child_idx];
42 const BT::NodeStatus child_status = current_child_node->executeTick();
44 switch (child_status) {
45 case BT::NodeStatus::RUNNING:
48 case BT::NodeStatus::FAILURE:
51 current_child_idx = 0;
52 setOutput(
"current_child_idx", 0);
55 case BT::NodeStatus::SUCCESS:
56 case BT::NodeStatus::SKIPPED:
59 setOutput(
"current_child_idx", current_child_idx);
62 case BT::NodeStatus::IDLE:
63 throw std::runtime_error(
"A child node must never return IDLE");
68 if (current_child_idx >= children_count) {
70 setOutput(
"current_child_idx", 0);
72 return BT::NodeStatus::SUCCESS;
77 BT_REGISTER_NODES(factory)
79 BT::NodeBuilder builder =
80 [](
const std::string & name,
const BT::NodeConfiguration & config)
82 return std::make_unique<nav2_behavior_tree::PersistentSequenceNode>(
87 "PersistentSequence", builder);
The PersistentSequenceNode is similar to the SequenceNode, but it stores the index of the last runnin...