broken_sequence.cpp
Go to the documentation of this file.
1 #include "Blackboard/blackboard_local.h"
4 
5 using namespace BT;
6 
8 {
9  printf("hello\n");
10  return NodeStatus::SUCCESS;
11 }
12 
13 class ActionTestNode : public ActionNode
14 {
15 public:
16  ActionTestNode(const std::string& name) : ActionNode(name)
17  {}
18 
19  NodeStatus tick() override
20  {
21  time_ = 5;
22  stop_loop_ = false;
23  int i = 0;
24  while (!stop_loop_ && i++ < time_)
25  {
26  std::this_thread::sleep_for(std::chrono::milliseconds(100));
27  }
28  return NodeStatus::SUCCESS;
29  }
30 
31  virtual void halt() override
32  {
33  stop_loop_ = true;
34  setStatus(NodeStatus::IDLE);
35  }
36 
37 private:
38  int time_;
39  std::atomic_bool stop_loop_;
40 };
41 
42 int main()
43 {
44  BT::SequenceNode root("root");
45  BT::SimpleActionNode action1("say_hello", std::bind(SayHello));
46  ActionTestNode action2("async_action");
47 
48  root.addChild(&action1);
49  root.addChild(&action2);
50 
51  int count = 0;
52 
54 
55  while (status == NodeStatus::RUNNING)
56  {
57  status = root.executeTick();
58 
59  std::cout << count++ << " : " << root.status() << " / " << action1.status() << " / "
60  << action2.status() << std::endl;
61 
62  std::this_thread::sleep_for(std::chrono::milliseconds(100));
63  }
64 
65  return 0;
66 }
67 // Output
68 /*
69 
70 hello
71 0 : RUNNING / SUCCESS / RUNNING
72 hello
73 1 : RUNNING / SUCCESS / RUNNING
74 hello
75 2 : RUNNING / SUCCESS / RUNNING
76 hello
77 3 : RUNNING / SUCCESS / RUNNING
78 hello
79 4 : RUNNING / SUCCESS / RUNNING
80 hello
81 5 : SUCCESS / IDLE / IDLE
82 
83 */
BT
Definition: ex01_wrap_legacy.cpp:29
ActionTestNode
Definition: broken_sequence.cpp:13
ActionTestNode::stop_loop_
std::atomic_bool stop_loop_
Definition: broken_sequence.cpp:39
minitrace::count
static volatile int count
Definition: minitrace.cpp:55
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:32
bt_factory.h
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:84
behavior_tree.h
main
int main()
Definition: broken_sequence.cpp:42
BT::NodeStatus::SUCCESS
@ SUCCESS
ActionTestNode::time_
int time_
Definition: broken_sequence.cpp:38
BT::NodeStatus::RUNNING
@ RUNNING
ActionTestNode::tick
NodeStatus tick() override
Definition: broken_sequence.cpp:19
BT::NodeStatus::IDLE
@ IDLE
BT::SimpleActionNode
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:78
BT::SequenceNode
The SequenceNode is used to tick children in an ordered sequence. If any child returns RUNNING,...
Definition: sequence_node.h:33
SayHello
NodeStatus SayHello()
Definition: broken_sequence.cpp:7
BT::ControlNode::addChild
void addChild(TreeNode *child)
The method used to add nodes to the children vector.
Definition: control_node.cpp:22
BT::NodeStatus
NodeStatus
Definition: basic_types.h:35
ActionTestNode::halt
virtual void halt() override
Definition: broken_sequence.cpp:31
ActionTestNode::ActionTestNode
ActionTestNode(const std::string &name)
Definition: broken_sequence.cpp:16


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19