refactor: restructure lidarlib into xlidar-driver plugin SDK
- LidarManager facade (liblidar_manager.so): dlopen plugin discovery, available_drivers map<driver_id, PluginRegistry>, create_lidar_device, config.json load/save with legacy lidarlib migration - Common LidarDriverInterface + DriverInfo/DeviceConfig plugin ABI (extern C get_driver_info / create_driver_instance) - Plugins: driver_rplidar (ported from xlocd, Slamtec SDK), driver_olei, driver_sick_code (TiM CoLa-A), driver_sick_safety (nanoScan3), driver_espe - Diagnostics extended with rplidar health + firmware; FOV filter window, range override and legacy remap window unified in DeviceConfig - Rewritten README, diagnostics doc and examples (list_drivers, example, lidar_app) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,57 +1,71 @@
|
||||
// OLEI driver example.
|
||||
#include "lidarlib/lidar.hpp"
|
||||
// Open one lidar through the manager and read 10 scans.
|
||||
// ./example <driver_id> [plugins_dir]
|
||||
// ./example olei_lidar_driver
|
||||
// ./example rplidar_c1_driver
|
||||
#include "lidar_manager.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
int main() {
|
||||
lidarlib::Driver drv(lidarlib::MODEL_VB);
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 2) {
|
||||
fprintf(stderr, "usage: %s <driver_id> [plugins_dir]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
const std::string plugins_dir = (argc > 2) ? argv[2] : "plugins";
|
||||
|
||||
lidarlib::ErrorCode err = drv.open();
|
||||
if (err != lidarlib::ErrorCode::Ok) {
|
||||
fprintf(stderr, "Không mở được socket: %s\n", lidarlib::to_string(err));
|
||||
xlidar::LidarManager manager(plugins_dir);
|
||||
manager.load_all_plugins();
|
||||
|
||||
xlidar::DeviceConfig cfg;
|
||||
cfg.name = "demo";
|
||||
cfg.driver_id = argv[1];
|
||||
// Network drivers use cfg.ip / cfg.port (0 = driver default port);
|
||||
// the rplidar driver uses cfg.serial_port / cfg.baudrate instead.
|
||||
|
||||
auto lidar = manager.create_lidar_device(cfg);
|
||||
if (!lidar) return 1;
|
||||
|
||||
xlidar::DriverInfo info = lidar->get_driver_info();
|
||||
printf("driver: %s (%s %s)\n", info.driver_id.c_str(), info.vendor.c_str(),
|
||||
info.model.c_str());
|
||||
|
||||
xlidar::ErrorCode err = lidar->open();
|
||||
if (err != xlidar::ErrorCode::Ok) {
|
||||
fprintf(stderr, "open failed: %s\n", xlidar::to_string(err));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Chờ cảm biến sẵn sàng: đã nhận được ít nhất một scan hoàn chỉnh
|
||||
// và thiết bị không báo lỗi (motor/điện áp/nhiệt độ...)
|
||||
if (!drv.wait_ready(5000)) {
|
||||
fprintf(stderr, "Cảm biến chưa sẵn sàng: %s (diag: %s)\n",
|
||||
lidarlib::to_string(drv.last_error()),
|
||||
lidarlib::to_string(drv.get_diagnostics()).c_str());
|
||||
drv.close();
|
||||
// Wait until the sensor is usable: at least one complete scan decoded
|
||||
// and the device reports no fault (motor/voltage/pollution/...).
|
||||
if (!lidar->wait_ready(5000)) {
|
||||
fprintf(stderr, "sensor not ready: %s (diag: %s)\n",
|
||||
xlidar::to_string(lidar->last_error()),
|
||||
xlidar::to_string(lidar->get_diagnostics()).c_str());
|
||||
lidar->close();
|
||||
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 packet\n");
|
||||
xlidar::ScanResult result;
|
||||
if (!lidar->recv_scan(result, 2000)) {
|
||||
fprintf(stderr, "recv_scan failed: %s\n", xlidar::to_string(lidar->last_error()));
|
||||
break;
|
||||
}
|
||||
const lidarlib::LaserScan& scan = result.scan;
|
||||
const lidarlib::ExtraInfo& info = result.info;
|
||||
printf("Scan #%d: %zu điểm, ts=%u ms, err=0x%02X, model=%s\n",
|
||||
i, scan.ranges.size(), scan.timestamp_ms, info.error_status,
|
||||
info.detected_model.c_str());
|
||||
const xlidar::LaserScan& scan = result.scan;
|
||||
printf("scan #%d: %zu points, ts=%u ms, model=%s\n",
|
||||
i, scan.ranges.size(), scan.timestamp_ms,
|
||||
result.info.detected_model.c_str());
|
||||
|
||||
// Chẩn đoán thiết bị từ scan mới nhất
|
||||
lidarlib::Diagnostics diag = drv.get_diagnostics();
|
||||
printf(" diag: %s\n", lidarlib::to_string(diag).c_str());
|
||||
if (diag.has_fault()) {
|
||||
if (diag.monitor_fault()) printf(" !! lỗi monitor/motor\n");
|
||||
if (diag.voltage_fault()) printf(" !! điện áp bất thường\n");
|
||||
if (diag.temperature_fault()) printf(" !! nhiệt độ bất thường\n");
|
||||
}
|
||||
// Device self-diagnostics from the newest scan.
|
||||
xlidar::Diagnostics diag = lidar->get_diagnostics();
|
||||
printf(" diag: %s\n", xlidar::to_string(diag).c_str());
|
||||
|
||||
for (size_t j = 0; j < 20 && j < scan.ranges.size(); ++j) {
|
||||
for (size_t j = 0; j < 5 && 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();
|
||||
lidar->close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Build:
|
||||
// g++ -std=c++17 -O2 -Iinclude -o example examples/example.cpp src/olei_lidar.cpp
|
||||
|
||||
Reference in New Issue
Block a user