FlatBuffers 使用

一、FlatBuffers root_type 至多1个

  • root_type 关键字用于指定根类型,而且只能指定一个根类型。
  • 每个 FlatBuffers schema 文件只能有一个根类型。
  • root_type 声明。这个声明指定了 FlatBuffers 文件的顶层结构,也就是文件的入口点,用于表示整个数据结构的起始点。
  • 只有根节点才会使用相关GetRoot的方法
  • 如果有union,需要判断类型再做实例
  • 观察生成python脚本,两个重要的理念就是Bytes(字节),Pos(偏移量)

二、使用示例

sudo apt install flatbuffers-compiler
flatc --python AdcBridge.fbs
pip install flatbuffers

三、python示例,网络教程比较简单,这里嵌套比较深入

namespace map.adas.flat;
table GPSLocation {
    latitude: double;
    longitude: double;
    bearing: double;
    altitude: double;
    locationType: GPSLocationType = G_NOT_VALID;
}

table NavMapLocVersion2 {
    egoLocAfterMatching: GPSLocation;
    angle: float;
    distance: int;
    roadName: string;
    usedRoadName: string;
    eRoadName: string;
    linkLength: double;
    linkId: uint64;
    speedLimit: int;
    formOfWay: int;
    matchState: int;
    roadClass: RoadClass = R_NONE;
    timestamp: uint64;
    last_route_change_timestamp: uint64;
    last_route_count: int32;
    link_remain_dist: float;
    linkIndex: int;
}

union SDMapInfo {
    nodeInfo: NodeInfoData,
    adasInfo: AdasInfoData,
    nopInfo: NopInfoData,
    mapLoc: NavMapLocVersion2,
    routeMatchRequest: RouteMatchRequest,
    routeMatchResponse: RouteMatchResponse,
    nadInfo: NADInfo,
    roadConditionInfo: RoadConditionInfo,
    resetPredictiveTree: ResetPredictiveTree,
    hdRoutesEvents: HDRoutesEvents,
    bumpyPois: BumpyPoisInfo,
    trafficLightCountdown: TrafficLightCountdownList,
}

table SDMapTable {
    requestId: int;
    data: SDMapInfo;
    engineType:int;
}

root_type SDMapTable;

编译:

flatc --python AdcBridge.fbs

因为嵌套比较多,会生成很多脚本,使用时接口需要参照生成脚本,python示例(示例仅供理解,代码无法直接使用)

from map.adas.flat import SDMapTable,NavMapLocVersion2
monster = SDMapTable.SDMapTable.GetRootAsSDMapTable(SdMapPackage_one, 0)
if monster.DataType() == SDMapInfo.SDMapInfo().mapLoc:
    mapLoc_class = NavMapLocVersion2.NavMapLocVersion2()
    mapLoc_class.Init(monster.Data().Bytes, monster.Data().Pos)
else:
    pass
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值