t03_sequence_star.cpp
Go to the documentation of this file.
00001 #include "behaviortree_cpp/xml_parsing.h"
00002 
00003 #include "dummy_nodes.h"
00004 #include "movebase_node.h"
00005 
00006 using namespace BT;
00007 
00016 // clang-format off
00017 
00018 const std::string xml_text_sequence = R"(
00019 
00020  <root main_tree_to_execute = "MainTree" >
00021 
00022      <BehaviorTree ID="MainTree">
00023         <Sequence name="root">
00024             <BatteryOK/>
00025             <TemperatureOK />
00026             <SaySomething   message="mission started..." />
00027             <MoveBase goal="1;2;3"/>
00028             <SaySomething   message="mission completed!" />
00029         </Sequence>
00030      </BehaviorTree>
00031 
00032  </root>
00033  )";
00034 
00035 const std::string xml_text_sequence_star = R"(
00036 
00037  <root main_tree_to_execute = "MainTree" >
00038 
00039      <BehaviorTree ID="MainTree">
00040         <SequenceStar name="root">
00041             <BatteryOK/>
00042             <TemperatureOK />
00043             <SaySomething   message="mission started..." />
00044             <MoveBase goal="1;2;3"/>
00045             <SaySomething   message="mission completed!" />
00046         </SequenceStar>
00047      </BehaviorTree>
00048 
00049  </root>
00050  )";
00051 
00052 // clang-format on
00053 
00054 void Assert(bool condition)
00055 {
00056     if (!condition)
00057         throw std::runtime_error("this is not what I expected");
00058 }
00059 
00060 int main()
00061 {
00062     using namespace DummyNodes;
00063 
00064     BehaviorTreeFactory factory;
00065     factory.registerSimpleCondition("TemperatureOK", std::bind(CheckBattery));
00066     factory.registerSimpleCondition("BatteryOK", std::bind(CheckTemperature));
00067     factory.registerNodeType<MoveBaseAction>("MoveBase");
00068     factory.registerNodeType<SaySomething>("SaySomething");
00069 
00070     // Compare the state transitions and messages using either
00071     // xml_text_sequence and xml_text_sequence_star
00072 
00073     // The main difference that you should notice is:
00074     //  1) When Sequence is used, BatteryOK and TempearaturOK is executed at each tick()
00075     //  2) When SequenceStar is used, those ConditionNodes are executed only once.
00076 
00077     for (auto& xml_text : {xml_text_sequence, xml_text_sequence_star})
00078     {
00079         std::cout << "\n------------ BUILDING A NEW TREE ------------" << std::endl;
00080 
00081         auto tree = buildTreeFromText(factory, xml_text);
00082 
00083         NodeStatus status;
00084 
00085         std::cout << "\n--- 1st executeTick() ---" << std::endl;
00086         status = tree.root_node->executeTick();
00087         Assert(status == NodeStatus::RUNNING);
00088 
00089         SleepMS(150);
00090         std::cout << "\n--- 2nd executeTick() ---" << std::endl;
00091         status = tree.root_node->executeTick();
00092         Assert(status == NodeStatus::RUNNING);
00093 
00094         SleepMS(150);
00095         std::cout << "\n--- 3rd executeTick() ---" << std::endl;
00096         status = tree.root_node->executeTick();
00097         Assert(status == NodeStatus::SUCCESS);
00098 
00099         std::cout << std::endl;
00100     }
00101     return 0;
00102 }
00103 
00104 /*
00105  Expected output:
00106 
00107 ------------ BUILDING A NEW TREE ------------
00108 
00109 --- 1st executeTick() ---
00110 [ Temperature: OK ]
00111 [ Battery: OK ]
00112 Robot says: "mission started..."
00113 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
00114 
00115 --- 2nd executeTick() ---
00116 [ Temperature: OK ]
00117 [ Battery: OK ]
00118 [ MoveBase: FINISHED ]
00119 
00120 --- 3rd executeTick() ---
00121 [ Temperature: OK ]
00122 [ Battery: OK ]
00123 Robot says: "mission completed!"
00124 
00125 
00126 ------------ BUILDING A NEW TREE ------------
00127 
00128 --- 1st executeTick() ---
00129 [ Temperature: OK ]
00130 [ Battery: OK ]
00131 Robot says: "mission started..."
00132 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
00133 
00134 --- 2nd executeTick() ---
00135 [ MoveBase: FINISHED ]
00136 
00137 --- 3rd executeTick() ---
00138 Robot says: "mission completed!"
00139 
00140 */


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