t16_global_blackboard.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
27 // clang-format off
28 static const char* xml_main = R"(
29 <root BTCPP_format="4">
30 
31  <BehaviorTree ID="MainTree">
32  <Sequence>
33  <PrintNumber name="main_print" val="{@value}" />
34  <SubTree ID="MySub"/>
35  </Sequence>
36  </BehaviorTree>
37 
38  <BehaviorTree ID="MySub">
39  <Sequence>
40  <PrintNumber name="sub_print" val="{@value}" />
41  <Script code="@value_sqr := @value * @value" />
42  </Sequence>
43  </BehaviorTree>
44 </root>
45  )";
46 
47 // clang-format on
48 
50 {
51 public:
52  PrintNumber(const std::string& name, const BT::NodeConfig& config)
53  : BT::SyncActionNode(name, config)
54  {}
55 
56  NodeStatus tick() override
57  {
58  const int val = getInput<int>("val").value();
59  // If you prefer not having a port and accessing the top-level blackboard
60  // directly with an hardcoded address... you should question your own choices!
61  // But this is the way it is done
62  // val = config().blackboard-><int>("@value");
63  std::cout << "[" << name() << "] val: " << val << std::endl;
64  return NodeStatus::SUCCESS;
65  }
66 
68  {
69  return { BT::InputPort<int>("val") };
70  }
71 };
72 
73 //---------------------------------------------------
74 int main()
75 {
76  BehaviorTreeFactory factory;
77 
78  factory.registerNodeType<PrintNumber>("PrintNumber");
80 
81  // No one "own" this blackboard
82  auto global_blackboard = BT::Blackboard::create();
83  // This blackboard will be owned by "MainTree". Its parent is global_blackboard
84  auto root_blackboard = BT::Blackboard::create(global_blackboard);
85 
86  auto tree = factory.createTree("MainTree", root_blackboard);
87 
88  // we can interact directly with global_blackboard
89  for(int i = 1; i <= 3; i++)
90  {
91  global_blackboard->set("value", i);
92  tree.tickOnce();
93  int value_sqr = global_blackboard->get<int>("value_sqr");
94  std::cout << "[While loop] value: " << i << " value_sqr: " << value_sqr << "\n\n";
95  }
96 
97  return 0;
98 }
99 
100 /* Expecte output:
101 
102 [main_print] val: 1
103 [sub_print] val: 1
104 [While loop] value: 1 value_sqr: 1
105 
106 [main_print] val: 2
107 [sub_print] val: 2
108 [While loop] value: 2 value_sqr: 4
109 
110 [main_print] val: 3
111 [sub_print] val: 3
112 [While loop] value: 3 value_sqr: 9
113 
114 */
PrintNumber::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t16_global_blackboard.cpp:56
BT
Definition: ex01_wrap_legacy.cpp:29
main
int main()
Definition: t16_global_blackboard.cpp:74
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
PrintNumber::PrintNumber
PrintNumber(const std::string &name, const BT::NodeConfig &config)
Definition: t16_global_blackboard.cpp:52
BT::Tree::tickOnce
NodeStatus tickOnce()
by default, tickOnce() sends a single tick, BUT as long as there is at least one node of the tree inv...
Definition: bt_factory.cpp:604
bt_factory.h
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::Blackboard::create
static Blackboard::Ptr create(Blackboard::Ptr parent={})
Definition: blackboard.h:63
BT::NodeStatus::SUCCESS
@ SUCCESS
xml_main
static const char * xml_main
Definition: t16_global_blackboard.cpp:28
PrintNumber
Definition: t16_global_blackboard.cpp:49
BT::BehaviorTreeFactory::registerBehaviorTreeFromText
void registerBehaviorTreeFromText(const std::string &xml_text)
Definition: bt_factory.cpp:277
PrintNumber::providedPorts
static BT::PortsList providedPorts()
Definition: t16_global_blackboard.cpp:67
BT::NodeConfig
Definition: tree_node.h:73
BT::SyncActionNode
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


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