livox_mid360_core
C++ driver library for the Livox Mid-360 LiDAR
Loading...
Searching...
No Matches
frame.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
5#pragma once
6
7#include <chrono>
8#include <condition_variable>
9#include <cstddef>
10#include <cstdint>
11#include <deque>
12#include <mutex>
13#include <optional>
14#include <span>
15#include <utility>
16#include <vector>
17
19#include "livox/mid360/keys.hpp"
21#include "livox/mid360/tag.hpp"
22
23LIVOX_MID360_API_BEGIN
24namespace livox::mid360
25{
26
29struct Point
30{
31 float x = 0;
32 float y = 0;
33 float z = 0;
34 std::uint8_t reflectivity = 0;
35 std::uint8_t tag = 0;
36 std::uint8_t line = 0;
37 std::uint32_t offset_ns = 0;
38
40 [[nodiscard]] constexpr TagInfo tag_info() const noexcept { return decode_tag(tag); }
42 [[nodiscard]] constexpr TagConfidence adjacent_glue() const noexcept
43 {
45 }
46
47 [[nodiscard]] constexpr TagConfidence particles() const noexcept
48 {
49 return decode_tag(tag).particles;
50 }
51
52 [[nodiscard]] constexpr TagConfidence other() const noexcept { return decode_tag(tag).other; }
54 [[nodiscard]] constexpr bool is_noise(
55 TagConfidence worst_accepted = TagConfidence::kHigh) const noexcept
56 {
57 return livox::mid360::is_noise(tag, worst_accepted);
58 }
59};
60
62struct Frame
63{
64 std::uint32_t index = 0;
65 std::uint64_t base_time_ns = 0;
66 std::uint64_t end_time_ns = 0;
67 std::vector<Point> points;
68 std::uint32_t packets = 0;
69 std::uint32_t dropped_packets = 0;
70 std::uint8_t frame_cnt = 0;
73};
74
77struct ImuData
78{
79 std::uint64_t time_ns = 0;
81};
82
87{
89 enum class Mode : std::uint8_t
90 {
91 kFrameCounter,
92 kTimeWindow,
93 };
97 std::chrono::nanoseconds window{std::chrono::milliseconds{100}};
98};
99
101enum class TimestampPolicy : std::uint8_t
102{
106};
107
115template <class T>
117{
118public:
120 explicit BoundedQueue(std::size_t capacity = 8) : capacity_(capacity) {}
121
123 void push(T && item)
124 {
125 {
126 std::lock_guard lock(mutex_);
127 if (closed_) {
128 return;
129 }
130 if (items_.size() >= capacity_) {
131 items_.pop_front();
132 ++dropped_;
133 }
134 items_.push_back(std::move(item));
135 }
136 cv_.notify_one();
137 }
138
140 [[nodiscard]] std::optional<T> pop(std::chrono::nanoseconds timeout)
141 {
142 std::unique_lock lock(mutex_);
143 cv_.wait_for(lock, timeout, [this] { return closed_ || !items_.empty(); });
144 return take();
145 }
146
148 [[nodiscard]] std::optional<T> try_pop()
149 {
150 std::lock_guard lock(mutex_);
151 return take();
152 }
153
155 void close()
156 {
157 {
158 std::lock_guard lock(mutex_);
159 closed_ = true;
160 }
161 cv_.notify_all();
162 }
163
165 [[nodiscard]] std::uint64_t dropped() const
166 {
167 std::lock_guard lock(mutex_);
168 return dropped_;
169 }
170
171 [[nodiscard]] std::size_t size() const
172 {
173 std::lock_guard lock(mutex_);
174 return items_.size();
175 }
176
177 [[nodiscard]] std::size_t capacity() const noexcept { return capacity_; }
178
179private:
180 std::optional<T> take()
181 {
182 if (items_.empty()) {
183 return std::nullopt;
184 }
185 std::optional<T> out{std::move(items_.front())};
186 items_.pop_front();
187 return out;
188 }
189
190 std::size_t capacity_;
191 mutable std::mutex mutex_;
192 std::condition_variable cv_;
193 std::deque<T> items_;
194 std::uint64_t dropped_ = 0;
195 bool closed_ = false;
196};
197
202{
203 float r[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
204 float t[3] = {0, 0, 0};
205};
206
209[[nodiscard]] Extrinsic extrinsic_from(const InstallAttitude & a) noexcept;
212void apply(const Extrinsic & e, std::span<Point> points) noexcept;
214void apply(const Extrinsic & e, Frame & frame) noexcept;
215
216} // namespace livox::mid360
217LIVOX_MID360_API_END
std::size_t size() const
Items currently queued.
Definition frame.hpp:171
std::uint64_t dropped() const
Items discarded by overflow so far.
Definition frame.hpp:165
void push(T &&item)
Producer side (receive thread). Never blocks.
Definition frame.hpp:123
std::optional< T > try_pop()
Consumer side without waiting. Empty when nothing is queued.
Definition frame.hpp:148
std::optional< T > pop(std::chrono::nanoseconds timeout)
Consumer side. Empty after timeout or once closed and drained.
Definition frame.hpp:140
BoundedQueue(std::size_t capacity=8)
An empty, open queue that holds at most capacity items.
Definition frame.hpp:120
std::size_t capacity() const noexcept
Maximum number of queued items, as passed to the constructor.
Definition frame.hpp:177
void close()
Wakes every waiting pop(); later pushes are ignored, queued items can still be popped.
Definition frame.hpp:155
Symbol visibility helpers.
Key definitions and typed encoders/decoders for the Mid-360 key-value list (section "0x0102 LiDAR Inf...
The livox_mid360_core library: everything declared by the public headers.
Definition bytes.hpp:14
TimeType
Clock source of a data packet timestamp (time_type of the header).
Definition protocol.hpp:134
@ kNoSync
timestamp counts from LiDAR power-on
Definition protocol.hpp:135
Extrinsic extrinsic_from(const InstallAttitude &a) noexcept
Rotation = Rz(yaw) * Ry(pitch) * Rx(roll) (intrinsic ZYX, right-handed, degrees); translation = offse...
TagConfidence
Confidence that a point is a normal return for one tag category; higher = more likely noise.
Definition tag.hpp:17
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
constexpr TagInfo decode_tag(std::uint8_t tag) noexcept
Splits a raw tag byte into its four fields.
Definition tag.hpp:40
void apply(const Extrinsic &e, std::span< Point > points) noexcept
Transforms every point in place; reflectivity, tag, line and offset are untouched.
TimestampPolicy
How point/IMU timestamps are produced from the packet timestamp (#6).
Definition frame.hpp:102
@ kHostOffsetOnce
default: LiDAR time + (host - LiDAR) measured once at the first packet
Definition frame.hpp:104
@ kLidar
packet timestamp as is (use with PTP / GPS synchronisation)
Definition frame.hpp:103
@ kHostReceive
kernel receive time of the packet
Definition frame.hpp:105
constexpr bool is_noise(std::uint8_t tag, TagConfidence worst_accepted=TagConfidence::kHigh) noexcept
True when the adjacent-glue or particle confidence is worse than worst_accepted; the other and reserv...
Definition tag.hpp:49
Pure-function protocol layer for the Livox Mid-360: framing, parsing and serialisation of control-com...
Host-side install attitude transform (issue #51).
Definition frame.hpp:202
float t[3]
metres
Definition frame.hpp:204
float r[3][3]
row-major rotation
Definition frame.hpp:203
How a Device cuts the packet stream into Frames (#6).
Definition frame.hpp:87
std::chrono::nanoseconds window
kTimeWindow's period.
Definition frame.hpp:97
Mode
What closes a frame besides the conditions common to both modes.
Definition frame.hpp:90
@ kFrameCounter
close on a change of header frame_cnt; a jump still closes one frame
Definition frame.hpp:91
Mode mode
default: follow the header frame_cnt
Definition frame.hpp:94
A group of points closed by FramePolicy. Owns its storage; delivered by value.
Definition frame.hpp:63
DataType source_type
header data_type; one format per Frame
Definition frame.hpp:71
std::vector< Point > points
in packet order
Definition frame.hpp:67
std::uint32_t index
+1 per delivered frame, per Device (Lvx2Player: per device)
Definition frame.hpp:64
std::uint32_t dropped_packets
udp_cnt gaps observed while assembling it
Definition frame.hpp:69
std::uint64_t end_time_ns
time of the last point
Definition frame.hpp:66
std::uint32_t packets
data packets merged into this frame
Definition frame.hpp:68
TimeType time_type
header time_type; one time base per Frame
Definition frame.hpp:72
std::uint8_t frame_cnt
header frame_cnt of the first packet
Definition frame.hpp:70
std::uint64_t base_time_ns
time of the first point (after timestamp policy)
Definition frame.hpp:65
One IMU packet (the LiDAR sends one sample per packet at 200 Hz) with its time.
Definition frame.hpp:78
ImuSample sample
the decoded packet sample
Definition frame.hpp:80
std::uint64_t time_ns
after timestamp policy
Definition frame.hpp:79
The sample of an IMU packet (data type 0).
Definition protocol.hpp:321
Key 0x0012: mounting rotation (degrees) and offset (millimetres) of the LiDAR.
Definition keys.hpp:113
One return, same semantics as livox_ros_driver2's CustomPoint.
Definition frame.hpp:30
std::uint8_t line
Mid-360 has no physical lines: sample index % 4.
Definition frame.hpp:36
constexpr TagConfidence adjacent_glue() const noexcept
Tag bit 0-1: glue points between adjacent objects.
Definition frame.hpp:42
std::uint32_t offset_ns
sample time - Frame::base_time_ns
Definition frame.hpp:37
constexpr TagConfidence other() const noexcept
Tag bit 4-5: other properties.
Definition frame.hpp:52
float y
metres, LiDAR frame
Definition frame.hpp:32
std::uint8_t tag
raw tag byte; decoded by the accessors below (#34)
Definition frame.hpp:35
constexpr TagInfo tag_info() const noexcept
All four fields of the tag byte.
Definition frame.hpp:40
constexpr bool is_noise(TagConfidence worst_accepted=TagConfidence::kHigh) const noexcept
Glue or particle confidence worse than worst_accepted; see is_noise(uint8_t, ...).
Definition frame.hpp:54
float x
metres, LiDAR frame
Definition frame.hpp:31
float z
metres, LiDAR frame
Definition frame.hpp:33
constexpr TagConfidence particles() const noexcept
Tag bit 2-3: rain, fog, dust.
Definition frame.hpp:47
std::uint8_t reflectivity
0-255
Definition frame.hpp:34
Decoded tag byte: bit 0-1 adjacent glue, 2-3 rain / fog / dust, 4-5 other, 6-7 reserved.
Definition tag.hpp:28
TagConfidence particles
bit 2-3: rain, fog, dust
Definition tag.hpp:30
TagConfidence adjacent_glue
bit 0-1: glue points between adjacent objects
Definition tag.hpp:29
TagConfidence other
bit 4-5: other properties
Definition tag.hpp:31
Point tag byte (issue #34): four 2-bit confidence fields, section "Tag Information".