slam,odometry,perception: di tren doc (G4, slam.yaml slope.enabled, mac dinh tat) -- z robot = tich -ds*sin(pitch IMU) kep phang < 1.5 do va neo vao ban do (HeightEstimator), odometry ngang, scan cho SLAM bu nghieng + bo tia cham san/doc theo ban do do cao tu depth (ScanLeveler, ElevationMap), z/roll/pitch luu vao node + do thi z 1-D + mat san theo node (height_map), voxel dat o z that va vat can tinh tu san cuc bo, xoa tuong gia LiDAR tren doc noi camera da thay mat san, web z uoc luong, --sim-imu-hz

This commit is contained in:
trung09122002
2026-10-04 00:03:02 +07:00
parent 6735b53423
commit f064dcf3ec
32 changed files with 1799 additions and 10 deletions

View File

@@ -19,6 +19,8 @@ vslam_add_library(vslam_perception
gravity_align.h
cliff_detect.h
perception_pipeline.h
elevation_map.h
scan_leveler.h
FIND_DEPS
vslam_common
)

View File

@@ -0,0 +1,72 @@
#ifndef VSLAM_ELEVATION_MAP_H
#define VSLAM_ELEVATION_MAP_H
// Ban do do cao SAN cuc bo, frame odom: moi o 10 cm giu do cao THAP NHAT da
// thay (diem depth) hoac do cao noi banh xe DA cham (vet robot).
//
// ================== VI SAO ==================
// LiDAR mot mat phang cao 0.2165 m tren san. Lai tren san phang toi chan mot
// doc len, tia ngang cham mat doc o 0.2165 / tan(goc) truoc chan doc (15 do:
// 0.81 m, 30 do: 0.38 m) -- mot "buc tuong" tai MOT diem co dinh tren the gioi,
// tuc trong y het tuong that va bi ve vao ban do (G0, 03/10). Rieng scan thi
// khong phan biet duoc. Camera thay mat doc tu 1.1 m den ~4 m TRUOC khi robot
// toi do, nen tich lai do cao san o day roi hoi lai khi loc scan
// (ScanLeveler). Camera mu duoi 1.1 m, nen phai TICH LAI qua thoi gian, khong
// dung duoc mat na depth tung khung.
//
// Gia tri moi o la MIN: o co ca san lan vat can (chan tuong, chan ban) van ra
// do cao san mien la camera thay duoc chan. Nhieu depth keo min xuong vai cm
// -- an toan cho phep "cham san?" (nguong 8 cm).
//
// KHONG khoa -- nguoi goi giu mutex.
#include <cstdint>
#include <unordered_map>
#include <vector>
#include "vslam/unix_time.h"
#include "vslam/vslam_common.h"
namespace vslam {
struct ElevationMapConfig {
double cell_m = 0.10;
// Bo o xa robot hon / cu hon nguong nay (Prune). z robot troi dan, nen o
// cu lau thi do cao cua no khong con cung goc voi z hien tai.
double radius_m = 8.0;
double max_age_sec = 60.0;
};
class ElevationMap {
public:
explicit ElevationMap(ElevationMapConfig cfg = {});
// Diem DA o frame odom (x, y, z tuyet doi).
void AddPoints(const std::vector<PointXYZ>& pts, UnixTime stamp);
// Banh xe dang cham san tai (x, y) o do cao z: moi o trong ban kinh r lay
// DUNG z (ghi de -- vet banh la do cao san chac chan nhat).
void AddFootprint(double x, double y, double z, double r, UnixTime stamp);
// Do cao san tai (x, y); false neu chua biet.
bool FloorAt(double x, double y, double* z) const;
void Prune(double cx, double cy, UnixTime now);
void Clear() { cells_.clear(); }
size_t size() const { return cells_.size(); }
const ElevationMapConfig& config() const { return cfg_; }
private:
struct Cell {
float z;
int64_t stamp_ns;
};
uint64_t Key(double x, double y) const;
ElevationMapConfig cfg_;
std::unordered_map<uint64_t, Cell> cells_;
};
} // namespace vslam
#endif // VSLAM_ELEVATION_MAP_H

View File

@@ -0,0 +1,70 @@
#ifndef VSLAM_SCAN_LEVELER_H
#define VSLAM_SCAN_LEVELER_H
// Bu nghieng scan LiDAR cho SLAM 2D tren doc.
//
// ================== VAN DE ==================
// RTAB-Map 2D coi moi diem scan nam tren MOT mat phang ngang. Tren doc thi sai
// theo hai kieu:
// 1. Than nghieng -> tia nghieng: tuong cach 2 m tren doc 15 do doc ra
// 2 / cos(15) = 2.07 m, va goc cua tia lech di (tia 45 do -> 46 do).
// 2. Tia CHAM SAN / MAT DOC: dung tren san phang truoc doc len, tia cham
// mat doc o 0.2165 / tan(goc) (tuong gia co dinh tren the gioi); dang
// xuong doc (chuc mui), tia cham san duoi chan doc.
//
// ================== CACH LAM ==================
// Moi tia: diem cham -> frame base_link (ngoai lai tinh) -> base_footprint DA
// CAN trong luc (TiltLift, CHUNG ham voi GravityAlign / CliffDetect) -> do cao
// h so voi mat dat duoi robot, va do cao tuyet doi H = z_robot + h.
// * Biet do cao san tai do (ElevationMap) va H - san < floor_margin -> tia
// cham san/doc -> BO (NaN).
// * Khong biet san va h < -below_ground -> diem THAP hon cho robot dung, tuc
// san duoi chan doc khi dang xuong -> BO.
// * Con lai: chieu xuong mat phang ngang, tinh lai khoang cach va goc tu goc
// LiDAR (vi tri TINH trong base_link -- RTAB-Map dat lai diem dung cho do),
// xep vao o goc gan nhat, giu tia gan nhat moi o.
// Tia bo thanh NaN ("khong do duoc"), KHONG phai inf ("trong toi range_max"):
// inf se xoa trang ca vung sau mat doc tren luoi 2D.
//
// ⚠️ GIOI HAN: tuong gia khi tien toi chan doc LEN chi bo duoc neu camera da
// thay mat doc (ElevationMap co o do). Re vao chan doc o goc hep / camera bi
// che thi tuong gia van con.
//
// Chi cap cho SLAM / dinh vi lai / canh lac. Lop AN TOAN van dung scan THO:
// heuristic o day khong bao gio duoc xoa mot vat can that khoi luong dung xe.
#include <cstdint>
#include "vslam/elevation_map.h"
#include "vslam/vslam_common.h"
namespace vslam {
struct ScanLevelerConfig {
// Diem cao hon san da biet duoi nguong nay = cham san / mat doc.
double floor_margin = 0.08;
// Khong biet san: diem thap hon mat dat duoi robot qua nguong nay = san duoi
// chan doc.
double below_ground = 0.05;
};
struct ScanLevelStats {
uint64_t scans = 0;
uint64_t rays = 0; // tia huu han vao
uint64_t kept = 0;
uint64_t dropped_floor = 0; // cham san theo ElevationMap
uint64_t dropped_below = 0; // thap hon mat dat duoi robot
uint64_t rebinned = 0; // doi sang o goc khac sau khi chieu
};
// laser_to_base: base_link <- laser_link (tinh). roll/pitch: nghieng than
// (GravityAlign). odom: base_footprint trong odom; z_robot: do cao robot
// (HeightEstimator). em co the null.
void LevelScan(const LaserScan& in, const Transform& laser_to_base, double roll,
double pitch, double wheel_radius, const Pose2D& odom, double z_robot,
const ElevationMap* em, const ScanLevelerConfig& cfg, LaserScan* out,
ScanLevelStats* stats);
} // namespace vslam
#endif // VSLAM_SCAN_LEVELER_H

View File

@@ -0,0 +1,66 @@
#include "vslam/elevation_map.h"
#include <cmath>
#include <utility>
namespace vslam {
ElevationMap::ElevationMap(ElevationMapConfig cfg) : cfg_(std::move(cfg)) {}
uint64_t ElevationMap::Key(double x, double y) const {
// Hai chi so 32 bit co dau gop vao 64 bit KHONG dau: dich trai so am la hanh
// vi khong xac dinh truoc C++20.
const int32_t ix = static_cast<int32_t>(std::floor(x / cfg_.cell_m));
const int32_t iy = static_cast<int32_t>(std::floor(y / cfg_.cell_m));
return (static_cast<uint64_t>(static_cast<uint32_t>(ix)) << 32) | static_cast<uint32_t>(iy);
}
void ElevationMap::AddPoints(const std::vector<PointXYZ>& pts, UnixTime stamp) {
const int64_t ns = stamp.toNSec();
for (const PointXYZ& p : pts) {
if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) continue;
auto it = cells_.find(Key(p.x, p.y));
if (it == cells_.end()) {
cells_.emplace(Key(p.x, p.y), Cell{p.z, ns});
} else {
if (p.z < it->second.z) it->second.z = p.z;
it->second.stamp_ns = ns;
}
}
}
void ElevationMap::AddFootprint(double x, double y, double z, double r, UnixTime stamp) {
const int64_t ns = stamp.toNSec();
const int n = static_cast<int>(std::ceil(r / cfg_.cell_m));
for (int dx = -n; dx <= n; ++dx) {
for (int dy = -n; dy <= n; ++dy) {
const double px = x + dx * cfg_.cell_m, py = y + dy * cfg_.cell_m;
if (std::hypot(px - x, py - y) > r) continue;
cells_[Key(px, py)] = Cell{static_cast<float>(z), ns};
}
}
}
bool ElevationMap::FloorAt(double x, double y, double* z) const {
auto it = cells_.find(Key(x, y));
if (it == cells_.end()) return false;
*z = it->second.z;
return true;
}
void ElevationMap::Prune(double cx, double cy, UnixTime now) {
const int64_t now_ns = now.toNSec();
const int64_t max_age = static_cast<int64_t>(cfg_.max_age_sec * 1e9);
for (auto it = cells_.begin(); it != cells_.end();) {
const int32_t ix = static_cast<int32_t>(static_cast<uint32_t>(it->first >> 32));
const int32_t iy = static_cast<int32_t>(static_cast<uint32_t>(it->first & 0xffffffffu));
const double x = (ix + 0.5) * cfg_.cell_m, y = (iy + 0.5) * cfg_.cell_m;
if (std::hypot(x - cx, y - cy) > cfg_.radius_m || now_ns - it->second.stamp_ns > max_age) {
it = cells_.erase(it);
} else {
++it;
}
}
}
} // namespace vslam

View File

@@ -0,0 +1,87 @@
#include "vslam/scan_leveler.h"
#include <cmath>
#include <limits>
#include <vector>
#include "vslam/geometry.h"
#include "vslam/gravity_align.h"
namespace vslam {
void LevelScan(const LaserScan& in, const Transform& laser_to_base, double roll,
double pitch, double wheel_radius, const Pose2D& odom, double z_robot,
const ElevationMap* em, const ScanLevelerConfig& cfg, LaserScan* out,
ScanLevelStats* stats) {
ScanLevelStats local;
ScanLevelStats& st = stats != nullptr ? *stats : local;
st.scans++;
*out = in;
const size_t n = in.ranges.size();
if (n == 0 || in.angle_increment == 0.0f) return;
const float nan = std::numeric_limits<float>::quiet_NaN();
out->ranges.assign(n, nan);
if (!out->intensities.empty()) out->intensities.assign(n, 0.0f);
// base_footprint (can trong luc) <- laser_link.
const Transform fp_from_laser =
TransformCompose(TiltLift(roll, pitch, wheel_radius), laser_to_base);
// Goc LiDAR TINH tren mat phang ngang (RTAB-Map dat diem bang ngoai lai
// tinh, nen tinh lai khoang cach/goc tu dung goc do).
const double lx0 = laser_to_base.translation.x, ly0 = laser_to_base.translation.y;
const double laser_yaw = YawFromQuaternion(laser_to_base.rotation);
const double c_th = std::cos(odom.theta), s_th = std::sin(odom.theta);
// Luot 1: tia KHONG huu han va sentinel cua sector mask giu nguyen o cu.
// Sentinel (> range_max) = than robot bi cat -- khong tia nao duoc de len.
std::vector<char> masked(n, 0);
for (size_t i = 0; i < n; ++i) {
const float r = in.ranges[i];
if (std::isnan(r)) continue;
if (std::isinf(r)) {
out->ranges[i] = r;
} else if (r > in.range_max) {
out->ranges[i] = r;
masked[i] = 1;
}
}
// Luot 2: tia huu han -> chieu, loc, xep lai.
for (size_t i = 0; i < n; ++i) {
const float r = in.ranges[i];
if (!std::isfinite(r) || r > in.range_max) continue;
st.rays++;
const double a = in.angle_min + static_cast<double>(i) * in.angle_increment;
const Point p = TransformPoint(fp_from_laser, Point{r * std::cos(a), r * std::sin(a), 0.0});
const double h = p.z;
const double wx = odom.x + c_th * p.x - s_th * p.y;
const double wy = odom.y + s_th * p.x + c_th * p.y;
double floor = 0.0;
if (em != nullptr && em->FloorAt(wx, wy, &floor)) {
if (z_robot + h - floor < cfg.floor_margin) {
st.dropped_floor++;
continue;
}
} else if (h < -cfg.below_ground) {
st.dropped_below++;
continue;
}
const double dx = p.x - lx0, dy = p.y - ly0;
const double rh = std::hypot(dx, dy);
const double ah = NormalizeAngle(std::atan2(dy, dx) - laser_yaw);
const long j = std::lround((ah - in.angle_min) / in.angle_increment);
if (j < 0 || j >= static_cast<long>(n) || masked[static_cast<size_t>(j)]) continue;
if (static_cast<size_t>(j) != i) st.rebinned++;
float& slot = out->ranges[static_cast<size_t>(j)];
if (!std::isfinite(slot) || rh < slot) {
slot = static_cast<float>(rh);
if (!out->intensities.empty() && i < in.intensities.size()) {
out->intensities[static_cast<size_t>(j)] = in.intensities[i];
}
}
st.kept++;
}
}
} // namespace vslam