t02_basic_ports.cpp
Go to the documentation of this file.
2 
3 #include "dummy_nodes.h"
4 
5 using namespace BT;
6 
27 // clang-format off
28 static const char* xml_text = R"(
29 
30  <root BTCPP_format="4" >
31 
32  <BehaviorTree ID="MainTree">
33  <Sequence name="root">
34  <SaySomething message="hello" />
35  <SaySomething2 message="this works too" />
36  <ThinkWhatToSay text="{the_answer}"/>
37  <SaySomething2 message="{the_answer}" />
38  </Sequence>
39  </BehaviorTree>
40 
41  </root>
42  )";
43 // clang-format on
44 
46 {
47 public:
48  ThinkWhatToSay(const std::string& name, const BT::NodeConfig& config)
49  : BT::SyncActionNode(name, config)
50  {}
51 
52  // This Action simply write a value in the port "text"
53  BT::NodeStatus tick() override
54  {
55  setOutput("text", "The answer is 42");
57  }
58 
59  // A node having ports MUST implement this STATIC method
61  {
62  return { BT::OutputPort<std::string>("text") };
63  }
64 };
65 
66 int main()
67 {
68  using namespace DummyNodes;
69 
70  BehaviorTreeFactory factory;
71 
72  // The class SaySomething has a method called providedPorts() that define the INPUTS.
73  // In this case, it requires an input called "message"
74  factory.registerNodeType<SaySomething>("SaySomething");
75 
76  // Similarly to SaySomething, ThinkWhatToSay has an OUTPUT port called "text"
77  // Both these ports are std::string, therefore they can connect to each other
78  factory.registerNodeType<ThinkWhatToSay>("ThinkWhatToSay");
79 
80  // SimpleActionNodes can not define their own method providedPorts(), therefore
81  // we have to pass the PortsList explicitly if we want the Action to use getInput()
82  // or setOutput();
83  PortsList say_something_ports = { InputPort<std::string>("message") };
84  factory.registerSimpleAction("SaySomething2", SaySomethingSimple, say_something_ports);
85 
86  /* An INPUT can be either a string, for instance:
87  *
88  * <SaySomething message="hello" />
89  *
90  * or contain a "pointer" to a type erased entry in the Blackboard,
91  * using this syntax: {name_of_entry}. Example:
92  *
93  * <SaySomething message="{the_answer}" />
94  */
95 
96  auto tree = factory.createTreeFromText(xml_text);
97 
98  tree.tickWhileRunning();
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
xml_text
static const char * xml_text
Definition: t02_basic_ports.cpp:28
main
int main()
Definition: t02_basic_ports.cpp:66
ThinkWhatToSay::providedPorts
static BT::PortsList providedPorts()
Definition: t02_basic_ports.cpp:60
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:53
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:609
dummy_nodes.h
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
ThinkWhatToSay
Definition: t02_basic_ports.cpp:45
DummyNodes::SaySomething
Definition: dummy_nodes.h:47
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::NodeStatus::SUCCESS
@ SUCCESS
ThinkWhatToSay::ThinkWhatToSay
ThinkWhatToSay(const std::string &name, const BT::NodeConfig &config)
Definition: t02_basic_ports.cpp:48
BT::NodeConfig
Definition: tree_node.h:73
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:33
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:166


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:08