update
This commit is contained in:
@@ -1,133 +1,58 @@
|
||||
// Driver LakiBeam 1L (UDP MSOP, port 2368).
|
||||
// Driver LakiBeam 1L -- LOP BOC quanh `lidar_core` cua base_kit.
|
||||
//
|
||||
// ================== NGUON CUA MOI CON SO O DAY ==================
|
||||
// Khong con nao la doan. Hai nguon, doi chieu duoc voi nhau:
|
||||
// 1. `vslam_lidar_probe --seconds 10` tren thiet bi that (Jetson AGX)
|
||||
// 2. base_kit/src/lidar -- SDK chinh hang + driver ROS dang chay tren robot
|
||||
// nay, dan lai LakiBeam User Manual RBDOC-UM-1001
|
||||
// ================== 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:
|
||||
//
|
||||
// ================== KHUNG GOI (1206 byte CO DINH) ==================
|
||||
// 12 block x 100 byte + 6 byte duoi. Khop `udp_pack_t` cua SDK.
|
||||
// 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.
|
||||
//
|
||||
// Block (100 byte) -- `little_pack_t`:
|
||||
// +0 u16 head 0xFFEE
|
||||
// +2 u16 azimuth, LE, don vi 1/100 do
|
||||
// +4 96B 16 x `depth_t`, moi cai 6 byte:
|
||||
// u16 distance0 | u8 rssi0 | u16 distance1 | u8 rssi1
|
||||
// `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.
|
||||
//
|
||||
// ⚠️ CHI distance0/rssi0 duoc dung. SDK (LakiBeamUDP.cpp, fill_havestart)
|
||||
// khong bao gio doc distance1 -- va probe xac nhan: 3 byte thu hai cua moi
|
||||
// `depth_t` LUON bang 0 tren ban 1L. Doc ca 32 slot se nhan doi so tia bang
|
||||
// mot nua so tia rong.
|
||||
// ================== 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
|
||||
//
|
||||
// Duoi goi: +1200 u32 timestamp (us), +1204 u16 factory.
|
||||
//
|
||||
// ================== GOC: NOI SUY, KHONG DUNG BUOC CO DINH ==================
|
||||
// Azimuth chi co MOT gia tri cho ca 16 diem trong block. SDK rai 16 diem do
|
||||
// deu giua azimuth cua block NAY va azimuth cua block KE TIEP:
|
||||
// div_angle = (azimuth[i+1] - azimuth[i]) / 16
|
||||
//
|
||||
// Lam theo, khong dung buoc co dinh tu config, vi hai ly do:
|
||||
// - toc do quay khong hang so; buoc co dinh lam tia lech dan trong sweep
|
||||
// - block cuoi cua goi phai lay azimuth tu goi SAU, nen driver luon giu lai
|
||||
// mot goi va giai ma tre mot nhip. Do la ly do co `pending_`.
|
||||
//
|
||||
// ================== QUY UOC GOC + TU THE GAN ==================
|
||||
// Manual §8.1: LakiBeam tra ve X_dev = R*sin(a), Y_dev = R*cos(a)
|
||||
// REP-103: he nay dung x = R*cos(t), y = R*sin(t)
|
||||
//
|
||||
// LiDAR tren robot nay GAN UP NGUOC, tuc xoay 180do quanh truc X (Y doi dau):
|
||||
// gan xuoi -> t = 90do - a (offset +90, inverted = true)
|
||||
// gan up nguoc -> t = a - 90do (offset -90, inverted = FALSE)
|
||||
//
|
||||
// Hai phep dao dau -- cua quy uoc va cua tu the gan -- triet tieu nhau. Chi
|
||||
// tiet va canh bao "dung bu lan thu hai trong TF" nam o driver_config.h.
|
||||
//
|
||||
// Don vi range la MILIMET (scan_conversion.cpp:36 chia 1000), KHONG phai 2 mm
|
||||
// nhu Velodyne -- suy tu layout goi se ra sai dung gap doi.
|
||||
//
|
||||
// ================== VI SAO KHONG BOC SDK ==================
|
||||
// SDK keo theo boost::asio + boost::thread, va header cua no co
|
||||
// `using namespace std;` o pham vi toan cuc. Layout goi da biet het va chi
|
||||
// co bay nhieu, nen giai ma thang re hon nhieu so voi cai gia phai tra.
|
||||
// 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 <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "lidar_core/lakibeam_driver.hpp"
|
||||
|
||||
#include "vslam/clock.h"
|
||||
#include "vslam/driver_config.h"
|
||||
#include "vslam/driver_interfaces.h"
|
||||
#include "vslam/log.h"
|
||||
#include "vslam/scan_sanitizer.h"
|
||||
|
||||
namespace vslam {
|
||||
namespace {
|
||||
|
||||
constexpr size_t kPacketBytes = 1206;
|
||||
constexpr size_t kBlocks = 12;
|
||||
constexpr size_t kBlockBytes = 100;
|
||||
constexpr size_t kDepthsPerBlock = 16; // `CONFIG_BLOCK_DEPTHS` cua SDK
|
||||
constexpr size_t kDepthBytes = 6; // distance0 rssi0 distance1 rssi1
|
||||
// ⚠️ 0xEEFF, khong phai 0xFFEE.
|
||||
//
|
||||
// Tren DAY, hai byte dau moi block la `ff ee` theo dung thu tu do. Doc chung
|
||||
// bang RdLe16 (little-endian, p[0] | p[1]<<8) ra 0xEEFF. Hang so 0xFFEE la
|
||||
// cach NGUOI DOC viet chuoi byte, khong phai gia tri sau khi doc LE.
|
||||
//
|
||||
// Da mac dung loi nay: lan chay dau tren robot, MOI goi deu bi tu choi voi
|
||||
// "goi khong bat dau bang 0xFFEE" trong khi du lieu hoan toan binh thuong.
|
||||
constexpr uint16_t kBlockHead = 0xEEFF;
|
||||
constexpr uint32_t kDegreeMax = 36000; // `CONFIG_DEGREE_MAX`
|
||||
|
||||
// Coi la sang vong moi khi goc TUT lai qua nua vong. Dung `cur < prev` tran
|
||||
// se cat sweep thanh hang tram manh vun moi lan thiet bi jitter mot don vi.
|
||||
constexpr uint32_t kWrapGuard = 18000;
|
||||
|
||||
constexpr double kDeg2Rad = M_PI / 180.0;
|
||||
constexpr float kInf = std::numeric_limits<float>::infinity();
|
||||
|
||||
uint16_t RdLe16(const uint8_t* p) {
|
||||
return static_cast<uint16_t>(p[0] | (static_cast<uint16_t>(p[1]) << 8));
|
||||
}
|
||||
|
||||
double NormalizePi(double a) {
|
||||
while (a > M_PI) a -= 2.0 * M_PI;
|
||||
while (a < -M_PI) a += 2.0 * M_PI;
|
||||
return a;
|
||||
}
|
||||
|
||||
struct Point {
|
||||
double angle_rad = 0.0; // da doi sang quy uoc REP-103
|
||||
float range_m = kInf;
|
||||
float intensity = 0.0f;
|
||||
};
|
||||
|
||||
class LakiBeamDriver : public LidarInterface {
|
||||
class LakiBeamWrapper : public LidarInterface {
|
||||
public:
|
||||
LakiBeamDriver(const LidarConfig& cfg, ClockPtr clock)
|
||||
LakiBeamWrapper(const LidarConfig& cfg, ClockPtr clock)
|
||||
: cfg_(cfg), clock_(std::move(clock)) {}
|
||||
|
||||
~LakiBeamDriver() override { Close(); }
|
||||
~LakiBeamWrapper() override { Close(); }
|
||||
|
||||
void SetCallback(ScanCallback cb) override { cb_ = std::move(cb); }
|
||||
|
||||
StatusResponse Open() override {
|
||||
if (fd_ >= 0) {
|
||||
if (impl_) {
|
||||
return StatusResponse::Error(StatusCode::kFailedPrecondition,
|
||||
"lidar: da mo roi");
|
||||
}
|
||||
@@ -135,318 +60,115 @@ class LakiBeamDriver : public LidarInterface {
|
||||
return StatusResponse::Error(StatusCode::kInvalidArgument,
|
||||
"lidar: thieu clock");
|
||||
}
|
||||
if (cfg_.samples < 2) {
|
||||
return StatusResponse::Error(StatusCode::kInvalidArgument,
|
||||
"lidar: samples phai >= 2");
|
||||
}
|
||||
|
||||
fd_ = ::socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (fd_ < 0) {
|
||||
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;
|
||||
|
||||
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,
|
||||
std::string("lidar: socket() that bai: ") + std::strerror(errno));
|
||||
"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)");
|
||||
}
|
||||
|
||||
const int reuse = 1;
|
||||
::setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||
|
||||
// Timeout tren socket thay vi non-blocking + poll: vong nay khong co viec
|
||||
// gi khac lam giua hai goi, nen them mot syscall moi vong la phi ton
|
||||
// khong doi lai duoc gi.
|
||||
timeval tv{};
|
||||
tv.tv_sec = static_cast<time_t>(cfg_.recv_timeout_sec);
|
||||
tv.tv_usec = static_cast<suseconds_t>(
|
||||
(cfg_.recv_timeout_sec - static_cast<double>(tv.tv_sec)) * 1e6);
|
||||
::setsockopt(fd_, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
|
||||
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(cfg_.port);
|
||||
if (::inet_pton(AF_INET, cfg_.host_ip.c_str(), &addr.sin_addr) != 1) {
|
||||
CloseFd();
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kInvalidArgument,
|
||||
"lidar: host_ip khong hop le: " + cfg_.host_ip +
|
||||
" (day la dia chi de BIND, khong phai IP cua LiDAR)");
|
||||
}
|
||||
if (::bind(fd_, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) {
|
||||
const std::string err = std::strerror(errno);
|
||||
CloseFd();
|
||||
return StatusResponse::Error(
|
||||
StatusCode::kUnavailable,
|
||||
"lidar: bind " + cfg_.host_ip + ":" + std::to_string(cfg_.port) +
|
||||
" that bai: " + err +
|
||||
(errno == EADDRINUSE
|
||||
? " -- co node khac dang giu port 2368? (lakibeam_ros cua"
|
||||
" base_kit chang han)"
|
||||
: ""));
|
||||
}
|
||||
|
||||
VSLAM_INFO("lidar: bind " << cfg_.host_ip << ":" << cfg_.port
|
||||
<< " range_unit=" << cfg_.range_unit_m << " m/dv"
|
||||
<< " angle_offset=" << cfg_.angle_offset_deg
|
||||
<< " do inverted=" << (cfg_.inverted ? "true" : "false"));
|
||||
|
||||
Reset();
|
||||
running_ = true;
|
||||
thread_ = std::thread([this] { RecvLoop(); });
|
||||
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 {
|
||||
running_ = false;
|
||||
// Dong fd TRUOC khi join: recv() dang ket trong timeout toi 1 s va
|
||||
// shutdown() tren socket UDP khong danh thuc no tren Linux.
|
||||
CloseFd();
|
||||
if (thread_.joinable()) thread_.join();
|
||||
if (!impl_) return;
|
||||
impl_->stop();
|
||||
impl_.reset();
|
||||
}
|
||||
|
||||
DriverStats Stats() const override {
|
||||
std::lock_guard<std::mutex> lk(stats_mu_);
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
return stats_;
|
||||
}
|
||||
|
||||
private:
|
||||
void CloseFd() {
|
||||
if (fd_ >= 0) {
|
||||
::close(fd_);
|
||||
fd_ = -1;
|
||||
}
|
||||
}
|
||||
void OnScan(const lidar_core::LaserScan& in) {
|
||||
auto out = std::make_shared<LaserScan>();
|
||||
out->header.seq = seq_++;
|
||||
out->header.frame_id = cfg_.frame_id;
|
||||
|
||||
void Reset() {
|
||||
points_.clear();
|
||||
points_.reserve(cfg_.samples + kDepthsPerBlock);
|
||||
have_pending_ = false;
|
||||
sweep_start_ = UnixTime();
|
||||
sweep_end_ = UnixTime();
|
||||
}
|
||||
|
||||
void RecvLoop() {
|
||||
std::vector<uint8_t> buf(kPacketBytes + 64);
|
||||
while (running_) {
|
||||
const ssize_t n = ::recv(fd_, buf.data(), buf.size(), 0);
|
||||
if (n < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
Fail("khong nhan duoc goi nao trong " +
|
||||
std::to_string(cfg_.recv_timeout_sec) + " s");
|
||||
continue;
|
||||
}
|
||||
if (!running_) break; // Close() vua dong fd
|
||||
Fail(std::string("recv() that bai: ") + std::strerror(errno));
|
||||
continue;
|
||||
}
|
||||
if (static_cast<size_t>(n) != kPacketBytes) {
|
||||
// Kich thuoc khac la dau hieu doi firmware hoac co luong khac vao
|
||||
// cung port. Noi RO thay vi giai ma bua roi cho ra rac.
|
||||
Fail("goi dai " + std::to_string(n) + " byte, doi " +
|
||||
std::to_string(kPacketBytes));
|
||||
continue;
|
||||
}
|
||||
OnPacket(buf.data(), clock_->Now());
|
||||
}
|
||||
}
|
||||
|
||||
// Giai ma TRE MOT NHIP: block cuoi cua goi truoc can azimuth dau cua goi
|
||||
// nay de noi suy goc. Do la ly do co `pending_`.
|
||||
void OnPacket(const uint8_t* pkt, UnixTime rx) {
|
||||
if (RdLe16(pkt) != kBlockHead) {
|
||||
Fail("goi khong bat dau bang 0xFFEE");
|
||||
return;
|
||||
}
|
||||
const uint32_t first_az = RdLe16(pkt + 2);
|
||||
|
||||
if (have_pending_) {
|
||||
DecodePacket(pending_.data(), first_az, pending_rx_);
|
||||
}
|
||||
std::memcpy(pending_.data(), pkt, kPacketBytes);
|
||||
pending_rx_ = rx;
|
||||
have_pending_ = true;
|
||||
}
|
||||
|
||||
void DecodePacket(const uint8_t* pkt, uint32_t next_pkt_az, UnixTime rx) {
|
||||
for (size_t b = 0; b < kBlocks; ++b) {
|
||||
const uint8_t* blk = pkt + b * kBlockBytes;
|
||||
if (RdLe16(blk) != kBlockHead) {
|
||||
// Bo rieng block hong, giu cac block con lai: mat 16 tia de hon la
|
||||
// vut ca goi 192 tia.
|
||||
Fail("block " + std::to_string(b) + " thieu head 0xFFEE");
|
||||
continue;
|
||||
}
|
||||
|
||||
const uint32_t az = RdLe16(blk + 2);
|
||||
if (az >= kDegreeMax) {
|
||||
Fail("azimuth " + std::to_string(az) + " >= 36000");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Block cuoi lay azimuth ket tu goi sau -- dung cach SDK lam.
|
||||
const uint32_t az_next =
|
||||
(b + 1 < kBlocks) ? RdLe16(blk + kBlockBytes + 2) : next_pkt_az;
|
||||
|
||||
if (have_prev_az_ && az + kWrapGuard < prev_az_) Emit();
|
||||
prev_az_ = az;
|
||||
have_prev_az_ = true;
|
||||
|
||||
if (sweep_start_.isZero()) sweep_start_ = rx;
|
||||
sweep_end_ = rx;
|
||||
|
||||
DecodeBlock(blk, az, az_next);
|
||||
}
|
||||
}
|
||||
|
||||
void DecodeBlock(const uint8_t* blk, uint32_t az, uint32_t az_next) {
|
||||
// div_angle = (az_next - az) / 16, co xu ly quay vong -- y het SDK.
|
||||
const uint32_t span =
|
||||
(az_next >= az) ? (az_next - az) : (kDegreeMax - az + az_next);
|
||||
const double step = static_cast<double>(span) / kDepthsPerBlock;
|
||||
|
||||
for (size_t j = 0; j < kDepthsPerBlock; ++j) {
|
||||
const uint8_t* d = blk + 4 + j * kDepthBytes;
|
||||
const uint16_t dist = RdLe16(d); // distance0
|
||||
const uint8_t rssi = d[2]; // rssi0
|
||||
// distance1/rssi1 tai d[3..5] CO Y bo qua -- xem dau file.
|
||||
|
||||
double a_dev = (static_cast<double>(az) + step * static_cast<double>(j));
|
||||
if (a_dev >= kDegreeMax) a_dev -= kDegreeMax;
|
||||
a_dev *= 0.01; // 1/100 do -> do
|
||||
|
||||
// t = offset - a khi inverted, nguoc lai t = offset + a
|
||||
double t_deg = cfg_.inverted ? (cfg_.angle_offset_deg - a_dev)
|
||||
: (cfg_.angle_offset_deg + a_dev);
|
||||
|
||||
Point pt;
|
||||
pt.angle_rad = NormalizePi(t_deg * kDeg2Rad);
|
||||
// dist == 0 la KHONG CO TIENG VONG (manual §7.2), khong phai "0 met".
|
||||
// Bien no thanh 0.0 se lam robot thay vat can dan vao mat cam bien.
|
||||
pt.range_m = (dist == 0) ? kInf
|
||||
: static_cast<float>(dist * cfg_.range_unit_m);
|
||||
pt.intensity = static_cast<float>(rssi);
|
||||
points_.push_back(pt);
|
||||
}
|
||||
}
|
||||
|
||||
void Emit() {
|
||||
if (points_.empty()) {
|
||||
Reset();
|
||||
return;
|
||||
}
|
||||
|
||||
auto scan = std::make_shared<LaserScan>();
|
||||
scan->header.seq = seq_++;
|
||||
scan->header.frame_id = cfg_.frame_id;
|
||||
|
||||
// Do dai sweep THAT, khong lay tu scan_freq_hz. Probe do 72 goi/s trong
|
||||
// khi sensors.yaml ghi 25 Hz -- mot trong hai sai, va lay tu config se
|
||||
// lam time_increment sai theo, keo moi phep bu chuyen dong lech.
|
||||
const double measured = (sweep_end_ - sweep_start_).toSec();
|
||||
|
||||
scan->angle_min = static_cast<float>(-cfg_.fov_rad / 2.0);
|
||||
scan->angle_max = static_cast<float>(cfg_.fov_rad / 2.0);
|
||||
scan->angle_increment =
|
||||
static_cast<float>(cfg_.fov_rad / static_cast<double>(cfg_.samples - 1));
|
||||
scan->scan_time = static_cast<float>(measured);
|
||||
scan->time_increment =
|
||||
static_cast<float>(measured / static_cast<double>(cfg_.samples));
|
||||
scan->range_min = static_cast<float>(cfg_.sanitizer.range_min);
|
||||
scan->range_max = static_cast<float>(cfg_.sanitizer.range_max);
|
||||
scan->ranges.assign(cfg_.samples, kInf);
|
||||
scan->intensities.assign(cfg_.samples, 0.0f);
|
||||
|
||||
size_t placed = 0;
|
||||
for (const Point& pt : points_) {
|
||||
if (pt.angle_rad < scan->angle_min || pt.angle_rad > scan->angle_max) {
|
||||
continue; // ngoai FOV 270 do -- phan bi than robot che
|
||||
}
|
||||
const auto idx = static_cast<size_t>(std::lround(
|
||||
(pt.angle_rad - scan->angle_min) / scan->angle_increment));
|
||||
if (idx >= cfg_.samples) continue;
|
||||
scan->ranges[idx] = pt.range_m;
|
||||
scan->intensities[idx] = pt.intensity;
|
||||
++placed;
|
||||
}
|
||||
|
||||
// stamp = luc nhan goi DAU cua sweep, theo quy uoc LaserScan (stamp ung
|
||||
// voi TIA DAU TIEN). Che do kia tru scan_time khoi goi cuoi.
|
||||
UnixTime stamp = (cfg_.stamp_mode == LidarStampMode::kFirstPacket)
|
||||
? sweep_start_
|
||||
: sweep_end_ - Duration::fromSec(measured);
|
||||
// ⚠️ 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);
|
||||
}
|
||||
scan->header.stamp = stamp;
|
||||
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;
|
||||
|
||||
// ⚠️ KHONG goi ScanSanitizer o day, du LidarConfig co mang mot
|
||||
// ScanSanitizerConfig. Driver tra ve scan THO.
|
||||
//
|
||||
// Hai ly do, ca hai deu tu kien truc co san:
|
||||
// 1. Node da ve sinh roi (wiring.cc goi sanitizer_->Apply), va no dung
|
||||
// DUNG MOT doi tuong ScanSanitizer cho moi nguon. Ve sinh hai lan
|
||||
// la lam thua, va lam lech thong ke cua chinh sanitizer do.
|
||||
// 2. Ban ghi .vsr luu du lieu THO co y -- replay_source.h noi ro "de doi
|
||||
// nguong roi test lai duoc". Neu driver ve sinh truoc khi ghi thi
|
||||
// nguong bi dong cung vao file va khong bao gio chinh lai duoc.
|
||||
//
|
||||
// cfg_.sanitizer o day CHI dung de dien range_min/range_max cua header.
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(stats_mu_);
|
||||
std::lock_guard<std::mutex> lk(mu_);
|
||||
++stats_.frames;
|
||||
stats_.connected = true;
|
||||
stats_.hz = (measured > 0.0) ? 1.0 / measured : 0.0;
|
||||
// O trong = mat goi UDP. Bo phat hien mien phi: khong doc bo dem goi
|
||||
// nao, chi dem o chua ai ghi.
|
||||
if (placed + 1 < cfg_.samples) {
|
||||
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;
|
||||
stats_.packets_lost += (cfg_.samples - placed) / (kDepthsPerBlock * kBlocks) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
const double expected =
|
||||
(cfg_.scan_freq_hz > 0.0) ? 1.0 / cfg_.scan_freq_hz : 0.0;
|
||||
if (expected > 0.0 &&
|
||||
std::fabs(measured - expected) > cfg_.sweep_duration_tolerance_sec) {
|
||||
// THROTTLE: neu config sai toc do that thi dong nay no ra hang chuc lan
|
||||
// moi giay va nhan chim moi log khac.
|
||||
VSLAM_WARN_THROTTLE(5.0, "lidar: sweep " << measured * 1e3 << " ms, config doi "
|
||||
<< expected * 1e3 << " ms ("
|
||||
<< placed << "/" << cfg_.samples
|
||||
<< " tia)");
|
||||
}
|
||||
|
||||
if (cb_) cb_(scan);
|
||||
Reset();
|
||||
}
|
||||
|
||||
void Fail(const std::string& msg) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(stats_mu_);
|
||||
++stats_.errors;
|
||||
stats_.connected = false;
|
||||
stats_.last_error = msg;
|
||||
}
|
||||
VSLAM_WARN_THROTTLE(5.0, "lidar: " << msg);
|
||||
if (cb_) cb_(out);
|
||||
}
|
||||
|
||||
LidarConfig cfg_;
|
||||
ClockPtr clock_;
|
||||
ScanCallback cb_;
|
||||
|
||||
int fd_ = -1;
|
||||
std::atomic<bool> running_{false};
|
||||
std::thread thread_;
|
||||
|
||||
std::array<uint8_t, kPacketBytes> pending_{};
|
||||
UnixTime pending_rx_;
|
||||
bool have_pending_ = false;
|
||||
|
||||
std::vector<Point> points_;
|
||||
UnixTime sweep_start_;
|
||||
UnixTime sweep_end_;
|
||||
uint32_t prev_az_ = 0;
|
||||
bool have_prev_az_ = false;
|
||||
std::unique_ptr<lidar_core::LakiBeamDriver> impl_;
|
||||
uint32_t seq_ = 0;
|
||||
|
||||
mutable std::mutex stats_mu_;
|
||||
mutable std::mutex mu_;
|
||||
DriverStats stats_;
|
||||
};
|
||||
|
||||
@@ -454,7 +176,7 @@ class LakiBeamDriver : public LidarInterface {
|
||||
|
||||
std::unique_ptr<LidarInterface> CreateLakiBeamDriver(const LidarConfig& cfg,
|
||||
ClockPtr clock) {
|
||||
return std::make_unique<LakiBeamDriver>(cfg, std::move(clock));
|
||||
return std::make_unique<LakiBeamWrapper>(cfg, std::move(clock));
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
Reference in New Issue
Block a user