ex01_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
12 {
13  double x, y, z;
14 };
15 
16 // We want to create an ActionNode that calls the method MyLegacyMoveTo::go
18 {
19 public:
20  bool go(Point3D goal)
21  {
22  printf("Going to: %f %f %f\n", goal.x, goal.y, goal.z);
23  return true; // true means success in my legacy code
24  }
25 };
26 
27 // Similarly to the previous tutorials, we need to implement this parsing method,
28 // providing a specialization of BT::convertFromString
29 namespace BT
30 {
31 template <>
33 {
34  // three real numbers separated by semicolons
35  auto parts = BT::splitString(key, ';');
36  if(parts.size() != 3)
37  {
38  throw RuntimeError("invalid input)");
39  }
40  else
41  {
42  Point3D output;
43  output.x = convertFromString<double>(parts[0]);
44  output.y = convertFromString<double>(parts[1]);
45  output.z = convertFromString<double>(parts[2]);
46  return output;
47  }
48 }
49 } // namespace BT
50 
51 // clang-format off
52 static const char* xml_text = R"(
53 
54  <root BTCPP_format="4">
55  <BehaviorTree>
56  <MoveTo goal="-1;3;0.5" />
57  </BehaviorTree>
58  </root>
59  )";
60 
61 // clang-format on
62 
63 int main()
64 {
65  using namespace BT;
66 
67  MyLegacyMoveTo move_to;
68 
69  // Here we use a lambda that captures the reference of move_to
70  auto MoveToWrapperWithLambda = [&move_to](TreeNode& parent_node) -> NodeStatus {
71  Point3D goal;
72  // thanks to paren_node, you can access easily the input and output ports.
73  parent_node.getInput("goal", goal);
74 
75  bool res = move_to.go(goal);
76  // convert bool to NodeStatus
78  };
79 
80  BehaviorTreeFactory factory;
81 
82  // Register the lambda with BehaviorTreeFactory::registerSimpleAction
83 
84  PortsList ports = { BT::InputPort<Point3D>("goal") };
85  factory.registerSimpleAction("MoveTo", MoveToWrapperWithLambda, ports);
86 
87  auto tree = factory.createTreeFromText(xml_text);
88 
89  tree.tickWhileRunning();
90 
91  return 0;
92 }
93 
94 /* Expected output:
95 
96 Going to: -1.000000 3.000000 0.500000
97 
98 */
BT
Definition: ex01_wrap_legacy.cpp:29
MyLegacyMoveTo::go
bool go(Point3D goal)
Definition: ex01_wrap_legacy.cpp:20
Point3D::z
double z
Definition: ex01_wrap_legacy.cpp:13
main
int main()
Definition: ex01_wrap_legacy.cpp:63
BT::StringView
std::string_view StringView
Definition: basic_types.h:59
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
bt_factory.h
MyLegacyMoveTo
Definition: ex01_wrap_legacy.cpp:17
Point3D::y
double y
Definition: ex01_wrap_legacy.cpp:13
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:609
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::NodeStatus::FAILURE
@ FAILURE
BT::convertFromString
Point3D convertFromString(StringView key)
Definition: ex01_wrap_legacy.cpp:32
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
Point3D::x
double x
Definition: ex01_wrap_legacy.cpp:13
BT::RuntimeError
Definition: exceptions.h:58
BT::splitString
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
Definition: basic_types.cpp:348
bt_cout_logger.h
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::NodeStatus::SUCCESS
@ SUCCESS
Point3D
Definition: ex01_wrap_legacy.cpp:11
BT::convertFromString< double >
double convertFromString< double >(StringView str)
Definition: basic_types.cpp:191
xml_text
static const char * xml_text
Definition: ex01_wrap_legacy.cpp:52
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::BehaviorTreeFactory::registerSimpleAction
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:166


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