Skip to content

Commit

Permalink
tick API changed
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Oct 11, 2022
1 parent 10fa55f commit 33f5628
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 123 deletions.
2 changes: 1 addition & 1 deletion examples/ex01_wrap_legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ int main()

auto tree = factory.createTreeFromText(xml_text);

tree.tickRoot();
tree.tickWhileRunning();

return 0;
}
Expand Down
3 changes: 2 additions & 1 deletion examples/ex02_runtime_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int main()
factory.registerNodeType<SayRuntimePort>("SayRuntimePort", say_ports);

auto tree = factory.createTreeFromText(xml_text);
tree.tickRoot();
tree.tickWhileRunning();

return 0;
}
2 changes: 1 addition & 1 deletion examples/ex03_ncurses_manual_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main()
factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");

auto tree = factory.createTreeFromText(xml_text);
auto ret = tree.tickRoot();
auto ret = tree.tickWhileRunning();

std::cout << "Result: " << ret << std::endl;

Expand Down
5 changes: 1 addition & 4 deletions examples/ex04_waypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ int main()
for (const auto& xml_text : {xml_implicit, xml_A, xml_B})
{
auto tree = factory.createTreeFromText(xml_text);
while (tree.tickRoot() == NodeStatus::RUNNING)
{
tree.sleep(std::chrono::milliseconds(10));
}
tree.tickWhileRunning();
std::cout << "--------------" << std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/t01_build_your_first_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main()
// The tick is propagated to the children based on the logic of the tree.
// In this case, the entire sequence is executed, because all the children
// of the Sequence return SUCCESS.
tree.tickRoot();
tree.tickWhileRunning();

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/t02_basic_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ int main()

auto tree = factory.createTreeFromText(xml_text);

tree.tickRoot();
tree.tickWhileRunning();

/* Expected output:
*
Expand Down
2 changes: 1 addition & 1 deletion examples/t03_generic_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ int main()
factory.registerNodeType<PrintTarget>("PrintTarget");

auto tree = factory.createTreeFromText(xml_text);
tree.tickRoot();
tree.tickWhileRunning();

/* Expected output:
*
Expand Down
4 changes: 2 additions & 2 deletions examples/t04_reactive_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ int main()
// Tick the root until we receive either SUCCESS or RUNNING
// same as: tree.tickRoot(Tree::WHILE_RUNNING)
std::cout << "--- ticking\n";
status = tree.tickRoot();
status = tree.tickWhileRunning();
std::cout << "--- status: " << toStr(status) << "\n\n";
#else
// If we need to run code between one tick() and the next,
// we can implement our own while loop
while (status != NodeStatus::SUCCESS)
{
std::cout << "--- ticking\n";
status = tree.tickRoot(Tree::ONCE);
status = tree.tickOnce();
std::cout << "--- status: " << toStr(status) << "\n\n";

// if still running, add some wait time
Expand Down
2 changes: 1 addition & 1 deletion examples/t05_crossdoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main()
BT::printTreeRecursively(tree.rootNode());

// Tick multiple times, until either FAILURE of SUCCESS is returned
tree.tickRoot(BT::Tree::WHILE_RUNNING);
tree.tickWhileRunning();

return 0;
}
2 changes: 1 addition & 1 deletion examples/t06_subtree_port_remapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main()

auto tree = factory.createTreeFromText(xml_text);

tree.tickRoot(Tree::WHILE_RUNNING);
tree.tickWhileRunning();

// let's visualize some information about the current state of the blackboards.
std::cout << "\n------ First BB ------" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions examples/t07_load_multiple_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ int main()
// You can create the MainTree and the subtrees will be added automatically.
std::cout << "----- MainTree tick ----" << std::endl;
auto main_tree = factory.createTree("MainTree");
main_tree.tickRoot();
main_tree.tickWhileRunning();

// ... or you can create only one of the subtree
std::cout << "----- SubA tick ----" << std::endl;
auto subA_tree = factory.createTree("SubA");
subA_tree.tickRoot();
subA_tree.tickWhileRunning();

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/t08_additional_node_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int main()
// apply the visitor to all the nodes of the tree
tree.applyVisitor(visitor);

tree.tickRoot();
tree.tickWhileRunning();

/* Expected output:
Action_A: 42 / hello world
Expand Down
2 changes: 1 addition & 1 deletion examples/t09_scripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main()
factory.registerScriptingEnum("THE_ANSWER", 42);

auto tree = factory.createTreeFromText(xml_text);
tree.tickRoot();
tree.tickWhileRunning();

return 0;
}
Expand Down
1 change: 0 additions & 1 deletion include/behaviortree_cpp/action_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class StatefulActionNode : public ActionNodeBase
std::atomic_bool halt_requested_;
};


#ifndef BT_NO_COROUTINES

/**
Expand Down
69 changes: 25 additions & 44 deletions include/behaviortree_cpp/bt_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ See examples for more information about configuring CMake correctly
/**
* @brief Struct used to store a tree.
* If this object goes out of scope, the tree is destroyed.
*
* To tick the tree, simply call:
*
* NodeStatus status = my_tree.tickRoot();
*/
class Tree
{
Expand Down Expand Up @@ -154,49 +150,22 @@ class Tree

TreeNode* rootNode() const;

enum TickOption
{
ONCE,
WHILE_RUNNING
};

NodeStatus tickRoot(TickOption opt = TickOption::WHILE_RUNNING)
{
NodeStatus status = NodeStatus::IDLE;

while (status == NodeStatus::IDLE ||
(opt == TickOption::WHILE_RUNNING && status == NodeStatus::RUNNING))
{
if (!wake_up_)
{
initialize();
}

if (!rootNode())
{
throw RuntimeError("Empty Tree");
}
status = rootNode()->executeTick();
if (status == NodeStatus::SUCCESS || status == NodeStatus::FAILURE)
{
rootNode()->resetStatus();
}
if (status == NodeStatus::RUNNING)
{
sleep(std::chrono::milliseconds(1));
}
}

return status;
}

/// Sleep for a certain amount of time.
/// This sleep could be interrupted by the method
/// TreeNode::emitWakeUpSignal()
void sleep(std::chrono::system_clock::duration timeout);

~Tree();

/// Tick the root of the tree once.
NodeStatus tickOnce();

/// Call tickOnce until the status is different from RUNNING.
/// Note that between one tick and the following one,
/// a Tree::sleep() is used
NodeStatus
tickWhileRunning(std::chrono::milliseconds sleep_time = std::chrono::milliseconds(10));

Blackboard::Ptr rootBlackboard();

//Call the visitor for each node of the tree.
Expand All @@ -207,6 +176,14 @@ class Tree

private:
std::shared_ptr<WakeUpSignal> wake_up_;

enum TickOption
{
ONCE,
WHILE_RUNNING
};

NodeStatus tickRoot(TickOption opt, std::chrono::milliseconds sleep_time);
};

class Parser;
Expand Down Expand Up @@ -394,10 +371,14 @@ class BehaviorTreeFactory
std::is_constructible<T, const std::string&, const NodeConfig&>::value;
constexpr bool has_static_ports_list = has_static_method_providedPorts<T>::value;

static_assert(default_constructable || param_constructable,
"[registerNode]: the registered class must have at "
"least one of these two constructors: (const std::string&, const "
"NodeConfig&) or (const std::string&).");
static_assert(default_constructable || param_constructable, "[registerNode]: the "
"registered class must "
"have at "
"least one of these two "
"constructors: (const "
"std::string&, const "
"NodeConfig&) or (const "
"std::string&).");

static_assert(!has_static_ports_list, "[registerNode]: ports are passed to this node "
"explicitly. The static method"
Expand Down
Loading

0 comments on commit 33f5628

Please sign in to comment.