t04_reactive_sequence.cpp
Go to the documentation of this file.
2 
3 #include "dummy_nodes.h"
4 #include "movebase_node.h"
5 
6 using namespace BT;
7 
15 // clang-format off
16 
17 static const char* xml_text_sequence = R"(
18 
19  <root BTCPP_format="4" >
20 
21  <BehaviorTree ID="MainTree">
22  <Sequence name="root">
23  <BatteryOK/>
24  <SaySomething message="mission started..." />
25  <MoveBase goal="1;2;3"/>
26  <SaySomething message="mission completed!" />
27  </Sequence>
28  </BehaviorTree>
29 
30  </root>
31  )";
32 
33 static const char* xml_text_reactive = R"(
34 
35  <root BTCPP_format="4" >
36 
37  <BehaviorTree ID="MainTree">
38  <ReactiveSequence name="root">
39  <BatteryOK/>
40  <Sequence>
41  <SaySomething message="mission started..." />
42  <MoveBase goal="1;2;3"/>
43  <SaySomething message="mission completed!" />
44  </Sequence>
45  </ReactiveSequence>
46  </BehaviorTree>
47 
48  </root>
49  )";
50 
51 // clang-format on
52 
53 using namespace DummyNodes;
54 
55 int main()
56 {
57  BehaviorTreeFactory factory;
58 
59  factory.registerSimpleCondition("BatteryOK", std::bind(CheckBattery));
60  factory.registerNodeType<MoveBaseAction>("MoveBase");
61  factory.registerNodeType<SaySomething>("SaySomething");
62 
63  // Compare the state transitions and messages using either
64  // xml_text_sequence and xml_text_reactive.
65 
66  // The main difference that you should notice is:
67  // 1) When Sequence is used, the ConditionNode is executed only __once__ because it returns SUCCESS.
68  // 2) When ReactiveSequence is used, BatteryOK is executed at __each__ tick()
69 
71  {
72  std::cout << "\n------------ BUILDING A NEW TREE ------------\n\n";
73 
74  auto tree = factory.createTreeFromText(xml_text);
75 
77 #if 0
78  // Tick the root until we receive either SUCCESS or RUNNING
79  // same as: tree.tickRoot(Tree::WHILE_RUNNING)
80  std::cout << "--- ticking\n";
81  status = tree.tickWhileRunning();
82  std::cout << "--- status: " << toStr(status) << "\n\n";
83 #else
84  // If we need to run code between one tick() and the next,
85  // we can implement our own while loop
86  while(status != NodeStatus::SUCCESS)
87  {
88  std::cout << "--- ticking\n";
89  status = tree.tickOnce();
90  std::cout << "--- status: " << toStr(status) << "\n\n";
91 
92  // if still running, add some wait time
93  if(status == NodeStatus::RUNNING)
94  {
95  tree.sleep(std::chrono::milliseconds(100));
96  }
97  }
98 #endif
99  }
100  return 0;
101 }
BT
Definition: ex01_wrap_legacy.cpp:29
xml_text_reactive
static const char * xml_text_reactive
Definition: t04_reactive_sequence.cpp:33
DummyNodes::CheckBattery
BT::NodeStatus CheckBattery()
Definition: dummy_nodes.cpp:13
bt_factory.h
DummyNodes
Definition: dummy_nodes.cpp:10
MoveBaseAction
Definition: movebase_node.h:54
dummy_nodes.h
movebase_node.h
lexy::bind
constexpr auto bind(Callback &&callback, BoundArgs &&... args)
Binds the operator() of the callback with pre-defined/remapped values.
Definition: bind.hpp:321
main
int main()
Definition: t04_reactive_sequence.cpp:55
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
DummyNodes::SaySomething
Definition: dummy_nodes.h:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeStatus::RUNNING
@ RUNNING
BT::NodeStatus::IDLE
@ IDLE
xml_text_sequence
static const char * xml_text_sequence
Definition: t04_reactive_sequence.cpp:17
BT::BehaviorTreeFactory::registerSimpleCondition
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleCondition help you register nodes of type SimpleConditionNode.
Definition: bt_factory.cpp:153
xml_text
static const char * xml_text
Definition: ex01_wrap_legacy.cpp:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::toStr
std::string toStr(const T &value)
toStr is the reverse operation of convertFromString.
Definition: basic_types.h:252


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17