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:
@@ -1,19 +1,15 @@
|
||||
// example.cpp — quick try-out of the OLEI LiDAR driver
|
||||
// OLEI driver example.
|
||||
#include "lidarlib/lidar.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
int main() {
|
||||
// ── pick a model ────────────────────────────────────────────────────────
|
||||
// lidarlib::Driver drv(lidarlib::MODEL_VF); // 2D 360°
|
||||
// lidarlib::Driver drv(lidarlib::MODEL_LR1F); // 2D 360°, 50m
|
||||
lidarlib::Driver drv(lidarlib::MODEL_VB); // 2D 270°
|
||||
lidarlib::Driver drv(lidarlib::MODEL_VB);
|
||||
|
||||
if (!drv.open()) {
|
||||
fprintf(stderr, "Không mở được socket\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// ── option 1: blocking recv ─────────────────────────────────────────────
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
lidarlib::ScanResult result;
|
||||
if (!drv.recv_scan(result, 2000)) {
|
||||
@@ -26,7 +22,6 @@ int main() {
|
||||
i, scan.ranges.size(), scan.timestamp_ms, info.error_status,
|
||||
info.detected_model.c_str());
|
||||
|
||||
// Print the first few points
|
||||
for (size_t j = 0; j < 20 && j < scan.ranges.size(); ++j) {
|
||||
float angle_deg = (scan.angle_min + j * scan.angle_increment) * 180.f / 3.14159265f;
|
||||
printf(" [%zu] angle=%.2f° dist=%.3fm intensity=%.0f\n",
|
||||
@@ -34,12 +29,6 @@ int main() {
|
||||
}
|
||||
}
|
||||
|
||||
// ── option 2: callback (your own loop) ──────────────────────────────────
|
||||
// drv.set_scan_callback([](const lidarlib::ScanResult& result) {
|
||||
// printf("Got scan: %zu pts\n", result.scan.ranges.size());
|
||||
// });
|
||||
// while (true) drv.spin_once();
|
||||
|
||||
drv.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
37
examples/nanoscan_example.cpp
Normal file
37
examples/nanoscan_example.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// SICK nanoScan3 example. The sensor's UDP output target must be configured
|
||||
// in SICK Safety Designer; this driver only binds a local UDP port.
|
||||
#include "lidarlib/sick_lidar.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
int main() {
|
||||
lidarlib::NanoScanDriver drv(lidarlib::MODEL_SICK_NANOSCAN3, "0.0.0.0", 6060);
|
||||
|
||||
if (!drv.open()) {
|
||||
fprintf(stderr, "Không mở được UDP socket cho nanoScan3\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
lidarlib::ScanResult result;
|
||||
if (!drv.recv_scan(result, 2000)) {
|
||||
fprintf(stderr, "Timeout hoặc lỗi nhận UDP datagram\n");
|
||||
break;
|
||||
}
|
||||
const lidarlib::LaserScan& scan = result.scan;
|
||||
const lidarlib::ExtraInfo& info = result.info;
|
||||
printf("Scan #%d: %zu điểm, ts=%u, model=%s\n",
|
||||
i, scan.ranges.size(), scan.timestamp_ms, info.detected_model.c_str());
|
||||
|
||||
for (size_t j = 0; j < 20 && j < scan.ranges.size(); ++j) {
|
||||
float angle_deg = (scan.angle_min + j * scan.angle_increment) * 180.f / 3.14159265f;
|
||||
printf(" [%zu] angle=%.2f° dist=%.3fm intensity=%.0f\n",
|
||||
j, angle_deg, scan.ranges[j], scan.intensities[j]);
|
||||
}
|
||||
}
|
||||
|
||||
drv.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Build:
|
||||
// g++ -std=c++17 -O2 -Iinclude -o nanoscan_example examples/nanoscan_example.cpp src/sick_lidar.cpp
|
||||
@@ -1,7 +1,4 @@
|
||||
// sick_example.cpp — quick try-out of the SICK TiM driver (SOPAS/CoLa-A, TCP)
|
||||
//
|
||||
// Verified against a real SICK TiM781S — see the caveat in
|
||||
// include/lidarlib/sick_lidar.hpp for exactly what was (and wasn't) confirmed.
|
||||
// SICK TiM driver example (SOPAS/CoLa-A, TCP).
|
||||
#include "lidarlib/sick_lidar.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
// test_dual.cpp — test 2 Olei lidars (front + rear) concurrently, per appsettings.json
|
||||
// Olei-front: scan_1, DeviceIp 192.168.100.11, LocalIp 192.168.100.100, DevicePort 2368
|
||||
// Olei-rear : scan_2, DeviceIp 192.168.100.12, LocalIp 192.168.100.100, DevicePort 2369
|
||||
// Test 2 Olei lidars (front + rear) concurrently.
|
||||
#include "lidarlib/lidar.hpp"
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
@@ -36,14 +34,7 @@ static void run_lidar(const char* tag, const lidarlib::ModelConfig& cfg,
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Both front and rear are Family B in practice — front's real header
|
||||
// string is "OLELR-1BS2", rear's is "OLELR-1BS5" (verified via live UDP
|
||||
// sniff), NOT the VB (Family A) model the config name suggested. With
|
||||
// MODEL_AUTO, the driver reads the real model name from the header and
|
||||
// narrows the FOV when it matches a known entry in kModelTable
|
||||
// (olei_lidar.cpp); "1BS5" matches (→ full 360°), but "1BS2" doesn't, so
|
||||
// front currently stays at the unfiltered 360° default. Call
|
||||
// drv.detected_model() to see which name was actually read.
|
||||
// Real headers (UDP sniff): front = "OLELR-1BS2", rear = "OLELR-1BS5".
|
||||
std::thread t_front(run_lidar, "front/scan_1", lidarlib::MODEL_AUTO,
|
||||
"192.168.100.100", 2368, false, 5);
|
||||
std::thread t_rear(run_lidar, "rear/scan_2", lidarlib::MODEL_AUTO,
|
||||
|
||||
Reference in New Issue
Block a user