MCAP存储格式是一种用于机器人数据的标准化容器格式。MCAP(发音为“em-cap”),可以用于异构时间戳数据的模块化容器文件格式。它非常适用于机器人应用,因为它可以在单个文件中记录多个流的结构化和非结构化数据(例如ROS、Protobuf、JSON Schema等)。
使用MCAP存储格式优点:
- 用于在多种序列化格式(Protobuf、MessagePack、JSON等)中存储数据。
- 高性能写入。(采用追加结构,数据可以流式传输到磁盘或网络,无需回溯)
- 解码不需要额外的依赖项。(自包含消息模式)
- 高效的读取和定位。
- 能够轻松集成现有的第三方工具(如Foxglove Studio、Foxglove Data Platform等)。
这里以Protobuf数据保存作为案例:
1. 先给出一个简单的结构体, speed代表车速, acc代表加速度
#include <cstdint>
struct Head{
int id;
int index;
uint64_t timestamp;
};
struct Data{
Head head;
int speed;
float acc;
};
2. 接下来用写一个proto存储
syntax = "proto3";
message Head {
int32 id = 1;
int32 index = 2;
uint64 timestamp = 3;
}
message Data {
Head head = 1;
int32 speed = 2;
float acc = 3;
}
3. 用protobuf序列化, 这里序列化为cpp的
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: mcapTest/data.proto
#ifndef PROTOBUF_mcapTest_2fdata_2eproto__INCLUDED
#define PROTOBUF_mcapTest_2fdata_2eproto__INCLUDED
#include <string>
#include <google/protobuf/stubs/common.h>
#if GOOGLE_PROTOBUF_VERSION < 3003000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3003000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_table_driven.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
#include <google/protobuf/extension_set.h> // IWYU pragma: export
#include <google/protobuf/unknown_field_set.h>
// @@protoc_insertion_point(includes)
class Data;
class DataDefaultTypeInternal;
extern DataDefaultTypeInternal _Data_default_instance_;
class Head;
class HeadDefaultTypeInternal;
extern HeadDefaultTypeInternal _Head_default_instance_;
namespace protobuf_mcapTest_2fdata_2eproto {
// Internal implementation detail -- do not call these.
struct TableStruct {
static const ::google::protobuf::internal::ParseTableField entries[];
static const ::google::protobuf::internal::AuxillaryParseTableField aux[];
static const ::google::protobuf::internal::ParseTable schema[];
static const ::google::protobuf::uint32 offsets[];
static void InitDefaultsImpl();
static void Shutdown();
};
void AddDescriptors();
void InitDefaults();
} // namespace protobuf_mcapTest_2fdata_2eproto
// =====================