Repository navigation
Creating Asynchronous Action without inheritance #952
Replies: 1 comment
|
The factory has no async version of You can still write the class only once. Make a small adapter around #include <behaviortree_cpp/bt_factory.h>
using namespace BT;
class LambdaStatefulAction : public StatefulActionNode
{
public:
using TickFn = std::function<NodeStatus(TreeNode&)>;
using HaltFn = std::function<void(TreeNode&)>;
LambdaStatefulAction(const std::string& name, const NodeConfig& cfg,
TickFn on_start, TickFn on_running, HaltFn on_halted)
: StatefulActionNode(name, cfg), on_start_(std::move(on_start)),
on_running_(std::move(on_running)), on_halted_(std::move(on_halted)) {}
NodeStatus onStart() override { return on_start_(*this); }
NodeStatus onRunning() override { return on_running_(*this); }
void onHalted() override { if(on_halted_) on_halted_(*this); }
private:
TickFn on_start_, on_running_;
HaltFn on_halted_;
};Registration, without a new class per action: using Clock = std::chrono::steady_clock;
TreeNodeManifest manifest{ NodeType::ACTION, "Wait", { InputPort<int>("msec") }, {} };
factory.registerBuilder(manifest, [](const std::string& name, const NodeConfig& cfg) {
auto deadline = std::make_shared<Clock::time_point>(); // one per node instance
return std::make_unique<LambdaStatefulAction>(
name, cfg,
[deadline](TreeNode& n) { // onStart
*deadline = Clock::now() + std::chrono::milliseconds(n.getInput<int>("msec").value());
return NodeStatus::RUNNING;
},
[deadline](TreeNode&) { // onRunning
return Clock::now() >= *deadline ? NodeStatus::SUCCESS : NodeStatus::RUNNING;
},
[](TreeNode&) { /* cancel the request */ }); // onHalted
});Create any per-node state inside the builder lambda, as shown. If you capture it at registration time, all I tested this against 4.11.1. Two |
Uh oh!
There was an error while loading. Please reload this page.
Is there any method to create asynchronous action using a method of "factory", similar to factory.registerSimpleAction(), which makes synchronous actions?
I want to make async actions without making classes inheriting from "ActionNode".
All reactions