t09_async_actions_coroutines.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
13 {
14  public:
15  MyAsyncAction(const std::string& name):
16  CoroActionNode(name, {})
17  {}
18 
19  private:
20  // This is the ideal skeleton/template of an async action:
21  // - A request to a remote service provider.
22  // - A loop where we check if the reply has been received.
23  // - You may call setStatusRunningAndYield() to "pause".
24  // - Code to execute after the reply.
25  // - A simple way to handle halt().
26 
27  NodeStatus tick() override
28 
29  {
30  std::cout << name() <<": Started. Send Request to server." << std::endl;
31 
32  auto Now = [](){ return std::chrono::high_resolution_clock::now(); };
33 
34  TimePoint initial_time = Now();
35  TimePoint time_before_reply = initial_time + std::chrono::milliseconds(100);
36 
37  int count = 0;
38  bool reply_received = false;
39 
40  while( !reply_received )
41  {
42  if( count++ == 0)
43  {
44  // call this only once
45  std::cout << name() <<": Waiting Reply..." << std::endl;
46  }
47  // pretend that we received a reply
48  if( Now() >= time_before_reply )
49  {
50  reply_received = true;
51  }
52 
53  if( !reply_received )
54  {
55  // set status to RUNNING and "pause/sleep"
56  // If halt() is called, we will not resume execution (stack destroyed)
57  setStatusRunningAndYield();
58  }
59  }
60 
61  // This part of the code is never reached if halt() is invoked,
62  // only if reply_received == true;
63  std::cout << name() <<": Done. 'Waiting Reply' loop repeated "
64  << count << " times" << std::endl;
65  cleanup(false);
66  return NodeStatus::SUCCESS;
67  }
68 
69  // you might want to cleanup differently if it was halted or successful
70  void cleanup(bool halted)
71  {
72  if( halted )
73  {
74  std::cout << name() <<": cleaning up after an halt()\n" << std::endl;
75  }
76  else{
77  std::cout << name() <<": cleaning up after SUCCESS\n" << std::endl;
78  }
79  }
80  void halt() override
81  {
82  std::cout << name() <<": Halted." << std::endl;
83  cleanup(true);
84  // Do not forget to call this at the end.
86  }
87 };
88 
89 
90 // clang-format off
91 static const char* xml_text = R"(
92 
93  <root >
94  <BehaviorTree>
95  <Timeout msec="150">
96  <SequenceStar name="sequence">
97  <MyAsyncAction name="action_A"/>
98  <MyAsyncAction name="action_B"/>
99  </SequenceStar>
100  </Timeout>
101  </BehaviorTree>
102  </root>
103  )";
104 
105 // clang-format on
106 
107 int main()
108 {
109  // Simple tree: a sequence of two asycnhronous actions,
110  // but the second will be halted because of the timeout.
111 
112  BehaviorTreeFactory factory;
113  factory.registerNodeType<MyAsyncAction>("MyAsyncAction");
114 
115  auto tree = factory.createTreeFromText(xml_text);
116 
117  //---------------------------------------
118  // keep executin tick until it returns etiher SUCCESS or FAILURE
119  while( tree.tickRoot() == NodeStatus::RUNNING)
120  {
121  std::this_thread::sleep_for( std::chrono::milliseconds(10) );
122  }
123  return 0;
124 }
125 
126 /* Expected output:
127 
128 action_A: Started. Send Request to server.
129 action_A: Waiting Reply...
130 action_A: Done. 'Waiting Reply' loop repeated 11 times
131 action_A: cleaning up after SUCCESS
132 
133 action_B: Started. Send Request to server.
134 action_B: Waiting Reply...
135 action_B: Halted.
136 action_B: cleaning up after an halt()
137 
138 */
std::chrono::high_resolution_clock::time_point TimePoint
Definition: basic_types.h:339
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:290
void halt() override
The CoroActionNode class is an ideal candidate for asynchronous actions which need to communicate wit...
Definition: action_node.h:190
NodeStatus tick() override
Method to be implemented by the user.
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
static const char * xml_text
static volatile int count
Definition: minitrace.cpp:55
MyAsyncAction(const std::string &name)
NodeStatus
Definition: basic_types.h:35


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25