t01_build_your_first_tree.cpp
Go to the documentation of this file.
00001 #include "behaviortree_cpp/bt_factory.h"
00002 
00003 //#define MANUAL_STATIC_LINKING
00004 
00005 #ifdef MANUAL_STATIC_LINKING
00006 #include "dummy_nodes.h"
00007 #endif
00008 
00009 using namespace BT;
00010 
00020 // clang-format off
00021 static const char* xml_text = R"(
00022 
00023  <root main_tree_to_execute = "MainTree" >
00024 
00025      <BehaviorTree ID="MainTree">
00026         <Sequence name="root_sequence">
00027             <ApproachObject name="approach"/>
00028             <CheckBattery   name="battery_ok"/>
00029             <OpenGripper    name="open_gripper"/>
00030             <ApproachObject name="approach_object"/>
00031             <CloseGripper   name="close_gripper"/>
00032         </Sequence>
00033      </BehaviorTree>
00034 
00035  </root>
00036  )";
00037 
00038 // clang-format on
00039 
00040 int main()
00041 {
00042     // We use the BehaviorTreeFactory to register our custom nodes
00043     BehaviorTreeFactory factory;
00044 
00045     /* There are two ways to register nodes:
00046     *    - statically, i.e. registering all the nodes one by one.
00047     *    - dynamically, loading the TreeNodes from a shared library (plugin).
00048     * */
00049 
00050 #ifdef MANUAL_STATIC_LINKING
00051     // Note: the name used to register should be the same used in the XML.
00052     // Note that the same operations could be done using DummyNodes::RegisterNodes(factory)
00053 
00054     using namespace DummyNodes;
00055 
00056     // The recommended way to create a Node is through inheritance.
00057     // Even if it requires more boilerplate, it allows you to use more functionalities
00058     // like ports (we will discuss this in future tutorials).
00059     factory.registerNodeType<ApproachObject>("ApproachObject");
00060 
00061     // Registering a SimpleActionNode using a function pointer.
00062     // you may also use C++11 lambdas instead of std::bind
00063     factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
00064 
00065     //You can also create SimpleActionNodes using methods of a class
00066     GripperInterface gripper;
00067     factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &gripper));
00068     factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &gripper));
00069 
00070 #else
00071     // Load dynamically a plugin and register the TreeNodes it contains
00072     // it automated the registering step.
00073     factory.registerFromPlugin("./libdummy_nodes_dyn.so");
00074 #endif
00075 
00076     // Trees are created at deployment-time (i.e. at run-time, but only once at the beginning).
00077     // The currently supported format is XML.
00078     // IMPORTANT: when the object "tree" goes out of scope, all the TreeNodes are destroyed
00079     auto tree = factory.createTreeFromText(xml_text);
00080 
00081     // To "execute" a Tree you need to "tick" it.
00082     // The tick is propagated to the children based on the logic of the tree.
00083     // In this case, the entire sequence is executed, because all the children
00084     // of the Sequence return SUCCESS.
00085     tree.root_node->executeTick();
00086 
00087     return 0;
00088 }
00089 
00090 /* Expected output:
00091 *
00092        [ Battery: OK ]
00093        GripperInterface::open
00094        ApproachObject: approach_object
00095        GripperInterface::close
00096 */


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 20:17:15