t02_basic_ports.cpp
Go to the documentation of this file.
2 
3 #include "dummy_nodes.h"
4 #include "movebase_node.h"
5 
6 using namespace BT;
7 
28 // clang-format off
29 static const char* xml_text = R"(
30 
31  <root main_tree_to_execute = "MainTree" >
32 
33  <BehaviorTree ID="MainTree">
34  <Sequence name="root">
35  <SaySomething message="start thinking..." />
36  <ThinkWhatToSay text="{the_answer}"/>
37  <SaySomething message="{the_answer}" />
38  <SaySomething2 message="SaySomething2 works too..." />
39  <SaySomething2 message="{the_answer}" />
40  </Sequence>
41  </BehaviorTree>
42 
43  </root>
44  )";
45 // clang-format on
46 
47 
49 {
50  public:
51  ThinkWhatToSay(const std::string& name, const BT::NodeConfiguration& config)
52  : BT::SyncActionNode(name, config)
53  {
54  }
55 
56  // This Action simply write a value in the port "text"
57  BT::NodeStatus tick() override
58  {
59  setOutput("text", "The answer is 42" );
61  }
62 
63  // A node having ports MUST implement this STATIC method
65  {
66  return { BT::OutputPort<std::string>("text") };
67  }
68 };
69 
70 
71 int main()
72 {
73  using namespace DummyNodes;
74 
75  BehaviorTreeFactory factory;
76 
77  // The class SaySomething has a method called providedPorts() that define the INPUTS.
78  // In this case, it requires an input called "message"
79  factory.registerNodeType<SaySomething>("SaySomething");
80 
81 
82  // Similarly to SaySomething, ThinkWhatToSay has an OUTPUT port called "text"
83  // Both these ports are std::string, therefore they can connect to each other
84  factory.registerNodeType<ThinkWhatToSay>("ThinkWhatToSay");
85 
86  // SimpleActionNodes can not define their own method providedPorts(), therefore
87  // we have to pass the PortsList explicitly if we want the Action to use getInput()
88  // or setOutput();
89  PortsList say_something_ports = { InputPort<std::string>("message") };
90  factory.registerSimpleAction("SaySomething2", SaySomethingSimple, say_something_ports );
91 
92  /* An INPUT can be either a string, for instance:
93  *
94  * <SaySomething message="start thinking..." />
95  *
96  * or contain a "pointer" to a type erased entry in the Blackboard,
97  * using this syntax: {name_of_entry}. Example:
98  *
99  * <SaySomething message="{the_answer}" />
100  */
101 
102  auto tree = factory.createTreeFromText(xml_text);
103 
104  tree.root_node->executeTick();
105 
106  /* Expected output:
107  *
108  Robot says: start thinking...
109  Robot says: The answer is 42
110  Robot says: SaySomething2 works too...
111  Robot says: The answer is 42
112  *
113  * The way we "connect" output ports to input ports is to "point" to the same
114  * Blackboard entry.
115  *
116  * This means that ThinkSomething will write into the entry with key "the_answer";
117  * SaySomething and SaySomething2 will read the message from the same entry.
118  *
119  */
120  return 0;
121 }
122 
123 
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:174
BT::NodeStatus tick() override
Method to be implemented by the user.
static const char * xml_text
TreeNode * root_node
Definition: bt_factory.h:80
static BT::PortsList providedPorts()
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:52
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:98
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:170
ThinkWhatToSay(const std::string &name, const BT::NodeConfiguration &config)
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:92
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:316
BT::NodeStatus SaySomethingSimple(BT::TreeNode &self)
Definition: dummy_nodes.cpp:51
NodeStatus
Definition: basic_types.h:35
int main()
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:33


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05