|
| 1 | +# Wrap legacy code |
| 2 | + |
| 3 | +In this tutorial we see how to deal with legacy code that was not meant to be used |
| 4 | +with BehaviorTree.CPP. |
| 5 | + |
| 6 | +Let's start supposing that this is my class. |
| 7 | + |
| 8 | +``` c++ |
| 9 | +// This is my custom type. |
| 10 | +struct Point3D { double x,y,z; }; |
| 11 | + |
| 12 | +class MyLegacyMoveTo |
| 13 | +{ |
| 14 | +public: |
| 15 | + bool go(Point3D goal) |
| 16 | + { |
| 17 | + printf("Going to: %f %f %f\n", goal.x, goal.y, goal.z); |
| 18 | + return true; // true means success in my legacy code |
| 19 | + } |
| 20 | +}; |
| 21 | +``` |
| 22 | + |
| 23 | +We want to create an ActionNode called "MoveTo" that invokes the method __MyLegacyMoveTo::go()__. |
| 24 | + |
| 25 | +The final goal is to be able to use this ActionNode in a tree like this one: |
| 26 | + |
| 27 | +``` XML |
| 28 | + <root main_tree_to_execute = "MainTree" > |
| 29 | + <BehaviorTree ID="MainTree"> |
| 30 | + <SequenceStar name="root"> |
| 31 | + <MoveTo goal="-1;3;0.5" /> |
| 32 | + <MoveTo goal="${myGoal}" /> |
| 33 | + </SequenceStar> |
| 34 | + </BehaviorTree> |
| 35 | + </root> |
| 36 | +``` |
| 37 | + |
| 38 | +The first thing that we need to do is to allow our library to convert |
| 39 | +a NodeParameter (that is |
| 40 | +nothing more than a pair of strings representing key/value) into a Point3D. |
| 41 | + |
| 42 | +As we did in a previous tutorial, we should implement a template specialization |
| 43 | +for __convertFromString__. |
| 44 | + |
| 45 | +Our particular string representation of a Point3D consists in three semicolon-separated |
| 46 | +numbers, representing __x, y and z_. |
| 47 | + |
| 48 | + |
| 49 | +``` c++ |
| 50 | +namespace BT |
| 51 | +{ |
| 52 | +template <> Point3D convertFromString(const StringView& key) |
| 53 | +{ |
| 54 | + // three real numbers separated by semicolons |
| 55 | + auto parts = BT::splitString(key, ';'); |
| 56 | + if (parts.size() != 3) |
| 57 | + { |
| 58 | + throw std::runtime_error("invalid input)"); |
| 59 | + } |
| 60 | + else |
| 61 | + { |
| 62 | + Point3D output; |
| 63 | + output.x = convertFromString<double>(parts[0]); |
| 64 | + output.y = convertFromString<double>(parts[1]); |
| 65 | + output.z = convertFromString<double>(parts[2]); |
| 66 | + return output; |
| 67 | + } |
| 68 | +} |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +Finally, we can use a __C++11 lambda__ (or, alternatively, __std::bind__) to wrap |
| 73 | +out method into a function with the right signature. |
| 74 | + |
| 75 | +``` c++ |
| 76 | +int main() |
| 77 | +{ |
| 78 | + using namespace BT; |
| 79 | + |
| 80 | + MyLegacyMoveTo move_to; |
| 81 | + |
| 82 | + // Here we use a lambda that captures the reference of move_to |
| 83 | + auto MoveToWrapperWithLambda = [&move_to](TreeNode& parent_node) -> NodeStatus |
| 84 | + { |
| 85 | + Point3D goal; |
| 86 | + // thanks to paren_node, you can access easily the NodeParameters and the blackboard |
| 87 | + parent_node.getParam("goal", goal); |
| 88 | + |
| 89 | + bool res = move_to.go( goal ); |
| 90 | + // convert bool to NodeStatus |
| 91 | + return res ? NodeStatus::SUCCESS : NodeStatus::FAILURE; |
| 92 | + }; |
| 93 | + |
| 94 | + BehaviorTreeFactory factory; |
| 95 | + factory.registerSimpleAction("MoveTo", MoveToWrapperWithLambda); |
| 96 | + |
| 97 | + auto blackboard = Blackboard::create<BlackboardLocal>(); |
| 98 | + auto tree = buildTreeFromText(factory, xml_text, blackboard); |
| 99 | + |
| 100 | + // We set the entry "myGoal" in the blackboard. |
| 101 | + Point3D my_goal = {3,4,5}; |
| 102 | + blackboard->set("myGoal", my_goal); |
| 103 | + |
| 104 | + NodeStatus status = NodeStatus::RUNNING; |
| 105 | + while (status == NodeStatus::RUNNING) |
| 106 | + { |
| 107 | + status = tree.root_node->executeTick(); |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +/* Expected output: |
| 113 | +
|
| 114 | +Going to: -1.000000 3.000000 0.500000 |
| 115 | +Going to: 3.000000 4.000000 5.000000 |
| 116 | +
|
| 117 | +The first MoveTo read the parameter from the string "-1;3;0.5" |
| 118 | +whilst the second from the blackboard, that contains a copy of the Point3D my_goal. |
| 119 | +
|
| 120 | +*/ |
| 121 | +``` |
| 122 | + |
| 123 | + |
| 124 | +The functor we are passing to __SimpleActionNode__ requires the following signature: |
| 125 | + |
| 126 | + BT::NodeStatus myFunction(BT::TreeNode& parent) |
| 127 | + |
| 128 | +As a consequence, we can access a NodeParameter by |
| 129 | + |
| 130 | + parent.getParam() |
| 131 | + |
| 132 | +or even set/get an entry of the Blackboard using |
| 133 | + |
| 134 | + parent.blackboard() |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments