t11_groot_howto.cpp
Go to the documentation of this file.
2 #include "crossdoor_nodes.h"
8 
13 // A custom struct that I want to visualize in Groot2
14 struct Position2D
15 {
16  double x;
17  double y;
18 };
19 
20 // This macro will generate the code that is needed to convert
21 // the object to/from JSON.
22 // You still need to call BT::RegisterJsonDefinition<Position2D>()
23 // in main()
25 {
26  add_field("x", &pos.x);
27  add_field("y", &pos.y);
28 }
29 
30 // Simple Action that updates an instance of Position2D in the blackboard
32 {
33 public:
34  UpdatePosition(const std::string& name, const BT::NodeConfig& config)
36  {}
37 
38  BT::NodeStatus tick() override
39  {
40  _pos.x += 0.2;
41  _pos.y += 0.1;
42  setOutput("pos", _pos);
44  }
45 
47  {
48  return { BT::OutputPort<Position2D>("pos") };
49  }
50 
51 private:
52  Position2D _pos = { 0, 0 };
53 };
54 
55 // clang-format off
56 
57 static const char* xml_text = R"(
58 <root BTCPP_format="4">
59 
60  <BehaviorTree ID="MainTree">
61  <Sequence>
62  <Script code="door_open:=false" />
63  <UpdatePosition pos="{pos_2D}" />
64  <Fallback>
65  <Inverter>
66  <IsDoorClosed/>
67  </Inverter>
68  <SubTree ID="DoorClosed" _autoremap="true" door_open="{door_open}"/>
69  </Fallback>
70  <PassThroughDoor/>
71  </Sequence>
72  </BehaviorTree>
73 
74  <BehaviorTree ID="DoorClosed">
75  <Fallback name="tryOpen" _onSuccess="door_open:=true">
76  <OpenDoor/>
77  <RetryUntilSuccessful num_attempts="5">
78  <PickLock/>
79  </RetryUntilSuccessful>
80  <SmashDoor/>
81  </Fallback>
82  </BehaviorTree>
83 
84 </root>
85  )";
86 
87 // clang-format on
88 
89 int main()
90 {
92 
93  // Nodes registration, as usual
94  CrossDoor cross_door;
95  cross_door.registerNodes(factory);
96  factory.registerNodeType<UpdatePosition>("UpdatePosition");
97 
98  // Groot2 editor requires a model of your registered Nodes.
99  // You don't need to write that by hand, it can be automatically
100  // generated using the following command.
101  const std::string xml_models = BT::writeTreeNodesModelXML(factory);
102 
104 
105  // Add this to allow Groot2 to visualize your custom type
106  BT::RegisterJsonDefinition<Position2D>();
107 
108  auto tree = factory.createTree("MainTree");
109 
110  std::cout << "----------- XML file ----------\n"
111  << BT::WriteTreeToXML(tree, false, false)
112  << "--------------------------------\n";
113 
114  // Connect the Groot2Publisher. This will allow Groot2 to
115  // get the tree and poll status updates.
116  const unsigned port = 1667;
117  BT::Groot2Publisher publisher(tree, port);
118 
119  // Add two more loggers, to save the transitions into a file.
120  // Both formats are compatible with Groot2
121 
122  // Logging with lightweight serialization
123  BT::FileLogger2 logger2(tree, "t11_groot_howto.btlog");
124  BT::MinitraceLogger minilog(tree, "minitrace.json");
125 
126  while(1)
127  {
128  std::cout << "Start" << std::endl;
129  cross_door.reset();
130  tree.tickWhileRunning();
131  std::this_thread::sleep_for(std::chrono::milliseconds(2000));
132  }
133 
134  return 0;
135 }
BT
Definition: ex01_wrap_legacy.cpp:29
BT::BehaviorTreeFactory::createTree
Tree createTree(const std::string &tree_name, Blackboard::Ptr blackboard=Blackboard::create())
Definition: bt_factory.cpp:432
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT_JSON_CONVERTER
BT_JSON_CONVERTER(Position2D, pos)
Definition: t11_groot_howto.cpp:24
bt_factory.h
UpdatePosition::_pos
Position2D _pos
Definition: t11_groot_howto.cpp:52
UpdatePosition::tick
BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: t11_groot_howto.cpp:38
BT::WriteTreeToXML
std::string WriteTreeToXML(const Tree &tree, bool add_metadata, bool add_builtin_models)
WriteTreeToXML create a string that contains the XML that corresponds to a given tree....
Definition: xml_parsing.cpp:1533
CrossDoor::reset
void reset()
Definition: crossdoor_nodes.cpp:71
BT::PortsList
std::unordered_map< std::string, PortInfo > PortsList
Definition: basic_types.h:585
CrossDoor::registerNodes
void registerNodes(BT::BehaviorTreeFactory &factory)
Definition: crossdoor_nodes.cpp:56
UpdatePosition
Definition: t11_groot_howto.cpp:31
BT::BehaviorTreeFactory::registerNodeType
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:326
bt_file_logger_v2.h
groot2_publisher.h
BT::SyncActionNode::SyncActionNode
SyncActionNode(const std::string &name, const NodeConfig &config)
Definition: action_node.cpp:52
BT::Groot2Publisher
The Groot2Publisher is used to create an interface between your BT.CPP executor and Groot2.
Definition: groot2_publisher.h:19
xml_text
static const char * xml_text
Definition: t11_groot_howto.cpp:57
main
int main()
Definition: t11_groot_howto.cpp:89
json_export.h
BT::FileLogger2
The FileLogger2 is a logger that saves the tree as XML and all the transitions. Data is written to fi...
Definition: bt_file_logger_v2.h:23
BT::MinitraceLogger
Definition: bt_minitrace_logger.h:7
xml_parsing.h
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:209
BT::TreeNode::setOutput
Result setOutput(const std::string &key, const T &value)
setOutput modifies the content of an Output port
Definition: tree_node.h:558
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
Position2D::x
double x
Definition: t03_generic_ports.cpp:12
crossdoor_nodes.h
Position2D::y
double y
Definition: t03_generic_ports.cpp:12
CrossDoor
Definition: crossdoor_nodes.h:5
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
BT::writeTreeNodesModelXML
std::string writeTreeNodesModelXML(const BehaviorTreeFactory &factory, bool include_builtin=false)
writeTreeNodesModelXML generates an XMl that contains the manifests in the <TreeNodesModel>
Definition: xml_parsing.cpp:1214
UpdatePosition::UpdatePosition
UpdatePosition(const std::string &name, const BT::NodeConfig &config)
Definition: t11_groot_howto.cpp:34
Position2D
Definition: t03_generic_ports.cpp:10
bt_minitrace_logger.h
UpdatePosition::providedPorts
static BT::PortsList providedPorts()
Definition: t11_groot_howto.cpp:46


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17