livox_mid360_core
C++ driver library for the Livox Mid-360 LiDAR
Loading...
Searching...
No Matches
lvx2.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
6#pragma once
7
8#include <array>
9#include <cstdint>
10#include <expected>
11#include <filesystem>
12#include <functional>
13#include <memory>
14#include <optional>
15#include <span>
16#include <stop_token>
17#include <string>
18#include <string_view>
19#include <vector>
20
24
25LIVOX_MID360_API_BEGIN
26namespace livox::mid360
27{
28
31{
33 enum class Kind : std::uint8_t
34 {
35 kIo,
37 kUnsupportedDataType,
38 kBadFile,
39 };
41 int errno_value = 0;
42 std::string detail;
43};
44
47[[nodiscard]] std::string_view to_string(Lvx2Error::Kind kind) noexcept;
49[[nodiscard]] std::string to_string(const Lvx2Error & err);
50
53{
54 std::string lidar_sn;
55 std::string hub_sn;
56 std::uint32_t lidar_id = 0;
57 std::uint8_t lidar_type = 0;
58 std::uint8_t device_type = 9;
59 bool extrinsic_enable = false;
60 float roll_deg = 0;
61 float pitch_deg = 0;
62 float yaw_deg = 0;
63 float x_m = 0;
64 float y_m = 0;
65 float z_m = 0;
66};
67
70{
71 std::array<std::uint8_t, 4> version{2, 0, 0, 0};
72 std::uint32_t frame_duration_ms = 50;
73 std::uint8_t device_count = 0;
74};
75
79{
80 std::uint64_t frame_index = 0;
81 std::uint32_t lidar_id = 0;
82 std::uint8_t lidar_type = 0;
83 std::uint8_t timestamp_type = 0;
84 std::uint64_t timestamp_ns = 0;
85 std::uint16_t udp_counter = 0;
87 std::uint8_t frame_counter = 0;
88 std::span<const std::byte> points;
89
91 [[nodiscard]] DataPacketView to_data_packet_view() const noexcept;
92};
93
99class Lvx2Writer
100{
101public:
103 struct Stats
104 {
105 std::uint64_t packets = 0;
106 std::uint64_t frames = 0;
107 std::uint64_t bytes = 0;
108 std::uint64_t ignored = 0;
109 };
110
111 Lvx2Writer();
112 ~Lvx2Writer();
113 Lvx2Writer(const Lvx2Writer &) = delete;
114 Lvx2Writer & operator=(const Lvx2Writer &) = delete;
116 Lvx2Writer(Lvx2Writer &&) noexcept;
118 Lvx2Writer & operator=(Lvx2Writer &&) noexcept;
119
123 [[nodiscard]] std::expected<void, Lvx2Error> open(
124 const std::filesystem::path & path, std::span<const Lvx2DeviceInfo> devices,
125 std::uint32_t frame_duration_ms = 50);
128 [[nodiscard]] std::expected<bool, Lvx2Error> write(
129 std::size_t device_index, const DataPacketView & packet);
131 [[nodiscard]] std::expected<void, Lvx2Error> close();
133 [[nodiscard]] bool is_open() const noexcept;
135 [[nodiscard]] Stats stats() const noexcept;
136
137private:
138 struct Impl;
139 std::unique_ptr<Impl> impl_;
140};
141
144class Lvx2Reader
145{
146public:
147 Lvx2Reader();
148 ~Lvx2Reader();
149 Lvx2Reader(const Lvx2Reader &) = delete;
150 Lvx2Reader & operator=(const Lvx2Reader &) = delete;
152 Lvx2Reader(Lvx2Reader &&) noexcept;
154 Lvx2Reader & operator=(Lvx2Reader &&) noexcept;
155
158 [[nodiscard]] std::expected<void, Lvx2Error> open(const std::filesystem::path & path);
160 [[nodiscard]] const Lvx2FileHeader & header() const noexcept;
162 [[nodiscard]] const std::vector<Lvx2DeviceInfo> & devices() const noexcept;
165 [[nodiscard]] std::expected<std::optional<Lvx2Packet>, Lvx2Error> next_packet();
167 [[nodiscard]] bool truncated() const noexcept;
168
169private:
170 struct Impl;
171 std::unique_ptr<Impl> impl_;
172};
173
176{
178 double rate =
179 1.0;
180 bool loop = false;
183 std::optional<std::uint32_t> lidar_id;
184};
185
188{
189 std::uint32_t lidar_id = 0;
190 std::uint64_t packets = 0;
191 std::uint64_t frames = 0;
192 std::uint64_t points = 0;
193 std::uint64_t dropped_packets = 0;
194};
195
198{
199 std::uint64_t packets = 0;
200 std::uint64_t frames = 0;
201 std::uint64_t points = 0;
202 std::uint64_t dropped_packets = 0;
203 std::uint64_t loops = 0;
206 std::uint64_t unlisted_packets = 0;
207 std::vector<Lvx2DevicePlayStats> devices;
208};
209
218{
219public:
221 using PacketCallback = std::function<void(const Lvx2Packet &)>;
223 using DeviceFrameCallback = std::function<void(const Lvx2DeviceInfo & device, Frame &&)>;
224
226 explicit Lvx2Player(Lvx2PlayOptions options = {});
227 ~Lvx2Player();
228 Lvx2Player(const Lvx2Player &) = delete;
229 Lvx2Player & operator=(const Lvx2Player &) = delete;
232 Lvx2Player(Lvx2Player &&) noexcept;
235 Lvx2Player & operator=(Lvx2Player &&) noexcept;
236
239 [[nodiscard]] std::expected<void, Lvx2Error> open(const std::filesystem::path & path);
241 [[nodiscard]] const Lvx2FileHeader & header() const noexcept;
243 [[nodiscard]] const std::vector<Lvx2DeviceInfo> & devices() const noexcept;
249 void on_frame(std::function<void(Frame &&)> cb);
257 [[nodiscard]] std::expected<Lvx2PlayStats, Lvx2Error> run(const std::stop_token & stop = {});
258
259private:
260 struct Impl;
261 std::unique_ptr<Impl> impl_;
262};
263
264} // namespace livox::mid360
265LIVOX_MID360_API_END
Replays a file: packets go to on_packet as-is, and through a FrameAssembler per device (timestamps ar...
Definition lvx2.hpp:218
void on_packet(PacketCallback cb)
Every played package, before it goes to the frame assembler.
std::function< void(const Lvx2Packet &)> PacketCallback
Receives every played package as read from the file.
Definition lvx2.hpp:221
std::expected< void, Lvx2Error > open(const std::filesystem::path &path)
Opens the file for run().
void on_frame(std::function< void(Frame &&)> cb)
Frames without their device.
const Lvx2FileHeader & header() const noexcept
File header of the open file.
std::function< void(const Lvx2DeviceInfo &device, Frame &&)> DeviceFrameCallback
device is the entry of devices() that the frame belongs to.
Definition lvx2.hpp:223
const std::vector< Lvx2DeviceInfo > & devices() const noexcept
Device-info block of the open file.
void on_device_frame(DeviceFrameCallback cb)
Frames with the device they belong to; also called for a single device.
Lvx2Player(Lvx2PlayOptions options={})
A player that is not open yet; options apply to every run().
std::expected< Lvx2PlayStats, Lvx2Error > run(const std::stop_token &stop={})
Plays until the end of the file (once, or forever with loop) or until stop is requested; the partial ...
Lvx2Player(Lvx2Player &&) noexcept
Takes over the other player's file and callbacks; the moved-from player must not be used again.
bool truncated() const noexcept
True once the reader has hit the cut-off tail of a truncated file.
const Lvx2FileHeader & header() const noexcept
File header of the open file.
Lvx2Reader(Lvx2Reader &&) noexcept
Takes over the other reader's file; the moved-from reader must not be used again.
std::expected< void, Lvx2Error > open(const std::filesystem::path &path)
Opens path and reads the headers and the device-info block, so header() and devices() are valid after...
std::expected< std::optional< Lvx2Packet >, Lvx2Error > next_packet()
Next package in file order; nullopt at the end (or at a truncated tail).
const std::vector< Lvx2DeviceInfo > & devices() const noexcept
Device-info block of the open file.
Stats stats() const noexcept
Counters since the last open().
bool is_open() const noexcept
True between a successful open() and close().
std::expected< void, Lvx2Error > close()
Flushes the open frame and closes the file. Idempotent.
Lvx2Writer(Lvx2Writer &&) noexcept
Takes over the other writer's file; the moved-from writer must not be used again.
std::expected< bool, Lvx2Error > write(std::size_t device_index, const DataPacketView &packet)
Appends one packet for devices[device_index].
std::expected< void, Lvx2Error > open(const std::filesystem::path &path, std::span< const Lvx2DeviceInfo > devices, std::uint32_t frame_duration_ms=50)
Creates (truncates) path and writes the headers.
Symbol visibility helpers.
Public API skeleton (issue #9): output types of the device layer.
The livox_mid360_core library: everything declared by the public headers.
Definition bytes.hpp:14
DataType
Sample format of a data packet (data_type of the header).
Definition protocol.hpp:125
@ kCartesian32
int32 mm, 14 bytes/point, default
Definition protocol.hpp:127
std::string_view to_string(Event::Kind kind) noexcept
Name of an event kind, e.g. "state_changed".
@ kInvalidArgument
rejected before sending; see error_key for the offending key
Definition session.hpp:51
Pure-function protocol layer for the Livox Mid-360: framing, parsing and serialisation of control-com...
A validated, non-owning view over a data packet.
Definition protocol.hpp:256
How a Device cuts the packet stream into Frames (#6).
Definition frame.hpp:87
A group of points closed by FramePolicy. Owns its storage; delivered by value.
Definition frame.hpp:63
One entry of the lvx2 device-info block (63 bytes on disk).
Definition lvx2.hpp:53
std::string lidar_sn
at most 16 chars; zero padded
Definition lvx2.hpp:54
float x_m
extrinsic translation x in metres
Definition lvx2.hpp:63
float z_m
extrinsic translation z in metres
Definition lvx2.hpp:65
std::string hub_sn
empty for a Mid-360
Definition lvx2.hpp:55
std::uint8_t lidar_type
reserved in the spec
Definition lvx2.hpp:57
float roll_deg
extrinsic roll in degrees
Definition lvx2.hpp:60
bool extrinsic_enable
the extrinsic below is set
Definition lvx2.hpp:59
float y_m
extrinsic translation y in metres
Definition lvx2.hpp:64
std::uint32_t lidar_id
SDK2 handle: the IP as a big-endian u32 (docs/lvx2.md).
Definition lvx2.hpp:56
std::uint8_t device_type
9 = Mid-360 (10 = HAP)
Definition lvx2.hpp:58
float yaw_deg
extrinsic yaw in degrees
Definition lvx2.hpp:62
float pitch_deg
extrinsic pitch in degrees
Definition lvx2.hpp:61
The share of one played device in Lvx2PlayStats.
Definition lvx2.hpp:188
std::uint64_t packets
packages played
Definition lvx2.hpp:190
std::uint64_t points
points in the delivered frames
Definition lvx2.hpp:192
std::uint64_t frames
frames delivered
Definition lvx2.hpp:191
std::uint32_t lidar_id
Lvx2DeviceInfo::lidar_id of the device.
Definition lvx2.hpp:189
std::uint64_t dropped_packets
udp_counter gaps seen by the frame assembler
Definition lvx2.hpp:193
Error of the lvx2 writer, reader and player.
Definition lvx2.hpp:31
int errno_value
errno of a kIo error, 0 otherwise
Definition lvx2.hpp:41
std::string detail
human-readable context (path, offset, offending value)
Definition lvx2.hpp:42
Kind
Error category.
Definition lvx2.hpp:34
@ kIo
open/read/write failed, see errno_value
Definition lvx2.hpp:35
Kind kind
what went wrong
Definition lvx2.hpp:40
Public + private header of a file.
Definition lvx2.hpp:70
std::array< std::uint8_t, 4 > version
file format version; the reader needs 2.x
Definition lvx2.hpp:71
std::uint8_t device_count
entries in the device-info block
Definition lvx2.hpp:73
std::uint32_t frame_duration_ms
length of one file frame
Definition lvx2.hpp:72
One package (packet) of a frame as read from a file.
Definition lvx2.hpp:79
std::span< const std::byte > points
the packed point samples
Definition lvx2.hpp:88
std::uint8_t frame_counter
frame_cnt of the recorded packet
Definition lvx2.hpp:87
std::uint64_t frame_index
index of the file frame that holds the package
Definition lvx2.hpp:80
std::uint8_t lidar_type
reserved in the spec
Definition lvx2.hpp:82
std::uint8_t timestamp_type
TimeType value of the recorded packet.
Definition lvx2.hpp:83
std::uint32_t lidar_id
Lvx2DeviceInfo::lidar_id of the recording device.
Definition lvx2.hpp:81
std::uint16_t udp_counter
udp_cnt of the recorded packet
Definition lvx2.hpp:85
DataPacketView to_data_packet_view() const noexcept
A DataPacketView over points (dot_num from the length, time_interval 0, no CRC).
std::uint64_t timestamp_ns
timestamp of the recorded packet
Definition lvx2.hpp:84
DataType data_type
kCartesian32 / kCartesian16 only
Definition lvx2.hpp:86
Settings of an Lvx2Player.
Definition lvx2.hpp:176
double rate
playback speed relative to the recorded timestamps; 0 = as fast as possible
Definition lvx2.hpp:178
bool loop
restart at the end of the file until the stop token is set
Definition lvx2.hpp:180
FramePolicy frame_policy
how the frame assemblers close frames
Definition lvx2.hpp:177
std::optional< std::uint32_t > lidar_id
Only packages of this lidar_id, which must be one of the file's devices (open() fails with kInvalidAr...
Definition lvx2.hpp:183
Result of Lvx2Player::run(): the totals over all played devices, plus each device's share.
Definition lvx2.hpp:198
std::vector< Lvx2DevicePlayStats > devices
one per played device, in devices() order
Definition lvx2.hpp:207
std::uint64_t unlisted_packets
Packages skipped because the device info lists no device with their lidar_id (only counted when sever...
Definition lvx2.hpp:206
std::uint64_t frames
frames delivered
Definition lvx2.hpp:200
std::uint64_t loops
completed passes over the file
Definition lvx2.hpp:203
std::uint64_t points
points in the delivered frames
Definition lvx2.hpp:201
std::uint64_t dropped_packets
udp_counter gaps seen by the frame assemblers
Definition lvx2.hpp:202
std::uint64_t packets
packages played
Definition lvx2.hpp:199
Counters of the file being written.
Definition lvx2.hpp:104
std::uint64_t frames
frames flushed to disk
Definition lvx2.hpp:106
std::uint64_t bytes
file size so far (headers + flushed frames)
Definition lvx2.hpp:107
std::uint64_t packets
packages written (or buffered for the open frame)
Definition lvx2.hpp:105
std::uint64_t ignored
IMU packets seen.
Definition lvx2.hpp:108