example.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <behavior_tree.h>
3 
5 {
6 public:
7  MyCondition(const std::string& name);
8  ~MyCondition();
9  BT::ReturnStatus Tick();
10 };
11 
13 {}
14 
15 BT::ReturnStatus MyCondition::Tick()
16 {
17  std::cout << "The Condition is true" << std::endl;
18 
19  return NodeStatus::SUCCESS;
20 }
21 
22 class MyAction : public BT::ActionNode
23 {
24 public:
25  MyAction(const std::string& name);
26  ~MyAction();
27  BT::ReturnStatus Tick();
28  void Halt();
29 };
30 
31 MyAction::MyAction(const std::string& name) : ActionNode::ActionNode(name)
32 {}
33 
34 BT::ReturnStatus MyAction::Tick()
35 {
36  std::cout << "The Action is doing some operations" << std::endl;
37  std::this_thread::sleep_for(std::chrono::milliseconds(500));
38  if (is_halted())
39  {
40  return NodeStatus::IDLE;
41  }
42 
43  std::cout << "The Action is doing some others operations" << std::endl;
44  std::this_thread::sleep_for(std::chrono::milliseconds(500));
45  if (is_halted())
46  {
47  return NodeStatus::IDLE;
48  }
49 
50  std::cout << "The Action is doing more operations" << std::endl;
51  std::this_thread::sleep_for(std::chrono::milliseconds(500));
52  if (is_halted())
53  {
54  return NodeStatus::IDLE;
55  }
56 
57  std::cout << "The Action has succeeded" << std::endl;
58  return NodeStatus::SUCCESS;
59 }
60 
62 {}
63 
64 int main(int argc, char* argv[])
65 {
66  BT::SequenceNode* seq = new BT::SequenceNode("Sequence");
67  MyCondition* my_con_1 = new MyCondition("Condition");
68  MyAction* my_act_1 = new MyAction("Action");
69  int tick_time_milliseconds = 1000;
70 
71  seq->AddChild(my_con_1);
72  seq->AddChild(my_act_1);
73 
74  Execute(seq, tick_time_milliseconds);
75 
76  return 0;
77 }
MyAction(const std::string &name)
Definition: example.cpp:31
BT::ReturnStatus Tick()
Definition: example.cpp:15
ConditionNode(const std::string &name, const NodeConfiguration &config)
MyCondition(const std::string &name)
Definition: example.cpp:12
void Halt()
Definition: example.cpp:61
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
int main(int argc, char *argv[])
Definition: example.cpp:64
BT::ReturnStatus Tick()
Definition: example.cpp:34
The SequenceNode is used to tick children in an ordered sequence. If any child returns RUNNING...
Definition: sequence_node.h:33


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14