|
| 1 | +#include "behaviortree_cpp/bt_factory.h" |
| 2 | +#include "behaviortree_cpp/decorators/loop_node.h" |
| 3 | +#include "behaviortree_cpp/loggers/bt_cout_logger.h" |
| 4 | +#include <list> |
| 5 | + |
| 6 | +using namespace BT; |
| 7 | + |
| 8 | +/* |
| 9 | + * Demonstrate how to use a SubTree Model (since version 4.4) |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * You can optinally add a model to a SubTrees, in this case "MySub". |
| 14 | + * We are telling to the factory that the callee should remap |
| 15 | + * two mandatory inputs, called: |
| 16 | + * |
| 17 | + * - in_value (that has the default value 42) |
| 18 | + * - in_name (no default) |
| 19 | + * |
| 20 | + * Similarly, there are two output values: |
| 21 | + * |
| 22 | + * - out_value (default remapping to port {output}) |
| 23 | + * - out_state (no default) |
| 24 | + * |
| 25 | + * The callee MUST specify, at least, those remapping that have |
| 26 | + * no default value. |
| 27 | + */ |
| 28 | + |
| 29 | +// clang-format off |
| 30 | +static const char* xml_subtree = R"( |
| 31 | +<root BTCPP_format="4"> |
| 32 | +
|
| 33 | + <TreeNodesModel> |
| 34 | + <SubTree ID="MySub"> |
| 35 | + <input_port name="in_value" default="42"/> |
| 36 | + <input_port name="in_name"/> |
| 37 | + <output_port name="out_result" default="{output}"/> |
| 38 | + <output_port name="out_state"/> |
| 39 | + </SubTree> |
| 40 | + </TreeNodesModel> |
| 41 | +
|
| 42 | + <BehaviorTree ID="MySub"> |
| 43 | + <Sequence> |
| 44 | + <ScriptCondition code="in_name=='john' && in_value==42" /> |
| 45 | + <Script code="out_result:=69; out_state:='ACTIVE'" /> |
| 46 | + </Sequence> |
| 47 | + </BehaviorTree> |
| 48 | +</root> |
| 49 | + )"; |
| 50 | + |
| 51 | +/** |
| 52 | + * Here, when calling "MySub", only in_name and out_state are explicitly |
| 53 | + * remapped. Will we use the default values for the other two. |
| 54 | + */ |
| 55 | + |
| 56 | +static const char* xml_maintree = R"( |
| 57 | +<root BTCPP_format="4"> |
| 58 | +
|
| 59 | + <BehaviorTree ID="MainTree"> |
| 60 | + <Sequence> |
| 61 | + <Script code="name_arg:= 'john' "/> |
| 62 | + <SubTree ID="MySub" in_name="{name_arg}" out_state="{state}"/> |
| 63 | + <ScriptCondition code=" output==69 && state=='ACTIVE' " /> |
| 64 | + </Sequence> |
| 65 | + </BehaviorTree> |
| 66 | +
|
| 67 | +</root> |
| 68 | + )"; |
| 69 | + |
| 70 | +// clang-format on |
| 71 | + |
| 72 | +int main() |
| 73 | +{ |
| 74 | + BehaviorTreeFactory factory; |
| 75 | + factory.registerBehaviorTreeFromText(xml_subtree); |
| 76 | + factory.registerBehaviorTreeFromText(xml_maintree); |
| 77 | + |
| 78 | + auto tree = factory.createTree("MainTree"); |
| 79 | + StdCoutLogger logger(tree); |
| 80 | + tree.tickWhileRunning(); |
| 81 | + |
| 82 | + // We expect the sequence to be succesful. |
| 83 | + |
| 84 | + // The full remapping was: |
| 85 | + // |
| 86 | + // - in_name <-> {name_arg} // specified by callee |
| 87 | + // - in_value <-> 42 // default |
| 88 | + // - out_result <-> {output} // default |
| 89 | + // - out_state <-> {state} // specified by callee |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments