t08_async_actions_coroutines.cpp
Go to the documentation of this file.
3 
4 using namespace BT;
5 
13 {
14  public:
15  MyAsyncAction(const std::string& name):
17  {}
18 
19  // This is the ideal skeleton/template of an async action:
20  // - A request to a remote service provider.
21  // - A loop where we check if the reply has been received.
22  // Call setStatusRunningAndYield() to "pause".
23  // - Code to execute after the reply.
24  // - a simple way to handle halt().
25 
26  NodeStatus tick() override
27  {
28  std::cout << name() <<": Started. Send Request to server." << std::endl;
29 
30  int cycle = 0;
31  bool reply_received = false;
32 
33  while( !reply_received )
34  {
35  std::cout << name() <<": Waiting reply." << std::endl;
36  reply_received = ++cycle >= 3;
37 
38  if( !reply_received )
39  {
40  // set status to RUNNING and "pause/sleep"
41  // If halt() is called, we will not resume execution
42  setStatusRunningAndYield();
43  }
44  }
45 
46  // this part of the code is never reached if halt() is invoked.
47  std::cout << name() <<": Done." << std::endl;
48  return NodeStatus::SUCCESS;
49  }
50 
51  void halt() override
52  {
53  std::cout << name() <<": Halted. Do your cleanup here." << std::endl;
54 
55  // Do not forget to call this at the end.
57  }
58 };
59 
60 
61 int main()
62 {
63  // Simple tree: a sequence of two asycnhronous actions
64  BT::SequenceNode sequence_root("sequence");
65  MyAsyncAction action_A("actionA");
66  MyAsyncAction action_B("actionB");
67 
68  // Add children to the sequence.
69  sequence_root.addChild(&action_A);
70  sequence_root.addChild(&action_B);
71 
73 
74  while( status != NodeStatus::SUCCESS && status != NodeStatus::FAILURE)
75  {
76  status = sequence_root.executeTick();
77 
78  // It is often a good idea to add a sleep here to avoid busy loops
79  std::this_thread::sleep_for( std::chrono::milliseconds(1) );
80  }
81 
82  return 0;
83 }
84 
85 /* Expected output:
86 
87 actionA: Started. Request service using async call
88 actionA: Waiting reply
89 actionA: Waiting reply
90 actionA: Waiting reply
91 actionA: Done
92 actionB: Started. Request service using async call
93 actionB: Waiting reply
94 actionB: Waiting reply
95 actionB: Waiting reply
96 actionB: Done
97 */
void halt() override
The method used to interrupt the execution of a RUNNING node.
void halt() override
The CoroActionNode class is an ideal candidate for asynchronous actions which need to communicate wit...
Definition: action_node.h:158
NodeStatus tick() override
Method to be implemented by the user.
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
void addChild(TreeNode *child)
MyAsyncAction(const std::string &name)
NodeStatus
Definition: basic_types.h:28
virtual BT::NodeStatus executeTick()
The method that will be executed to invoke tick(); and setStatus();.
Definition: tree_node.cpp:35
The SequenceNode is used to execute a sequence of children. If any child returns RUNNING, previous children will be ticked again.
Definition: sequence_node.h:34


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53