| 37 | // clang-format on |
| 38 | |
| 39 | int main() |
| 40 | { |
| 41 | // We use the BehaviorTreeFactory to register our custom nodes |
| 42 | BehaviorTreeFactory factory; |
| 43 | |
| 44 | /* There are two ways to register nodes: |
| 45 | * - statically, i.e. registering all the nodes one by one. |
| 46 | * - dynamically, loading the TreeNodes from a shared library (plugin). |
| 47 | * */ |
| 48 | |
| 49 | #ifdef MANUAL_STATIC_LINKING |
| 50 | // Note: the name used to register should be the same used in the XML. |
| 51 | // Note that the same operations could be done using DummyNodes::RegisterNodes(factory) |
| 52 | |
| 53 | using namespace DummyNodes; |
| 54 | |
| 55 | // The recommended way to create a Node is through inheritance. |
| 56 | // Even if it requires more boilerplate, it allows you to use more functionalities |
| 57 | // like ports (we will discuss this in future tutorials). |
| 58 | factory.registerNodeType<ApproachObject>("ApproachObject"); |
| 59 | |
| 60 | // Registering a SimpleActionNode using a function pointer. |
| 61 | // you may also use C++11 lambdas instead of std::bind |
| 62 | factory.registerSimpleCondition("CheckBattery", |
| 63 | [&](TreeNode&) { return CheckBattery(); }); |
| 64 | |
| 65 | //You can also create SimpleActionNodes using methods of a class |
| 66 | GripperInterface gripper; |
| 67 | factory.registerSimpleAction("OpenGripper", [&](TreeNode&) { return gripper.open(); }); |
| 68 | factory.registerSimpleAction("CloseGripper", |
| 69 | [&](TreeNode&) { return gripper.close(); }); |
| 70 | |
| 71 | #else |
| 72 | // Load dynamically a plugin and register the TreeNodes it contains |
| 73 | // it automated the registering step. |
| 74 | factory.registerFromPlugin("../sample_nodes/bin/libdummy_nodes_dyn.so"); |
| 75 | #endif |
| 76 | |
| 77 | // Trees are created at deployment-time (i.e. at run-time, but only once at the beginning). |
| 78 | // The currently supported format is XML. |
| 79 | // IMPORTANT: when the object "tree" goes out of scope, all the TreeNodes are destroyed |
| 80 | auto tree = factory.createTreeFromText(xml_text); |
| 81 | |
| 82 | // To "execute" a Tree you need to "tick" it. |
| 83 | // The tick is propagated to the children based on the logic of the tree. |
| 84 | // In this case, the entire sequence is executed, because all the children |
| 85 | // of the Sequence return SUCCESS. |
| 86 | tree.tickWhileRunning(); |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* Expected output: |
| 92 | * |
nothing calls this directly
no test coverage detected