t06_subtree_port_remapping.cpp
Go to the documentation of this file.
3 
4 #include "movebase_node.h"
5 #include "dummy_nodes.h"
6 
21 // clang-format off
22 
23 static const char* xml_text = R"(
24 <root main_tree_to_execute = "MainTree">
25 
26  <BehaviorTree ID="MainTree">
27 
28  <Sequence name="main_sequence">
29  <SetBlackboard output_key="move_goal" value="1;2;3" />
30  <SubTree ID="MoveRobot" target="move_goal" output="move_result" />
31  <SaySomething message="{move_result}"/>
32  </Sequence>
33 
34  </BehaviorTree>
35 
36  <BehaviorTree ID="MoveRobot">
37  <Fallback name="move_robot_main">
38  <SequenceStar>
39  <MoveBase goal="{target}"/>
40  <SetBlackboard output_key="output" value="mission accomplished" />
41  </SequenceStar>
42  <ForceFailure>
43  <SetBlackboard output_key="output" value="mission failed" />
44  </ForceFailure>
45  </Fallback>
46  </BehaviorTree>
47 
48 </root>
49  )";
50 
51 // clang-format on
52 
53 
54 using namespace BT;
55 using namespace DummyNodes;
56 
57 int main()
58 {
59  BehaviorTreeFactory factory;
60 
61  factory.registerNodeType<SaySomething>("SaySomething");
62  factory.registerNodeType<MoveBaseAction>("MoveBase");
63 
64  auto tree = factory.createTreeFromText(xml_text);
65 
67  // Keep on ticking until you get either a SUCCESS or FAILURE state
68  while( status == NodeStatus::RUNNING)
69  {
70  status = tree.tickRoot();
71  SleepMS(1); // optional sleep to avoid "busy loops"
72  }
73 
74  // let's visualize some information about the current state of the blackboards.
75  std::cout << "--------------" << std::endl;
76  tree.blackboard_stack[0]->debugMessage();
77  std::cout << "--------------" << std::endl;
78  tree.blackboard_stack[1]->debugMessage();
79  std::cout << "--------------" << std::endl;
80 
81  return 0;
82 }
83 
84 /* Expected output:
85 
86  [ MoveBase: STARTED ]. goal: x=1 y=2.0 theta=3.00
87  [ MoveBase: FINISHED ]
88  Robot says: mission accomplished
89  --------------
90  move_result (std::string) -> full
91  move_goal (Pose2D) -> full
92  --------------
93  output (std::string) -> remapped to parent [move_result]
94  target (Pose2D) -> remapped to parent [move_goal]
95  --------------
96 
97 */
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:290
void SleepMS(int ms)
Definition: movebase_node.h:12
static const char * xml_text
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
NodeStatus
Definition: basic_types.h:35


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