Refactor lidar library: rename olei_config to lidar_config, add nanoscan example and shared byte helpers

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-03 09:27:43 +07:00
parent 59880871b0
commit 323715eab0
16 changed files with 621 additions and 814 deletions

View File

@@ -1,14 +1,4 @@
// lidar_app.cpp — headless skeleton app and integration template.
//
// Loads the lidar list from config.json, opens each one through the SINGLE
// config function lidarlib::make_lidar() (no per-brand branching), then reads scans
// on one thread per lidar and prints a one-line summary. There is no web UI:
// edit config.json directly, or build your own GUI on top of this same API.
//
// What a GUI author keeps: load_config() + make_lidar() + the recv_scan() loop.
// What a GUI author replaces: the printf() with their own rendering/persistence,
// and save_config() to write edits back.
//
// Headless skeleton app: loads config.json, one reader thread per lidar.
// ./lidar_app [config.json]
#include "lidarlib/lidarlib.hpp"
#include <atomic>
@@ -23,10 +13,8 @@ namespace {
std::atomic<bool> g_running{true};
void on_signal(int) { g_running = false; }
// One reader thread per lidar. Owns the handle for its whole lifetime so the
// per-instance receive buffers never race another thread.
void run_lidar(lidarlib::LidarConfig cfg) {
std::unique_ptr<lidarlib::Lidar> lidar = lidarlib::make_lidar(cfg); // the one config call
std::unique_ptr<lidarlib::Lidar> lidar = lidarlib::make_lidar(cfg);
if (!lidar->open()) {
fprintf(stderr, "[%s] khong mo duoc %s %s:%u\n",
cfg.name.c_str(), cfg.brand.c_str(), cfg.ip.c_str(), cfg.port);
@@ -38,11 +26,9 @@ void run_lidar(lidarlib::LidarConfig cfg) {
while (g_running) {
lidarlib::ScanResult result;
if (!lidar->recv_scan(result, 1000)) continue; // timeout -> retry
if (!lidar->recv_scan(result, 1000)) continue;
// Output #1: ROS-shaped LaserScan (same for every lidar)
const lidarlib::LaserScan& scan = result.scan;
// Output #2: ExtraInfo (fields vary by model/family)
const lidarlib::ExtraInfo& info = result.info;
printf("[%s] %zu diem | ts=%u ms | model=%s | err=0x%02X\n",
@@ -57,12 +43,12 @@ void run_lidar(lidarlib::LidarConfig cfg) {
} // namespace
int main(int argc, char** argv) {
setvbuf(stdout, nullptr, _IOLBF, 0); // line-buffer so logs show promptly
setvbuf(stdout, nullptr, _IOLBF, 0);
const std::string config_path = (argc > 1) ? argv[1] : "config.json";
lidarlib::Config cfg = lidarlib::load_config(config_path);
lidarlib::save_config(config_path, cfg); // ensure the file exists & is editable
lidarlib::save_config(config_path, cfg); // ensure the file exists
if (cfg.lidars.empty()) {
fprintf(stderr, "Khong co lidar nao trong %s\n", config_path.c_str());