bt_flatbuffer_helper.h
Go to the documentation of this file.
1 #ifndef BT_FLATBUFFER_HELPER_H
2 #define BT_FLATBUFFER_HELPER_H
3 
5 #include "BT_logger_generated.h"
6 
7 namespace BT
8 {
9 
10 typedef std::array<uint8_t, 12> SerializedTransition;
11 
13 {
14  switch (type)
15  {
28  }
30 }
31 
33 {
34  switch (type)
35  {
44  }
46 }
47 
49 {
50  switch (direction)
51  {
58  }
60 }
61 
63  const BT::Tree& tree)
64 {
65  std::vector<flatbuffers::Offset<Serialization::TreeNode>> fb_nodes;
66 
68  {
69  std::vector<uint16_t> children_uid;
70  if (auto control = dynamic_cast<BT::ControlNode*>(node))
71  {
72  children_uid.reserve(control->children().size());
73  for (const auto& child : control->children())
74  {
75  children_uid.push_back(child->UID());
76  }
77  }
78  else if (auto decorator = dynamic_cast<BT::DecoratorNode*>(node))
79  {
80  const auto& child = decorator->child();
81  children_uid.push_back(child->UID());
82  }
83 
84  std::vector<flatbuffers::Offset<Serialization::PortConfig>> ports;
85  for (const auto& it : node->config().input_ports)
86  {
88  builder, it.first.c_str(), it.second.c_str()));
89  }
90  for (const auto& it : node->config().output_ports)
91  {
93  builder, it.first.c_str(), it.second.c_str()));
94  }
95 
97  builder,
98  node->UID(),
99  builder.CreateVector(children_uid),
100  convertToFlatbuffers(node->status()),
101  builder.CreateString(node->name().c_str()),
102  builder.CreateString(node->registrationName().c_str()),
103  builder.CreateVector(ports));
104 
105  fb_nodes.push_back(tn);
106  });
107 
108  std::vector<flatbuffers::Offset<Serialization::NodeModel>> node_models;
109 
110  for (const auto& node_it: tree.manifests)
111  {
112  const auto& manifest = node_it.second;
113  std::vector<flatbuffers::Offset<Serialization::PortModel>> port_models;
114 
115  for (const auto& port_it: manifest.ports)
116  {
117  const auto& port_name = port_it.first;
118  const auto& port = port_it.second;
119  auto port_model = Serialization::CreatePortModel(
120  builder,
121  builder.CreateString( port_name.c_str() ),
122  convertToFlatbuffers( port.direction() ),
123  builder.CreateString( demangle(port.type()).c_str() ),
124  builder.CreateString( port.description().c_str() )
125  );
126  port_models.push_back(port_model);
127  }
128 
129  auto node_model = Serialization::CreateNodeModel(
130  builder,
131  builder.CreateString(manifest.registration_ID.c_str()),
133  builder.CreateVector(port_models) );
134 
135  node_models.push_back(node_model);
136  }
137 
138  auto behavior_tree = Serialization::CreateBehaviorTree(builder, tree.root_node->UID(),
139  builder.CreateVector(fb_nodes),
140  builder.CreateVector(node_models));
141 
142  builder.Finish(behavior_tree);
143 }
144 
149  Duration timestamp,
150  NodeStatus prev_status,
151  NodeStatus status)
152 {
153  using namespace std::chrono;
155  int64_t usec = duration_cast<microseconds>(timestamp).count();
156  int64_t t_sec = usec / 1000000;
157  int64_t t_usec = usec % 1000000;
158 
159  flatbuffers::WriteScalar(&buffer[0], t_sec);
160  flatbuffers::WriteScalar(&buffer[4], t_usec);
161  flatbuffers::WriteScalar(&buffer[8], UID);
162 
163  flatbuffers::WriteScalar(&buffer[10], static_cast<int8_t>(convertToFlatbuffers(prev_status)));
164  flatbuffers::WriteScalar(&buffer[11], static_cast<int8_t>(convertToFlatbuffers(status)));
165 
166  return buffer;
167 }
168 
169 } // end namespace
170 
171 #endif // BT_FLATBUFFER_HELPER_H
flatbuffers::Offset< NodeModel > CreateNodeModel(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::String > registration_name=0, NodeType type=NodeType::UNDEFINED, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< PortModel >>> ports=0)
flatbuffers::Offset< BehaviorTree > CreateBehaviorTree(flatbuffers::FlatBufferBuilder &_fbb, uint16_t root_uid=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< TreeNode >>> nodes=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< NodeModel >>> node_models=0)
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
manifest
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
static raw_event_t * buffer
Definition: minitrace.cpp:54
flatbuffers::Offset< TreeNode > CreateTreeNode(flatbuffers::FlatBufferBuilder &_fbb, uint16_t uid=0, flatbuffers::Offset< flatbuffers::Vector< uint16_t >> children_uid=0, NodeStatus status=NodeStatus::IDLE, flatbuffers::Offset< flatbuffers::String > instance_name=0, flatbuffers::Offset< flatbuffers::String > registration_name=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< PortConfig >>> port_remaps=0)
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder &builder, const BT::Tree &tree)
std::array< uint8_t, 12 > SerializedTransition
TreeNode * root_node
Definition: bt_factory.h:80
uint16_t UID() const
Definition: tree_node.cpp:89
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
Serialization::NodeType convertToFlatbuffers(BT::NodeType type)
std::string demangle(char const *name)
Definition: demangle_util.h:78
builder
Definition: build.py:70
PortsRemapping output_ports
Definition: tree_node.h:49
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:53
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
static volatile int count
Definition: minitrace.cpp:55
flatbuffers::Offset< PortModel > CreatePortModel(flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset< flatbuffers::String > port_name=0, PortDirection direction=PortDirection::INPUT, flatbuffers::Offset< flatbuffers::String > type_info=0, flatbuffers::Offset< flatbuffers::String > description=0)
NodeStatus status() const
Definition: tree_node.cpp:56
NodeStatus
Definition: basic_types.h:35
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:78
const std::string & registrationName() const
registrationName is the ID used by BehaviorTreeFactory to create an instance.
Definition: tree_node.cpp:94
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:22
PortDirection
Definition: basic_types.h:43
std::chrono::high_resolution_clock::duration Duration
Definition: basic_types.h:339
PortsRemapping input_ports
Definition: tree_node.h:48
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)
flatbuffers::Offset< PortConfig > CreatePortConfigDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *port_name=nullptr, const char *remap=nullptr)
SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp, NodeStatus prev_status, NodeStatus status)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04