t04_reactive_sequence.cpp
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 // clang-format off
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 // clang-format on
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     // Compare the state transitions and messages using either
00070     // xml_text_sequence and xml_text_sequence_star
00071 
00072     // The main difference that you should notice is:
00073     //  1) When Sequence is used, BatteryOK is executed at __each__ tick()
00074     //  2) When SequenceStar is used, those ConditionNodes are executed only __once__.
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  Expected output:
00105 
00106 ------------ BUILDING A NEW TREE ------------
00107 
00108 --- 1st executeTick() ---
00109 [ Battery: OK ]
00110 Robot says: "mission started..."
00111 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
00112 
00113 --- 2nd executeTick() ---
00114 [ Battery: OK ]
00115 [ MoveBase: FINISHED ]
00116 
00117 --- 3rd executeTick() ---
00118 [ Battery: OK ]
00119 Robot says: "mission completed!"
00120 
00121 
00122 ------------ BUILDING A NEW TREE ------------
00123 
00124 --- 1st executeTick() ---
00125 [ Battery: OK ]
00126 Robot says: "mission started..."
00127 [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
00128 
00129 --- 2nd executeTick() ---
00130 [ MoveBase: FINISHED ]
00131 
00132 --- 3rd executeTick() ---
00133 Robot says: "mission completed!"
00134 
00135 */


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