t03_generic_ports.cpp
Go to the documentation of this file.
2 
3 using namespace BT;
4 
5 /* This tutorial will teach you how to deal with ports when its
6  * type is not std::string.
7 */
8 
9 // We want to be able to use this custom type
10 struct Position2D
11 {
12  double x, y;
13 };
14 
15 // It is recommended (or, in some cases, mandatory) to define a template
16 // specialization of convertFromString that converts a string to Position2D.
17 namespace BT
18 {
19 template <>
21 {
22  printf("Converting string: \"%s\"\n", str.data());
23 
24  // real numbers separated by semicolons
25  auto parts = splitString(str, ';');
26  if (parts.size() != 2)
27  {
28  throw RuntimeError("invalid input)");
29  }
30  else
31  {
32  Position2D output;
33  output.x = convertFromString<double>(parts[0]);
34  output.y = convertFromString<double>(parts[1]);
35  return output;
36  }
37 }
38 } // end namespace BT
39 
41 {
42 public:
43  CalculateGoal(const std::string& name, const NodeConfiguration& config) :
44  SyncActionNode(name, config)
45  {}
46 
47  NodeStatus tick() override
48  {
49  Position2D mygoal = {1.1, 2.3};
50  setOutput("goal", mygoal);
51  return NodeStatus::SUCCESS;
52  }
54  {
55  return {OutputPort<Position2D>("goal")};
56  }
57 };
58 
60 {
61 public:
62  PrintTarget(const std::string& name, const NodeConfiguration& config) :
63  SyncActionNode(name, config)
64  {}
65 
66  NodeStatus tick() override
67  {
68  auto res = getInput<Position2D>("target");
69  if (!res)
70  {
71  throw RuntimeError("error reading port [target]:", res.error());
72  }
73  Position2D goal = res.value();
74  printf("Target positions: [ %.1f, %.1f ]\n", goal.x, goal.y);
75  return NodeStatus::SUCCESS;
76  }
77 
79  {
80  // Optionally, a port can have a human readable description
81  const char* description = "Simply print the target on console...";
82  return {InputPort<Position2D>("target", description)};
83  }
84 };
85 
86 //----------------------------------------------------------------
87 
103 // clang-format off
104 static const char* xml_text = R"(
105 
106  <root main_tree_to_execute = "MainTree" >
107  <BehaviorTree ID="MainTree">
108  <Sequence name="root">
109  <CalculateGoal goal="{GoalPosition}" />
110  <PrintTarget target="{GoalPosition}" />
111  <SetBlackboard output_key="OtherGoal" value="-1;3" />
112  <PrintTarget target="{OtherGoal}" />
113  </Sequence>
114  </BehaviorTree>
115  </root>
116  )";
117 
118 // clang-format on
119 
120 int main()
121 {
122  using namespace BT;
123 
124  BehaviorTreeFactory factory;
125  factory.registerNodeType<CalculateGoal>("CalculateGoal");
126  factory.registerNodeType<PrintTarget>("PrintTarget");
127 
128  auto tree = factory.createTreeFromText(xml_text);
129  tree.tickRootWhileRunning();
130 
131  /* Expected output:
132  *
133  Target positions: [ 1.1, 2.3 ]
134  Converting string: "-1;3"
135  Target positions: [ -1.0, 3.0 ]
136 */
137  return 0;
138 }
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
static PortsList providedPorts()
NodeStatus tick() override
Method to be implemented by the user.
int main()
CalculateGoal(const std::string &name, const NodeConfiguration &config)
static PortsList providedPorts()
NodeStatus tick() override
Method to be implemented by the user.
PrintTarget(const std::string &name, const NodeConfiguration &config)
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
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:251
nonstd::string_view StringView
Definition: basic_types.h:55
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
static const char * xml_text
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
NodeStatus
Definition: basic_types.h:35
double convertFromString< double >(StringView str)
Point3D convertFromString(StringView key)


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14