t07_wrap_legacy.cpp
Go to the documentation of this file.
3 
9 // This is my custom type. We won't know how to read this from a string,
10 // unless we implement convertFromString<Point3D>()
11 struct Point3D { double x,y,z; };
12 
13 // We want to create an ActionNode that calls the method MyLegacyMoveTo::go
15 {
16 public:
17  bool go(Point3D goal)
18  {
19  printf("Going to: %f %f %f\n", goal.x, goal.y, goal.z);
20  return true; // true means success in my legacy code
21  }
22 };
23 
24 // Similarly to the previous tutorials, we need to implement this parsing method,
25 // providing a specialization of BT::convertFromString
26 namespace BT
27 {
29 {
30  // three real numbers separated by semicolons
31  auto parts = BT::splitString(key, ';');
32  if (parts.size() != 3)
33  {
34  throw RuntimeError("invalid input)");
35  }
36  else
37  {
38  Point3D output;
39  output.x = convertFromString<double>(parts[0]);
40  output.y = convertFromString<double>(parts[1]);
41  output.z = convertFromString<double>(parts[2]);
42  return output;
43  }
44 }
45 } // end anmespace BT
46 
47 
48 // clang-format off
49 static const char* xml_text = R"(
50 
51  <root>
52  <BehaviorTree>
53  <MoveTo goal="-1;3;0.5" />
54  </BehaviorTree>
55  </root>
56  )";
57 
58 // clang-format on
59 
60 int main()
61 {
62  using namespace BT;
63 
64  MyLegacyMoveTo move_to;
65 
66  // Here we use a lambda that captures the reference of move_to
67  auto MoveToWrapperWithLambda = [&move_to](TreeNode& parent_node) -> NodeStatus
68  {
69  Point3D goal;
70  // thanks to paren_node, you can access easily the input and output ports.
71  parent_node.getInput("goal", goal);
72 
73  bool res = move_to.go( goal );
74  // convert bool to NodeStatus
75  return res ? NodeStatus::SUCCESS : NodeStatus::FAILURE;
76  };
77 
78  BehaviorTreeFactory factory;
79 
80  // Register the lambda with BehaviorTreeFactory::registerSimpleAction
81 
82  PortsList ports = { BT::InputPort<Point3D>("goal") };
83  factory.registerSimpleAction("MoveTo", MoveToWrapperWithLambda, ports );
84 
85  auto tree = factory.createTreeFromText(xml_text);
86 
87  tree.tickRoot();
88 
89  return 0;
90 }
91 
92 /* Expected output:
93 
94 Going to: -1.000000 3.000000 0.500000
95 
96 */
static const char * xml_text
int main()
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
bool go(Point3D goal)
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
nonstd::string_view StringView
Definition: basic_types.h:50
NodeStatus tickRoot()
Definition: bt_factory.h:181
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleAction help you register nodes of type SimpleActionNode.
Definition: bt_factory.cpp:111
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:53
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:317
NodeStatus
Definition: basic_types.h:35
double convertFromString< double >(StringView str)
Point3D convertFromString(StringView key)


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25