t11_runtime_ports.cpp
Go to the documentation of this file.
2 using namespace BT;
3 
4 // clang-format off
5 static const char* xml_text = R"(
6  <root main_tree_to_execute = "MainTree" >
7  <BehaviorTree ID="MainTree">
8  <Sequence name="root">
9  <ThinkRuntimePort text="{the_answer}"/>
10  <SayRuntimePort message="{the_answer}" />
11  </Sequence>
12  </BehaviorTree>
13  </root>
14  )";
15 // clang-format on
16 
18 {
19  public:
20  ThinkRuntimePort(const std::string& name,
21  const BT::NodeConfiguration& config)
22  : BT::SyncActionNode(name, config)
23  {
24  }
25 
26  BT::NodeStatus tick() override {
27  setOutput("text", "The answer is 42" );
28  return NodeStatus::SUCCESS;
29  }
30 };
31 
33 {
34  public:
35  SayRuntimePort(const std::string& name, const BT::NodeConfiguration& config)
36  : BT::SyncActionNode(name, config)
37  {
38  }
39 
40  // You must override the virtual function tick()
41  BT::NodeStatus tick() override
42  {
43  auto msg = getInput<std::string>("message");
44  if (!msg){
45  throw BT::RuntimeError( "missing required input [message]: ", msg.error() );
46  }
47  std::cout << "Robot says: " << msg.value() << std::endl;
49  }
50 };
51 
52 
53 int main()
54 {
55  BehaviorTreeFactory factory;
56 
57  //-------- register ports that might be defined at runtime --------
58  // more verbose way
59  PortsList think_ports = {BT::OutputPort<std::string>("text")};
60  factory.registerBuilder(CreateManifest<ThinkRuntimePort>("ThinkRuntimePort", think_ports),
61  CreateBuilder<ThinkRuntimePort>());
62  // less verbose way
63  PortsList say_ports = {BT::InputPort<std::string>("message")};
64  factory.registerNodeType<SayRuntimePort>("SayRuntimePort", say_ports);
65 
66  auto tree = factory.createTreeFromText(xml_text);
67  tree.tickRoot();
68  return 0;
69 }
70 
71 
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:290
int main()
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn&#39;t require a...
Definition: action_node.h:53
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:207
static const char * xml_text
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:249
SayRuntimePort(const std::string &name, const BT::NodeConfiguration &config)
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:317
BT::NodeStatus tick() override
Method to be implemented by the user.
BT::NodeStatus tick() override
Method to be implemented by the user.
NodeStatus
Definition: basic_types.h:35
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
The most generic way to register your own builder.
Definition: bt_factory.cpp:87
ThinkRuntimePort(const std::string &name, const BT::NodeConfiguration &config)


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