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 
12 MyCondition::MyCondition(const std::string& name) : BT::ConditionNode::ConditionNode(name)
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 }
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ConditionNode
Definition: condition_node.h:21
MyCondition::Tick
BT::ReturnStatus Tick()
Definition: example.cpp:15
MyAction::~MyAction
~MyAction()
MyCondition::MyCondition
MyCondition(const std::string &name)
Definition: example.cpp:12
MyCondition
Definition: example.cpp:4
MyAction
Definition: example.cpp:22
behavior_tree.h
main
int main(int argc, char *argv[])
Definition: example.cpp:64
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:296
MyAction::MyAction
MyAction(const std::string &name)
Definition: example.cpp:31
MyCondition::~MyCondition
~MyCondition()
BT::SequenceNode
The SequenceNode is used to tick children in an ordered sequence. If any child returns RUNNING,...
Definition: sequence_node.h:34
MyAction::Tick
BT::ReturnStatus Tick()
Definition: example.cpp:34
MyAction::Halt
void Halt()
Definition: example.cpp:61


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07