|
| 1 | +#include "behaviortree_cpp/xml_parsing.h" |
| 2 | +#include "behaviortree_cpp/loggers/bt_cout_logger.h" |
| 3 | +#include "behaviortree_cpp/blackboard/blackboard_local.h" |
| 4 | + |
| 5 | + |
| 6 | +/** In this tutorial we will see how to wrap legacy code into a |
| 7 | + * BehaviorTree in a non-intrusive way, i.e. without modifying the |
| 8 | + * original class |
| 9 | +*/ |
| 10 | + |
| 11 | +// clang-format off |
| 12 | +const std::string xml_text = R"( |
| 13 | +
|
| 14 | + <root main_tree_to_execute = "MainTree" > |
| 15 | + <BehaviorTree ID="MainTree"> |
| 16 | + <SequenceStar name="root"> |
| 17 | + <MoveTo goal="-1;3;0.5" /> |
| 18 | + <MoveTo goal="${myGoal}" /> |
| 19 | + </SequenceStar> |
| 20 | + </BehaviorTree> |
| 21 | + </root> |
| 22 | + )"; |
| 23 | + |
| 24 | +// clang-format on |
| 25 | + |
| 26 | +// This is my custom type. |
| 27 | +// By default, we don't know how to read this from a NodeParameter. |
| 28 | +struct Point3D { double x,y,z; }; |
| 29 | + |
| 30 | +// We want to create an ActionNode that calls the method MyLegacyMoveTo::go |
| 31 | +class MyLegacyMoveTo |
| 32 | +{ |
| 33 | +public: |
| 34 | + bool go(Point3D goal) |
| 35 | + { |
| 36 | + printf("Going to: %f %f %f\n", goal.x, goal.y, goal.z); |
| 37 | + return true; // true means success in my legacy code |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +// Similarly to the previous tutorials, we need to implement this parsing method, |
| 42 | +// deplaring a specialization of BT::convertFromString |
| 43 | +namespace BT |
| 44 | +{ |
| 45 | +template <> Point3D convertFromString(const StringView& key) |
| 46 | +{ |
| 47 | + // three real numbers separated by semicolons |
| 48 | + auto parts = BT::splitString(key, ';'); |
| 49 | + if (parts.size() != 3) |
| 50 | + { |
| 51 | + throw std::runtime_error("invalid input)"); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + Point3D output; |
| 56 | + output.x = convertFromString<double>(parts[0]); |
| 57 | + output.y = convertFromString<double>(parts[1]); |
| 58 | + output.z = convertFromString<double>(parts[2]); |
| 59 | + return output; |
| 60 | + } |
| 61 | +} |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +int main() |
| 66 | +{ |
| 67 | + using namespace BT; |
| 68 | + |
| 69 | + MyLegacyMoveTo move_to; |
| 70 | + |
| 71 | + // Here we use a lambda that captures the reference of move_to |
| 72 | + auto MoveToWrapperWithLambda = [&move_to](TreeNode& parent_node) -> NodeStatus |
| 73 | + { |
| 74 | + Point3D goal; |
| 75 | + // thanks to paren_node, you can access easily the NodeParameters and the blackboard |
| 76 | + parent_node.getParam("goal", goal); |
| 77 | + |
| 78 | + // you can write and read the blabkcoard if you like |
| 79 | + //parent_node.blackboard() .... |
| 80 | + |
| 81 | + bool res = move_to.go( goal ); |
| 82 | + // convert bool to NodeStatus |
| 83 | + return res ? NodeStatus::SUCCESS : NodeStatus::FAILURE; |
| 84 | + }; |
| 85 | + |
| 86 | + BehaviorTreeFactory factory; |
| 87 | + // Regoister the lambda with BehaviorTreeFactory::registerSimpleAction |
| 88 | + factory.registerSimpleAction("MoveTo", MoveToWrapperWithLambda); |
| 89 | + |
| 90 | + auto blackboard = Blackboard::create<BlackboardLocal>(); |
| 91 | + auto tree = buildTreeFromText(factory, xml_text, blackboard); |
| 92 | + |
| 93 | + // We set the entry "myGoal" in the blackboard. |
| 94 | + Point3D my_goal = {3,4,5}; |
| 95 | + blackboard->set("myGoal", my_goal); |
| 96 | + |
| 97 | + NodeStatus status = NodeStatus::RUNNING; |
| 98 | + while (status == NodeStatus::RUNNING) |
| 99 | + { |
| 100 | + status = tree.root_node->executeTick(); |
| 101 | + } |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +/* Expected output: |
| 106 | +
|
| 107 | +Going to: -1.000000 3.000000 0.500000 |
| 108 | +Going to: 3.000000 4.000000 5.000000 |
| 109 | +
|
| 110 | +The first MoveTo read the parameter from the string "-1;3;0.5" |
| 111 | +whilst the second from the blackboard, that contains a copy of the Point3D my_goal. |
| 112 | +
|
| 113 | +*/ |
0 commit comments