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


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19