Go to the documentation of this file.00001 #include "behaviortree_cpp/bt_factory.h"
00002
00003 using namespace BT;
00004
00012 class MyAsyncAction: public CoroActionNode
00013 {
00014 public:
00015 MyAsyncAction(const std::string& name):
00016 CoroActionNode(name, {})
00017 {}
00018
00019 private:
00020
00021
00022
00023
00024
00025
00026
00027 NodeStatus tick() override
00028
00029 {
00030 std::cout << name() <<": Started. Send Request to server." << std::endl;
00031
00032 auto Now = [](){ return std::chrono::high_resolution_clock::now(); };
00033
00034 TimePoint initial_time = Now();
00035 TimePoint time_before_reply = initial_time + std::chrono::milliseconds(100);
00036
00037 int count = 0;
00038 bool reply_received = false;
00039
00040 while( !reply_received )
00041 {
00042 if( count++ == 0)
00043 {
00044
00045 std::cout << name() <<": Waiting Reply..." << std::endl;
00046 }
00047
00048 if( Now() >= time_before_reply )
00049 {
00050 reply_received = true;
00051 }
00052
00053 if( !reply_received )
00054 {
00055
00056
00057 setStatusRunningAndYield();
00058 }
00059 }
00060
00061
00062
00063 std::cout << name() <<": Done. 'Waiting Reply' loop repeated "
00064 << count << " times" << std::endl;
00065 cleanup(false);
00066 return NodeStatus::SUCCESS;
00067 }
00068
00069
00070 void cleanup(bool halted)
00071 {
00072 if( halted )
00073 {
00074 std::cout << name() <<": cleaning up after an halt()\n" << std::endl;
00075 }
00076 else{
00077 std::cout << name() <<": cleaning up after SUCCESS\n" << std::endl;
00078 }
00079 }
00080 void halt() override
00081 {
00082 std::cout << name() <<": Halted." << std::endl;
00083 cleanup(true);
00084
00085 CoroActionNode::halt();
00086 }
00087 };
00088
00089
00090
00091 static const char* xml_text = R"(
00092
00093 <root >
00094 <BehaviorTree>
00095 <Timeout msec="150">
00096 <SequenceStar name="sequence">
00097 <MyAsyncAction name="action_A"/>
00098 <MyAsyncAction name="action_B"/>
00099 </SequenceStar>
00100 </Timeout>
00101 </BehaviorTree>
00102 </root>
00103 )";
00104
00105
00106
00107 int main()
00108 {
00109
00110
00111
00112 BehaviorTreeFactory factory;
00113 factory.registerNodeType<MyAsyncAction>("MyAsyncAction");
00114
00115 auto tree = factory.createTreeFromText(xml_text);
00116
00117
00118
00119 while( tree.root_node->executeTick() == NodeStatus::RUNNING)
00120 {
00121 std::this_thread::sleep_for( std::chrono::milliseconds(10) );
00122 }
00123 return 0;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138