livox_mid360_core
C++ driver library for the Livox Mid-360 LiDAR
Loading...
Searching...
No Matches
livox::mid360::Device Class Reference

One LiDAR: commands through its own Session, data through the Context it was opened on. More...

#include <livox/mid360/device.hpp>

Public Member Functions

 ~Device ()
 Unregisters from the Context (waits for an in-flight callback to return) and drops the Session.
 Device (const Device &)=delete
Device & operator= (const Device &)=delete
 Device (Device &&)=delete
Device & operator= (Device &&)=delete
void cancel () noexcept
 Interrupts the blocking command in progress from another thread (not serialised); it returns kCancelled.
Callbacks

Callbacks: one per kind, settable while sampling has not been requested (before start_sampling() or after stop_sampling()); otherwise kInvalidState.

Pass an empty function to clear. Packets arriving while no callback is set are dropped silently.

std::expected< void, DeviceError > on_packet (PacketCallback cb)
 Receive thread, once per parsed data packet, before frame assembly.
std::expected< void, DeviceError > on_frame (FrameCallback cb)
 Receive thread, once per completed Frame.
std::expected< void, DeviceError > on_imu (ImuCallback cb)
 Receive thread, once per IMU sample.
std::expected< void, DeviceError > on_event (EventCallback cb)
 Receive thread, once per Event.
std::expected< void, DeviceError > on_push (PushCallback cb)
 Receive thread, once per parsed push, after the push's kStateChanged / kHms / kDiagChanged events; the argument is the merged snapshot (missing keys carried over).
std::expected< void, DeviceError > on_firmware_log (FirmwareLogCallback cb)
 Receive thread, once per 0x0300 push including begin / end packets (#44).
std::expected< void, DeviceError > on_debug_data (DebugDataCallback cb)
 Receive thread, once per datagram on the Context's debug data socket (#93).
Commands

Commands (caller's thread, serialised, blocking; see Session for the semantics).

std::expected< void, DeviceError > start_sampling (std::optional< RequestOptions > opts=std::nullopt)
 work_tgt_mode = SAMPLING, then wait for cur_work_state (host_setup.wait_timeout).
std::expected< void, DeviceError > stop_sampling (std::optional< RequestOptions > opts=std::nullopt)
 work_tgt_mode = IDLE, then wait. A partial frame is discarded, not delivered.
std::expected< ParamConfigAck, DeviceError > configure (std::span< const KeyValue > values, std::optional< RequestOptions > opts=std::nullopt)
 0x0100 with raw key-value pairs (Session::configure()).
std::expected< InquireResult, DeviceError > inquire (std::span< const std::uint16_t > keys, std::optional< RequestOptions > opts=std::nullopt)
 0x0101 for raw key numbers (Session::inquire()). Prefer get() / get_many().
std::expected< InquireResult, DeviceError > inquire (std::span< const Key > keys, std::optional< RequestOptions > opts=std::nullopt)
 0x0101 for typed keys.
std::expected< void, DeviceError > reboot (std::optional< RequestOptions > opts=std::nullopt)
 0x0200.
Aggregated read-back

Aggregated read-back (issues #38 / #41): one 0x0101 each, not cached.

std::expected< DeviceIdentity, DeviceError > identity (std::optional< RequestOptions > opts=std::nullopt)
 Keys 0x8000-0x8005. Missing keys leave their field empty; see decode_identity().
std::expected< LidarSettings, DeviceError > settings (std::optional< RequestOptions > opts=std::nullopt)
 The 16 modelled writable keys as stored by the LiDAR; see decode_settings().
std::expected< LidarStatus, DeviceError > status (std::optional< RequestOptions > opts=std::nullopt)
 Keys 0x8006-0x8011 by inquire; see decode_status().
std::expected< DiagStatus, DeviceError > diag_status (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x800E by inquire (#55); the pushed value is pushed_status()->lidar_diag_status and changes raise Event::kDiagChanged.
Point stream

Point stream (issue #40): keys 0x0000 / 0x0001 and the host-side frame policy.

std::expected< SetResult, DeviceError > set_point_format (DataType format, std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0000.
std::expected< DataType, DeviceError > point_format (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0000 as stored by the LiDAR.
std::expected< SetResult, DeviceError > set_scan_pattern (ScanPattern pattern, std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0001.
std::expected< ScanPattern, DeviceError > scan_pattern (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0001 as stored by the LiDAR.
std::expected< void, DeviceError > set_frame_policy (const FramePolicy &policy)
 Replace DeviceOptions::frame_policy at run time (window must be > 0 → kInvalidArgument).
FramePolicy frame_policy () const
 The policy last requested (DeviceOptions::frame_policy until set_frame_policy()).
FOV

FOV (issue #39): keys 0x0015 / 0x0016 / 0x0017.

std::expected< SetResult, DeviceError > set_fov (const FovSettings &fov, std::optional< RequestOptions > opts=std::nullopt)
 One 0x0100 with the present fields of fov (the LiDAR applies all or none).
std::expected< FovSettings, DeviceError > fov (std::optional< RequestOptions > opts=std::nullopt)
 One 0x0101 for the three keys. A key the ACK lacks leaves its field empty.
Install attitude

Install attitude (issue #51): key 0x0012.

std::expected< SetResult, DeviceError > set_install_attitude (const InstallAttitude &a, std::optional< RequestOptions > opts=std::nullopt)
 install_attitude_valid() → else kInvalidArgument with key before any I/O.
std::expected< InstallAttitude, DeviceError > install_attitude (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0012 as stored by the LiDAR.
Function IO

Function IO (issue #52): key 0x0019, PPS / GPS inputs and the two outputs.

std::expected< SetResult, DeviceError > set_func_io_config (const FuncIoConfig &c, std::optional< RequestOptions > opts=std::nullopt)
 func_io_config_valid() → else kInvalidArgument with key before any I/O.
std::expected< FuncIoConfig, DeviceError > func_io_config (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0019 as stored by the LiDAR.
Stored settings

Stored settings (issues #46 / #47 / #54): keys 0x0018, 0x001C, 0x002B, 0x0026.

std::expected< SetResult, DeviceError > set_detect_mode (DetectMode mode, std::optional< RequestOptions > opts=std::nullopt)
 Thin wrappers over set<K>() / get<K>().
std::expected< DetectMode, DeviceError > detect_mode (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0018 as stored by the LiDAR.
std::expected< SetResult, DeviceError > set_imu_enabled (bool on, std::optional< RequestOptions > opts=std::nullopt)
 Key 0x001C: whether the LiDAR sends IMU data.
std::expected< bool, DeviceError > imu_enabled (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x001C as stored by the LiDAR.
std::expected< SetResult, DeviceError > set_imu_sensor_config (const ImuSensorConfig &cfg, std::optional< RequestOptions > opts=std::nullopt)
 Key 0x002B is absent on older firmware: the LiDAR then answers kLidarRejected with ret_code kParamNotSupport and error_key 0x002B (no distinct Kind).
std::expected< ImuSensorConfig, DeviceError > imu_sensor_config (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x002B as stored by the LiDAR.
std::expected< SetResult, DeviceError > set_time_filter (bool on, std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0026: with 1 the LiDAR keeps streaming through a GPS time rollback (wiki).
std::expected< bool, DeviceError > time_filter (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0026 as stored by the LiDAR.
Time synchronisation

Time synchronisation (issue #53): keys 0x8009–0x800C and command 0x0202.

std::expected< TimeSyncStatus, DeviceError > time_sync_status (std::optional< RequestOptions > opts=std::nullopt)
 One 0x0101 for the four keys; a key missing or undecodable → kDecodeFailed with key.
std::expected< void, DeviceError > set_gps_time (std::uint64_t pps_time_ns, std::optional< RequestOptions > opts=std::nullopt)
 0x0202: hands the host's GPS time of the last PPS edge to the LiDAR.
Firmware log collection

Firmware log collection (issue #44): 0x0301 on the LiDAR's log port, pushes on the Context's log socket.

Layouts follow SDK2, checked on hardware (#11).

std::expected< void, DeviceError > start_firmware_log (FirmwareLogType type=FirmwareLogType::kRealTime, std::optional< RequestOptions > opts=std::nullopt)
 Writes key 0x0009 (this host, the Context's log port) through the session, then sends 0x0301 enable from the log socket and waits for its ACK (opts: timeout / attempts, the session defaults otherwise).
std::expected< void, DeviceError > stop_firmware_log (FirmwareLogType type=FirmwareLogType::kRealTime, std::optional< RequestOptions > opts=std::nullopt)
 0x0301 disable. The destructor does not send it: the LiDAR keeps pushing.
Debug raw data collection

Debug raw data collection (issue #93): 0x0303 on the LiDAR's log port, the stream on the Context's debug data socket.

The Mid-360 streams only while sampling (#225).

std::expected< void, DeviceError > start_debug_data (std::optional< RequestOptions > opts=std::nullopt)
 Sends 0x0303 enable with this host and the Context's debug data port from the log socket and waits for its ACK (opts: timeout / attempts, the session defaults otherwise).
std::expected< void, DeviceError > stop_debug_data (std::optional< RequestOptions > opts=std::nullopt)
 0x0303 disable. The destructor does not send it: the LiDAR keeps streaming.
LiDAR network config

LiDAR network config (issue #50): key 0x0004.

std::expected< SetResult, DeviceError > set_lidar_ip_config (const LidarIpConfig &cfg, std::optional< RequestOptions > opts=std::nullopt)
 lidar_ip_config_valid() → else kInvalidArgument with key before any I/O.
std::expected< LidarIpConfig, DeviceError > lidar_ip_config (std::optional< RequestOptions > opts=std::nullopt)
 Key 0x0004 as stored by the LiDAR (the address in use until the next reboot may differ).
Typed key access

Typed key access (issue #57): key_traits<K> in keys.hpp gives each key its C++ type.

template<Key K>
requires writable_key<K>
std::expected< SetResult, DeviceError > set (const key_value_t< K > &value, std::optional< RequestOptions > opts=std::nullopt)
 One 0x0100 with the encoded value.
template<Key... Ks>
requires (sizeof...(Ks) > 0 && (writable_key<Ks> && ...))
std::expected< SetResult, DeviceError > set_many (const key_value_t< Ks > &... values)
 Several keys in one 0x0100 (the LiDAR applies all or rejects the whole request).
template<Key... Ks>
requires (sizeof...(Ks) > 0 && (writable_key<Ks> && ...))
std::expected< SetResult, DeviceError > set_many (std::optional< RequestOptions > opts, const key_value_t< Ks > &... values)
 set_many() with request options.
template<Key K>
requires typed_key<K>
std::expected< key_value_t< K >, DeviceError > get (std::optional< RequestOptions > opts=std::nullopt)
 One 0x0101 for K, decoded.
template<Key... Ks>
requires (sizeof...(Ks) > 0 && (typed_key<Ks> && ...))
std::expected< std::tuple< key_value_t< Ks >... >, DeviceError > get_many (std::optional< RequestOptions > opts=std::nullopt)
 Several keys in one 0x0101, decoded into a tuple in the order of Ks.
Connection

Connection (issue #8).

bool connected () const noexcept
 False between kDisconnected and kReconnected.
std::expected< void, DeviceError > reconnect ()
 One recovery attempt on the caller's thread (serialised with the commands): direct endpoint, then discovery; host setup and sampling replayed.
void disconnect ()
 Declares the Device disconnected (reason kUser) without touching the LiDAR; the automatic worker, when enabled, reconnects.
Observation

Observation (thread-safe snapshots).

DiscoveredDevice info () const
 Discovery record; ip / cmd_port / from follow a reconnect to a new address.
std::optional< LidarStatus > pushed_status () const
 The latest value of every status key seen in a 0x0102 push, time_ns = receive time of the last push; nullopt before the first push.
std::optional< WorkState > work_state () const
 cur_work_state from pushed_status(); nullopt before the first push.
std::array< HmsCode, 8 > hms () const
 hms_code slots from pushed_status() (all inactive before the first push).
DeviceStats stats () const
 Snapshot of the data-path counters.
SessionStats session_stats () const
 Snapshot of the command Session's counters.

Static Public Member Functions

static std::expected< std::unique_ptr< Device >, DeviceError > open (Context &context, const DiscoveredDevice &device, const DeviceOptions &opts={})
 Registers with the Context (kAlreadyRegistered when another Device has the same IP), connects the Session and applies opts.host_setup pointed at the command socket's local address (or host_setup.ip) and the Context's ports.

Detailed Description

One LiDAR: commands through its own Session, data through the Context it was opened on.

See the file description for the threading rules. Non-copyable, non-movable.

Constructor & Destructor Documentation

◆ ~Device()

livox::mid360::Device::~Device ( )

Unregisters from the Context (waits for an in-flight callback to return) and drops the Session.

The LiDAR keeps streaming; call stop_sampling() first if that matters.

Member Function Documentation

◆ open()

std::expected< std::unique_ptr< Device >, DeviceError > livox::mid360::Device::open ( Context & context,
const DiscoveredDevice & device,
const DeviceOptions & opts = {} )
staticnodiscard

Registers with the Context (kAlreadyRegistered when another Device has the same IP), connects the Session and applies opts.host_setup pointed at the command socket's local address (or host_setup.ip) and the Context's ports.

Does not change the work mode.

◆ on_debug_data()

std::expected< void, DeviceError > livox::mid360::Device::on_debug_data ( DebugDataCallback cb)

Receive thread, once per datagram on the Context's debug data socket (#93).

Allowed without that socket; the callback is then never called.

◆ start_sampling()

std::expected< void, DeviceError > livox::mid360::Device::start_sampling ( std::optional< RequestOptions > opts = std::nullopt)

work_tgt_mode = SAMPLING, then wait for cur_work_state (host_setup.wait_timeout).

Idempotent. Callbacks are frozen from the first successful call on.

◆ configure()

std::expected< ParamConfigAck, DeviceError > livox::mid360::Device::configure ( std::span< const KeyValue > values,
std::optional< RequestOptions > opts = std::nullopt )

0x0100 with raw key-value pairs (Session::configure()).

Accepted values are folded into the replayed HostSetup like the typed setters'. Prefer set() / set_many().

◆ reboot()

std::expected< void, DeviceError > livox::mid360::Device::reboot ( std::optional< RequestOptions > opts = std::nullopt)

0x0200.

Once acknowledged the Device declares itself disconnected (kRebootRequested) and, when ReconnectOptions::enabled, reconnects after the LiDAR comes back. A Mid-360 keeps running for about 1.25 s after the ACK, so the worker first waits for it to go down: a push going into ERROR, half a push_timeout without a push, or at most one push_timeout (#238). Device::reconnect() does not wait.

◆ settings()

std::expected< LidarSettings, DeviceError > livox::mid360::Device::settings ( std::optional< RequestOptions > opts = std::nullopt)

The 16 modelled writable keys as stored by the LiDAR; see decode_settings().

A key the firmware rejects with kParamNotSupport (0x0026 and 0x002B on firmware 13.18.0244) is dropped and the inquire repeated, so its optional is empty rather than the whole call failing.

◆ status()

std::expected< LidarStatus, DeviceError > livox::mid360::Device::status ( std::optional< RequestOptions > opts = std::nullopt)

Keys 0x8006-0x8011 by inquire; see decode_status().

pushed_status() has the same data from the last 0x0102 push without a round trip.

◆ set_point_format()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_point_format ( DataType format,
std::optional< RequestOptions > opts = std::nullopt )

Key 0x0000.

kImu → kInvalidArgument with key before any I/O. The receive side needs no help: the frame being assembled is closed and delivered when the first packet in the new format arrives, so every Frame has one source_type.

◆ set_scan_pattern()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_scan_pattern ( ScanPattern pattern,
std::optional< RequestOptions > opts = std::nullopt )

Key 0x0001.

Not pre-checked. A Mid-360 accepts all three patterns and restarts the motor when the value changes (MOTORSTARTUP, READY, then the target again); a value past kLowRateRepetitive is answered with kLidarRejected, ret_code 0x03.

◆ set_frame_policy()

std::expected< void, DeviceError > livox::mid360::Device::set_frame_policy ( const FramePolicy & policy)

Replace DeviceOptions::frame_policy at run time (window must be > 0 → kInvalidArgument).

Takes effect at the next data packet; the frame in progress is closed by whichever policy is active then. The base Mid-360 has no frame-rate key: the frame rate is this policy's window (or the LiDAR's frame_cnt period).

◆ set_fov()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_fov ( const FovSettings & fov,
std::optional< RequestOptions > opts = std::nullopt )

One 0x0100 with the present fields of fov (the LiDAR applies all or none).

Validated before any I/O: no field → kInvalidArgument without key; a window outside fov_in_range() (ranges or a reversed window) → kInvalidArgument with key = 0x0015 / 0x0016. HostSetup::fov does the same at open() and after a reconnect.

◆ set_install_attitude()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_install_attitude ( const InstallAttitude & a,
std::optional< RequestOptions > opts = std::nullopt )

install_attitude_valid() → else kInvalidArgument with key before any I/O.

Only stores the value on the LiDAR; the firmware does not apply it to the emitted points (13.18.0244, #11). To transform on the host use extrinsic_from() / apply() in frame.hpp.

◆ set_func_io_config()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_func_io_config ( const FuncIoConfig & c,
std::optional< RequestOptions > opts = std::nullopt )

func_io_config_valid() → else kInvalidArgument with key before any I/O.

Persisted on the LiDAR, not replayed on reconnect. IN1 = GPS is the input set_gps_time() (0x0202) complements; time_sync_status() reports the resulting synchronisation (#53).

◆ set_detect_mode()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_detect_mode ( DetectMode mode,
std::optional< RequestOptions > opts = std::nullopt )

Thin wrappers over set<K>() / get<K>().

The setters return the LiDAR's answer; a value outside its enum (DetectMode > 1, an ImuSensorConfig field past its last enumerator) is kInvalidArgument with key before any I/O. Every accepted write is folded into the replayed HostSetup, so a reconnect restores it (see set_point_format()).

◆ imu_enabled()

std::expected< bool, DeviceError > livox::mid360::Device::imu_enabled ( std::optional< RequestOptions > opts = std::nullopt)

Key 0x001C as stored by the LiDAR.

A stored 2, which a Mid-360 accepts with an unknown meaning, is kDecodeFailed with key 0x001C (#277).

◆ time_sync_status()

std::expected< TimeSyncStatus, DeviceError > livox::mid360::Device::time_sync_status ( std::optional< RequestOptions > opts = std::nullopt)

One 0x0101 for the four keys; a key missing or undecodable → kDecodeFailed with key.

The pushed copy is in pushed_status(). Diagnostics only: the data path acts on each packet's own time_type (see TimestampPolicy), never on these keys.

◆ set_gps_time()

std::expected< void, DeviceError > livox::mid360::Device::set_gps_time ( std::uint64_t pps_time_ns,
std::optional< RequestOptions > opts = std::nullopt )

0x0202: hands the host's GPS time of the last PPS edge to the LiDAR.

Not validated; read time_sync_status() back to see whether the LiDAR took it (type becomes kGps).

◆ start_firmware_log()

std::expected< void, DeviceError > livox::mid360::Device::start_firmware_log ( FirmwareLogType type = FirmwareLogType::kRealTime,
std::optional< RequestOptions > opts = std::nullopt )

Writes key 0x0009 (this host, the Context's log port) through the session, then sends 0x0301 enable from the log socket and waits for its ACK (opts: timeout / attempts, the session defaults otherwise).

Idempotent; replayed after a reconnect until a successful stop_firmware_log(). ret_code != 0 → kSession / kLidarRejected.

◆ start_debug_data()

std::expected< void, DeviceError > livox::mid360::Device::start_debug_data ( std::optional< RequestOptions > opts = std::nullopt)

Sends 0x0303 enable with this host and the Context's debug data port from the log socket and waits for its ACK (opts: timeout / attempts, the session defaults otherwise).

kInvalidState when ContextOptions::debug_data_port is not set. Does not depend on the work state. Idempotent; replayed after a reconnect until a successful stop_debug_data(). ret_code != 0 → kSession / kLidarRejected.

◆ set_lidar_ip_config()

std::expected< SetResult, DeviceError > livox::mid360::Device::set_lidar_ip_config ( const LidarIpConfig & cfg,
std::optional< RequestOptions > opts = std::nullopt )

lidar_ip_config_valid() → else kInvalidArgument with key before any I/O.

The LiDAR answers a change with ret_code 0x21 (reboot_required, passed through as is): the new address is used after reboot(). Reconnection then finds the LiDAR by serial through discovery, which also unicasts to {cfg.ip, reconnect.discovery_port}, so a subnet change that broadcast does not reach is still recovered. Not rebooted automatically.

◆ set()

template<Key K>
requires writable_key<K>
std::expected< SetResult, DeviceError > livox::mid360::Device::set ( const key_value_t< K > & value,
std::optional< RequestOptions > opts = std::nullopt )
inline

One 0x0100 with the encoded value.

Only the ACK is awaited: set<Key::kWorkTgtMode>() does not wait for the state change (start_sampling() / stop_sampling() do). Read-only keys and the unmodelled Mid-360S / 360L keys do not compile.

◆ get()

template<Key K>
requires typed_key<K>
std::expected< key_value_t< K >, DeviceError > livox::mid360::Device::get ( std::optional< RequestOptions > opts = std::nullopt)
inline

One 0x0101 for K, decoded.

kDecodeFailed (with key) when the ACK lacks the key or its value has the wrong length / an out-of-range value.

◆ cancel()

void livox::mid360::Device::cancel ( )
noexcept

Interrupts the blocking command in progress from another thread (not serialised); it returns kCancelled.

Without one in progress, the next command (or reconnect()) does. One cancel() aborts one command.

◆ reconnect()

std::expected< void, DeviceError > livox::mid360::Device::reconnect ( )

One recovery attempt on the caller's thread (serialised with the commands): direct endpoint, then discovery; host setup and sampling replayed.

No-op when connected. The automatic worker uses the same path. kSession on failure.

◆ disconnect()

void livox::mid360::Device::disconnect ( )

Declares the Device disconnected (reason kUser) without touching the LiDAR; the automatic worker, when enabled, reconnects.

Useful for tests and for forcing a re-discovery after a known network change.

◆ pushed_status()

std::optional< LidarStatus > livox::mid360::Device::pushed_status ( ) const
nodiscard

The latest value of every status key seen in a 0x0102 push, time_ns = receive time of the last push; nullopt before the first push.

A key a push omits keeps the value an earlier push carried, so a field is empty only if no push has carried it yet (#56).


The documentation for this class was generated from the following file: