bt_flatbuffer_helper.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "behaviortree_cpp/flatbuffers/BT_logger_generated.h"
5 
6 namespace BT
7 {
8 
9 using SerializedTransition = std::array<uint8_t, 12>;
10 
12 {
13  switch(type)
14  {
16  return Serialization::NodeType::ACTION;
18  return Serialization::NodeType::DECORATOR;
20  return Serialization::NodeType::CONTROL;
22  return Serialization::NodeType::CONDITION;
24  return Serialization::NodeType::SUBTREE;
27  }
29 }
30 
32 {
33  switch(type)
34  {
37  return Serialization::NodeStatus::IDLE;
39  return Serialization::NodeStatus::SUCCESS;
41  return Serialization::NodeStatus::RUNNING;
43  return Serialization::NodeStatus::FAILURE;
44  }
45  return Serialization::NodeStatus::IDLE;
46 }
47 
49 {
50  switch(direction)
51  {
53  return Serialization::PortDirection::INPUT;
55  return Serialization::PortDirection::OUTPUT;
57  return Serialization::PortDirection::INOUT;
58  }
59  return Serialization::PortDirection::INOUT;
60 }
61 
62 inline void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder& builder,
63  const BT::Tree& tree)
64 {
65  std::vector<flatbuffers::Offset<Serialization::TreeNode>> fb_nodes;
66 
67  applyRecursiveVisitor(tree.rootNode(), [&](BT::TreeNode* node) {
68  std::vector<uint16_t> children_uid;
69  if(auto control = dynamic_cast<BT::ControlNode*>(node))
70  {
71  children_uid.reserve(control->children().size());
72  for(const auto& child : control->children())
73  {
74  children_uid.push_back(child->UID());
75  }
76  }
77  else if(auto decorator = dynamic_cast<BT::DecoratorNode*>(node))
78  {
79  const auto& child = decorator->child();
80  children_uid.push_back(child->UID());
81  }
82 
83  // Const cast to ensure public access to config() overload
84  const auto& node_config = const_cast<BT::TreeNode const&>(*node).config();
85  std::vector<flatbuffers::Offset<Serialization::PortConfig>> ports;
86  for(const auto& it : node_config.input_ports)
87  {
88  ports.push_back(Serialization::CreatePortConfigDirect(builder, it.first.c_str(),
89  it.second.c_str()));
90  }
91  for(const auto& it : node_config.output_ports)
92  {
93  ports.push_back(Serialization::CreatePortConfigDirect(builder, it.first.c_str(),
94  it.second.c_str()));
95  }
96 
97  auto tn = Serialization::CreateTreeNode(
98  builder, node->UID(), builder.CreateVector(children_uid),
99  convertToFlatbuffers(node->status()), builder.CreateString(node->name().c_str()),
100  builder.CreateString(node->registrationName().c_str()),
101  builder.CreateVector(ports));
102 
103  fb_nodes.push_back(tn);
104  });
105 
106  std::vector<flatbuffers::Offset<Serialization::NodeModel>> node_models;
107 
108  for(const auto& node_it : tree.manifests)
109  {
110  const auto& manifest = node_it.second;
111  std::vector<flatbuffers::Offset<Serialization::PortModel>> port_models;
112 
113  for(const auto& port_it : manifest.ports)
114  {
115  const auto& port_name = port_it.first;
116  const auto& port = port_it.second;
117  auto port_model = Serialization::CreatePortModel(
118  builder, builder.CreateString(port_name.c_str()),
119  convertToFlatbuffers(port.direction()),
120  builder.CreateString(demangle(port.type()).c_str()),
121  builder.CreateString(port.description().c_str()));
122  port_models.push_back(port_model);
123  }
124 
125  auto node_model = Serialization::CreateNodeModel(
126  builder, builder.CreateString(manifest.registration_ID.c_str()),
127  convertToFlatbuffers(manifest.type), builder.CreateVector(port_models));
128 
129  node_models.push_back(node_model);
130  }
131 
132  auto behavior_tree = Serialization::CreateBehaviorTree(
133  builder, tree.rootNode()->UID(), builder.CreateVector(fb_nodes),
134  builder.CreateVector(node_models));
135 
136  builder.Finish(behavior_tree);
137 }
138 
142 inline SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp,
143  NodeStatus prev_status, NodeStatus status)
144 {
145  using namespace std::chrono;
147  int64_t usec = duration_cast<microseconds>(timestamp).count();
148  int64_t t_sec = usec / 1000000;
149  int64_t t_usec = usec % 1000000;
150 
151  flatbuffers::WriteScalar(&buffer[0], t_sec);
152  flatbuffers::WriteScalar(&buffer[4], t_usec);
153  flatbuffers::WriteScalar(&buffer[8], UID);
154 
155  flatbuffers::WriteScalar(&buffer[10],
156  static_cast<int8_t>(convertToFlatbuffers(prev_status)));
157  flatbuffers::WriteScalar(&buffer[11],
158  static_cast<int8_t>(convertToFlatbuffers(status)));
159 
160  return buffer;
161 }
162 
163 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::SerializedTransition
std::array< uint8_t, 12 > SerializedTransition
Definition: bt_flatbuffer_helper.h:9
BT::demangle
std::string demangle(char const *name)
Definition: demangle_util.h:74
BT::PortDirection::INOUT
@ INOUT
BT::TreeNode::UID
uint16_t UID() const
Definition: tree_node.cpp:336
BT::NodeType
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:20
BT::Monitor::UNDEFINED
@ UNDEFINED
Definition: groot2_protocol.h:58
BT::TreeNode::config
const NodeConfig & config() const
Definition: tree_node.cpp:351
BT::NodeType::DECORATOR
@ DECORATOR
BT::NodeType::CONDITION
@ CONDITION
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
BT::Tree
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:91
BT::NodeType::SUBTREE
@ SUBTREE
BT::DecoratorNode
Definition: decorator_node.h:8
manifest
string manifest
bt_factory.h
BT::Duration
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:628
BT::PortDirection::OUTPUT
@ OUTPUT
BT::SerializeTransition
SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp, NodeStatus prev_status, NodeStatus status)
Definition: bt_flatbuffer_helper.h:142
BT::TreeNode::registrationName
const std::string & registrationName() const
registrationName is the ID used by BehaviorTreeFactory to create an instance.
Definition: tree_node.cpp:346
BT::PortDirection
PortDirection
Definition: basic_types.h:52
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:285
BT::NodeType::UNDEFINED
@ UNDEFINED
BT::NodeStatus::FAILURE
@ FAILURE
BT::Tree::rootNode
TreeNode * rootNode() const
Definition: bt_factory.cpp:579
BT::NodeStatus::SKIPPED
@ SKIPPED
BT::NodeType::ACTION
@ ACTION
BT::CreateFlatbuffersBehaviorTree
void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder &builder, const BT::Tree &tree)
Definition: bt_flatbuffer_helper.h:62
lexy::buffer
buffer(const CharT *, const CharT *) -> buffer< deduce_encoding< CharT >>
BT::PortDirection::INPUT
@ INPUT
BT::applyRecursiveVisitor
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)
Definition: behavior_tree.cpp:18
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeStatus::RUNNING
@ RUNNING
BT::NodeType::CONTROL
@ CONTROL
BT::convertToFlatbuffers
Serialization::PortDirection convertToFlatbuffers(BT::PortDirection direction)
Definition: bt_flatbuffer_helper.h:48
BT::NodeStatus::IDLE
@ IDLE
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::convertToFlatbuffers
Serialization::NodeType convertToFlatbuffers(BT::NodeType type)
Definition: bt_flatbuffer_helper.h:11


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