ex02_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, const BT::NodeConfiguration& config) :
21  BT::SyncActionNode(name, config)
22  {}
23 
24  BT::NodeStatus tick() override
25  {
26  setOutput("text", "The answer is 42");
27  return NodeStatus::SUCCESS;
28  }
29 };
30 
32 {
33 public:
34  SayRuntimePort(const std::string& name, const BT::NodeConfiguration& config) :
35  BT::SyncActionNode(name, config)
36  {}
37 
38  // You must override the virtual function tick()
39  BT::NodeStatus tick() override
40  {
41  auto msg = getInput<std::string>("message");
42  if (!msg)
43  {
44  throw BT::RuntimeError("missing required input [message]: ", msg.error());
45  }
46  std::cout << "Robot says: " << msg.value() << std::endl;
48  }
49 };
50 
51 int main()
52 {
53  BehaviorTreeFactory factory;
54 
55  //-------- register ports that might be defined at runtime --------
56  // more verbose way
57  PortsList think_ports = {BT::OutputPort<std::string>("text")};
58  factory.registerBuilder(
59  CreateManifest<ThinkRuntimePort>("ThinkRuntimePort", think_ports),
60  CreateBuilder<ThinkRuntimePort>());
61  // less verbose way
62  PortsList say_ports = {BT::InputPort<std::string>("message")};
63  factory.registerNodeType<SayRuntimePort>("SayRuntimePort", say_ports);
64 
65  auto tree = factory.createTreeFromText(xml_text);
66  tree.tickRoot();
67  return 0;
68 }
void registerNodeType(const std::string &ID)
Definition: bt_factory.h:364
static const char * xml_text
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:251
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:278
SayRuntimePort(const std::string &name, const BT::NodeConfiguration &config)
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:333
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)
Definition: bt_factory.cpp:91
int main()
ThinkRuntimePort(const std::string &name, const BT::NodeConfiguration &config)


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14