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 NodeConfig& 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 NodeConfig& 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 BTCPP_format="4" >
107  <BehaviorTree ID="MainTree">
108  <Sequence name="root">
109  <CalculateGoal goal="{GoalPosition}" />
110  <PrintTarget target="{GoalPosition}" />
111  <Script code="OtherGoal='-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 
130  tree.tickWhileRunning();
131 
132  /* Expected output:
133  *
134  Target positions: [ 1.1, 2.3 ]
135  Converting string: "-1;3"
136  Target positions: [ -1.0, 3.0 ]
137 */
138  return 0;
139 }
BT
Definition: ex01_wrap_legacy.cpp:29
xml_text
static const char * xml_text
Definition: t03_generic_ports.cpp:104
CalculateGoal::CalculateGoal
CalculateGoal(const std::string &name, const NodeConfig &config)
Definition: t03_generic_ports.cpp:43
BT::StringView
std::string_view StringView
Definition: basic_types.h:59
PrintTarget::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t03_generic_ports.cpp:66
main
int main()
Definition: t03_generic_ports.cpp:120
bt_factory.h
CalculateGoal::providedPorts
static PortsList providedPorts()
Definition: t03_generic_ports.cpp:53
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::convertFromString
Point3D convertFromString(StringView key)
Definition: ex01_wrap_legacy.cpp:32
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
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::RuntimeError
Definition: exceptions.h:58
BT::splitString
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
Definition: basic_types.cpp:348
CalculateGoal::tick
NodeStatus tick() override
Method to be implemented by the user.
Definition: t03_generic_ports.cpp:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
PrintTarget
Definition: t03_generic_ports.cpp:59
BT::NodeStatus::SUCCESS
@ SUCCESS
Position2D::x
double x
Definition: t03_generic_ports.cpp:12
Position2D::y
double y
Definition: t03_generic_ports.cpp:12
PrintTarget::PrintTarget
PrintTarget(const std::string &name, const NodeConfig &config)
Definition: t03_generic_ports.cpp:62
BT::convertFromString< double >
double convertFromString< double >(StringView str)
Definition: basic_types.cpp:191
BT::NodeConfig
Definition: tree_node.h:73
CalculateGoal
Definition: t03_generic_ports.cpp:40
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
Position2D
Definition: t03_generic_ports.cpp:10
PrintTarget::providedPorts
static PortsList providedPorts()
Definition: t03_generic_ports.cpp:78


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