t02_factory_tree.cpp
Go to the documentation of this file.
00001 #include "behaviortree_cpp/xml_parsing.h"
00002 #include "behaviortree_cpp/blackboard/blackboard_local.h"
00003 
00004 //#define MANUAL_STATIC_LINKING
00005 
00006 #ifdef MANUAL_STATIC_LINKING
00007 #include "dummy_nodes.h"
00008 #endif
00009 
00010 using namespace BT;
00011 
00012 // clang-format off
00013 const std::string xml_text = R"(
00014 
00015  <root main_tree_to_execute = "MainTree" >
00016 
00017      <BehaviorTree ID="MainTree">
00018         <Sequence name="root_sequence">
00019             <SayHello       name="action_hello"/>
00020             <OpenGripper    name="open_gripper"/>
00021             <ApproachObject name="approach_object"/>
00022             <CloseGripper   name="close_gripper"/>
00023         </Sequence>
00024      </BehaviorTree>
00025 
00026  </root>
00027  )";
00028 
00029 // clang-format on
00030 
00031 int main()
00032 {
00033     /* In this example we build a tree at run-time.
00034      * The tree is defined using an XML (see xml_text).
00035      * To achieve this we must first register our TreeNodes into
00036      * a BehaviorTreeFactory.
00037      */
00038     BehaviorTreeFactory factory;
00039 
00040     /* There are two ways to register nodes:
00041     *    - statically, including directly DummyNodes.
00042     *    - dynamically, loading the TreeNodes from a shared library (plugin).
00043     * */
00044 
00045 #ifdef MANUAL_STATIC_LINKING
00046     // Note: the name used to register should be the same used in the XML.
00047     // Note that the same operations could be done using DummyNodes::RegisterNodes(factory)
00048 
00049     using namespace DummyNodes;
00050 
00051     factory.registerSimpleAction("SayHello", std::bind(SayHello));
00052     factory.registerSimpleCondition("CheckBattery", std::bind(CheckBattery));
00053     factory.registerSimpleCondition("CheckTemperature", std::bind(CheckTemperature));
00054 
00055     GripperInterface gripper;
00056     factory.registerSimpleAction("OpenGripper", std::bind(&GripperInterface::open, &gripper));
00057     factory.registerSimpleAction("CloseGripper", std::bind(&GripperInterface::close, &gripper));
00058 
00059     factory.registerNodeType<ApproachObject>("ApproachObject");
00060 
00061 #else
00062     // Load dynamically a plugin and register the TreeNodes it contains
00063     factory.registerFromPlugin("./libdummy_nodes.so");
00064 #endif
00065 
00066     // IMPORTANT: when the object tree goes out of scope, all the TreeNodes are destroyed
00067     auto tree = buildTreeFromText(factory, xml_text);
00068 
00069     // The tick is propagated to all the children.
00070     // until one of the returns FAILURE or RUNNING.
00071     // In this case all of the return SUCCESS
00072     tree.root_node->executeTick();
00073 
00074     return 0;
00075 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10