t08_additional_node_args.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
5 // To demonstrate how to pass arguments by reference, we
6 // use a simple non-copyable object
7 class NoCopyObj
8 {
9 public:
10  NoCopyObj(int val) : _value(val)
11  {}
12 
13  NoCopyObj(const NoCopyObj&) = delete;
14  NoCopyObj& operator=(const NoCopyObj&) = delete;
15  int value()
16  {
17  return _value;
18  }
19 
20 private:
21  int _value = 0;
22 };
23 
24 /*
25  * Sometimes, it is convenient to pass additional (static) arguments to a Node.
26  * If these parameters are known at compilation time or at deployment-time
27  * and they don't change at run-time, input ports are probably overkill and cumbersome.
28  *
29  * This tutorial demonstrates two possible ways to initialize a custom node with
30  * additional arguments.
31  */
32 
33 // Action_A has a different constructor than the default one.
34 class Action_A : public SyncActionNode
35 {
36 public:
37  // additional arguments passed to the constructor
38  Action_A(const std::string& name, const NodeConfig& config, int arg_int,
39  std::string arg_str, NoCopyObj& nc)
40  : SyncActionNode(name, config), _arg1(arg_int), _arg2(arg_str), _nc(nc)
41  {}
42 
43  NodeStatus tick() override
44  {
45  std::cout << name() << ": " << _arg1 << " / " << _arg2 << " / " << _nc.value()
46  << std::endl;
47  return NodeStatus::SUCCESS;
48  }
50  {
51  return {};
52  }
53 
54 private:
55  int _arg1;
56  std::string _arg2;
58 };
59 
60 // Action_B implements an init(...) method that must be called once at the beginning.
61 class Action_B : public SyncActionNode
62 {
63 public:
64  Action_B(const std::string& name, const NodeConfig& config)
65  : SyncActionNode(name, config)
66  {}
67 
68  // we want this method to be called ONCE and BEFORE the first tick()
69  void initialize(int arg_int, std::string arg_str)
70  {
71  _arg1 = (arg_int);
72  _arg2 = (arg_str);
73  }
74 
75  NodeStatus tick() override
76  {
77  std::cout << name() << ": " << _arg1 << " / " << _arg2 << std::endl;
78  return NodeStatus::SUCCESS;
79  }
81  {
82  return {};
83  }
84 
85 private:
86  int _arg1;
87  std::string _arg2;
88 };
89 
90 // Simple tree, used to execute once each action.
91 static const char* xml_text = R"(
92 
93  <root BTCPP_format="4">
94  <BehaviorTree>
95  <Sequence>
96  <Action_A/>
97  <Action_B/>
98  </Sequence>
99  </BehaviorTree>
100  </root>
101  )";
102 
103 int main()
104 {
105  BehaviorTreeFactory factory;
106 
107  NoCopyObj non_copyable(88);
108 
109  // Passing the extra parameters to the constructor of Action_A
110  // note that if you want to pass an object by ref, instead of value
111  // (copy), you must use std::ref wrapper.
112  factory.registerNodeType<Action_A>("Action_A", 42, "hello world",
113  std::ref(non_copyable));
114 
115  // Action_B will require initialization
116  factory.registerNodeType<Action_B>("Action_B");
117 
118  auto tree = factory.createTreeFromText(xml_text);
119 
120  auto visitor = [](TreeNode* node) {
121  if(auto action_B_node = dynamic_cast<Action_B*>(node))
122  {
123  action_B_node->initialize(69, "interesting_value");
124  }
125  };
126  // apply the visitor to all the nodes of the tree
127  tree.applyVisitor(visitor);
128 
129  tree.tickWhileRunning();
130 
131  /* Expected output:
132  Action_A: 42 / hello world / 88
133  Action_B: 69 / interesting_value
134  */
135  return 0;
136 }
BT
Definition: ex01_wrap_legacy.cpp:29
NoCopyObj::NoCopyObj
NoCopyObj(int val)
Definition: t08_additional_node_args.cpp:10
Action_A::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t08_additional_node_args.cpp:43
xml_text
static const char * xml_text
Definition: t08_additional_node_args.cpp:91
Action_B
Definition: t08_additional_node_args.cpp:61
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
NoCopyObj::value
int value()
Definition: t08_additional_node_args.cpp:15
bt_factory.h
Action_A::Action_A
Action_A(const std::string &name, const NodeConfig &config, int arg_int, std::string arg_str, NoCopyObj &nc)
Definition: t08_additional_node_args.cpp:38
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
Action_B::providedPorts
static PortsList providedPorts()
Definition: t08_additional_node_args.cpp:80
Action_A
Definition: t08_additional_node_args.cpp:34
Action_A::_arg2
std::string _arg2
Definition: t08_additional_node_args.cpp:56
NoCopyObj
Definition: t08_additional_node_args.cpp:7
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
Action_B::_arg2
std::string _arg2
Definition: t08_additional_node_args.cpp:87
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
BT::Tree::initialize
void initialize()
Definition: bt_factory.cpp:550
Action_B::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t08_additional_node_args.cpp:75
Action_A::providedPorts
static PortsList providedPorts()
Definition: t08_additional_node_args.cpp:49
Action_A::_arg1
int _arg1
Definition: t08_additional_node_args.cpp:55
main
int main()
Definition: t08_additional_node_args.cpp:103
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::NodeStatus::SUCCESS
@ SUCCESS
Action_A::_nc
NoCopyObj & _nc
Definition: t08_additional_node_args.cpp:57
Action_B::Action_B
Action_B(const std::string &name, const NodeConfig &config)
Definition: t08_additional_node_args.cpp:64
Action_B::initialize
void initialize(int arg_int, std::string arg_str)
Definition: t08_additional_node_args.cpp:69
Action_B::_arg1
int _arg1
Definition: t08_additional_node_args.cpp:86
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