282 lines
10 KiB
C++
282 lines
10 KiB
C++
// Driver LakiBeam 1L -- LOP BOC quanh `lidar_core` cua base_kit.
|
|
//
|
|
// ================== VI SAO BOC CHU KHONG TU GIAI MA ==================
|
|
// Ban truoc cua file nay tu giai ma goi UDP 1206 byte. No chay duoc, nhung
|
|
// ba lan lien tiep lo ra nhung chi tiet chi ban SDK moi biet:
|
|
//
|
|
// 1. Hang magic: byte tren day la `ff ee`, doc little-endian ra 0xEEFF chu
|
|
// khong phai 0xFFEE. Moi goi bi tu choi cho toi khi sua.
|
|
// 2. Mot block trong moi goi deu dan khong co header -- chua bao gio tim ra
|
|
// vi sao.
|
|
// 3. Thiet bi gui ca vong quet thanh MOT CHUM roi nghi ~30 ms. Do khoang
|
|
// trong chum ra "sweep 0.9 ms" tuc 1100 Hz, va scan_time sai ~30 lan
|
|
// mot cach hoan toan im lang.
|
|
//
|
|
// `lidar_core` + `lakibeam_sdk` la ban driver CHUAN dang chay that tren robot
|
|
// nay, va ba chuyen tren no da xu ly het. Nhung vao repo (xem
|
|
// thirdparty/PROVENANCE.md) va boc lai re hon nhieu so voi tiep tuc do lai
|
|
// tung chi tiet cua mot dinh dang da co nguoi giai xong.
|
|
//
|
|
// ================== PHAN CON LAI CUA FILE NAY ==================
|
|
// Chi lam ba viec:
|
|
// * doi LidarConfig cua vslam sang LakibeamConfig cua lidar_core
|
|
// * doi lidar_core::LaserScan sang vslam::LaserScan
|
|
// * dong dau bang Clock cua vslam, khong dung dong ho ma SDK tu lay
|
|
//
|
|
// KHONG goi ScanSanitizer: node tu ve sinh (wiring.cc) bang MOT doi tuong duy
|
|
// nhat, va ban ghi .vsr co y luu du lieu THO de doi nguong roi test lai.
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
|
|
#include "lidar_core/lakibeam_driver.hpp"
|
|
#include "lidar_core/scan_filter.hpp"
|
|
|
|
#include "vslam/clock.h"
|
|
#include "vslam/driver_config.h"
|
|
#include "vslam/driver_interfaces.h"
|
|
#include "vslam/log.h"
|
|
|
|
namespace vslam {
|
|
namespace {
|
|
|
|
class LakiBeamWrapper : public LidarInterface {
|
|
public:
|
|
LakiBeamWrapper(const LidarConfig& cfg, ClockPtr clock)
|
|
: cfg_(cfg), clock_(std::move(clock)) {}
|
|
|
|
~LakiBeamWrapper() override { Close(); }
|
|
|
|
void SetCallback(ScanCallback cb) override { cb_ = std::move(cb); }
|
|
|
|
StatusResponse Open() override {
|
|
if (impl_) {
|
|
return StatusResponse::Error(StatusCode::kFailedPrecondition,
|
|
"lidar: da mo roi");
|
|
}
|
|
if (!clock_) {
|
|
return StatusResponse::Error(StatusCode::kInvalidArgument,
|
|
"lidar: thieu clock");
|
|
}
|
|
|
|
lidar_core::LakibeamConfig lc;
|
|
lc.host_ip = cfg_.host_ip;
|
|
lc.host_port = std::to_string(cfg_.port);
|
|
lc.lidar_port = std::to_string(cfg_.port);
|
|
lc.frame_id = cfg_.frame_id;
|
|
lc.range_min = static_cast<float>(cfg_.sanitizer.range_min);
|
|
lc.range_max = static_cast<float>(cfg_.sanitizer.range_max);
|
|
|
|
// Quy uoc goc. Manual §8.1 cho X = R*sin(a), Y = R*cos(a); REP-103 dung
|
|
// x = R*cos(t) => t = 90do - a. LiDAR tren robot nay GAN UP NGUOC, tuc
|
|
// xoay 180do quanh truc X (Y doi dau), nen thanh t = a - 90do.
|
|
//
|
|
// lidar_core tinh: t = (angle_flip ? -a : a) + angle_offset_deg
|
|
// Voi gan up nguoc: angle_flip = false, offset = -90.
|
|
lc.angle_offset_deg = cfg_.angle_offset_deg;
|
|
lc.angle_flip = cfg_.inverted;
|
|
|
|
// Lay TRON 360 do, khong crop.
|
|
//
|
|
// vslam co lop cat quat rieng (ScanSanitizer + sector_masks trong
|
|
// sensors.yaml) va no la lop DUY NHAT duoc cat. Bat crop o day nua thi co
|
|
// hai cho cat doc lap, va khi mot vat can bien mat khoi scan se khong ai
|
|
// biet cho nao cat no.
|
|
lc.crop_fov = false;
|
|
|
|
// Bon bo loc chay TREN scan tho, truoc khi doi sang kieu cua vslam.
|
|
//
|
|
// Chung xu ly loai rac ma ScanSanitizer khong bat duoc: ScanSanitizer chi
|
|
// cat QUAT CO DINH, con tia keo dai sau cot chong / tan xa yeu / cum diem
|
|
// don le thi khong nam gon trong quat nao.
|
|
//
|
|
// Box filter cung la thu thay the dung dan cho `sector_masks`: LiDAR nam
|
|
// o TAM robot nen vung bi khung gan che khong con la mot quat goc ma la
|
|
// mot vung hinh hop quanh goc toa do.
|
|
{
|
|
lidar_core::ScanFilterConfig fc;
|
|
const LidarFilterConfig& f = cfg_.filter;
|
|
fc.shadow_filter_enabled = f.shadow_enabled;
|
|
fc.shadow_min_angle_deg = f.shadow_min_angle_deg;
|
|
fc.shadow_window = f.shadow_window;
|
|
fc.shadow_min_range_diff = f.shadow_min_range_diff;
|
|
fc.shadow_max_range = f.shadow_max_range;
|
|
fc.shadow_clear_start = f.shadow_clear_start;
|
|
fc.shadow_clear_neighbors = f.shadow_clear_neighbors;
|
|
fc.intensity_filter_enabled = f.intensity_enabled;
|
|
fc.intensity_min = static_cast<float>(f.intensity_min);
|
|
fc.intensity_max = static_cast<float>(f.intensity_max);
|
|
fc.intensity_filter_max_range = static_cast<float>(f.intensity_max_range);
|
|
fc.isolated_filter_enabled = f.isolated_enabled;
|
|
fc.isolated_min_cluster_points = f.isolated_min_cluster_points;
|
|
fc.isolated_max_range_jump = f.isolated_max_range_jump;
|
|
fc.isolated_max_range = f.isolated_max_range;
|
|
fc.box_filter_enabled = f.box_enabled;
|
|
fc.box_min_x = f.box_min_x;
|
|
fc.box_max_x = f.box_max_x;
|
|
fc.box_min_y = f.box_min_y;
|
|
fc.box_max_y = f.box_max_y;
|
|
filter_ = std::make_unique<lidar_core::ScanFilter>(fc);
|
|
VSLAM_INFO("lidar: bo loc shadow=" << f.shadow_enabled
|
|
<< " intensity=" << f.intensity_enabled
|
|
<< " isolated=" << f.isolated_enabled
|
|
<< " box=" << f.box_enabled);
|
|
}
|
|
|
|
impl_ = std::make_unique<lidar_core::LakiBeamDriver>(lc);
|
|
impl_->setCallback([this](const lidar_core::LaserScan& s) { OnScan(s); });
|
|
|
|
if (!impl_->start()) {
|
|
impl_.reset();
|
|
return StatusResponse::Error(
|
|
StatusCode::kUnavailable,
|
|
"lidar: lidar_core start() that bai tren " + cfg_.host_ip + ":" +
|
|
std::to_string(cfg_.port) +
|
|
" -- co tien trinh khac dang giu port 2368 khong? "
|
|
"(lakibeam_ros cua base_kit chang han)");
|
|
}
|
|
|
|
VSLAM_INFO("lidar: lidar_core tren " << cfg_.host_ip << ":" << cfg_.port
|
|
<< " angle_offset=" << lc.angle_offset_deg
|
|
<< " do flip=" << (lc.angle_flip ? "true" : "false")
|
|
<< " range=[" << lc.range_min << ", " << lc.range_max << "] m");
|
|
return StatusResponse::Ok();
|
|
}
|
|
|
|
void Close() override {
|
|
if (!impl_) return;
|
|
impl_->stop();
|
|
impl_.reset();
|
|
filter_.reset();
|
|
}
|
|
|
|
DriverStats Stats() const override {
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
return stats_;
|
|
}
|
|
|
|
private:
|
|
void OnScan(const lidar_core::LaserScan& raw) {
|
|
// Loc TRUOC khi doi kieu. ScanFilter thay diem rac bang +Inf, khong xoa
|
|
// phan tu, nen chi so tia va goc giu nguyen.
|
|
lidar_core::LaserScan in = raw;
|
|
if (filter_) filter_->filter(in);
|
|
|
|
auto out = std::make_shared<LaserScan>();
|
|
out->header.seq = seq_++;
|
|
out->header.frame_id = cfg_.frame_id;
|
|
|
|
// ⚠️ Dong dau bang Clock cua vslam, KHONG dung in.header.stamp_*.
|
|
// lidar_core lay thang system_clock; vslam bat buoc moi stage di qua
|
|
// Clock& (xem clock.h) vi do la thu giu cho replay tat dinh. Mot cho goi
|
|
// dong ho he thong truc tiep la mat tinh tat dinh, va khong sua duoc
|
|
// bang cach sua rieng replay.
|
|
UnixTime stamp = clock_->Now();
|
|
if (cfg_.stamp_offset_sec != 0.0) {
|
|
stamp = stamp + Duration::fromSec(cfg_.stamp_offset_sec);
|
|
}
|
|
out->header.stamp = stamp;
|
|
|
|
out->angle_min = in.angle_min;
|
|
out->angle_max = in.angle_max;
|
|
out->angle_increment = in.angle_increment;
|
|
out->time_increment = in.time_increment;
|
|
out->scan_time = in.scan_time;
|
|
out->range_min = in.range_min;
|
|
out->range_max = in.range_max;
|
|
out->ranges = in.ranges;
|
|
out->intensities = in.intensities;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lk(mu_);
|
|
++stats_.frames;
|
|
stats_.connected = true;
|
|
if (in.scan_time > 0.0f) stats_.hz = 1.0 / in.scan_time;
|
|
|
|
// O trong = mat goi UDP. Dem tia huu han thay vi tin mot bo dem goi nao.
|
|
const size_t finite = static_cast<size_t>(std::count_if(
|
|
in.ranges.begin(), in.ranges.end(),
|
|
[](float r) { return std::isfinite(r); }));
|
|
if (!in.ranges.empty() && finite * 2 < in.ranges.size()) {
|
|
++stats_.short_sweeps;
|
|
}
|
|
}
|
|
|
|
ReportBlindSector(in);
|
|
|
|
if (cb_) cb_(out);
|
|
}
|
|
|
|
// Tim quat MU rong nhat va bao no nam o dau.
|
|
//
|
|
// LakiBeam 1L co FOV 270 do, tuc mot quat mu 90 do co dinh theo THIET BI.
|
|
// Gan dung thi quat do phai nam phia SAU robot, quanh theta = 180 do.
|
|
//
|
|
// Day la cach do huong lap DUY NHAT khong can dat vat moc, khong can nhin
|
|
// anh, va khong the cai nhau: quat mu la mot dac tinh vat ly cua thiet bi,
|
|
// no o dau thi goc lap lech bay nhieu. Neu tam quat mu bao 90 do thay vi
|
|
// 180 do thi angle_offset_deg dang lech dung 90 do.
|
|
//
|
|
// Thay cho `vslam_scan_check` -- cong cu ma sensors.yaml nhac toi nhung
|
|
// chua ai viet.
|
|
void ReportBlindSector(const lidar_core::LaserScan& in) {
|
|
const size_t n = in.ranges.size();
|
|
if (n < 8 || in.angle_increment <= 0.0f) return;
|
|
|
|
// Quet vong (2n) de mot quat vat qua moc 0 van duoc dem lien mach.
|
|
size_t best_len = 0, best_end = 0, cur = 0;
|
|
for (size_t k = 0; k < 2 * n; ++k) {
|
|
if (std::isfinite(in.ranges[k % n])) {
|
|
cur = 0;
|
|
} else if (++cur > best_len) {
|
|
best_len = cur;
|
|
best_end = k;
|
|
}
|
|
}
|
|
if (best_len == 0 || best_len >= n) return;
|
|
|
|
const double inc = in.angle_increment;
|
|
const double mid_idx =
|
|
static_cast<double>(best_end) - static_cast<double>(best_len - 1) / 2.0;
|
|
double mid_deg = (static_cast<double>(in.angle_min) +
|
|
std::fmod(mid_idx, static_cast<double>(n)) * inc) *
|
|
180.0 / M_PI;
|
|
while (mid_deg > 180.0) mid_deg -= 360.0;
|
|
while (mid_deg < -180.0) mid_deg += 360.0;
|
|
const double width_deg = static_cast<double>(best_len) * inc * 180.0 / M_PI;
|
|
|
|
// 180 do = phia sau = dung. Sai lech chinh la so phai cong vao offset.
|
|
double err = 180.0 - mid_deg;
|
|
while (err > 180.0) err -= 360.0;
|
|
while (err < -180.0) err += 360.0;
|
|
|
|
VSLAM_INFO_THROTTLE(10.0,
|
|
"lidar: quat mu rong " << width_deg << " do, tam o " << mid_deg
|
|
<< " do. Dung ra phai o 180 do (phia sau). Lech " << err
|
|
<< " do -> thu angle_offset_deg = "
|
|
<< (cfg_.angle_offset_deg + err) << " trong sensors.yaml.");
|
|
}
|
|
|
|
LidarConfig cfg_;
|
|
ClockPtr clock_;
|
|
ScanCallback cb_;
|
|
std::unique_ptr<lidar_core::LakiBeamDriver> impl_;
|
|
std::unique_ptr<lidar_core::ScanFilter> filter_;
|
|
uint32_t seq_ = 0;
|
|
|
|
mutable std::mutex mu_;
|
|
DriverStats stats_;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
std::unique_ptr<LidarInterface> CreateLakiBeamDriver(const LidarConfig& cfg,
|
|
ClockPtr clock) {
|
|
return std::make_unique<LakiBeamWrapper>(cfg, std::move(clock));
|
|
}
|
|
|
|
} // namespace vslam
|