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 BTCPP_format="4" >
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::NodeConfig& 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::NodeConfig& 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 
66  auto tree = factory.createTree("MainTree");
67  tree.tickWhileRunning();
68 
69  return 0;
70 }
BT
Definition: ex01_wrap_legacy.cpp:29
main
int main()
Definition: ex02_runtime_ports.cpp:51
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
xml_text
static const char * xml_text
Definition: ex02_runtime_ports.cpp:5
bt_factory.h
SayRuntimePort::SayRuntimePort
SayRuntimePort(const std::string &name, const BT::NodeConfig &config)
Definition: ex02_runtime_ports.cpp:34
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:609
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
ThinkRuntimePort
Definition: ex02_runtime_ports.cpp:17
BT::BehaviorTreeFactory::registerBuilder
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
Definition: bt_factory.cpp:140
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:322
ThinkRuntimePort::ThinkRuntimePort
ThinkRuntimePort(const std::string &name, const BT::NodeConfig &config)
Definition: ex02_runtime_ports.cpp:20
BT::RuntimeError
Definition: exceptions.h:58
SayRuntimePort::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: ex02_runtime_ports.cpp:39
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
ThinkRuntimePort::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: ex02_runtime_ports.cpp:24
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::BehaviorTreeFactory::registerBehaviorTreeFromText
void registerBehaviorTreeFromText(const std::string &xml_text)
Definition: bt_factory.cpp:277
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
SayRuntimePort
Definition: ex02_runtime_ports.cpp:31


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