Go to the documentation of this file.00001 #include "behaviortree_cpp/bt_factory.h"
00002
00003 #include "dummy_nodes.h"
00004 #include "movebase_node.h"
00005
00006 using namespace BT;
00007
00016
00017
00018 static const char* xml_text_sequence = R"(
00019
00020 <root main_tree_to_execute = "MainTree" >
00021
00022 <BehaviorTree ID="MainTree">
00023 <Sequence name="root">
00024 <BatteryOK/>
00025 <SaySomething message="mission started..." />
00026 <MoveBase goal="1;2;3"/>
00027 <SaySomething message="mission completed!" />
00028 </Sequence>
00029 </BehaviorTree>
00030
00031 </root>
00032 )";
00033
00034 static const char* xml_text_reactive = R"(
00035
00036 <root main_tree_to_execute = "MainTree" >
00037
00038 <BehaviorTree ID="MainTree">
00039 <ReactiveSequence name="root">
00040 <BatteryOK/>
00041 <Sequence>
00042 <SaySomething message="mission started..." />
00043 <MoveBase goal="1;2;3"/>
00044 <SaySomething message="mission completed!" />
00045 </Sequence>
00046 </ReactiveSequence>
00047 </BehaviorTree>
00048
00049 </root>
00050 )";
00051
00052
00053
00054 void Assert(bool condition)
00055 {
00056 if (!condition)
00057 throw RuntimeError("this is not what I expected");
00058 }
00059
00060 int main()
00061 {
00062 using namespace DummyNodes;
00063
00064 BehaviorTreeFactory factory;
00065 factory.registerSimpleCondition("BatteryOK", std::bind(CheckBattery));
00066 factory.registerNodeType<MoveBaseAction>("MoveBase");
00067 factory.registerNodeType<SaySomething>("SaySomething");
00068
00069
00070
00071
00072
00073
00074
00075
00076 for (auto& xml_text : {xml_text_sequence, xml_text_reactive})
00077 {
00078 std::cout << "\n------------ BUILDING A NEW TREE ------------" << std::endl;
00079
00080 auto tree = factory.createTreeFromText(xml_text);
00081
00082 NodeStatus status;
00083
00084 std::cout << "\n--- 1st executeTick() ---" << std::endl;
00085 status = tree.root_node->executeTick();
00086 Assert(status == NodeStatus::RUNNING);
00087
00088 SleepMS(150);
00089 std::cout << "\n--- 2nd executeTick() ---" << std::endl;
00090 status = tree.root_node->executeTick();
00091 Assert(status == NodeStatus::RUNNING);
00092
00093 SleepMS(150);
00094 std::cout << "\n--- 3rd executeTick() ---" << std::endl;
00095 status = tree.root_node->executeTick();
00096 Assert(status == NodeStatus::SUCCESS);
00097
00098 std::cout << std::endl;
00099 }
00100 return 0;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135