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

@@ -7,6 +7,7 @@ vslam_add_library(vslam_odometry
PUBLIC_HDRS
wheel_odometry.h
ekf_se2.h
height_estimator.h
PUBLIC_DEPS
vslam_common
FIND_DEPS

View File

@@ -0,0 +1,102 @@
#ifndef VSLAM_HEIGHT_ESTIMATOR_H
#define VSLAM_HEIGHT_ESTIMATOR_H
// Do cao z cua robot (base_footprint) khi di tren doc: bo loc Kalman MOT
// chieu.
//
// ================== VI SAO KHONG LAY TU SLAM ==================
// Pose SLAM la SE(2) (`Reg/Force3DoF`): RTAB-Map khong co z. Lop nay tinh z
// BEN CANH, roi z di vao node ban do (userData), ban do 3D va web.
//
// ================== DU DOAN ==================
// dz = -ds * sin(pitch)
// ds = quang duong banh (DOC mat doc), pitch = GravityAlign (duong = mui chuc
// xuong, nen len doc pitch am). Da so trong sim (G3, 03/10): IMU 100 Hz cho sai
// dz qua doc 1.5 m trung vi 1.8 cm, max 8 cm -- tot hon odometry thi giac F2M
// (6 / 33 cm). IMU 10 Hz thi KHONG du (19 cm): bo loc bu cua GravityAlign o
// 10 Hz co hang so thoi gian 5 s, bias gyro bi khuech dai thanh ~0.6 do.
//
// KEP MAT BANG: |pitch| < clamp -> dz = 0. Bias gia toc ke 0.02 m/s^2 lam
// lech pitch 0.12 do = 21 cm troi moi 100 m tren san PHANG; kep la triet tieu
// no. Doi lai: doc that < clamp (mac dinh 1.5 do) bi coi la phang -- 1 do qua
// 30 m la 52 cm mat. Doc tiep can cho xe lan la >= 2.9 do (1:20), nen nguong
// 1.5 do khong cat doc nao trong pham vi du an.
//
// ================== NEO ==================
// `Anchor(z_ref)`: z cua ban do tai cho robot dang dung (node gan nhat). Do
// khong chac lon dan theo do cao DA LEO tu lan neo truoc (sai ti le tren doc)
// va theo quang duong (sai nho tren phang), nen sau mot doan doc thi neo keo
// manh, tren san phang thi neo keo nhe. Lech qua xa -> tu choi (node sai tang,
// sai cho).
//
// ⚠️ Sai tren doc la sai HE THONG (truot banh, lech pitch cung dau suot doan
// doc), nen no tang TUYEN TINH theo do cao da leo: sigma = rel * sum|dz|.
// Cong phuong sai tung buoc (sum (rel*dz)^2) la coi moi buoc doc lap -- ra
// sigma 3 mm sau 1.4 m doc thay vi 7 cm, va neo khong keo duoc gi.
//
// Khong khoa: nguoi goi giu mutex (luong encoder ghi, luong slam neo).
#include <cstdint>
namespace vslam {
struct HeightEstimatorConfig {
double clamp_rad = 0.0261799; // 1.5 do: duoi nguong = mat bang, dz = 0
// Sai tuong doi cua dz tren doc (truot banh, sai pitch): sigma = rel * |dz|
// cong don tu lan neo truoc.
double sigma_rel = 0.05;
// Troi tren mat bang, m / sqrt(m): sigma = flat * sqrt(quang duong).
double sigma_flat = 0.005;
// Sai cua z ban do dung de neo (z node + noi suy theo doc), m.
double sigma_anchor = 0.03;
// Neo lech hon max(gate_min, gate_sigma * sigma) thi tu choi.
double gate_min = 0.30;
double gate_sigma = 4.0;
};
struct HeightEstimatorStats {
uint64_t steps = 0;
uint64_t clamped = 0; // buoc bi kep ve 0 (mat bang)
uint64_t anchors = 0;
uint64_t anchors_rejected = 0;
double last_anchor_err = 0.0; // z_ref - z truoc khi neo
double climbed = 0.0; // tong |dz| da cong
};
class HeightEstimator {
public:
explicit HeightEstimator(HeightEstimatorConfig cfg = {});
// Dat lai z (vd sau dinh vi lai: z cua ban do tai pose moi). sigma = do
// khong chac ban dau.
void Reset(double z, double sigma);
// Mot buoc banh: ds co dau (lui = am), pitch cua than LUC do.
void OnWheelStep(double ds, double pitch);
// Keo z ve z_ref (z ban do tai cho robot). Tra false neu bi tu choi.
bool Anchor(double z_ref);
double z() const { return z_; }
double sigma() const;
double pitch() const { return pitch_; } // pitch cua buoc cuoi
bool on_slope() const { return on_slope_; } // buoc cuoi KHONG bi kep
const HeightEstimatorStats& stats() const { return stats_; }
const HeightEstimatorConfig& config() const { return cfg_; }
private:
HeightEstimatorConfig cfg_;
double P() const;
double z_ = 0.0;
double P_base_ = 0.0; // phuong sai sau lan neo / reset cuoi
double climb_ = 0.0; // sum |dz| tu do
double dist_ = 0.0; // sum |ds| tu do
double pitch_ = 0.0;
bool on_slope_ = false;
HeightEstimatorStats stats_;
};
} // namespace vslam
#endif // VSLAM_HEIGHT_ESTIMATOR_H

View File

@@ -0,0 +1,60 @@
#include "vslam/height_estimator.h"
#include <algorithm>
#include <cmath>
#include <utility>
namespace vslam {
HeightEstimator::HeightEstimator(HeightEstimatorConfig cfg) : cfg_(std::move(cfg)) {}
void HeightEstimator::Reset(double z, double sigma) {
z_ = z;
P_base_ = sigma * sigma;
climb_ = 0.0;
dist_ = 0.0;
}
double HeightEstimator::P() const {
const double rel = cfg_.sigma_rel * climb_;
return P_base_ + rel * rel + cfg_.sigma_flat * cfg_.sigma_flat * dist_;
}
double HeightEstimator::sigma() const { return std::sqrt(P()); }
void HeightEstimator::OnWheelStep(double ds, double pitch) {
stats_.steps++;
pitch_ = pitch;
on_slope_ = std::fabs(pitch) >= cfg_.clamp_rad;
double dz = 0.0;
if (on_slope_) {
dz = -ds * std::sin(pitch);
} else {
stats_.clamped++;
}
z_ += dz;
stats_.climbed += std::fabs(dz);
climb_ += std::fabs(dz);
dist_ += std::fabs(ds);
}
bool HeightEstimator::Anchor(double z_ref) {
const double err = z_ref - z_;
stats_.last_anchor_err = err;
const double R = cfg_.sigma_anchor * cfg_.sigma_anchor;
const double P_now = P();
const double gate = std::max(cfg_.gate_min, cfg_.gate_sigma * std::sqrt(P_now + R));
if (std::fabs(err) > gate) {
stats_.anchors_rejected++;
return false;
}
const double K = P_now / (P_now + R);
z_ += K * err;
P_base_ = (1.0 - K) * P_now;
climb_ = 0.0;
dist_ = 0.0;
stats_.anchors++;
return true;
}
} // namespace vslam

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

View File

@@ -38,6 +38,7 @@ vslam_add_library(vslam_slam
sensor_data_builder.h
occupancy_export.h
voxel_export.h
height_map.h
global_localizer.h
vpr_encoder.h
visual_verifier.h

View File

@@ -0,0 +1,98 @@
#ifndef VSLAM_HEIGHT_MAP_H
#define VSLAM_HEIGHT_MAP_H
// z cua ban do khi pose SLAM la SE(2): do cao tung node va mat san quanh no.
//
// ================== NGUON ==================
// Moi node mang z / roll / pitch DO luc tao (HeightEstimator + GravityAlign),
// luu trong userData cua RTAB-Map. z do co troi (truot banh, sai pitch), va
// do thi SE(2) cua RTAB-Map khong biet gi ve no.
//
// ================== DO THI z 1-D ==================
// Sau moi lan toi uu cua RTAB-Map, giai lai z bang binh phuong toi thieu:
// * lien ket LANG GIENG (node lien tiep): z_j - z_i = z_do_j - z_do_i,
// sigma = min + rel * |dz| (sai he thong ti le voi do cao leo);
// * lien ket VONG / lan can (cung cho, hai lan qua): z_j - z_i = mat phang
// doc cua node i keo toi node j. Ca hai node PHANG (|pitch| < clamp) ->
// 0, chat (3 cm). Tren doc -> theo pitch, long hon (hai node cach 1 m tren
// doc 30 do lech 0.58 m -- dz = 0 o day la SAI);
// * node dau tien neo o z do cua no; moi node co mot prior rat long ve z do
// de do thi khong lien thong van giai duoc.
// Nho vay khi robot quay ve cho cu sau mot vong co doc, sai z don ra deu
// tren vong thay vi don het vao mot cho.
//
// ================== MAT SAN ==================
// FloorModel: do cao san tai (x, y) = MAT PHANG cua node GAN NHAT (z, pitch,
// roll cua no) keo toi do. Dung cho: cat voxel theo do cao TREN san, lop vat
// can tinh theo san cuc bo (doc khong con la tuong), va neo z robot luc dinh
// vi. Node gan nhat chu khong trung binh: canh dinh doc, trung binh hai mat
// phang (doc + chieu nghi) cho ra mot san khong co that.
//
// Khong phu thuoc RTAB-Map -- test duoc rieng.
#include <map>
#include <vector>
namespace vslam {
struct NodeHeight {
double z = 0.0; // do cao base_footprint (mat san duoi robot), m
double roll = 0.0; // nghieng than luc tao node (ROS: duong = trai len)
double pitch = 0.0; // duong = mui chuc xuong
};
struct ZGraphLink {
int from = 0;
int to = 0;
bool neighbor = false; // lien tiep (odometry) hay vong / lan can
// Vi tri node `to` trong frame node `from` (mat phang ngang), m.
double dx = 0.0;
double dy = 0.0;
};
struct ZGraphConfig {
double clamp_rad = 0.0261799; // 1.5 do: duoi nguong = phang
double sigma_neighbor = 0.01;
double sigma_rel = 0.05;
double sigma_loop_flat = 0.03;
double sigma_loop_slope = 0.10;
double sigma_first = 0.01;
double sigma_prior = 2.0; // prior long ve z do cua MOI node
};
// Tra z da toi uu cho moi node trong `meas`. Lien ket toi node khong co trong
// `meas` bi bo qua.
std::map<int, double> SolveZGraph(const std::map<int, NodeHeight>& meas,
const std::vector<ZGraphLink>& links,
const ZGraphConfig& cfg = {});
struct FloorNode {
double x = 0.0, y = 0.0, theta = 0.0; // pose (da toi uu) trong frame map
double z = 0.0, roll = 0.0, pitch = 0.0;
};
class FloorModel {
public:
// radius: chi dung node trong ban kinh nay (camera thay toi ~4 m).
void Build(const std::vector<FloorNode>& nodes, double radius = 4.0, double cell = 0.25);
// Do cao san tai (x, y) theo mat phang node gan nhat; false neu khong node
// nao trong ban kinh. `dist`: khoang cach toi node do.
bool FloorAt(double x, double y, double* z, double* dist = nullptr) const;
// Node gan nhat (trong ban kinh) cua (x, y); false neu khong co.
bool NearestNode(double x, double y, FloorNode* node, double* dist = nullptr) const;
bool empty() const { return nodes_.empty(); }
size_t size() const { return nodes_.size(); }
private:
std::vector<FloorNode> nodes_;
double radius_ = 4.0, cell_ = 0.25, x0_ = 0.0, y0_ = 0.0;
int w_ = 0, h_ = 0;
std::vector<int> nearest_; // moi o: chi so node gan nhat, -1 = khong co
};
// Do cao mat phang cua node n tai (x, y).
double PlaneZ(const FloorNode& n, double x, double y);
} // namespace vslam
#endif // VSLAM_HEIGHT_MAP_H

View File

@@ -24,6 +24,7 @@
#include <opencv2/core.hpp>
#include "vslam/height_map.h"
#include "vslam/rtabmap_params.h"
#include "vslam/slam_types.h"
#include "vslam/voxel_export.h"
@@ -101,6 +102,20 @@ class RtabmapSlam {
std::vector<int> MapNodeIds() const;
StatusResponse GetNodeView(int id, bool with_depth, NodeView* out);
// ---- z ban do khi di tren doc (height_map.h) ----
// Giai lai do thi z tu z DO cua node (userData) va lien ket cua do thi,
// dung lai mat san. Node chua co z do thi nap tu DB (ban do quet truoc).
// CHI luong slam goi. `nodes_out`: so node co z.
StatusResponse UpdateHeights(const ZGraphConfig& cfg, size_t* nodes_out = nullptr);
// Do cao san tai (x, y) frame map theo mat phang node gan nhat, chi khi
// node do trong `radius`. false neu khong co. CHI luong slam goi.
bool HeightAt(double x, double y, double radius, double* z) const;
const FloorModel& floor_model() const;
// Bat dung z node + mat san cho ban do voxel (sau UpdateHeights).
void SetUseHeights(bool on);
// O 2D noi camera thay mat san trong lan ExportVoxels cuoi (che do doc).
const std::vector<uint64_t>& floor_cells() const;
StatusResponse Close(bool save_database);
const SlamStats& stats() const;

View File

@@ -51,6 +51,13 @@ struct SlamInput {
// Than robot chuc len/xuong khi tang/giam toc. Coi camera nam ngang thi san
// o 3 m bi day len ~0.2 m va thanh vat can gia trong ban do 3D.
const Transform* camera_to_base = nullptr;
// Di tren doc (slam.yaml `slope:`): do cao base_footprint va nghieng than
// luc nay. Co thi luu vao node (userData cua RTAB-Map) -- xem height_map.h.
bool has_height = false;
double z = 0.0;
double roll = 0.0;
double pitch = 0.0;
};
struct SlamOutput {

View File

@@ -22,9 +22,11 @@
#include <cstddef>
#include <cstdint>
#include <map>
#include <unordered_map>
#include <vector>
#include "vslam/height_map.h"
#include "vslam/vslam_common.h"
namespace rtabmap {
@@ -65,13 +67,32 @@ struct VoxelExportOptions {
// nhieu depth va nguoi di ngang chi o 1-2 khung; tuong, ban ghe dung yen
// thi nhieu node thay. 1 = giu moi voxel (hanh vi cu).
size_t min_node_hits = 1;
// Di tren doc: z cua tung node (do thi z) + mat san. Co ca hai thi diem dat
// o z THAT (z node + z cuc bo), va dai z_min..z_max tinh tu SAN tai cho diem
// do (mat doc khong lot vao ban do nhu vat can). Null = duong cu (moi node
// o z = 0, dai tinh tu san duoi node).
const std::map<int, double>* node_z = nullptr;
const FloorModel* floor = nullptr;
// Che do doc: thu cac o 2D (kich thuoc emit_size_m, khoa PackFloorCell) noi
// camera THAY MAT SAN (|cao - san| <= floor_band). Dung cho
// ClearLidarOnSeenRamps. Null = khong thu.
std::vector<uint64_t>* floor_cells = nullptr;
double floor_band = 0.05;
};
// Khoa o 2D (ix, iy) cua floor_cells.
inline uint64_t PackFloorCell(int32_t ix, int32_t iy) {
return (static_cast<uint64_t>(static_cast<uint32_t>(ix)) << 32) | static_cast<uint32_t>(iy);
}
// Diem cua tung node, o frame CUA NODE, da gom voxel. Tinh MOT lan roi giu:
// depth cua node khong doi, chi pose cua no doi sau moi lan toi uu.
// CHI luong slam cham vao.
struct VoxelNodeCache {
std::unordered_map<int, std::vector<float>> nodes; // id -> x,y,z,x,y,z...
// Diem node da cat theo dai z RONG (che do doc). Doi che do thi xoa cache.
bool wide = false;
};
// Gom diem -> tap voxel. KHONG chua kieu nao cua RTAB-Map, nen test duoc ma
@@ -156,8 +177,26 @@ void NodeCloudFromSensorData(const rtabmap::SensorData& data,
// mat san). La LOP VAT CAN tu camera cho nhung thu LiDAR (0.22 m) khong cat
// ngang: mat ghe, khung giuong, ke thap. Chi THEM o bi chiem, khong xoa.
// Tra ve so o moi danh dau (truoc do trong/chua biet).
//
// `floor` (di tren doc): do cao tinh tu SAN TAI CHO voxel (FloorModel) thay vi
// tu z = 0 -- mat doc va san tang tren khong con bi danh la tuong. Null =
// duong cu.
size_t MarkVoxelObstacles(const VoxelGrid& vg, double z_min, double z_max,
OccupancyGrid* grid);
OccupancyGrid* grid, const FloorModel* floor = nullptr);
// Di tren doc: TREN DOC, lop LiDAR nhuong cho camera.
//
// LiDAR ngang thay mat doc len tu xa (> 4 m, ngoai tam depth) nhu mot buc
// tuong -- tuong gia vao node TRUOC khi camera kip thay mat doc, va cac scan
// sau BO tia cham doc (NaN) nen khong tia nao xoa duoc no. O day: o LiDAR bi
// chiem ma (1) node ban do gan nhat (trong `max_node_dist`) dang TREN DOC
// (|pitch| >= clamp) va (2) camera da THAY MAT SAN o do (floor_cells) -> tra
// ve trong. Vat can THAT tren doc van duoc MarkVoxelObstacles danh lai (goi
// SAU ham nay). Camera chua thay thi giu nguyen -- khong co bang chung.
// Tra ve so o da xoa.
size_t ClearLidarOnSeenRamps(OccupancyGrid* grid, const std::vector<uint64_t>& floor_cells,
double cell_size, const FloorModel& floor, double clamp_rad,
double max_node_dist);
// Hieu hai tap DA SAP XEP: `added` co trong `now` ma khong co trong `prev`,
// `removed` thi nguoc lai. O(n + m), mot luot.

View File

@@ -0,0 +1,150 @@
#include "vslam/height_map.h"
#include <Eigen/Sparse>
#include <Eigen/SparseCholesky>
#include <algorithm>
#include <cmath>
#include <limits>
namespace vslam {
std::map<int, double> SolveZGraph(const std::map<int, NodeHeight>& meas,
const std::vector<ZGraphLink>& links,
const ZGraphConfig& cfg) {
std::map<int, double> out;
if (meas.empty()) return out;
std::map<int, int> index;
for (const auto& kv : meas) {
const int k = static_cast<int>(index.size());
index[kv.first] = k;
}
const int n = static_cast<int>(index.size());
// Phuong trinh chuan H z = g, H = sum w a a^T.
std::vector<Eigen::Triplet<double>> trip;
Eigen::VectorXd g = Eigen::VectorXd::Zero(n);
auto add_rel = [&](int i, int j, double dz, double sigma) {
// z_j - z_i = dz
const double w = 1.0 / (sigma * sigma);
trip.emplace_back(i, i, w);
trip.emplace_back(j, j, w);
trip.emplace_back(i, j, -w);
trip.emplace_back(j, i, -w);
g(i) -= w * dz;
g(j) += w * dz;
};
auto add_abs = [&](int i, double z, double sigma) {
const double w = 1.0 / (sigma * sigma);
trip.emplace_back(i, i, w);
g(i) += w * z;
};
for (const auto& kv : meas) add_abs(index[kv.first], kv.second.z, cfg.sigma_prior);
add_abs(index[meas.begin()->first], meas.begin()->second.z, cfg.sigma_first);
for (const ZGraphLink& l : links) {
auto a = meas.find(l.from), b = meas.find(l.to);
if (a == meas.end() || b == meas.end() || l.from == l.to) continue;
const int i = index[l.from], j = index[l.to];
if (l.neighbor) {
const double dz = b->second.z - a->second.z;
add_rel(i, j, dz, cfg.sigma_neighbor + cfg.sigma_rel * std::fabs(dz));
continue;
}
const bool flat_a = std::fabs(a->second.pitch) < cfg.clamp_rad;
const bool flat_b = std::fabs(b->second.pitch) < cfg.clamp_rad;
if (flat_a && flat_b) {
add_rel(i, j, 0.0, cfg.sigma_loop_flat);
} else {
// Mat phang doc cua node `from`, keo toi `to`.
FloorNode fa;
fa.roll = a->second.roll;
fa.pitch = a->second.pitch;
const double dz = PlaneZ(fa, l.dx, l.dy);
add_rel(i, j, dz, cfg.sigma_loop_slope + cfg.sigma_rel * std::fabs(dz));
}
}
Eigen::SparseMatrix<double> H(n, n);
H.setFromTriplets(trip.begin(), trip.end());
Eigen::SimplicialLDLT<Eigen::SparseMatrix<double>> solver(H);
if (solver.info() != Eigen::Success) {
for (const auto& kv : meas) out[kv.first] = kv.second.z;
return out;
}
const Eigen::VectorXd z = solver.solve(g);
for (const auto& kv : index) out[kv.first] = z(kv.second);
return out;
}
double PlaneZ(const FloorNode& n, double x, double y) {
const double dx = x - n.x, dy = y - n.y;
const double c = std::cos(n.theta), s = std::sin(n.theta);
const double fwd = c * dx + s * dy;
const double left = -s * dx + c * dy;
// Ry(pitch): +x chuc xuong khi pitch duong; Rx(roll): +y len khi roll duong.
return n.z - fwd * std::tan(n.pitch) + left * std::tan(n.roll);
}
void FloorModel::Build(const std::vector<FloorNode>& nodes, double radius, double cell) {
nodes_ = nodes;
radius_ = radius;
cell_ = cell;
nearest_.clear();
w_ = h_ = 0;
if (nodes_.empty()) return;
double xmin = std::numeric_limits<double>::max(), ymin = xmin;
double xmax = std::numeric_limits<double>::lowest(), ymax = xmax;
for (const FloorNode& f : nodes_) {
xmin = std::min(xmin, f.x);
xmax = std::max(xmax, f.x);
ymin = std::min(ymin, f.y);
ymax = std::max(ymax, f.y);
}
x0_ = xmin - radius - cell;
y0_ = ymin - radius - cell;
w_ = static_cast<int>(std::ceil((xmax - xmin + 2 * radius + 2 * cell) / cell));
h_ = static_cast<int>(std::ceil((ymax - ymin + 2 * radius + 2 * cell) / cell));
nearest_.assign(static_cast<size_t>(w_) * h_, -1);
std::vector<float> best(nearest_.size(), std::numeric_limits<float>::max());
const int rc = static_cast<int>(std::ceil(radius / cell));
const double r2 = radius * radius;
for (size_t k = 0; k < nodes_.size(); ++k) {
const FloorNode& f = nodes_[k];
const int cx = static_cast<int>((f.x - x0_) / cell), cy = static_cast<int>((f.y - y0_) / cell);
for (int iy = std::max(0, cy - rc); iy <= std::min(h_ - 1, cy + rc); ++iy) {
for (int ix = std::max(0, cx - rc); ix <= std::min(w_ - 1, cx + rc); ++ix) {
const double px = x0_ + (ix + 0.5) * cell, py = y0_ + (iy + 0.5) * cell;
const double d2 = (px - f.x) * (px - f.x) + (py - f.y) * (py - f.y);
if (d2 > r2) continue;
const size_t i = static_cast<size_t>(iy) * w_ + ix;
if (d2 < best[i]) {
best[i] = static_cast<float>(d2);
nearest_[i] = static_cast<int>(k);
}
}
}
}
}
bool FloorModel::NearestNode(double x, double y, FloorNode* node, double* dist) const {
if (nodes_.empty()) return false;
const int ix = static_cast<int>(std::floor((x - x0_) / cell_));
const int iy = static_cast<int>(std::floor((y - y0_) / cell_));
if (ix < 0 || iy < 0 || ix >= w_ || iy >= h_) return false;
const int k = nearest_[static_cast<size_t>(iy) * w_ + ix];
if (k < 0) return false;
*node = nodes_[static_cast<size_t>(k)];
if (dist != nullptr) *dist = std::hypot(x - node->x, y - node->y);
return true;
}
bool FloorModel::FloorAt(double x, double y, double* z, double* dist) const {
FloorNode f;
if (!NearestNode(x, y, &f, dist)) return false;
*z = PlaneZ(f, x, y);
return true;
}
} // namespace vslam

View File

@@ -11,6 +11,7 @@
#include <algorithm>
#include <chrono>
#include <mutex>
#include <set>
#include "vslam/geometry.h"
#include "vslam/log.h"
@@ -45,6 +46,14 @@ class RtabmapSlamImpl {
// Diem depth theo node cho ban do voxel. Chi ExportVoxels (luong slam) cham.
VoxelNodeCache voxel_cache;
// ---- z ban do (di tren doc). CHI luong slam cham. ----
std::map<int, NodeHeight> node_meas; // z DO luc tao node
std::set<int> node_no_height; // node DB khong co userData z
std::map<int, double> node_z; // z sau do thi z
FloorModel floor;
bool use_heights = false;
std::vector<uint64_t> floor_cells;
void RecordMs(double ms) {
stats.last_ms = ms;
stats.max_ms = std::max(stats.max_ms, ms);
@@ -282,6 +291,15 @@ SlamOutput RtabmapSlam::Process(const SlamInput& in) {
in.camera_to_base != nullptr ? *in.camera_to_base
: impl_->camera_to_base,
impl_->next_id++);
if (in.has_height) {
// z / roll / pitch DO cua node -> DB (userData). Ban do quet xong mo lai
// o localization van co z.
cv::Mat ud(1, 3, CV_64FC1);
ud.at<double>(0) = in.z;
ud.at<double>(1) = in.roll;
ud.at<double>(2) = in.pitch;
data.setUserData(ud);
}
// Phuong sai odom: nguoi goi dat thi dung, khong thi lay tu slam.yaml.
const double lin_var = in.odom_linear_var > 0.0
@@ -319,6 +337,12 @@ SlamOutput RtabmapSlam::Process(const SlamInput& in) {
}
impl_->stats.frames_processed++;
if (in.has_height && impl_->cfg.mode == SlamMode::kMapping) {
// RTAB-Map tu sinh id node (Mem/GenerateIds) -- id cua SensorData chi la
// goi y. Node vua them la node cuoi cua bo nho.
const int id = impl_->rtabmap.getLastLocationId();
if (id > 0) impl_->node_meas[id] = NodeHeight{in.z, in.roll, in.pitch};
}
out.loop_closure_id = impl_->rtabmap.getLoopClosureId();
const rtabmap::Statistics& st = impl_->rtabmap.getStatistics();
auto stat = [&](const std::string& k, double dflt) {
@@ -377,9 +401,109 @@ StatusResponse RtabmapSlam::ExportVoxels(const VoxelExportOptions& opt,
if (!impl_->inited) {
return StatusResponse::Error(StatusCode::kFailedPrecondition, "chua init");
}
if (impl_->use_heights && !impl_->node_z.empty()) {
VoxelExportOptions o = opt;
o.node_z = &impl_->node_z;
o.floor = &impl_->floor;
impl_->floor_cells.clear();
o.floor_cells = &impl_->floor_cells;
return ExportVoxelGrid(impl_->rtabmap, o, &impl_->voxel_cache, out);
}
return ExportVoxelGrid(impl_->rtabmap, opt, &impl_->voxel_cache, out);
}
void RtabmapSlam::SetUseHeights(bool on) { impl_->use_heights = on; }
const std::vector<uint64_t>& RtabmapSlam::floor_cells() const { return impl_->floor_cells; }
const FloorModel& RtabmapSlam::floor_model() const { return impl_->floor; }
StatusResponse RtabmapSlam::UpdateHeights(const ZGraphConfig& cfg, size_t* nodes_out) {
if (!impl_->inited) {
return StatusResponse::Error(StatusCode::kFailedPrecondition, "chua init");
}
std::map<int, rtabmap::Transform> poses;
std::multimap<int, rtabmap::Link> links;
impl_->rtabmap.getGraph(poses, links, /*optimized=*/true, /*global=*/false);
if (poses.empty()) {
return StatusResponse::Error(StatusCode::kFailedPrecondition, "do thi rong");
}
// Node chua co z do: nap userData tu DB (ban do quet o phien truoc). Node
// DB khong co (quet truoc khi co che do doc) thi nho lai, khong hoi mai.
const rtabmap::Memory* mem = impl_->rtabmap.getMemory();
for (const auto& kv : poses) {
const int id = kv.first;
if (id <= 0 || impl_->node_meas.count(id) || impl_->node_no_height.count(id)) continue;
if (mem == nullptr) break;
const rtabmap::SensorData d = mem->getNodeData(id, false, false, true, false);
cv::Mat ud;
d.uncompressDataConst(nullptr, nullptr, nullptr, &ud);
if (ud.type() == CV_64FC1 && ud.total() == 3) {
impl_->node_meas[id] = NodeHeight{ud.at<double>(0), ud.at<double>(1), ud.at<double>(2)};
} else {
impl_->node_no_height.insert(id);
}
}
std::map<int, NodeHeight> meas;
for (const auto& kv : poses) {
auto it = impl_->node_meas.find(kv.first);
if (it != impl_->node_meas.end()) meas[kv.first] = it->second;
}
std::vector<ZGraphLink> zl;
for (const auto& kv : links) {
const rtabmap::Link& l = kv.second;
ZGraphLink z;
z.from = l.from();
z.to = l.to();
switch (l.type()) {
case rtabmap::Link::kNeighbor:
case rtabmap::Link::kNeighborMerged:
z.neighbor = true;
break;
case rtabmap::Link::kGlobalClosure:
case rtabmap::Link::kLocalSpaceClosure:
case rtabmap::Link::kLocalTimeClosure:
case rtabmap::Link::kUserClosure:
z.neighbor = false;
break;
default:
continue;
}
if (l.transform().isNull()) continue;
z.dx = l.transform().x();
z.dy = l.transform().y();
zl.push_back(z);
}
impl_->node_z = SolveZGraph(meas, zl, cfg);
std::vector<FloorNode> fn;
fn.reserve(impl_->node_z.size());
for (const auto& kv : impl_->node_z) {
const auto p = poses.find(kv.first);
if (p == poses.end() || p->second.isNull()) continue;
const NodeHeight& m = meas[kv.first];
FloorNode f;
f.x = p->second.x();
f.y = p->second.y();
f.theta = p->second.theta();
f.z = kv.second;
f.roll = m.roll;
f.pitch = m.pitch;
fn.push_back(f);
}
impl_->floor.Build(fn);
if (nodes_out != nullptr) *nodes_out = impl_->node_z.size();
return StatusResponse::Ok();
}
bool RtabmapSlam::HeightAt(double x, double y, double radius, double* z) const {
double d = 0.0;
if (!impl_->floor.FloorAt(x, y, z, &d)) return false;
return d <= radius;
}
namespace {
cv::Matx44d ToMatx44(const rtabmap::Transform& t) {
return cv::Matx44d(t.r11(), t.r12(), t.r13(), t.x(), t.r21(), t.r22(), t.r23(), t.y(),

View File

@@ -230,13 +230,17 @@ void DiffVoxelGrids(const std::vector<VoxelIndex>& prev,
}
size_t MarkVoxelObstacles(const VoxelGrid& vg, double z_min, double z_max,
OccupancyGrid* grid) {
OccupancyGrid* grid, const FloorModel* floor) {
if (grid->width == 0 || grid->height == 0 || grid->resolution <= 0.0f) return 0;
const double r = vg.resolution;
const double inv = 1.0 / grid->resolution;
size_t marked = 0;
for (const VoxelIndex& k : vg.cells) {
const double z = vg.origin_z + (k.z + 0.5) * r;
double z = vg.origin_z + (k.z + 0.5) * r;
if (floor != nullptr) {
double f = 0.0;
if (floor->FloorAt(vg.origin_x + (k.x + 0.5) * r, vg.origin_y + (k.y + 0.5) * r, &f)) z -= f;
}
if (z < z_min || z > z_max) continue;
// Luoi 2D khong xoay (occupancy_export dat orientation = dong nhat), va
// hang 0 la ymin -- cung quy uoc voi RTAB-Map/map_server.
@@ -255,6 +259,38 @@ size_t MarkVoxelObstacles(const VoxelGrid& vg, double z_min, double z_max,
return marked;
}
size_t ClearLidarOnSeenRamps(OccupancyGrid* grid, const std::vector<uint64_t>& floor_cells,
double cell_size, const FloorModel& floor, double clamp_rad,
double max_node_dist) {
if (grid->width == 0 || grid->height == 0 || grid->resolution <= 0.0f || floor_cells.empty()) {
return 0;
}
std::vector<uint64_t> seen(floor_cells);
std::sort(seen.begin(), seen.end());
seen.erase(std::unique(seen.begin(), seen.end()), seen.end());
const double res = grid->resolution;
const double inv = 1.0 / cell_size;
size_t cleared = 0;
for (uint32_t r = 0; r < grid->height; ++r) {
for (uint32_t c = 0; c < grid->width; ++c) {
int8_t& v = grid->data[static_cast<size_t>(r) * grid->width + c];
if (v < 50) continue;
const double x = grid->origin.position.x + (c + 0.5) * res;
const double y = grid->origin.position.y + (r + 0.5) * res;
FloorNode n;
double d = 0.0;
if (!floor.NearestNode(x, y, &n, &d) || d > max_node_dist) continue;
if (std::fabs(n.pitch) < clamp_rad) continue;
const uint64_t k = PackFloorCell(static_cast<int32_t>(std::floor(x * inv)),
static_cast<int32_t>(std::floor(y * inv)));
if (!std::binary_search(seen.begin(), seen.end(), k)) continue;
v = 0;
++cleared;
}
}
return cleared;
}
void NodeCloudFromSensorData(const rtabmap::SensorData& data,
const VoxelExportOptions& opt,
std::vector<float>* out) {
@@ -298,11 +334,30 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
it = poses.count(it->first) ? std::next(it) : cache->nodes.erase(it);
}
// Di tren doc: diem dat o z THAT, va dai z cat theo SAN tai cho diem -- ca
// hai viec lam O DAY, nen bo gom nhan moi z. Cache diem cua node giu dai
// rong hon (mat doc xuong phia truoc o z am so voi node).
const bool use_floor = opt.node_z != nullptr && opt.floor != nullptr;
VoxelExportOptions acc_opt = opt;
if (use_floor) {
acc_opt.z_min = -1e9;
acc_opt.z_max = 1e9;
}
VoxelExportOptions node_opt = opt;
if (use_floor) {
node_opt.z_min = opt.z_min - 3.0;
node_opt.z_max = opt.z_max + 3.0;
}
if (cache->wide != use_floor) {
cache->nodes.clear();
cache->wide = use_floor;
}
// min_node_hits > 1: dem so node thay moi voxel (loc diem bay, nguoi di
// ngang). = 1: duong cu, re hon.
const bool count_hits = opt.min_node_hits > 1;
VoxelAccumulator acc(opt);
VoxelHitCounter hits(opt);
VoxelAccumulator acc(acc_opt);
VoxelHitCounter hits(acc_opt);
size_t nodes_used = 0, fetched = 0, pending = 0, no_points = 0;
for (const auto& kv : poses) {
@@ -320,7 +375,7 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
// moi node va ban do 3D im lang rong khong.
data.uncompressData();
std::vector<float> pts;
NodeCloudFromSensorData(data, opt, &pts);
NodeCloudFromSensorData(data, node_opt, &pts);
it = cache->nodes.emplace(kv.first, std::move(pts)).first;
}
const std::vector<float>& pts = it->second;
@@ -336,12 +391,31 @@ StatusResponse ExportVoxelGrid(rtabmap::Rtabmap& slam,
const float r10 = T.r21(), r11 = T.r22(), r12 = T.r23(), ty = T.y();
const float r20 = T.r31(), r21 = T.r32(), r22 = T.r33(), tz = T.z();
double z_off = 0.0;
if (use_floor) {
const auto zi = opt.node_z->find(kv.first);
if (zi != opt.node_z->end()) z_off = zi->second;
}
if (count_hits) hits.BeginNode();
for (size_t i = 0; i + 2 < pts.size(); i += 3) {
const float px = pts[i], py = pts[i + 1], pz = pts[i + 2];
const double wx = r00 * px + r01 * py + r02 * pz + tx;
const double wy = r10 * px + r11 * py + r12 * pz + ty;
const double wz = r20 * px + r21 * py + r22 * pz + tz;
double wz = r20 * px + r21 * py + r22 * pz + tz;
if (use_floor) {
wz += z_off;
double f = z_off;
opt.floor->FloorAt(wx, wy, &f);
const double h = wz - f;
if (opt.floor_cells != nullptr && std::fabs(h) <= opt.floor_band) {
const double inv = 1.0 / opt.emit_size_m;
opt.floor_cells->push_back(
PackFloorCell(static_cast<int32_t>(std::floor(wx * inv)),
static_cast<int32_t>(std::floor(wy * inv))));
}
if (h < opt.z_min || h > opt.z_max) continue;
}
if (count_hits) hits.AddPoint(wx, wy, wz);
else acc.AddPoint(wx, wy, wz);
}

View File

@@ -138,6 +138,13 @@ struct WebSnapshot {
double pitch = 0.0;
bool accel_ok = true;
// Di tren doc (slam.yaml `slope:`): z UOC LUONG cua robot. Chi xuat khi bat
// -- 0 khi tat la noi doi, client khong phan biet duoc "o san" voi "khong biet".
bool have_height = false;
double height_z = 0.0;
double height_sigma = 0.0;
bool on_slope = false;
// ============ GROUND TRUTH ============
// CHI co khi chay mo phong. Day la tuong duong cua /ground_truth/odom trong
// repo cu, noi ghi ro "khong estimator nao duoc subscribe".

View File

@@ -115,6 +115,15 @@ void WriteStateJson(const WebSnapshot& s, std::string* out) {
Num("x", s.pose_map.x, 3, out); Comma(out);
Num("y", s.pose_map.y, 3, out); Comma(out);
Num("yaw", s.pose_map.theta, 4, out);
if (s.have_height) {
// z + nghieng: khung 3D dat robot len doc. Chi khi BAT che do doc.
Comma(out);
Num("z", s.height_z, 3, out); Comma(out);
Num("roll", s.roll, 4, out); Comma(out);
Num("pitch", s.pitch, 4, out); Comma(out);
Num("z_sigma", s.height_sigma, 3, out); Comma(out);
Bool("on_slope", s.on_slope, out);
}
out->push_back('}'); Comma(out);
// Bu do tre cua scan: quy tia tu pose HIEN TAI ve pose LUC QUET. Cloud

View File

@@ -396,6 +396,11 @@ function onState(s) {
txt('gtErr', s.gt ? fmt(s.gt.err, 3) + ' m' : '--');
// Do cao san THAT (sim co doc). Pose uoc luong hien la 2D: chua co z.
txt('gtZ', s.gt && s.gt.z !== undefined ? fmt(s.gt.z, 3) + ' m' : '--');
// z uoc luong: chi co khi slam.yaml slope.enabled. +-sigma, (doc) khi |pitch|
// vuot nguong kep.
txt('estZ', s.pose && s.pose.z !== undefined
? fmt(s.pose.z, 3) + ' m \u00b1' + fmt(s.pose.z_sigma, 2) + (s.pose.on_slope ? ' (doc)' : '')
: '--');
state.slamMode = s.slam.mode;
txt('mode', s.slam.mode);

View File

@@ -74,6 +74,7 @@
cho biet uoc luong dang tot hay te. -->
<tr><td>sai so</td><td id="gtErr">--</td></tr>
<!-- Do cao san that (mo phong co doc). Uoc luong chua co z. -->
<tr><td>z uoc luong</td><td id="estZ">--</td></tr>
<tr><td>z that</td><td id="gtZ">--</td></tr>
</table>
</section>