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


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