update
This commit is contained in:
@@ -69,7 +69,7 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
|
||||
Reject(l, "sensors.yaml lidar",
|
||||
{"host", "port", "expected_rate_hz", "range_min", "range_max",
|
||||
"stamp_mismatch_warn", "sector_masks",
|
||||
"angle_offset_deg", "angle_flip"});
|
||||
"angle_offset_deg", "angle_flip", "filters"});
|
||||
if (!r.ok()) return r;
|
||||
}
|
||||
out->sanitizer.range_min = l.get<double>("range_min", 0.05);
|
||||
@@ -95,6 +95,57 @@ StatusResponse LoadAppConfig(const std::string& dir, AppConfig* out) {
|
||||
out->lidar_driver.inverted =
|
||||
l.get<bool>("angle_flip", out->lidar_driver.inverted);
|
||||
|
||||
// --- Bon bo loc scan ---
|
||||
const ConfigNode ft = l["filters"];
|
||||
{
|
||||
const StatusResponse r =
|
||||
Reject(ft, "sensors.yaml lidar.filters",
|
||||
{"shadow_enabled", "shadow_min_angle_deg", "shadow_window",
|
||||
"shadow_min_range_diff", "shadow_max_range",
|
||||
"shadow_clear_start", "shadow_clear_neighbors",
|
||||
"intensity_enabled", "intensity_min", "intensity_max",
|
||||
"intensity_max_range",
|
||||
"isolated_enabled", "isolated_min_cluster_points",
|
||||
"isolated_max_range_jump", "isolated_max_range",
|
||||
"box_enabled", "box_min_x", "box_max_x",
|
||||
"box_min_y", "box_max_y"});
|
||||
if (!r.ok()) return r;
|
||||
}
|
||||
{
|
||||
LidarFilterConfig& f = out->lidar_driver.filter;
|
||||
f.shadow_enabled = ft.get<bool>("shadow_enabled", f.shadow_enabled);
|
||||
f.shadow_min_angle_deg =
|
||||
ft.get<double>("shadow_min_angle_deg", f.shadow_min_angle_deg);
|
||||
f.shadow_window = ft.get<int>("shadow_window", f.shadow_window);
|
||||
f.shadow_min_range_diff =
|
||||
ft.get<double>("shadow_min_range_diff", f.shadow_min_range_diff);
|
||||
f.shadow_max_range = ft.get<double>("shadow_max_range", f.shadow_max_range);
|
||||
f.shadow_clear_start =
|
||||
ft.get<bool>("shadow_clear_start", f.shadow_clear_start);
|
||||
f.shadow_clear_neighbors =
|
||||
ft.get<int>("shadow_clear_neighbors", f.shadow_clear_neighbors);
|
||||
|
||||
f.intensity_enabled = ft.get<bool>("intensity_enabled", f.intensity_enabled);
|
||||
f.intensity_min = ft.get<double>("intensity_min", f.intensity_min);
|
||||
f.intensity_max = ft.get<double>("intensity_max", f.intensity_max);
|
||||
f.intensity_max_range =
|
||||
ft.get<double>("intensity_max_range", f.intensity_max_range);
|
||||
|
||||
f.isolated_enabled = ft.get<bool>("isolated_enabled", f.isolated_enabled);
|
||||
f.isolated_min_cluster_points =
|
||||
ft.get<int>("isolated_min_cluster_points", f.isolated_min_cluster_points);
|
||||
f.isolated_max_range_jump =
|
||||
ft.get<double>("isolated_max_range_jump", f.isolated_max_range_jump);
|
||||
f.isolated_max_range =
|
||||
ft.get<double>("isolated_max_range", f.isolated_max_range);
|
||||
|
||||
f.box_enabled = ft.get<bool>("box_enabled", f.box_enabled);
|
||||
f.box_min_x = ft.get<double>("box_min_x", f.box_min_x);
|
||||
f.box_max_x = ft.get<double>("box_max_x", f.box_max_x);
|
||||
f.box_min_y = ft.get<double>("box_min_y", f.box_min_y);
|
||||
f.box_max_y = ft.get<double>("box_max_y", f.box_max_y);
|
||||
}
|
||||
|
||||
// Sector mask ghi bang DO trong YAML (doc duoc bang mat), dung bang RAD.
|
||||
// Doi mot lan o day, khong de moi noi tu doi.
|
||||
const ConfigNode masks = l["sector_masks"];
|
||||
|
||||
@@ -52,6 +52,50 @@ lidar:
|
||||
# |(t_cuoi - t_dau) - scan_time| > nguong nay.
|
||||
stamp_mismatch_warn: 0.005
|
||||
|
||||
# ============ BON BO LOC SCAN ============
|
||||
# Chay TREN scan tho, truoc ScanSanitizer. Nguon: lakibeam.yaml cua
|
||||
# base_kit -- bo tham so da duoc chinh tren chinh con robot nay.
|
||||
#
|
||||
# Vi sao can, ngoai ScanSanitizer: ScanSanitizer chi cat QUAT CO DINH. Rac
|
||||
# sinh ra tu bon cot do hai tang tren khong nam gon trong quat nao -- no doi
|
||||
# cho theo goc nhin va theo khoang cach vat the.
|
||||
filters:
|
||||
# --- Tia keo dai (shadow / grazing angle) ---
|
||||
# Tia luot qua MEP mot vat (cot chong) ve ra mot dai diem cheo keo dai ra
|
||||
# sau vat do, trong nhu mot buc tuong khong co that.
|
||||
shadow_enabled: true
|
||||
shadow_min_angle_deg: 12.0 # duoi goc nay coi la tia keo dai
|
||||
shadow_window: 4 # so tia lan can de xet
|
||||
shadow_min_range_diff: 0.05 # buoc nhay cu ly toi thieu, m
|
||||
shadow_max_range: 2.0 # chi loc trong pham vi nay, m
|
||||
shadow_clear_start: false # giu diem mep -- de khong an mat tuong that
|
||||
shadow_clear_neighbors: 0 # 0 = khong lan sang diem tuong that
|
||||
|
||||
# --- Cuong do phan xa (RSSI 0-255) ---
|
||||
# Tan xa yeu o gan thuong la bui hoac phan xa lech tu khung gan.
|
||||
intensity_enabled: true
|
||||
intensity_min: 30.0
|
||||
intensity_max: 255.0
|
||||
intensity_max_range: 1.0 # chi loc trong pham vi nay -- bao ve vat o xa
|
||||
|
||||
# --- Cum diem don le ---
|
||||
isolated_enabled: true
|
||||
isolated_min_cluster_points: 3 # cum nho hon nguong nay thi xoa
|
||||
isolated_max_range_jump: 0.10 # hai tia lien tiep con coi la mot cum, m
|
||||
isolated_max_range: 2.0 # 0 = ap dung toan scan
|
||||
|
||||
# --- Hop quanh than robot ---
|
||||
#
|
||||
# ⚠️ Day moi la thu THAY THE dung dan cho `sector_masks` ben duoi.
|
||||
# sector_masks cat theo GOC, hop ly khi LiDAR nam o MEP truoc (x=0.24 nhu
|
||||
# cau hinh cu). LiDAR gio o TAM robot (x=0), nen vung bi khung gan che
|
||||
# khong con la mot quat goc ma la mot vung hinh HOP quanh goc toa do.
|
||||
box_enabled: true
|
||||
box_min_x: -0.25
|
||||
box_max_x: 0.25
|
||||
box_min_y: -0.25
|
||||
box_max_y: 0.0
|
||||
|
||||
# Quat bi than robot che, don vi DO.
|
||||
#
|
||||
# ⚠️⚠️ HAI GIA TRI DUOI DAY DA MAT CAN CU -- PHAI DO LAI TRUOC KHI BAT SAFETY.
|
||||
|
||||
@@ -37,11 +37,13 @@ find_package(Boost REQUIRED COMPONENTS system thread)
|
||||
target_sources(vslam_drivers PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lakibeam_sdk/LakiBeamUDP.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/lakibeam_driver.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/scan_conversion.cpp)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/scan_conversion.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/scan_filter.cpp)
|
||||
set_source_files_properties(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lakibeam_sdk/LakiBeamUDP.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/lakibeam_driver.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/scan_conversion.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lidar_core/src/scan_filter.cpp
|
||||
PROPERTIES COMPILE_OPTIONS "-w")
|
||||
target_include_directories(vslam_drivers PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/lakibeam_sdk
|
||||
|
||||
@@ -19,6 +19,48 @@ enum class LidarStampMode {
|
||||
kLastPacketMinusScanTime,
|
||||
};
|
||||
|
||||
// Bon bo loc chay TREN scan tho, truoc khi ra khoi driver.
|
||||
//
|
||||
// Vi sao can: LiDAR gan giua robot, giua bon cot do hai tang tren. Tia dap
|
||||
// vao cot va vao chinh khung gan sinh ra ba loai rac ma ScanSanitizer khong
|
||||
// bat duoc -- no chi cat QUAT co dinh, ma o day rac khong nam gon trong mot
|
||||
// quat nao.
|
||||
struct LidarFilterConfig {
|
||||
// --- Tia keo dai (shadow / grazing angle) ---
|
||||
// Tia luot qua MEP mot vat (cot chong) ve ra mot dai diem cheo keo dai ra
|
||||
// sau vat do -- trong nhu mot buc tuong khong co that.
|
||||
bool shadow_enabled = true;
|
||||
double shadow_min_angle_deg = 12.0; // duoi goc nay coi la tia keo dai
|
||||
int shadow_window = 4; // so tia lan can de xet
|
||||
double shadow_min_range_diff = 0.05; // buoc nhay cu ly toi thieu, m
|
||||
double shadow_max_range = 2.0; // chi loc trong pham vi nay, m
|
||||
bool shadow_clear_start = false; // false: giu diem mep de khong an mat tuong that
|
||||
int shadow_clear_neighbors = 0; // 0: khong lan sang diem tuong that
|
||||
|
||||
// --- Cuong do phan xa (RSSI) ---
|
||||
// Tan xa yeu o gan thuong la bui hoac phan xa lech tu khung gan.
|
||||
bool intensity_enabled = true;
|
||||
double intensity_min = 30.0; // RSSI 0-255
|
||||
double intensity_max = 255.0;
|
||||
double intensity_max_range = 1.0; // chi loc trong pham vi nay, m
|
||||
|
||||
// --- Cum diem don le ---
|
||||
// Mot vai diem roi rac o gan, khong lien voi be mat nao.
|
||||
bool isolated_enabled = true;
|
||||
int isolated_min_cluster_points = 3; // cum nho hon nguong nay thi xoa
|
||||
double isolated_max_range_jump = 0.10; // hai tia lien tiep con coi la mot cum, m
|
||||
double isolated_max_range = 2.0; // 0 = ap dung toan scan
|
||||
|
||||
// --- Hop quanh than robot ---
|
||||
// Thay cho `sector_masks` khi LiDAR nam o TAM robot: luc do vung bi che
|
||||
// khong con la mot quat goc ma la mot vung hinh hop quanh goc toa do.
|
||||
bool box_enabled = true;
|
||||
double box_min_x = -0.25;
|
||||
double box_max_x = 0.25;
|
||||
double box_min_y = -0.25;
|
||||
double box_max_y = 0.0;
|
||||
};
|
||||
|
||||
struct LidarConfig {
|
||||
std::string host_ip = "0.0.0.0"; // dia chi de bind, KHONG phai IP cua LiDAR
|
||||
uint16_t port = 2368; // UDP, theo datasheet
|
||||
@@ -83,6 +125,17 @@ struct LidarConfig {
|
||||
double recv_timeout_sec = 1.0;
|
||||
|
||||
ScanSanitizerConfig sanitizer;
|
||||
|
||||
// --- Bon bo loc scan cua LakiBeam ---
|
||||
//
|
||||
// Guong cua `lidar_core::ScanFilterConfig` (thirdparty). Khai o day thay vi
|
||||
// dung thang kieu kia vi driver_config.h la header CONG KHAI: keo kieu
|
||||
// thirdparty vao se bat moi nguoi dung vslam_drivers phai co include path
|
||||
// cua no.
|
||||
//
|
||||
// Gia tri mac dinh lay tu lakibeam.yaml cua base_kit -- bo da duoc chinh
|
||||
// tren chinh con robot nay.
|
||||
LidarFilterConfig filter;
|
||||
};
|
||||
|
||||
// --- Camera RealSense D410 -------------------------------------------------
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
#include <string>
|
||||
|
||||
#include "lidar_core/lakibeam_driver.hpp"
|
||||
#include "lidar_core/scan_filter.hpp"
|
||||
|
||||
#include "vslam/clock.h"
|
||||
#include "vslam/driver_config.h"
|
||||
@@ -86,6 +87,45 @@ class LakiBeamWrapper : public LidarInterface {
|
||||
// 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); });
|
||||
|
||||
@@ -110,6 +150,7 @@ class LakiBeamWrapper : public LidarInterface {
|
||||
if (!impl_) return;
|
||||
impl_->stop();
|
||||
impl_.reset();
|
||||
filter_.reset();
|
||||
}
|
||||
|
||||
DriverStats Stats() const override {
|
||||
@@ -118,7 +159,12 @@ class LakiBeamWrapper : public LidarInterface {
|
||||
}
|
||||
|
||||
private:
|
||||
void OnScan(const lidar_core::LaserScan& in) {
|
||||
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;
|
||||
@@ -218,6 +264,7 @@ class LakiBeamWrapper : public LidarInterface {
|
||||
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_;
|
||||
|
||||
80
libs/vslam_drivers/thirdparty/lidar_core/include/lidar_core/scan_filter.hpp
vendored
Normal file
80
libs/vslam_drivers/thirdparty/lidar_core/include/lidar_core/scan_filter.hpp
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef LIDAR_CORE__SCAN_FILTER_HPP_
|
||||
#define LIDAR_CORE__SCAN_FILTER_HPP_
|
||||
|
||||
#include <memory>
|
||||
|
||||
// [vslam] Chep tu base_kit/src/lidar/lakibeam_ros. Doi DUY NHAT: kieu
|
||||
// sensor_msgs::msg::LaserScan -> lidar_core::LaserScan. Hai kieu co cung bo
|
||||
// truong (lidar_core::LaserScan la ban sao thuan C++ cua sensor_msgs), va bo
|
||||
// loc chi dung ranges/intensities/angle_*/range_*.
|
||||
// Xem thirdparty/PROVENANCE.md.
|
||||
#include "lidar_core/laser_scan.hpp"
|
||||
|
||||
namespace lidar_core
|
||||
{
|
||||
|
||||
/// @brief Tham số cấu hình cho bộ lọc ScanFilter
|
||||
struct ScanFilterConfig
|
||||
{
|
||||
// ---- Shadow / Trailing Beam Filter ----
|
||||
bool shadow_filter_enabled = true; ///< Bật/tắt bộ lọc bóng/tia kéo dài
|
||||
double shadow_min_angle_deg = 12.0; ///< Ngưỡng góc tới hạn [deg] (nhỏ hơn ngưỡng sẽ coi là tia kéo dài)
|
||||
int shadow_window = 4; ///< Cửa sổ trượt kiểm tra (số tia lân cận)
|
||||
double shadow_min_range_diff = 0.05; ///< Bước nhảy cự ly tối thiểu [m] để xét dải chéo
|
||||
double shadow_max_range = 2.0; ///< Cự ly tối đa áp dụng lọc [m] (phạm vi quanh base robot)
|
||||
bool shadow_clear_start = false; ///< Xóa cả điểm mép dải dốc (mặc định false để bảo vệ vật thể thật)
|
||||
int shadow_clear_neighbors = 0; ///< Số tia lân cận xóa mở rộng (0 = không lấn sang tường thật)
|
||||
|
||||
// ---- Box / Footprint Filter (Khung robot base) ----
|
||||
bool box_filter_enabled = false; ///< Bật/tắt lọc theo vùng hộp thân robot (mặc định tắt)
|
||||
double box_min_x = -0.25; ///< Giới hạn X tối thiểu [m]
|
||||
double box_max_x = 0.25; ///< Giới hạn X tối đa [m]
|
||||
double box_min_y = -0.45; ///< Giới hạn Y tối thiểu [m]
|
||||
double box_max_y = 0.0; ///< Giới hạn Y tối đa [m]
|
||||
|
||||
// ---- Intensity Filter (Lọc theo cường độ phản xạ RSSI) ----
|
||||
bool intensity_filter_enabled = true; ///< Bật/tắt lọc theo cường độ phản xạ
|
||||
float intensity_min = 25.0f; ///< Cường độ nhỏ nhất được chấp nhận (nhỏ hơn sẽ bị lọc)
|
||||
float intensity_max = 255.0f; ///< Cường độ lớn nhất được chấp nhận
|
||||
float intensity_filter_max_range = 1.0f; ///< Cự ly tối đa [m] áp dụng lọc cường độ (0 = toàn bộ scan)
|
||||
|
||||
// ---- Isolated Cluster Filter (Lọc cụm điểm đơn lẻ / nhiễu li ti) ----
|
||||
bool isolated_filter_enabled = true; ///< Bật/tắt lọc cụm điểm đơn lẻ
|
||||
int isolated_min_cluster_points = 3; ///< Lọc cụm có số điểm < ngưỡng này (3 = lọc điểm đơn và cụm 2 điểm)
|
||||
double isolated_max_range_jump = 0.10; ///< Chênh lệch cự ly tối đa giữa 2 tia liên tiếp để tính cùng cụm [m]
|
||||
double isolated_max_range = 2.0; ///< Chỉ lọc cụm đơn lẻ trong phạm vi này [m] (0 = toàn bộ scan)
|
||||
};
|
||||
|
||||
/// @brief Interface cho bộ lọc LaserScan
|
||||
class IScanFilter
|
||||
{
|
||||
public:
|
||||
virtual ~IScanFilter() = default;
|
||||
|
||||
/// @brief Lọc trực tiếp trên bản tin LaserScan (thay các điểm rác bằng +Inf)
|
||||
virtual void filter(LaserScan & scan) const = 0;
|
||||
};
|
||||
|
||||
/// @brief Triển khai bộ lọc tia kéo dài (shadow / grazing angle filter)
|
||||
class ScanFilter : public IScanFilter
|
||||
{
|
||||
public:
|
||||
explicit ScanFilter(const ScanFilterConfig & config);
|
||||
~ScanFilter() override = default;
|
||||
|
||||
ScanFilter(const ScanFilter &) = delete;
|
||||
ScanFilter & operator=(const ScanFilter &) = delete;
|
||||
|
||||
void filter(LaserScan & scan) const override;
|
||||
|
||||
const ScanFilterConfig & config() const { return config_; }
|
||||
|
||||
private:
|
||||
ScanFilterConfig config_;
|
||||
double min_angle_rad_{0.0};
|
||||
};
|
||||
|
||||
} // namespace lidar_core
|
||||
|
||||
#endif // LIDAR_CORE__SCAN_FILTER_HPP_
|
||||
|
||||
270
libs/vslam_drivers/thirdparty/lidar_core/src/scan_filter.cpp
vendored
Normal file
270
libs/vslam_drivers/thirdparty/lidar_core/src/scan_filter.cpp
vendored
Normal file
@@ -0,0 +1,270 @@
|
||||
#include "lidar_core/scan_filter.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
namespace lidar_core
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
constexpr double kDeg2Rad = 3.14159265358979323846 / 180.0;
|
||||
constexpr double kTwoPi = 2.0 * 3.14159265358979323846;
|
||||
|
||||
inline bool isValidRange(float r, float range_min, float range_max)
|
||||
{
|
||||
return std::isfinite(r) && (r >= range_min) && (r <= range_max);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ScanFilter::ScanFilter(const ScanFilterConfig & config)
|
||||
: config_(config),
|
||||
min_angle_rad_(config.shadow_min_angle_deg * kDeg2Rad)
|
||||
{
|
||||
}
|
||||
|
||||
void ScanFilter::filter(LaserScan & scan) const
|
||||
{
|
||||
if ((!config_.shadow_filter_enabled && !config_.box_filter_enabled &&
|
||||
!config_.intensity_filter_enabled && !config_.isolated_filter_enabled) ||
|
||||
scan.ranges.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t n = scan.ranges.size();
|
||||
const float r_min = scan.range_min;
|
||||
const float r_max = scan.range_max;
|
||||
const double dtheta_base = std::abs(static_cast<double>(scan.angle_increment));
|
||||
const double span = std::abs(static_cast<double>(scan.angle_max - scan.angle_min));
|
||||
const bool is_360 = (span >= (kTwoPi - dtheta_base * 1.5));
|
||||
const int window = std::max(1, config_.shadow_window);
|
||||
const float min_range_diff = static_cast<float>(config_.shadow_min_range_diff);
|
||||
const float max_range_threshold = static_cast<float>(config_.shadow_max_range);
|
||||
const int clear_neighbors = std::max(0, config_.shadow_clear_neighbors);
|
||||
|
||||
std::vector<bool> to_filter(n, false);
|
||||
|
||||
// 1. Box / Footprint Filter: Lọc theo vùng hộp thân robot (tùy chọn)
|
||||
if (config_.box_filter_enabled) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const float r = scan.ranges[i];
|
||||
if (!isValidRange(r, r_min, r_max)) {
|
||||
continue;
|
||||
}
|
||||
const double theta = static_cast<double>(scan.angle_min) +
|
||||
static_cast<double>(i) * static_cast<double>(scan.angle_increment);
|
||||
const double x = static_cast<double>(r) * std::cos(theta);
|
||||
const double y = static_cast<double>(r) * std::sin(theta);
|
||||
|
||||
if (x >= config_.box_min_x && x <= config_.box_max_x &&
|
||||
y >= config_.box_min_y && y <= config_.box_max_y) {
|
||||
to_filter[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Shadow Filter: Lọc các dải dốc tia kéo dài (grazing angle)
|
||||
if (config_.shadow_filter_enabled) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const float r1 = scan.ranges[i];
|
||||
if (!isValidRange(r1, r_min, r_max)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int k = 1; k <= window; ++k) {
|
||||
const size_t j = is_360 ? ((i + static_cast<size_t>(k)) % n) : (i + static_cast<size_t>(k));
|
||||
if (j >= n) {
|
||||
break;
|
||||
}
|
||||
|
||||
const float r2 = scan.ranges[j];
|
||||
if (!isValidRange(r2, r_min, r_max)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Giới hạn cự ly áp dụng lọc (chỉ lọc các dải xuất phát từ vùng gần robot)
|
||||
if (max_range_threshold > 0.0f) {
|
||||
const float nearer_dist = std::min(r1, r2);
|
||||
if (nearer_dist > max_range_threshold) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Chênh lệch cự ly giữa 2 tia trong cửa sổ phải đủ lớn để cấu thành dải dốc
|
||||
const float range_diff = std::abs(r2 - r1);
|
||||
if (range_diff < min_range_diff) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Tính góc tới hạn alpha (grazing angle) tại điểm gần
|
||||
const double dtheta = static_cast<double>(k) * dtheta_base;
|
||||
const double d_perp = static_cast<double>(r2) * std::sin(dtheta);
|
||||
const double d_para = std::abs(static_cast<double>(r2) * std::cos(dtheta) - static_cast<double>(r1));
|
||||
const double alpha = std::atan2(d_perp, d_para);
|
||||
|
||||
if (alpha < min_angle_rad_) {
|
||||
// Chỉ đánh dấu các điểm trung gian nằm trên dải dốc (r_low < r_m < r_high)
|
||||
const float r_low = std::min(r1, r2);
|
||||
const float r_high = std::max(r1, r2);
|
||||
|
||||
for (size_t m = 1; m < static_cast<size_t>(k); ++m) {
|
||||
const size_t idx = (i + m) % n;
|
||||
const float rm = scan.ranges[idx];
|
||||
if (isValidRange(rm, r_min, r_max) && rm > (r_low + 0.005f) && rm < (r_high - 0.005f)) {
|
||||
to_filter[idx] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (config_.shadow_clear_start) {
|
||||
if (r1 < r2) {
|
||||
to_filter[i] = true;
|
||||
} else {
|
||||
to_filter[j] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Xóa mở rộng thêm các tia lân cận ở 2 đầu nếu được cấu hình
|
||||
for (int nb = 1; nb <= clear_neighbors; ++nb) {
|
||||
const size_t s_nb = static_cast<size_t>(nb);
|
||||
const size_t idx_before = is_360 ? ((i + n - s_nb) % n) : (i >= s_nb ? (i - s_nb) : 0);
|
||||
const size_t idx_after = is_360 ? ((j + s_nb) % n) : (std::min(j + s_nb, n - 1));
|
||||
to_filter[idx_before] = true;
|
||||
to_filter[idx_after] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Intensity Filter: Lọc theo cường độ phản xạ (RSSI)
|
||||
if (config_.intensity_filter_enabled && !scan.intensities.empty()) {
|
||||
const float int_min = config_.intensity_min;
|
||||
const float int_max = config_.intensity_max;
|
||||
const float int_max_range = static_cast<float>(config_.intensity_filter_max_range);
|
||||
const size_t n_int = std::min(n, scan.intensities.size());
|
||||
|
||||
for (size_t i = 0; i < n_int; ++i) {
|
||||
const float r = scan.ranges[i];
|
||||
if (!isValidRange(r, r_min, r_max)) {
|
||||
continue;
|
||||
}
|
||||
if (int_max_range > 0.0f && r > int_max_range) {
|
||||
continue;
|
||||
}
|
||||
const float intensity = scan.intensities[i];
|
||||
if (intensity < int_min || intensity > int_max) {
|
||||
to_filter[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Isolated Cluster Filter: Lọc cụm điểm đơn lẻ / nhiễu li ti
|
||||
// Chia scan thành các cụm điểm liên tiếp (2 tia liền nhau thuộc cùng cụm nếu
|
||||
// chênh lệch cự ly <= jump). Cụm có số điểm < min_cluster_points trong phạm vi
|
||||
// max_range (cụm đơn lẻ gần robot — nhiễu li ti từ khung gắn) sẽ bị xóa.
|
||||
// Tường/vật cản thật là cụm lớn nên được bảo toàn. Chạy sau cùng để các điểm
|
||||
// đã bị bộ lọc khác xử lý (sẽ thành +Inf) không tính là điểm lân cận hỗ trợ.
|
||||
if (config_.isolated_filter_enabled) {
|
||||
const int min_pts = std::max(1, config_.isolated_min_cluster_points);
|
||||
const float jump_thresh = static_cast<float>(config_.isolated_max_range_jump);
|
||||
const float iso_max_range = static_cast<float>(config_.isolated_max_range);
|
||||
|
||||
// Điểm đã bị lọc (to_filter) coi như không hợp lệ vì sẽ bị gán +Inf
|
||||
const auto is_valid_pt = [&](size_t idx) {
|
||||
return !to_filter[idx] && isValidRange(scan.ranges[idx], r_min, r_max);
|
||||
};
|
||||
|
||||
// Tìm mốc bắt đầu sao cho biên (start-1, start) là điểm ngăn cách cụm:
|
||||
// ưu tiên điểm gap (không hợp lệ); nếu toàn bộ scan hợp lệ thì dùng vị trí
|
||||
// break cự ly đầu tiên. Nhờ đó chia cụm theo vòng tròn không cần xử lý wrap.
|
||||
size_t start = 0;
|
||||
bool has_start = false;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (!is_valid_pt(i)) {
|
||||
has_start = true;
|
||||
start = (i + 1) % n;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!has_start) {
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
const size_t nxt = (i + 1) % n;
|
||||
if (std::abs(scan.ranges[nxt] - scan.ranges[i]) > jump_thresh) {
|
||||
has_start = true;
|
||||
start = nxt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (has_start) {
|
||||
// Gán cluster id cho từng điểm hợp lệ (duyệt circular từ sau gap)
|
||||
std::vector<int> cid(n, -1);
|
||||
int cur_id = -1;
|
||||
for (size_t off = 0; off < n; ++off) {
|
||||
const size_t i = (start + off) % n;
|
||||
if (!is_valid_pt(i)) {
|
||||
continue;
|
||||
}
|
||||
const size_t prev = (i + n - 1) % n;
|
||||
const bool new_cluster =
|
||||
(cur_id < 0) || !is_valid_pt(prev) ||
|
||||
(std::abs(scan.ranges[i] - scan.ranges[prev]) > jump_thresh);
|
||||
if (new_cluster) {
|
||||
++cur_id;
|
||||
}
|
||||
cid[i] = cur_id;
|
||||
}
|
||||
|
||||
if (cur_id >= 0) {
|
||||
// Đếm số điểm + cự ly gần nhất của từng cụm
|
||||
std::vector<int> cluster_count(static_cast<size_t>(cur_id) + 1, 0);
|
||||
std::vector<float> cluster_min_r(static_cast<size_t>(cur_id) + 1,
|
||||
std::numeric_limits<float>::infinity());
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (cid[i] < 0) {
|
||||
continue;
|
||||
}
|
||||
const size_t c = static_cast<size_t>(cid[i]);
|
||||
++cluster_count[c];
|
||||
cluster_min_r[c] = std::min(cluster_min_r[c], scan.ranges[i]);
|
||||
}
|
||||
|
||||
// Lọc toàn bộ cụm nhỏ trong phạm vi max_range
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (cid[i] < 0) {
|
||||
continue;
|
||||
}
|
||||
const size_t c = static_cast<size_t>(cid[i]);
|
||||
if (cluster_count[c] >= min_pts) {
|
||||
continue;
|
||||
}
|
||||
if (iso_max_range > 0.0f && cluster_min_r[c] > iso_max_range) {
|
||||
continue;
|
||||
}
|
||||
to_filter[i] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Áp dụng gán +Inf cho các điểm bị lọc
|
||||
const float inf = std::numeric_limits<float>::infinity();
|
||||
const bool has_intensities = (scan.intensities.size() == n);
|
||||
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
if (to_filter[i]) {
|
||||
scan.ranges[i] = inf;
|
||||
if (has_intensities) {
|
||||
scan.intensities[i] = 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace lidar_core
|
||||
|
||||
Reference in New Issue
Block a user