t08_additional_node_args.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
5 /*
6  * Sometimes, it is convenient to pass additional (static) arguments to a Node.
7  * If these parameters are known at compilation time or at deployment-time
8  * and they don't change at run-time, input ports are probably overkill and cumbersome.
9  *
10  * This tutorial demonstrates two possible ways to initialize a custom node with
11  * additional arguments.
12  */
13 
14 // Action_A has a different constructor than the default one.
15 class Action_A: public SyncActionNode
16 {
17 
18 public:
19  // additional arguments passed to the constructor
20  Action_A(const std::string& name, const NodeConfiguration& config,
21  int arg1, double arg2, std::string arg3 ):
22  SyncActionNode(name, config),
23  _arg1(arg1),
24  _arg2(arg2),
25  _arg3(arg3) {}
26 
27  NodeStatus tick() override
28  {
29  std::cout << "Action_A: " << _arg1 << " / " << _arg2 << " / " << _arg3 << std::endl;
30  return NodeStatus::SUCCESS;
31  }
32  static PortsList providedPorts() { return {}; }
33 
34 private:
35  int _arg1;
36  double _arg2;
37  std::string _arg3;
38 };
39 
40 // Action_B implements an init(...) method that must be called once at the beginning.
41 class Action_B: public SyncActionNode
42 {
43 
44 public:
45  Action_B(const std::string& name, const NodeConfiguration& config):
46  SyncActionNode(name, config) {}
47 
48  // we want this method to be called ONCE and BEFORE the first tick()
49  void init( int arg1, double arg2, std::string arg3 )
50  {
51  _arg1 = (arg1);
52  _arg2 = (arg2);
53  _arg3 = (arg3);
54  }
55 
56  NodeStatus tick() override
57  {
58  std::cout << "Action_B: " << _arg1 << " / " << _arg2 << " / " << _arg3 << std::endl;
59  return NodeStatus::SUCCESS;
60  }
61  static PortsList providedPorts() { return {}; }
62 
63 private:
64  int _arg1;
65  double _arg2;
66  std::string _arg3;
67 };
68 
69 // Simple tree, used to execute once each action.
70 static const char* xml_text = R"(
71 
72  <root >
73  <BehaviorTree>
74  <Sequence>
75  <Action_A/>
76  <Action_B/>
77  </Sequence>
78  </BehaviorTree>
79  </root>
80  )";
81 
82 int main()
83 {
84  BehaviorTreeFactory factory;
85 
86  // A node builder is nothing more than a function pointer to create a
87  // std::unique_ptr<TreeNode>.
88  // Using lambdas or std::bind, we can easily "inject" additional arguments.
89  NodeBuilder builder_A = [](const std::string& name, const NodeConfiguration& config)
90  {
91  return std::make_unique<Action_A>( name, config, 42, 3.14, "hello world" );
92  };
93 
94  // BehaviorTreeFactory::registerBuilder is the more general way to register a custom node.
95  // Not the most user friendly, but definitely the most flexible one.
96  factory.registerBuilder<Action_A>( "Action_A", builder_A);
97 
98  // The regitration of Action_B is done as usual, but we still need to call Action_B::init()
99  factory.registerNodeType<Action_B>( "Action_B" );
100 
101  auto tree = factory.createTreeFromText(xml_text);
102 
103  // Iterate through all the nodes and call init if it is an Action_B
104  for( auto& node: tree.nodes )
105  {
106  if( auto action_B_node = dynamic_cast<Action_B*>( node.get() ))
107  {
108  action_B_node->init( 69, 9.99, "interesting_value" );
109  }
110  }
111 
112  tree.root_node->executeTick();
113 
114  /* Expected output:
115 
116  Action_A: 42 / 3.14 / hello world
117  Action_B: 69 / 9.99 / interesting_value
118  */
119  return 0;
120 }
NodeStatus tick() override
Method to be implemented by the user.
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:174
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeConfiguration &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
Definition: bt_factory.h:33
Action_A(const std::string &name, const NodeConfiguration &config, int arg1, double arg2, std::string arg3)
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:52
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:98
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:170
static PortsList providedPorts()
static const char * xml_text
static PortsList providedPorts()
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:316
void init(int arg1, double arg2, std::string arg3)
virtual NodeStatus executeTick() override
throws if the derived class return RUNNING.
NodeStatus tick() override
Method to be implemented by the user.
NodeStatus
Definition: basic_types.h:35
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
The most generic way to register your own builder.
Definition: bt_factory.cpp:68
Action_B(const std::string &name, const NodeConfiguration &config)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05