// Simple function that return a NodeStatus 无参数的简单形式,也需要返回NodeStatus BT::NodeStatus CheckBattery() { std::cout << "[ Battery: OK ]" << std::endl; return BT::NodeStatus::SUCCESS; }
// We want to wrap into an ActionNode the methods open() and close() // 也可以将方法open(), close()封装到一个动作节点中,节点是一个名为GripperInterface的类 class GripperInterface { public: GripperInterface(): _open(true) {}
// file that contains the custom nodes definitions #include "dummy_nodes.h"
int main() { // We use the BehaviorTreeFactory to register our custom nodes BehaviorTreeFactory由 BehaviorTreeFactory factory;
// Note: the name used to register should be the same used in the XML. using namespace DummyNodes;
// The recommended way to create a Node is through inheritance. factory.registerNodeType<ApproachObject>("ApproachObject");
// Registering a SimpleActionNode using a function pointer. // you may also use C++11 lambdas instead of std::bind factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
//You can also create SimpleActionNodes using methods of a class GripperInterface gripper; factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &gripper)); factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &gripper));
// Trees are created at deployment-time (i.e. at run-time, but only // once at the beginning).
// IMPORTANT: when the object "tree" goes out of scope, all the // TreeNodes are destroyed auto tree = factory.createTreeFromFile("./my_tree.xml");
// To "execute" a Tree you need to "tick" it. // 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();