bt_flatbuffer_helper.h
Go to the documentation of this file.
00001 #ifndef BT_FLATBUFFER_HELPER_H
00002 #define BT_FLATBUFFER_HELPER_H
00003 
00004 #include "abstract_logger.h"
00005 #include "BT_logger_generated.h"
00006 
00007 namespace BT
00008 {
00009 inline BT_Serialization::Type convertToFlatbuffers(NodeType type)
00010 {
00011     switch (type)
00012     {
00013         case BT::NodeType::ACTION:
00014             return BT_Serialization::Type::ACTION;
00015         case BT::NodeType::DECORATOR:
00016             return BT_Serialization::Type::DECORATOR;
00017         case BT::NodeType::CONTROL:
00018             return BT_Serialization::Type::CONTROL;
00019         case BT::NodeType::CONDITION:
00020             return BT_Serialization::Type::CONDITION;
00021         case BT::NodeType::SUBTREE:
00022             return BT_Serialization::Type::SUBTREE;
00023         case BT::NodeType::UNDEFINED:
00024             return BT_Serialization::Type::UNDEFINED;
00025     }
00026     return BT_Serialization::Type::UNDEFINED;
00027 }
00028 
00029 inline BT_Serialization::Status convertToFlatbuffers(NodeStatus type)
00030 {
00031     switch (type)
00032     {
00033         case BT::NodeStatus::IDLE:
00034             return BT_Serialization::Status::IDLE;
00035         case BT::NodeStatus::SUCCESS:
00036             return BT_Serialization::Status::SUCCESS;
00037         case BT::NodeStatus::RUNNING:
00038             return BT_Serialization::Status::RUNNING;
00039         case BT::NodeStatus::FAILURE:
00040             return BT_Serialization::Status::FAILURE;
00041     }
00042     return BT_Serialization::Status::IDLE;
00043 }
00044 
00045 inline void CreateFlatbuffersBehaviorTree(flatbuffers::FlatBufferBuilder& builder,
00046                                           BT::TreeNode* root_node)
00047 {
00048     std::vector<flatbuffers::Offset<BT_Serialization::TreeNode>> fb_nodes;
00049 
00050     applyRecursiveVisitor(root_node, [&](BT::TreeNode* node) {
00051         std::vector<uint16_t> children_uid;
00052         if (auto control = dynamic_cast<BT::ControlNode*>(node))
00053         {
00054             children_uid.reserve(control->children().size());
00055             for (const auto& child : control->children())
00056             {
00057                 children_uid.push_back(child->UID());
00058             }
00059         }
00060         else if (auto decorator = dynamic_cast<BT::DecoratorNode*>(node))
00061         {
00062             const auto& child = decorator->child();
00063             children_uid.push_back(child->UID());
00064         }
00065 
00066         std::vector<flatbuffers::Offset<BT_Serialization::KeyValue>> params;
00067         const NodeParameters& init_params = node->initializationParameters();
00068         for (const auto& it : init_params)
00069         {
00070             params.push_back(BT_Serialization::CreateKeyValueDirect(builder, it.first.c_str(),
00071                                                                     it.second.c_str()));
00072         }
00073 
00074         auto tn = BT_Serialization::CreateTreeNode(
00075             builder, node->UID(), builder.CreateVector(children_uid),
00076             convertToFlatbuffers(node->type()), convertToFlatbuffers(node->status()),
00077             builder.CreateString(node->name().c_str()),
00078             builder.CreateString(node->registrationName().c_str()), builder.CreateVector(params));
00079 
00080         fb_nodes.push_back(tn);
00081     });
00082 
00083     auto behavior_tree = BT_Serialization::CreateBehaviorTree(builder, root_node->UID(),
00084                                                               builder.CreateVector(fb_nodes));
00085 
00086     builder.Finish(behavior_tree);
00087 }
00088 
00092 inline SerializedTransition SerializeTransition(uint16_t UID, Duration timestamp,
00093                                                    NodeStatus prev_status, NodeStatus status)
00094 {
00095     using namespace std::chrono;
00096     SerializedTransition buffer;
00097     auto usec = duration_cast<microseconds>(timestamp).count();
00098     uint32_t t_sec = usec / 1000000;
00099     uint32_t t_usec = usec % 1000000;
00100 
00101     flatbuffers::WriteScalar(&buffer[0], t_sec);
00102     flatbuffers::WriteScalar(&buffer[4], t_usec);
00103     flatbuffers::WriteScalar(&buffer[8], UID);
00104 
00105     flatbuffers::WriteScalar(&buffer[10], static_cast<int8_t>(convertToFlatbuffers(prev_status)));
00106     flatbuffers::WriteScalar(&buffer[11], static_cast<int8_t>(convertToFlatbuffers(status)));
00107 
00108     return buffer;
00109 }
00110 
00111 }   // end namespace
00112 
00113 #endif   // BT_FLATBUFFER_HELPER_H


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:09