bt_flatbuffer_helper.h
Go to the documentation of this file.
1 #ifndef BT_FLATBUFFER_HELPER_H
2 #define BT_FLATBUFFER_HELPER_H
3 
4 #include "abstract_logger.h"
5 #include "BT_logger_generated.h"
6 
7 namespace BT
8 {
10 {
11  switch (type)
12  {
25  }
27 }
28 
30 {
31  switch (type)
32  {
41  }
43 }
44 
46  BT::TreeNode* root_node)
47 {
48  std::vector<flatbuffers::Offset<BT_Serialization::TreeNode>> fb_nodes;
49 
50  applyRecursiveVisitor(root_node, [&](BT::TreeNode* node) {
51  std::vector<uint16_t> children_uid;
52  if (auto control = dynamic_cast<BT::ControlNode*>(node))
53  {
54  children_uid.reserve(control->children().size());
55  for (const auto& child : control->children())
56  {
57  children_uid.push_back(child->UID());
58  }
59  }
60  else if (auto decorator = dynamic_cast<BT::DecoratorNode*>(node))
61  {
62  const auto& child = decorator->child();
63  children_uid.push_back(child->UID());
64  }
65 
66  std::vector<flatbuffers::Offset<BT_Serialization::KeyValue>> params;
67  const NodeParameters& init_params = node->initializationParameters();
68  for (const auto& it : init_params)
69  {
70  params.push_back(BT_Serialization::CreateKeyValueDirect(builder, it.first.c_str(),
71  it.second.c_str()));
72  }
73 
75  builder, node->UID(), builder.CreateVector(children_uid),
77  builder.CreateString(node->name().c_str()),
78  builder.CreateString(node->registrationName().c_str()), builder.CreateVector(params));
79 
80  fb_nodes.push_back(tn);
81  });
82 
83  auto behavior_tree = BT_Serialization::CreateBehaviorTree(builder, root_node->UID(),
84  builder.CreateVector(fb_nodes));
85 
86  builder.Finish(behavior_tree);
87 }
88 
92 inline SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp,
93  NodeStatus prev_status, NodeStatus status)
94 {
95  using namespace std::chrono;
97  auto usec = duration_cast<microseconds>(timestamp).count();
98  uint32_t t_sec = usec / 1000000;
99  uint32_t t_usec = usec % 1000000;
100 
101  flatbuffers::WriteScalar(&buffer[0], t_sec);
102  flatbuffers::WriteScalar(&buffer[4], t_usec);
103  flatbuffers::WriteScalar(&buffer[8], UID);
104 
105  flatbuffers::WriteScalar(&buffer[10], static_cast<int8_t>(convertToFlatbuffers(prev_status)));
106  flatbuffers::WriteScalar(&buffer[11], static_cast<int8_t>(convertToFlatbuffers(status)));
107 
108  return buffer;
109 }
110 
111 } // end namespace
112 
113 #endif // BT_FLATBUFFER_HELPER_H
flatbuffers::Offset< KeyValue > CreateKeyValueDirect(flatbuffers::FlatBufferBuilder &_fbb, const char *key=nullptr, const char *value=nullptr)
std::array< uint8_t, 12 > SerializedTransition
virtual NodeType type() const =0
const std::string & name() const
Definition: tree_node.cpp:92
static raw_event_t * buffer
Definition: minitrace.cpp:54
const NodeParameters & initializationParameters() const
Definition: tree_node.cpp:137
void Finish(Offset< T > root, const char *file_identifier=nullptr)
Finish serializing a buffer by writing the root offset.
Definition: flatbuffers.h:1554
flatbuffers::Offset< TreeNode > CreateTreeNode(flatbuffers::FlatBufferBuilder &_fbb, uint16_t uid=0, flatbuffers::Offset< flatbuffers::Vector< uint16_t >> children_uid=0, Type type=Type::UNDEFINED, Status status=Status::IDLE, flatbuffers::Offset< flatbuffers::String > instance_name=0, flatbuffers::Offset< flatbuffers::String > registration_name=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< KeyValue >>> params=0)
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Definition: flatbuffers.h:1058
BT_Serialization::Type convertToFlatbuffers(NodeType type)
uint16_t UID() const
Definition: tree_node.cpp:108
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
Definition: flatbuffers.h:727
std::unordered_map< std::string, std::string > NodeParameters
Definition: tree_node.h:33
builder
Definition: build.py:70
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
Definition: flatbuffers.h:1198
static volatile int count
Definition: minitrace.cpp:55
flatbuffers::Offset< BehaviorTree > CreateBehaviorTree(flatbuffers::FlatBufferBuilder &_fbb, uint16_t root_uid=0, flatbuffers::Offset< flatbuffers::Vector< flatbuffers::Offset< TreeNode >>> nodes=0)
NodeStatus status() const
Definition: tree_node.cpp:75
void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder &builder, BT::TreeNode *root_node)
NodeStatus
Definition: basic_types.h:28
const std::string & registrationName() const
registrationName is the ID used by BehaviorTreeFactory to create an instance.
Definition: tree_node.cpp:132
NodeType
Definition: basic_types.h:16
std::chrono::high_resolution_clock::duration Duration
Definition: tree_node.h:36
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)
SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp, NodeStatus prev_status, NodeStatus status)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sun Feb 3 2019 03:14:32