upload
This commit is contained in:
26
libs/vslam_perception/CMakeLists.txt
Normal file
26
libs/vslam_perception/CMakeLists.txt
Normal file
@@ -0,0 +1,26 @@
|
||||
# Co y KHONG phu thuoc PCL.
|
||||
#
|
||||
# Ban ROS dung pcl::PassThrough + pcl::VoxelGrid + pcl::StatisticalOutlier.
|
||||
# Ba cai do cong lai ~100 dong thuat toan da biet ro. Doi lai:
|
||||
# - lib nay build va test duoc tren may KHONG co PCL (cung ly do voi
|
||||
# vslam_odometry);
|
||||
# - khong dinh bay -march=native/-msse4.2 ma PCL bom vao, thu vo hieu thang
|
||||
# tren arm64 (xem vslam_link_pcl trong cmake/vslam_helpers.cmake);
|
||||
# - VoxelGrid tu viet dung khoa int64 3x21-bit, khong co gioi han
|
||||
# 2^31 diem cua pcl::VoxelGrid.
|
||||
#
|
||||
# PCL chi con can o vslam_slam, noi RTAB-Map da keo no vao san.
|
||||
|
||||
vslam_add_library(vslam_perception
|
||||
PUBLIC_HDRS
|
||||
perception_types.h
|
||||
depth_to_cloud.h
|
||||
depth_filter.h
|
||||
gravity_align.h
|
||||
cliff_detect.h
|
||||
perception_pipeline.h
|
||||
FIND_DEPS
|
||||
vslam_common
|
||||
)
|
||||
|
||||
target_link_libraries(vslam_perception PUBLIC vslam_common)
|
||||
223
libs/vslam_perception/include/vslam/cliff_detect.h
Normal file
223
libs/vslam_perception/include/vslam/cliff_detect.h
Normal file
@@ -0,0 +1,223 @@
|
||||
#ifndef VSLAM_CLIFF_DETECT_H
|
||||
#define VSLAM_CLIFF_DETECT_H
|
||||
|
||||
// Phat hien vuc / bac thang xuong bang anh DEPTH.
|
||||
//
|
||||
// Port tu perception_3d/src/cliff_detect_nodelet.cpp (~450 dong, doan thuat
|
||||
// toan day nhat cua repo cu).
|
||||
//
|
||||
// ============ VI SAO CHAY TREN ANH DEPTH CHU KHONG PHAI POINT CLOUD ============
|
||||
// 1. Bac thang TOI tra ve LO (NaN / 0), khong tra ve diem thap.
|
||||
// pcl::PassThrough loai NaN => chinh cai tin hieu can tim bi xoa sach.
|
||||
// Tren anh depth, mot pixel le ra phai thay san ma khong co so do LA bang
|
||||
// chung cua vuc.
|
||||
// 2. Tia biet minh di huong nao. Voi moi pixel tinh duoc "san LE RA phai o
|
||||
// depth bao nhieu" (giao tia voi mat phang san). Cloud da mat thong tin do.
|
||||
// 3. Danh dau tai P_floor = giao tia voi mat san, tuc la MEP vuc, chu khong
|
||||
// phai tai diem tra ve (nam sau duoi day vuc, hoac khong ton tai).
|
||||
//
|
||||
// ============ VUNG MU 0 - 1.1 m ============
|
||||
// Camera cao 0.803 m nghieng xuong: tia cham san gan nhat o ~1.07 m. Duoi
|
||||
// nguong do khong tia nao cham san, detector khong the biet gi. Do la ly do
|
||||
// min_dist mac dinh 1.10 m, va la ly do BAT BUOC phai co cam bien khac (cliff
|
||||
// IR / bumper) cho vung gan.
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/perception_types.h"
|
||||
#include "vslam/unix_time.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct CliffDetectConfig {
|
||||
bool enabled = true;
|
||||
|
||||
// Cua so xet, tinh theo ban kinh tren mat san (m). Duoi min_dist la vung mu
|
||||
// hinh hoc, khong phai lua chon tuy y.
|
||||
double min_dist = 1.10;
|
||||
double max_dist = 2.50;
|
||||
|
||||
// Be mat phai thap hon san tham chieu bao nhieu moi tinh la vuc (m).
|
||||
double cliff_margin = 0.05;
|
||||
|
||||
// Sai so pitch con lai sau tu hieu chuan. Nguong no ra theo tam:
|
||||
// thr = z_ref - (cliff_margin + r * tan(pitch_uncertainty))
|
||||
double pitch_uncertainty_deg = 2.0;
|
||||
|
||||
int pixel_step = 4; // lay mau thua: 1/16 so pixel
|
||||
int min_neighbors = 3; // trong 8 lang gieng, de loai pixel le
|
||||
int min_cluster = 12; // so diem toi thieu de bao vuc
|
||||
|
||||
double imu_timeout = 0.5;
|
||||
// Bail khi gravity_align dang khong neo duoc vao trong luc. Pitch luc do la
|
||||
// gyro thuan va dang troi -> hinh hoc cat tia x san khong dung tin.
|
||||
bool require_accel_ok = true;
|
||||
|
||||
// --- Tu hieu chuan mat san ---
|
||||
bool self_level = true;
|
||||
int min_level_samples = 40;
|
||||
// Cong nay loai CA vat the (mat ban 0.7 m) LAN vuc that (z ~ -1 m) khoi phep
|
||||
// khop. Nho ve thu hai ma khop duoc tren TOAN cua so ma khong nuot mat chinh
|
||||
// cai vuc dang can tim.
|
||||
double level_gate = 0.25;
|
||||
// Doc vuot tran = dau hieu phep khop hong, KHONG phai san doc that. Ramp la
|
||||
// san doc THAT va robot phai di duoc -- cong phan biet ramp/vuc la MAD.
|
||||
double max_level_slope = 0.20;
|
||||
double max_level_residual = 0.05;
|
||||
|
||||
// Mat phang san khop duoc, NGOAI SUY ve r = 0, phai di qua chinh cho robot
|
||||
// dang dung -- vi robot DANG DUNG TREN no. Mot phep khop noi "san duoi banh
|
||||
// xe toi thap hon 25 cm" la mot menh de vat ly sai, khong phai mot nguong
|
||||
// can chinh.
|
||||
//
|
||||
// Day la cong bat duoc kieu hong ma `max_obstructed_frac` KHONG bat duoc:
|
||||
// hanh lang 2.5-3.0 m, tuong cach 1.25-1.5 m. Chi ~9% tia tra ve diem cao
|
||||
// hon level_gate (xa cong 0.60), nhung diem tuong o r lon van lot cong va
|
||||
// KEO DOC duong khop len +0.19 -- vua du duoi tran max_level_slope 0.20.
|
||||
// Sau do san THAT o r ~ 2 m bi do voi z_ref = +0.13 -> roi duoi nguong ->
|
||||
// vuc gia. Do duoc: 6/210 khung o be rong 3.0 m.
|
||||
//
|
||||
// Vi sao KHONG ha max_level_slope thay vi the: 0.20 duoc chon de HAP THU
|
||||
// dao dong pitch +-8.5 do do duoc tren robot that; ha xuong se vo hieu hoa
|
||||
// co che bu va tat phat hien vuc khi robot dang chay. Xem ghi chu trong
|
||||
// depth_filter.yaml cua repo cu.
|
||||
//
|
||||
// Vi sao 0.15: sai so cua z_ref(0) chi den tu sai so pitch va sai so chieu
|
||||
// cao camera, ma ca hai deu da bi chan boi max_tz_dev = 0.08. Mot ramp that
|
||||
// 8% bat dau o 1.5 m cho z0 = -0.096 -- van lot.
|
||||
double max_level_offset = 0.15;
|
||||
|
||||
// Camera lech khoi chieu cao danh dinh => robot dang nghieng / da nhoai len
|
||||
// vat gi do. Luc do phep khop mat san se bam theo TANG DUOI va bao "khong co
|
||||
// vuc" -- nguy hiem hon la im lang.
|
||||
// ⚠️ PHAI dat tu `RobotModel::camera_height_above_floor()`, khong go tay.
|
||||
// No la camera_link.z CONG wheel_radius -- mot he qua, khong phai mot so
|
||||
// doc lap. Mac dinh 0.803 o day tung LECH voi 0.80 ma bo mo phong dung, va
|
||||
// khong co gi bao. Gio mac dinh khop, nhung van phai dat tu RobotModel vi
|
||||
// robot.yaml moi la nguon.
|
||||
double nominal_tz = 0.80;
|
||||
double max_tz_dev = 0.08;
|
||||
|
||||
// Ti le toi da tia trong cua so duoc phep tra ve diem CAO hon level_gate
|
||||
// truoc khi coi nhu "dang nhin vao vat can, khong nhin thay san".
|
||||
//
|
||||
// KHONG co trong ban ROS goc, va no bit mot lo hong THAT cua ban do.
|
||||
// Trong hanh lang hep hon 2 x min_dist, tuong nam GAN HON vung mu 1.10 m:
|
||||
// hau het tia dap vao tuong chu khong toi san. Mot so it diem tuong co
|
||||
// z < level_gate van duoc nhan vao phep khop, va mat san tham chieu bi keo
|
||||
// LEN theo chung. Sau do san THAT o xa hon bi do voi mat phang bi keo len
|
||||
// do -> roi duoi nguong -> BAO VUC GIA giua hanh lang phang.
|
||||
//
|
||||
// Do duoc trong mo phong (hanh lang 1.6 m, robot quay tai cho): z0 khop
|
||||
// thanh +0.24 m, MAD chi 0.046 nen lot ca cong MAD, roi 46 tia san that
|
||||
// roi duoi nguong. Hong kieu nay la hong KHONG AN TOAN -- no bao CO vuc
|
||||
// chu khong bao "khong biet".
|
||||
//
|
||||
// Vi sao dem diem CAO chu khong phai be rong dai mau: be rong la proxy yeu
|
||||
// (da do: dai mau van trai 50% cua so ma van bao gia). Con dem diem cao thi
|
||||
// KHONG BAO GIO che duoc vuc that: vuc lam diem THAP hoac MAT HAN, khong
|
||||
// bao gio lam diem cao. Cong nay chi co the bien "co vuc" thanh "khong
|
||||
// biet", khong bao gio bien thanh "khong co vuc".
|
||||
double max_obstructed_frac = 0.60;
|
||||
|
||||
bool fail_safe = true;
|
||||
double fail_safe_after = 1.0; // mu LIEN TUC bao lau thi bao nguy
|
||||
|
||||
double wheel_radius = 0.075;
|
||||
};
|
||||
|
||||
// Vi sao khung nay khong phan xet duoc. Gia tri duoc dua ra ngoai de web UI
|
||||
// hien duoc -- bao "khong biet" khac han bao "khong co vuc".
|
||||
enum class CliffBail : uint8_t {
|
||||
kNone = 0,
|
||||
kDisabled,
|
||||
kNoRollPitch,
|
||||
kAccelGated, // robot dang tang/giam toc
|
||||
kBadGeometry, // tz <= 0
|
||||
kTzDeviation, // robot khong bang phang
|
||||
kFewSamples, // khong du mau san de khop
|
||||
kSlopeClamped, // doc cham tran -> khop hong
|
||||
kResidualLarge, // MAD lon -> khung nhin khong phai mot mat phang
|
||||
kBadEncoding,
|
||||
kObstructed, // phan lon tia dap vao vat can, khong thay san
|
||||
kOffsetLarge, // mat phang khop duoc khong di qua cho robot dang dung
|
||||
};
|
||||
|
||||
const char* CliffBailName(CliffBail b);
|
||||
|
||||
struct CliffResult {
|
||||
bool detected = false;
|
||||
bool judged = false; // false = khong phan xet duoc (bail)
|
||||
CliffBail bail = CliffBail::kNone;
|
||||
double bail_held = 0.0; // da mu lien tuc bao lau (s)
|
||||
|
||||
// Mep vuc trong base_footprint. z giu AM co y (xem ghi chu trong .cc).
|
||||
PointCloudXYZ edge_cloud;
|
||||
|
||||
// Chan doan -- lam nen cho khoi debug throttle cua ban ROS.
|
||||
int64_t n_total = 0, n_up = 0, n_zneg = 0, n_range = 0, n_inwin = 0;
|
||||
int64_t n_hole = 0, n_drop = 0;
|
||||
int n_cliff = 0;
|
||||
double r_min = 0.0, r_max = 0.0, z_lowest = 0.0;
|
||||
double lvl_z0 = 0.0, lvl_slope = 0.0, lvl_slope_raw = 0.0, lvl_mad = 0.0;
|
||||
int64_t lvl_samples = 0; // so mau vao phep khop mat san
|
||||
int64_t lvl_high = 0; // tia trong cua so tra ve diem CAO hon level_gate
|
||||
int64_t lvl_window = 0; // tong tia trong cua so o luot do muc san
|
||||
double lvl_r_lo = 0.0; // ban kinh nho nhat / lon nhat trong so mau do
|
||||
double lvl_r_hi = 0.0;
|
||||
bool leveled = false;
|
||||
double tz = 0.0;
|
||||
};
|
||||
|
||||
struct CliffDetectStats {
|
||||
uint64_t frames = 0;
|
||||
uint64_t judged = 0;
|
||||
uint64_t detected = 0;
|
||||
uint64_t bailed = 0;
|
||||
uint64_t bail_by[12] = {0};
|
||||
};
|
||||
|
||||
class CliffDetect {
|
||||
public:
|
||||
explicit CliffDetect(CliffDetectConfig cfg);
|
||||
|
||||
// roll/pitch tu GravityAlign. accel_ok = bo loc co dang neo duoc vao trong
|
||||
// luc khong, hay chi dang tich phan gyro.
|
||||
void SetTilt(UnixTime stamp, double roll, double pitch, bool accel_ok);
|
||||
|
||||
// `cam_to_base` la phep bien doi TINH camera -> base_link (tu robot.yaml);
|
||||
// nghieng do duoc se duoc ap them, giong het GravityAlign.
|
||||
CliffResult Process(const Image& depth, const CameraInfo& info,
|
||||
const Transform& cam_to_base, UnixTime now);
|
||||
|
||||
void Reset();
|
||||
const CliffDetectStats& stats() const { return stats_; }
|
||||
|
||||
private:
|
||||
CliffResult Bail(CliffBail why, bool immediate, UnixTime now);
|
||||
|
||||
CliffDetectConfig cfg_;
|
||||
|
||||
bool have_rp_ = false;
|
||||
double roll_ = 0.0, pitch_ = 0.0;
|
||||
bool accel_ok_ = true;
|
||||
UnixTime last_rp_;
|
||||
|
||||
bool bailing_ = false;
|
||||
UnixTime bail_since_;
|
||||
|
||||
// Bo dem tai su dung giua cac khung: 30 Hz x 4800 o, cap phat moi khung la
|
||||
// lang phi va la nguon jitter.
|
||||
std::vector<uint8_t> cand_;
|
||||
std::vector<double> pos_x_, pos_y_;
|
||||
std::vector<std::pair<double, double>> rz_;
|
||||
|
||||
CliffDetectStats stats_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_CLIFF_DETECT_H
|
||||
81
libs/vslam_perception/include/vslam/depth_filter.h
Normal file
81
libs/vslam_perception/include/vslam/depth_filter.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#ifndef VSLAM_DEPTH_FILTER_H
|
||||
#define VSLAM_DEPTH_FILTER_H
|
||||
|
||||
// Loc point cloud tu camera depth.
|
||||
//
|
||||
// Port tu perception_3d/src/depth_filter_nodelet.cpp. Chuoi:
|
||||
// PassThrough(z) -> VoxelGrid -> (tuy chon) xac nhan theo thoi gian
|
||||
//
|
||||
// KHONG dung PCL. Ba bo loc nay cong lai khoang 100 dong, va giu
|
||||
// vslam_perception khong phu thuoc PCL nghia la P5 build va test duoc trên
|
||||
// may chua co PCL -- cung ly do voi vslam_odometry.
|
||||
|
||||
#include <cstdint>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "vslam/unix_time.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct DepthFilterConfig {
|
||||
double min_range = 0.22; // Min-Z cua D410
|
||||
double max_range = 4.0;
|
||||
double voxel_size = 0.03;
|
||||
double max_rate = 10.0; // gioi han tan so dau ra, Hz
|
||||
|
||||
// Xac nhan theo thoi gian: mot voxel phai duoc thay `min_hits` lan trong
|
||||
// `temporal_window` giay moi duoc xuat. Loc nhieu nhap nhay cua depth.
|
||||
bool enable_temporal = false;
|
||||
int min_hits = 3;
|
||||
double temporal_window = 0.5;
|
||||
};
|
||||
|
||||
struct DepthFilterStats {
|
||||
uint64_t frames_in = 0;
|
||||
uint64_t frames_out = 0;
|
||||
uint64_t frames_rate_limited = 0;
|
||||
uint64_t points_in = 0;
|
||||
uint64_t points_after_range = 0;
|
||||
uint64_t points_after_voxel = 0;
|
||||
uint64_t points_out = 0;
|
||||
};
|
||||
|
||||
class DepthFilter {
|
||||
public:
|
||||
explicit DepthFilter(DepthFilterConfig cfg);
|
||||
|
||||
// Tra false neu khung bi bo vi gioi han tan so. `out` khong duoc ghi.
|
||||
//
|
||||
// `stamp` dung STAMP CUA BAN TIN, khong phai dong ho tuong: gioi han tan so
|
||||
// theo dong ho tuong se bop nham khi phat lai ban ghi nhanh.
|
||||
bool Apply(const PointCloudXYZ& in, UnixTime stamp, PointCloudXYZ* out);
|
||||
|
||||
void Reset();
|
||||
const DepthFilterStats& stats() const { return stats_; }
|
||||
|
||||
private:
|
||||
struct VoxelHit {
|
||||
int hits = 0;
|
||||
UnixTime last_seen;
|
||||
PointXYZ centroid;
|
||||
int count = 0;
|
||||
};
|
||||
|
||||
DepthFilterConfig cfg_;
|
||||
UnixTime last_output_;
|
||||
bool have_output_ = false;
|
||||
std::unordered_map<int64_t, VoxelHit> voxels_;
|
||||
DepthFilterStats stats_;
|
||||
};
|
||||
|
||||
// Khoa voxel: goi ba chi so 21 bit vao mot int64.
|
||||
//
|
||||
// 21 bit moi truc -> dai +-1048575 voxel. Voi voxel 3 cm do la +-31 km, thua
|
||||
// xa moi ban do trong nha. Goi vao mot int64 cho phep dung unordered_map ma
|
||||
// khong phai bam mot tuple.
|
||||
int64_t VoxelKey(double x, double y, double z, double size);
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_DEPTH_FILTER_H
|
||||
41
libs/vslam_perception/include/vslam/depth_to_cloud.h
Normal file
41
libs/vslam_perception/include/vslam/depth_to_cloud.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#ifndef VSLAM_DEPTH_TO_CLOUD_H
|
||||
#define VSLAM_DEPTH_TO_CLOUD_H
|
||||
|
||||
// Anh depth -> point cloud. Thay depth_image_proc/point_cloud_xyz.
|
||||
//
|
||||
// Cong thuc da XAC MINH tu
|
||||
// /opt/ros/noetic/include/depth_image_proc/depth_conversions.h:52-100:
|
||||
//
|
||||
// constant_x = unit_scaling / fx (uint16 -> 0.001 m moi don vi)
|
||||
// constant_y = unit_scaling / fy
|
||||
// x = (u - cx) * depth * constant_x
|
||||
// y = (v - cy) * depth * constant_y
|
||||
// z = depth
|
||||
// depth khong hop le (0 voi uint16, NaN voi float) -> diem = NaN
|
||||
//
|
||||
// fx/cx lay TU CameraInfo DA BU BINNING. crop_decimate cap nhat binning chu
|
||||
// khong chia lai K, nen quen bu la moi diem lech theo he so decimate -- va
|
||||
// cloud van "trong nhu that", chi la sai ti le.
|
||||
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// `out` duoc ghi de. Diem khong hop le duoc giu lai duoi dang NaN de cloud
|
||||
// van TO CHUC THEO ANH (width x height) -- buoc sau can dieu do.
|
||||
// Doc mot diem anh depth ra met. Tra NaN neu khong do duoc.
|
||||
// Cong khai vi CliffDetect phai doc CHINH cung mot cach -- no lam viec trong
|
||||
// khong gian anh chu khong qua cloud, va hai cach doc lech nhau (nhat la quy
|
||||
// uoc 0 = khong do duoc) se sinh vuc gia.
|
||||
double DepthAtMeters(const Image& img, uint32_t u, uint32_t v);
|
||||
|
||||
void DepthToCloud(const Image& depth, const CameraInfo& info, PointCloudXYZ* out);
|
||||
|
||||
// Ban KHONG to chuc: bo luon diem khong hop le. Nhanh hon va nho hon khi
|
||||
// buoc sau khong can cau truc anh.
|
||||
void DepthToCloudDense(const Image& depth, const CameraInfo& info,
|
||||
PointCloudXYZ* out);
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_DEPTH_TO_CLOUD_H
|
||||
128
libs/vslam_perception/include/vslam/gravity_align.h
Normal file
128
libs/vslam_perception/include/vslam/gravity_align.h
Normal file
@@ -0,0 +1,128 @@
|
||||
#ifndef VSLAM_GRAVITY_ALIGN_H
|
||||
#define VSLAM_GRAVITY_ALIGN_H
|
||||
|
||||
// Uoc luong roll/pitch tu IMU va xoay point cloud ve mat phang ngang.
|
||||
//
|
||||
// Port tu perception_3d/src/gravity_align_nodelet.cpp.
|
||||
//
|
||||
// ================== VAN DE COT LOI ==================
|
||||
// Gia toc ke KHONG phan biet duoc nghieng voi tang toc. No do TONG cua:
|
||||
// 1. trong luc chieu xuong ba truc cua than (phu thuoc roll/pitch)
|
||||
// 2. gia toc rieng cua robot (tinh tien + huong tam)
|
||||
//
|
||||
// Mot robot tang toc 0.3 m/s^2 tren san phang cho ra doc cung gia toc ke nhu
|
||||
// mot robot dung yen nghieng 1.75 do. Khong co cach nao tach hai truong hop
|
||||
// bang rieng gia toc ke.
|
||||
//
|
||||
// Cach lam: TRU gia toc rieng truoc khi coi phan con lai la trong luc.
|
||||
// axc = ax - a_lin (gia toc tinh tien, tu dao ham van toc odom)
|
||||
// ayc = ay - v * omega (gia toc huong tam khi quay)
|
||||
// Roi chi hieu chinh khi |a| con lai gan g -- tuc la dang gan trang thai tinh.
|
||||
//
|
||||
// ================== VI SAO PHAI LOC THONG THAP TRUOC ==================
|
||||
// Gia toc ke tho dao dong manh do rung tiep xuc banh-san (san gach + banh
|
||||
// cung). Ap cong |a| ~ g len mau tho se tu choi gan nhu moi mau, va bo loc
|
||||
// chi con lai phan tich phan gyro -- tuc la troi tu do.
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "vslam/unix_time.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// base_footprint <- base_link voi nghieng da do. Ca GravityAlign lan
|
||||
// CliffDetect deu phai dung CHINH ham nay -- hai ban sao hinh hoc lech nhau
|
||||
// la lop loi da co trong repo cu (URDF vs base_tilt_tf.py).
|
||||
Transform TiltLift(double roll, double pitch, double wheel_radius);
|
||||
|
||||
struct GravityAlignConfig {
|
||||
// He so bo loc bu: 1 = chi tin gyro, 0 = chi tin accel.
|
||||
// 0.98 nghia la gyro cho ngan han, accel keo ve dai han.
|
||||
double alpha = 0.98;
|
||||
|
||||
// LPF cho gia toc ke truoc khi dung. Nho = loc manh.
|
||||
double accel_lpf_alpha = 0.05;
|
||||
|
||||
// |a| duoc coi la "gan tinh" khi lech khoi g duoi nguong nay (m/s^2).
|
||||
double accel_gate = 1.0;
|
||||
|
||||
double imu_timeout = 0.5;
|
||||
double odom_timeout = 0.2;
|
||||
|
||||
// Tru gia toc rieng cua robot truoc khi hieu chinh. TAT cai nay la bo loc
|
||||
// se hieu moi lan tang toc thanh mot cu nghieng.
|
||||
bool use_odom_accel = true;
|
||||
double odom_accel_alpha = 0.3; // LPF cho dao ham van toc
|
||||
|
||||
// Cat dai chieu cao sau khi xoay ve base_footprint.
|
||||
double min_height = 0.06;
|
||||
double max_height = 1.20;
|
||||
|
||||
// Chieu cao banh xe: base_footprint o mat dat, base_link o tam truc.
|
||||
double wheel_radius = 0.075;
|
||||
};
|
||||
|
||||
struct GravityAlignStats {
|
||||
uint64_t imu_samples = 0;
|
||||
uint64_t accel_accepted = 0; // so lan cong |a| ~ g mo
|
||||
uint64_t accel_rejected = 0;
|
||||
uint64_t clouds_in = 0;
|
||||
uint64_t clouds_out = 0;
|
||||
uint64_t points_in = 0;
|
||||
uint64_t points_out = 0;
|
||||
double last_accel_norm = 0.0;
|
||||
};
|
||||
|
||||
class GravityAlign {
|
||||
public:
|
||||
explicit GravityAlign(GravityAlignConfig cfg);
|
||||
|
||||
// Goi moi mau IMU (200 Hz).
|
||||
void FeedImu(const Imu& imu);
|
||||
|
||||
// Goi moi ban tin odometry. Dung de tinh gia toc rieng cua robot.
|
||||
void FeedOdom(UnixTime stamp, double vx, double vyaw);
|
||||
|
||||
// Xoay cloud tu frame quang hoc cua camera ve base_footprint, da bu nghieng.
|
||||
//
|
||||
// `cam_to_base` la phep bien doi TINH tu camera toi base_link (tu robot.yaml).
|
||||
// Nghieng do bo loc uoc luong duoc ap THEM vao.
|
||||
//
|
||||
// Tra false neu chua co uoc luong nghieng nao.
|
||||
bool Align(const PointCloudXYZ& in, const Transform& cam_to_base,
|
||||
UnixTime stamp, PointCloudXYZ* out) const;
|
||||
|
||||
double roll() const { return roll_; }
|
||||
double pitch() const { return pitch_; }
|
||||
bool valid() const { return initialized_; }
|
||||
// true neu mau accel gan nhat duoc chap nhan (dang gan trang thai tinh).
|
||||
bool accel_ok() const { return accel_ok_; }
|
||||
|
||||
void Reset();
|
||||
const GravityAlignStats& stats() const { return stats_; }
|
||||
|
||||
private:
|
||||
GravityAlignConfig cfg_;
|
||||
|
||||
bool initialized_ = false;
|
||||
double roll_ = 0.0, pitch_ = 0.0;
|
||||
UnixTime last_imu_;
|
||||
|
||||
bool lpf_init_ = false;
|
||||
double ax_f_ = 0.0, ay_f_ = 0.0, az_f_ = 0.0;
|
||||
bool accel_ok_ = false;
|
||||
|
||||
bool have_odom_ = false;
|
||||
UnixTime last_odom_;
|
||||
double v_prev_ = 0.0, w_ = 0.0, a_lin_ = 0.0;
|
||||
|
||||
// mutable: Align() la const ve mat logic (khong doi uoc luong nghieng)
|
||||
// nhung van phai dem. const_cast o day se la mot loi cho, khong phai giai
|
||||
// phap.
|
||||
mutable GravityAlignStats stats_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_GRAVITY_ALIGN_H
|
||||
89
libs/vslam_perception/include/vslam/perception_pipeline.h
Normal file
89
libs/vslam_perception/include/vslam/perception_pipeline.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#ifndef VSLAM_PERCEPTION_PIPELINE_H
|
||||
#define VSLAM_PERCEPTION_PIPELINE_H
|
||||
|
||||
// Chuoi depth_to_cloud -> depth_filter -> gravity_align -> cliff_detect,
|
||||
// chay INLINE trong mot loi goi tren chinh luong camera.
|
||||
//
|
||||
// Thay cho ba nodelet chung mot manager trong ban ROS. Tot hon: khong
|
||||
// intrusive_ptr, khong dispatch theo topic, khong nodelet loader, va thu tu
|
||||
// cac buoc la thu tu dong lenh -- khong con phu thuoc vao thu tu remap trong
|
||||
// launch file.
|
||||
//
|
||||
// Doi tuong nay KHONG tu khoa. No thuoc ve dung mot luong (camera_rx). Ket
|
||||
// qua di ra ngoai bang shared_ptr<const> qua Channel.
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "vslam/cliff_detect.h"
|
||||
#include "vslam/clock.h"
|
||||
#include "vslam/depth_filter.h"
|
||||
#include "vslam/gravity_align.h"
|
||||
#include "vslam/perception_types.h"
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
struct PerceptionConfig {
|
||||
DepthFilterConfig filter;
|
||||
GravityAlignConfig gravity;
|
||||
CliffDetectConfig cliff;
|
||||
|
||||
// Bo qua buoc dung cloud va loc -- chi chay cliff_detect. Cliff chay tren
|
||||
// anh depth nen khong can cloud; tren Jetson day la cach ha tai khi chi can
|
||||
// an toan chu khong can cloud cho SLAM.
|
||||
bool enable_cloud = true;
|
||||
bool enable_cliff = true;
|
||||
};
|
||||
|
||||
struct PerceptionStats {
|
||||
uint64_t frames_in = 0;
|
||||
uint64_t frames_out = 0;
|
||||
double last_ms = 0.0;
|
||||
double max_ms = 0.0;
|
||||
double avg_ms = 0.0;
|
||||
};
|
||||
|
||||
class PerceptionPipeline {
|
||||
public:
|
||||
// `clock` chi dung cho do thoi gian xu ly va cho timeout cua cliff_detect.
|
||||
// Moi dau thoi gian tren du lieu lay tu stamp cua ban tin -- day la dieu
|
||||
// kien de phat lai ban ghi cho ket qua y het chay that.
|
||||
PerceptionPipeline(PerceptionConfig cfg, const Clock& clock);
|
||||
|
||||
// Dat phep bien doi TINH camera_depth_optical_frame -> base_link, doc tu
|
||||
// robot.yaml. Goi mot lan luc khoi tao.
|
||||
void SetCameraExtrinsic(const Transform& cam_to_base);
|
||||
|
||||
// IMU 200 Hz. Goi truc tiep tu luong mcu_rx.
|
||||
void FeedImu(const Imu& imu);
|
||||
// Odometry 50 Hz -- can de TRU gia toc rieng cua robot khoi gia toc ke.
|
||||
void FeedOdom(UnixTime stamp, double vx, double vyaw);
|
||||
|
||||
// Mot khung depth. Tra ve ket qua day du; nguoi goi quyet dinh publish gi.
|
||||
PerceptionOutput Process(const Image& depth, const CameraInfo& info);
|
||||
|
||||
const GravityAlign& gravity() const { return gravity_; }
|
||||
const CliffDetect& cliff() const { return cliff_; }
|
||||
const PerceptionStats& stats() const { return stats_; }
|
||||
void Reset();
|
||||
|
||||
private:
|
||||
PerceptionConfig cfg_;
|
||||
const Clock& clock_;
|
||||
Transform cam_to_base_;
|
||||
|
||||
GravityAlign gravity_;
|
||||
DepthFilter filter_;
|
||||
CliffDetect cliff_;
|
||||
|
||||
// Tai su dung giua cac khung. 30 Hz x ~300k diem: cap phat moi khung la
|
||||
// nguon jitter lon hon ban than phep tinh.
|
||||
PointCloudXYZ buf_raw_, buf_filt_, buf_align_;
|
||||
|
||||
PerceptionStats stats_;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_PERCEPTION_PIPELINE_H
|
||||
35
libs/vslam_perception/include/vslam/perception_types.h
Normal file
35
libs/vslam_perception/include/vslam/perception_types.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef VSLAM_PERCEPTION_TYPES_H
|
||||
#define VSLAM_PERCEPTION_TYPES_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "vslam/vslam_common.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// Ket qua cua mot vong perception. Moi truong deu co the null/rong neu buoc
|
||||
// tuong ung khong chay hoac khong co du lieu.
|
||||
struct PerceptionOutput {
|
||||
UnixTime stamp;
|
||||
|
||||
PointCloudXYZPtr cloud_raw; // sau depth_to_cloud
|
||||
PointCloudXYZPtr cloud_filtered; // sau depth_filter
|
||||
PointCloudXYZPtr cloud_aligned; // sau gravity_align, trong base_footprint
|
||||
PointCloudXYZPtr cloud_cliff; // diem danh dau mep vuc
|
||||
|
||||
// gravity_align
|
||||
double roll = 0.0;
|
||||
double pitch = 0.0;
|
||||
bool tilt_valid = false;
|
||||
bool accel_ok = false; // accel co dang gan trang thai tinh khong
|
||||
|
||||
// cliff_detect
|
||||
bool cliff_detected = false;
|
||||
std::string cliff_detail;
|
||||
};
|
||||
|
||||
} // namespace vslam
|
||||
|
||||
#endif // VSLAM_PERCEPTION_TYPES_H
|
||||
482
libs/vslam_perception/src/cliff_detect.cc
Normal file
482
libs/vslam_perception/src/cliff_detect.cc
Normal file
@@ -0,0 +1,482 @@
|
||||
#include "vslam/cliff_detect.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
#include "vslam/depth_to_cloud.h"
|
||||
#include "vslam/geometry.h"
|
||||
#include "vslam/gravity_align.h"
|
||||
#include "vslam/log.h"
|
||||
#include "vslam/node_constants.h"
|
||||
|
||||
namespace vslam {
|
||||
namespace {
|
||||
|
||||
// Trung vi cua z tren mot khoang da sap xep theo r; tra ve luon r tai giua
|
||||
// khoang. Hai trung vi -> mot duong thang. Ben vung hon binh phuong toi thieu:
|
||||
// mot vet vat can sot lai khong duoc phep keo ca duong.
|
||||
void MedianOf(std::vector<std::pair<double, double>>::iterator b,
|
||||
std::vector<std::pair<double, double>>::iterator e, double* r_out,
|
||||
double* z_out) {
|
||||
const size_t m = static_cast<size_t>(std::distance(b, e));
|
||||
std::vector<double> zs;
|
||||
zs.reserve(m);
|
||||
for (auto it = b; it != e; ++it) zs.push_back(it->second);
|
||||
std::nth_element(zs.begin(), zs.begin() + m / 2, zs.end());
|
||||
*z_out = zs[m / 2];
|
||||
*r_out = (b + static_cast<long>(m / 2))->first; // da sap xep theo r
|
||||
}
|
||||
|
||||
|
||||
// Khop BEN VUNG bang hai trung vi, khong dung binh phuong toi thieu: mot vet
|
||||
// vat can sot lai khong duoc phep keo ca duong thang.
|
||||
// Tra ve true neu doc bi kep (dau hieu phep khop hong).
|
||||
bool FitFloorLine(std::vector<std::pair<double, double>>& rz, double* z0,
|
||||
double* slope, double* slope_raw, double* mad,
|
||||
double max_slope) {
|
||||
const size_t n = rz.size(), h = n / 2;
|
||||
double r1, z1, r2, z2;
|
||||
MedianOf(rz.begin(), rz.begin() + static_cast<long>(h), &r1, &z1);
|
||||
MedianOf(rz.begin() + static_cast<long>(h), rz.end(), &r2, &z2);
|
||||
|
||||
bool clamped = false;
|
||||
if (r2 - r1 > 0.15) { // du duong day de uoc luong doc
|
||||
double sl = (z2 - z1) / (r2 - r1);
|
||||
*slope_raw = sl; // giu doc TRUOC khi kep, de in ra hieu chuan
|
||||
// Doc vuot tran = dau hieu phep khop hong, KHONG phai san doc that.
|
||||
// Ramp la san doc THAT va robot phai di duoc; cong phan biet ramp/vuc la
|
||||
// MAD, khong phai do lon cua doc.
|
||||
if (std::fabs(sl) > max_slope) {
|
||||
clamped = true;
|
||||
sl = (sl > 0 ? max_slope : -max_slope);
|
||||
}
|
||||
*slope = sl;
|
||||
*z0 = z1 - *slope * r1;
|
||||
} else {
|
||||
*slope = 0.0;
|
||||
*z0 = z1;
|
||||
}
|
||||
|
||||
// MAD cua phan du: duong khop co THUC SU ta duoc mat san khong?
|
||||
std::vector<double> resid;
|
||||
resid.reserve(n);
|
||||
for (const auto& pr : rz) {
|
||||
resid.push_back(std::fabs(pr.second - (*z0 + *slope * pr.first)));
|
||||
}
|
||||
std::nth_element(resid.begin(), resid.begin() + static_cast<long>(n / 2),
|
||||
resid.end());
|
||||
*mad = resid[n / 2];
|
||||
return clamped;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
const char* CliffBailName(CliffBail b) {
|
||||
switch (b) {
|
||||
case CliffBail::kNone: return "none";
|
||||
case CliffBail::kDisabled: return "disabled";
|
||||
case CliffBail::kNoRollPitch: return "no_roll_pitch";
|
||||
case CliffBail::kAccelGated: return "accel_gated";
|
||||
case CliffBail::kBadGeometry: return "bad_geometry";
|
||||
case CliffBail::kTzDeviation: return "tz_deviation";
|
||||
case CliffBail::kFewSamples: return "few_samples";
|
||||
case CliffBail::kSlopeClamped: return "slope_clamped";
|
||||
case CliffBail::kResidualLarge: return "residual_large";
|
||||
case CliffBail::kBadEncoding: return "bad_encoding";
|
||||
case CliffBail::kObstructed: return "obstructed";
|
||||
case CliffBail::kOffsetLarge: return "offset_large";
|
||||
}
|
||||
return "?";
|
||||
}
|
||||
|
||||
CliffDetect::CliffDetect(CliffDetectConfig cfg) : cfg_(std::move(cfg)) {
|
||||
if (cfg_.pixel_step < 1) cfg_.pixel_step = 1;
|
||||
}
|
||||
|
||||
void CliffDetect::Reset() {
|
||||
have_rp_ = false;
|
||||
bailing_ = false;
|
||||
stats_ = CliffDetectStats{};
|
||||
}
|
||||
|
||||
void CliffDetect::SetTilt(UnixTime stamp, double roll, double pitch, bool accel_ok) {
|
||||
roll_ = roll;
|
||||
pitch_ = pitch;
|
||||
accel_ok_ = accel_ok;
|
||||
last_rp_ = stamp;
|
||||
have_rp_ = true;
|
||||
}
|
||||
|
||||
CliffResult CliffDetect::Bail(CliffBail why, bool immediate, UnixTime now) {
|
||||
if (!bailing_) {
|
||||
bailing_ = true;
|
||||
bail_since_ = now;
|
||||
}
|
||||
const double held = SecondsBetween(now, bail_since_);
|
||||
|
||||
CliffResult r;
|
||||
r.judged = false;
|
||||
r.bail = why;
|
||||
r.bail_held = held;
|
||||
// immediate = true -> mot khung du de bao nguy (hinh hoc vo nghia, robot
|
||||
// dang nghieng / nhoai len vat)
|
||||
// immediate = false -> mot khung xau la nhieu nhat thoi; nhung mu LIEN TUC
|
||||
// qua fail_safe_after giay thi bao nguy.
|
||||
//
|
||||
// ============ TRU `kObstructed` RA KHOI FAIL-SAFE ============
|
||||
//
|
||||
// Fail-safe ton tai cho truong hop "toi khong thay san va KHONG BIET vi
|
||||
// sao": khong du mau, khop hong, mat phang vo nghia. Luc do khong loai tru
|
||||
// duoc mot cai vuc, nen phai dung.
|
||||
//
|
||||
// `kObstructed` khac han. No la mot quan sat KHANG DINH: cua so nhin san bi
|
||||
// mot vat DAC lap kin. Vat dac khong phai vuc, va da co nguoi lo -- LiDAR
|
||||
// thay no va FSM dung robot qua SCAN_SLOW/SCAN_STOP.
|
||||
//
|
||||
// Do duoc tren ban do benh vien that: dung o (0.5, 0) cach tuong 2.0 m --
|
||||
// cua so nhin san 1.1-2.5 m bi buc tuong lap kin -- ra 209/240 khung "bao
|
||||
// vuc" voi ly do `obstructed`. Hau qua: robot KHONG BAO GIO lai gan tuong
|
||||
// duoi ~2.5 m duoc, tuc khong di duoc trong bat ky toa nha that nao. Ban do
|
||||
// SLAM ket o 1.55 x 1.05 m sau 50 giay lai.
|
||||
//
|
||||
// ⚠️ Van BAO CAO bail nhu cu (`judged = false`): nguoi van hanh phai thay
|
||||
// detector dang mu. Chi khong bien cai mu DA GIAI THICH DUOC thanh lenh
|
||||
// dung.
|
||||
const bool blind_for_unknown_reason = (why != CliffBail::kObstructed);
|
||||
r.detected = cfg_.fail_safe && blind_for_unknown_reason &&
|
||||
(immediate || held > cfg_.fail_safe_after);
|
||||
|
||||
stats_.bailed++;
|
||||
stats_.bail_by[static_cast<size_t>(why)]++;
|
||||
if (r.detected) stats_.detected++;
|
||||
return r;
|
||||
}
|
||||
|
||||
CliffResult CliffDetect::Process(const Image& depth, const CameraInfo& info,
|
||||
const Transform& cam_to_base, UnixTime now) {
|
||||
stats_.frames++;
|
||||
|
||||
if (!cfg_.enabled) {
|
||||
CliffResult r;
|
||||
r.judged = false;
|
||||
r.bail = CliffBail::kDisabled;
|
||||
return r;
|
||||
}
|
||||
if (depth.data == nullptr ||
|
||||
(depth.format != PixelFormat::kMono16 && depth.format != PixelFormat::kFloat32)) {
|
||||
return Bail(CliffBail::kBadEncoding, false, now);
|
||||
}
|
||||
|
||||
const bool rp_fresh =
|
||||
have_rp_ && SecondsBetween(now, last_rp_) < cfg_.imu_timeout;
|
||||
double roll = 0.0, pitch = 0.0;
|
||||
if (rp_fresh) {
|
||||
roll = roll_;
|
||||
pitch = pitch_;
|
||||
}
|
||||
// Khong co roll/pitch moi -> gia dinh phang va di tiep. Ket qua KHONG dang
|
||||
// tin khi robot nghieng, nhung mot camera gan chac phang van tot hon la
|
||||
// khong phan xet gi.
|
||||
|
||||
// gravity_align dang khong neo duoc vao trong luc -> pitch la gyro thuan va
|
||||
// dang troi. Hinh hoc cat tia x san khong dung tin o trang thai nay.
|
||||
if (cfg_.require_accel_ok && rp_fresh && !accel_ok_) {
|
||||
return Bail(CliffBail::kAccelGated, false, now);
|
||||
}
|
||||
|
||||
// Cung mot hinh hoc voi GravityAlign -- dung chung TiltLift().
|
||||
const Transform T = TransformCompose(TiltLift(roll, pitch, cfg_.wheel_radius),
|
||||
cam_to_base);
|
||||
// Hang thu 3 cua ma tran quay: lay bang cach quay ba vector co so.
|
||||
const double r20 = RotateVector(T.rotation, Vector3{1, 0, 0}).z;
|
||||
const double r21 = RotateVector(T.rotation, Vector3{0, 1, 0}).z;
|
||||
const double r22 = RotateVector(T.rotation, Vector3{0, 0, 1}).z;
|
||||
const double tz = T.translation.z;
|
||||
|
||||
// Tia chi cham san khi no chuc XUONG trong base_footprint, va camera phai o
|
||||
// TREN san.
|
||||
if (tz <= 0.0) return Bail(CliffBail::kBadGeometry, true, now);
|
||||
|
||||
// Camera lech khoi chieu cao danh dinh => robot dang nghieng / da nhoai len
|
||||
// vat gi do. Dung luc do phep khop mat san se bam theo TANG DUOI va bao
|
||||
// "khong co vuc" -- im lang khi that su co vuc la kieu hong nguy hiem nhat.
|
||||
if (std::fabs(tz - cfg_.nominal_tz) > cfg_.max_tz_dev) {
|
||||
return Bail(CliffBail::kTzDeviation, true, now);
|
||||
}
|
||||
|
||||
// KHONG duoc doc thang info.K. crop_decimate giao anh 320x240 nhung
|
||||
// CameraInfo van mang K cua 640x480 va bao muc gop qua binning_x/y = 2.
|
||||
// Doc thang K thi moi tia bi coi nhu nam ngang -> r_floor [3.5, 134] m thay
|
||||
// vi [1.07, 3.18] -> khong tia nao roi vao cua so -> detector im lang hoan
|
||||
// toan MA KHONG BAO LOI GI.
|
||||
const double fx = info.fx_binned(), fy = info.fy_binned();
|
||||
const double cx = info.cx_binned(), cy = info.cy_binned();
|
||||
if (!(fx > 0.0) || !(fy > 0.0)) return Bail(CliffBail::kBadGeometry, true, now);
|
||||
|
||||
const double tan_unc = std::tan(cfg_.pitch_uncertainty_deg * M_PI / 180.0);
|
||||
|
||||
const int W = static_cast<int>(depth.width);
|
||||
const int H = static_cast<int>(depth.height);
|
||||
const int step = cfg_.pixel_step;
|
||||
const int gw = (W + step - 1) / step;
|
||||
const int gh = (H + step - 1) / step;
|
||||
const size_t ncell = static_cast<size_t>(gw) * static_cast<size_t>(gh);
|
||||
|
||||
cand_.assign(ncell, 0);
|
||||
pos_x_.assign(ncell, 0.0);
|
||||
pos_y_.assign(ncell, 0.0);
|
||||
|
||||
CliffResult res;
|
||||
res.judged = true;
|
||||
res.tz = tz;
|
||||
|
||||
double r_min = 1e9, r_max = -1e9, z_lowest = 1e9;
|
||||
|
||||
// ============ LUOT 1: DO MUC SAN THAM CHIEU ============
|
||||
// Tru di muc nay bien phep do thanh VI SAI -> sai so pitch, sai so chieu cao
|
||||
// camera va lun caster deu la common-mode va bi triet.
|
||||
// z_ref(r) = lvl_z0 + lvl_slope * r
|
||||
double lvl_z0 = 0.0, lvl_slope = 0.0, lvl_mad = 0.0, lvl_slope_raw = 0.0;
|
||||
bool leveled = false;
|
||||
|
||||
int64_t n_high = 0, n_window = 0;
|
||||
|
||||
if (cfg_.self_level) {
|
||||
rz_.clear();
|
||||
rz_.reserve(2048);
|
||||
|
||||
for (int gy = 0; gy < gh; ++gy) {
|
||||
const int v = gy * step;
|
||||
if (v >= H) break;
|
||||
for (int gx = 0; gx < gw; ++gx) {
|
||||
const int u = gx * step;
|
||||
if (u >= W) break;
|
||||
const double a = (u - cx) / fx;
|
||||
const double b = (v - cy) / fy;
|
||||
const double denom = r20 * a + r21 * b + r22;
|
||||
if (denom > -1e-6) continue;
|
||||
const double z_exp = -tz / denom;
|
||||
if (z_exp <= 0.0) continue;
|
||||
const Point pf = TransformPoint(T, Point{z_exp * a, z_exp * b, z_exp});
|
||||
const double rf = std::hypot(pf.x, pf.y);
|
||||
if (rf < cfg_.min_dist || rf > cfg_.max_dist) continue;
|
||||
const double d = DepthAtMeters(depth, static_cast<uint32_t>(u),
|
||||
static_cast<uint32_t>(v));
|
||||
if (!std::isfinite(d) || d <= 0.0) continue;
|
||||
const Point po = TransformPoint(T, Point{d * a, d * b, d});
|
||||
// Cong nay lam CA HAI viec: loai vat the (mat ban 0.7 m, mat ghe
|
||||
// 0.45 m) VA loai vuc that (z ~ -1 m) khoi phep khop. Nho ve thu hai
|
||||
// ma khop duoc tren TOAN cua so ma khong nuot mat chinh cai vuc dang
|
||||
// can tim.
|
||||
n_window++;
|
||||
if (po.z > cfg_.level_gate) {
|
||||
n_high++; // diem CAO: vat can che mat san (tuong, ban, ghe)
|
||||
continue;
|
||||
}
|
||||
if (po.z < -cfg_.level_gate) continue; // diem THAP: co the la vuc
|
||||
rz_.emplace_back(rf, po.z);
|
||||
}
|
||||
}
|
||||
|
||||
res.lvl_high = n_high;
|
||||
res.lvl_window = n_window;
|
||||
|
||||
// Phan lon cua so bi vat can che -> khong nhin thay san -> KHONG phan xet.
|
||||
// Phai kiem TRUOC min_level_samples: mot buc tuong gan van de lai du 40
|
||||
// mau z thap de "khop duoc" mot mat phang khong co that.
|
||||
if (n_window > 0 &&
|
||||
static_cast<double>(n_high) > cfg_.max_obstructed_frac * n_window) {
|
||||
return Bail(CliffBail::kObstructed, false, now);
|
||||
}
|
||||
|
||||
if (static_cast<int>(rz_.size()) < cfg_.min_level_samples) {
|
||||
// Khong du mau -> khong biet san o dau -> KHONG phan xet. Im lang con
|
||||
// hon bao gia: bao gia se chan duong robot giua nha hang va nguoi van
|
||||
// hanh se tat han node di.
|
||||
return Bail(CliffBail::kFewSamples, false, now);
|
||||
}
|
||||
|
||||
res.lvl_samples = static_cast<int64_t>(rz_.size());
|
||||
|
||||
std::sort(rz_.begin(), rz_.end(),
|
||||
[](const std::pair<double, double>& p, const std::pair<double, double>& q) {
|
||||
return p.first < q.first;
|
||||
});
|
||||
res.lvl_r_lo = rz_.front().first;
|
||||
res.lvl_r_hi = rz_.back().first;
|
||||
|
||||
bool slope_clamped = FitFloorLine(rz_, &lvl_z0, &lvl_slope, &lvl_slope_raw,
|
||||
&lvl_mad, cfg_.max_level_slope);
|
||||
|
||||
// --- LOAI NGOAI LAI ROI KHOP LAI ---
|
||||
//
|
||||
// Phep khop hai-trung-vi chi theo BAN KINH: z = z0 + doc*r. No gop mat
|
||||
// chieu phuong vi, nen mot buc tuong o MOT BEN lam lech ca vanh.
|
||||
//
|
||||
// Do duoc: hanh lang 3.0 m (tuong cach 1.5 m) -> tia nham r > 1.5 dap vao
|
||||
// tuong o z = 0.8*(1 - 1.5/r); tai r = 2.0 cho z = 0.20, VUA DU lot cong
|
||||
// level_gate 0.25. Duong khop bi keo len o giua cua so, roi tia san THAT
|
||||
// o 2.5 m bi do voi mat phang do -> roi duoi nguong -> vuc gia.
|
||||
// Cong `max_obstructed_frac` KHONG bat duoc: quet ngưỡng tu 0.60 xuong
|
||||
// 0.20 deu cho dung 6/210 khung sai o be rong 3.0 m, va che% = 0.0 o moi
|
||||
// nguong -- tuc la vat can chua du nhieu de kich cong.
|
||||
//
|
||||
// Mot luot loai ngoai lai giai quyet dung goc: diem tuong la THIEU SO nen
|
||||
// bi loai, con neu chung la DA SO thi MAD phinh ra va cong
|
||||
// max_level_residual kich. Khong the lam vuc that bien mat: diem vuc cung
|
||||
// bi loai khoi phep khop, ma loai chung ra thi mat san tham chieu cang
|
||||
// dung -> vuc CANG de thay o luot 2.
|
||||
if (lvl_mad > 1e-6 && rz_.size() >= 8) {
|
||||
const double cut = std::max(3.0 * lvl_mad, 0.01);
|
||||
size_t keep = 0;
|
||||
for (size_t i = 0; i < rz_.size(); ++i) {
|
||||
if (std::fabs(rz_[i].second - (lvl_z0 + lvl_slope * rz_[i].first)) <= cut) {
|
||||
rz_[keep++] = rz_[i];
|
||||
}
|
||||
}
|
||||
if (static_cast<int>(keep) >= cfg_.min_level_samples && keep < rz_.size()) {
|
||||
rz_.resize(keep);
|
||||
slope_clamped = FitFloorLine(rz_, &lvl_z0, &lvl_slope, &lvl_slope_raw,
|
||||
&lvl_mad, cfg_.max_level_slope);
|
||||
}
|
||||
}
|
||||
|
||||
res.lvl_z0 = lvl_z0;
|
||||
res.lvl_slope = lvl_slope;
|
||||
res.lvl_mad = lvl_mad;
|
||||
|
||||
if (slope_clamped) return Bail(CliffBail::kSlopeClamped, false, now);
|
||||
if (std::fabs(lvl_z0) > cfg_.max_level_offset) {
|
||||
// Mat phang khop duoc khong di qua cho robot dang dung -> no khong phai
|
||||
// mat san. Xem ghi chu o max_level_offset.
|
||||
return Bail(CliffBail::kOffsetLarge, false, now);
|
||||
}
|
||||
if (lvl_mad > cfg_.max_level_residual) {
|
||||
// Khung nhin khong phai mot mat phang san -> khong co goc quy chieu dang
|
||||
// tin -> KHONG phan xet.
|
||||
return Bail(CliffBail::kResidualLarge, false, now);
|
||||
}
|
||||
leveled = true;
|
||||
}
|
||||
|
||||
// ============ LUOT 2: PHAN XET TUNG TIA ============
|
||||
for (int gy = 0; gy < gh; ++gy) {
|
||||
const int v = gy * step;
|
||||
if (v >= H) break;
|
||||
for (int gx = 0; gx < gw; ++gx) {
|
||||
const int u = gx * step;
|
||||
if (u >= W) break;
|
||||
|
||||
res.n_total++;
|
||||
|
||||
const double a = (u - cx) / fx;
|
||||
const double b = (v - cy) / fy;
|
||||
|
||||
const double denom = r20 * a + r21 * b + r22;
|
||||
if (denom > -1e-6) {
|
||||
res.n_up++; // tia khong chuc xuong -> khong cham san
|
||||
continue;
|
||||
}
|
||||
const double z_exp = -tz / denom; // depth ma san LE RA phai o do
|
||||
if (z_exp <= 0.0) {
|
||||
res.n_zneg++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const Point p_floor = TransformPoint(T, Point{z_exp * a, z_exp * b, z_exp});
|
||||
const double r_floor = std::hypot(p_floor.x, p_floor.y);
|
||||
r_min = std::min(r_min, r_floor);
|
||||
r_max = std::max(r_max, r_floor);
|
||||
if (r_floor < cfg_.min_dist || r_floor > cfg_.max_dist) {
|
||||
res.n_range++;
|
||||
continue;
|
||||
}
|
||||
res.n_inwin++;
|
||||
|
||||
const double d_obs = DepthAtMeters(depth, static_cast<uint32_t>(u),
|
||||
static_cast<uint32_t>(v));
|
||||
|
||||
// Nguong no ra theo tam: bu sai so pitch con lai sau tu hieu chuan.
|
||||
const double z_ref = lvl_z0 + lvl_slope * r_floor;
|
||||
const double thr = z_ref - (cfg_.cliff_margin + r_floor * tan_unc);
|
||||
|
||||
bool is_cand = false;
|
||||
if (!std::isfinite(d_obs) || d_obs <= 0.0) {
|
||||
// LO: le ra phai thay san o day ma khong co so do nao. Day la ca cau
|
||||
// thang TOI, va la ly do phai chay tren anh depth.
|
||||
is_cand = true;
|
||||
res.n_hole++;
|
||||
} else {
|
||||
const Point p_obs = TransformPoint(T, Point{d_obs * a, d_obs * b, d_obs});
|
||||
z_lowest = std::min(z_lowest, p_obs.z);
|
||||
if (p_obs.z < thr) {
|
||||
is_cand = true; // SUT: be mat thap hon san qua nguong
|
||||
res.n_drop++;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_cand) {
|
||||
const size_t idx = static_cast<size_t>(gy) * static_cast<size_t>(gw) + gx;
|
||||
cand_[idx] = 1;
|
||||
// MARK TAI MEP (giao tia x mat san), khong tai diem tra ve. Diem tra
|
||||
// ve nam duoi day vuc -- hoac khong ton tai.
|
||||
pos_x_[idx] = p_floor.x;
|
||||
pos_y_[idx] = p_floor.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============ DOI CUM: LOAI PIXEL LE ============
|
||||
res.edge_cloud.header = depth.header;
|
||||
res.edge_cloud.header.frame_id = kBaseFootprintFrame;
|
||||
res.edge_cloud.points.clear();
|
||||
res.edge_cloud.is_dense = true;
|
||||
|
||||
const float mark_z = static_cast<float>(-(cfg_.cliff_margin + 0.05));
|
||||
|
||||
for (int gy = 0; gy < gh; ++gy) {
|
||||
for (int gx = 0; gx < gw; ++gx) {
|
||||
const size_t idx = static_cast<size_t>(gy) * static_cast<size_t>(gw) + gx;
|
||||
if (!cand_[idx]) continue;
|
||||
|
||||
int neigh = 0;
|
||||
for (int dy = -1; dy <= 1; ++dy) {
|
||||
const int ny = gy + dy;
|
||||
if (ny < 0 || ny >= gh) continue;
|
||||
for (int dx = -1; dx <= 1; ++dx) {
|
||||
const int nx = gx + dx;
|
||||
if (nx < 0 || nx >= gw) continue;
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
neigh += cand_[static_cast<size_t>(ny) * static_cast<size_t>(gw) + nx];
|
||||
}
|
||||
}
|
||||
if (neigh < cfg_.min_neighbors) continue;
|
||||
|
||||
// GIU z AM, khong nang len. Trong costmap cu, vat can ao dat o z duong
|
||||
// bi chinh tia clearing cua camera xoa ngay frame sau. Khong con costmap
|
||||
// nua nhung giu nguyen quy uoc de doc lai log cu van khop.
|
||||
res.edge_cloud.points.push_back(PointXYZ{static_cast<float>(pos_x_[idx]),
|
||||
static_cast<float>(pos_y_[idx]), mark_z});
|
||||
res.n_cliff++;
|
||||
}
|
||||
}
|
||||
|
||||
res.detected = (res.n_cliff >= cfg_.min_cluster);
|
||||
res.r_min = (r_min > 1e8 ? 0.0 : r_min);
|
||||
res.r_max = (r_max < -1e8 ? 0.0 : r_max);
|
||||
res.z_lowest = (z_lowest > 1e8 ? 0.0 : z_lowest);
|
||||
res.lvl_z0 = lvl_z0;
|
||||
res.lvl_slope = lvl_slope;
|
||||
res.lvl_slope_raw = lvl_slope_raw;
|
||||
res.lvl_mad = lvl_mad;
|
||||
res.leveled = leveled;
|
||||
|
||||
// Den day nghia la da phan xet duoc -> chuoi "khong biet" ket thuc.
|
||||
bailing_ = false;
|
||||
stats_.judged++;
|
||||
if (res.detected) stats_.detected++;
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
123
libs/vslam_perception/src/depth_filter.cc
Normal file
123
libs/vslam_perception/src/depth_filter.cc
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "vslam/depth_filter.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
#include "vslam/log.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
int64_t VoxelKey(double x, double y, double z, double size) {
|
||||
const int64_t ix = static_cast<int64_t>(std::floor(x / size));
|
||||
const int64_t iy = static_cast<int64_t>(std::floor(y / size));
|
||||
const int64_t iz = static_cast<int64_t>(std::floor(z / size));
|
||||
// Doi sang khong dau 21 bit roi goi. Khong kiem tran o day: voi voxel 3 cm
|
||||
// thi dai la +-31 km, va mot diem ngoai dai do da la rac tu truoc.
|
||||
constexpr int64_t kBias = 1 << 20;
|
||||
const int64_t ux = (ix + kBias) & 0x1FFFFF;
|
||||
const int64_t uy = (iy + kBias) & 0x1FFFFF;
|
||||
const int64_t uz = (iz + kBias) & 0x1FFFFF;
|
||||
return (ux << 42) | (uy << 21) | uz;
|
||||
}
|
||||
|
||||
DepthFilter::DepthFilter(DepthFilterConfig cfg) : cfg_(std::move(cfg)) {}
|
||||
|
||||
void DepthFilter::Reset() {
|
||||
voxels_.clear();
|
||||
have_output_ = false;
|
||||
stats_ = DepthFilterStats{};
|
||||
}
|
||||
|
||||
bool DepthFilter::Apply(const PointCloudXYZ& in, UnixTime stamp,
|
||||
PointCloudXYZ* out) {
|
||||
stats_.frames_in++;
|
||||
stats_.points_in += in.points.size();
|
||||
|
||||
// Gioi han tan so theo STAMP CUA BAN TIN, khong phai dong ho tuong.
|
||||
// Dung dong ho tuong se bop nham khi phat lai ban ghi nhanh hon thoi gian
|
||||
// that -- va bai regression se cho ket qua khac live.
|
||||
if (cfg_.max_rate > 0.0 && have_output_) {
|
||||
const double dt = SecondsBetween(stamp, last_output_);
|
||||
if (dt >= 0.0 && dt < 1.0 / cfg_.max_rate) {
|
||||
stats_.frames_rate_limited++;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// --- 1. PassThrough theo z (khoang cach toi camera) ---
|
||||
// Duoi Min-Z la vung mu cua D410, khong phai vat can o khoang cach do.
|
||||
// Tren max_range thi do chinh xac cua stereo giam nhanh theo binh phuong.
|
||||
std::unordered_map<int64_t, VoxelHit> frame_voxels;
|
||||
frame_voxels.reserve(in.points.size() / 4 + 16);
|
||||
|
||||
size_t after_range = 0;
|
||||
for (const PointXYZ& p : in.points) {
|
||||
if (!std::isfinite(p.z)) continue; // NaN cua cloud to chuc theo anh
|
||||
if (p.z < cfg_.min_range || p.z > cfg_.max_range) continue;
|
||||
if (!std::isfinite(p.x) || !std::isfinite(p.y)) continue;
|
||||
after_range++;
|
||||
|
||||
// --- 2. VoxelGrid: gop diem trong cung o ---
|
||||
const int64_t key = VoxelKey(p.x, p.y, p.z, cfg_.voxel_size);
|
||||
VoxelHit& h = frame_voxels[key];
|
||||
h.centroid.x += p.x;
|
||||
h.centroid.y += p.y;
|
||||
h.centroid.z += p.z;
|
||||
h.count++;
|
||||
}
|
||||
stats_.points_after_range += after_range;
|
||||
stats_.points_after_voxel += frame_voxels.size();
|
||||
|
||||
out->header = in.header;
|
||||
out->header.stamp = stamp;
|
||||
out->points.clear();
|
||||
out->points.reserve(frame_voxels.size());
|
||||
out->is_dense = true;
|
||||
|
||||
if (!cfg_.enable_temporal) {
|
||||
for (auto& kv : frame_voxels) {
|
||||
VoxelHit& h = kv.second;
|
||||
const float inv = 1.0f / static_cast<float>(h.count);
|
||||
out->points.push_back(PointXYZ{h.centroid.x * inv, h.centroid.y * inv,
|
||||
h.centroid.z * inv});
|
||||
}
|
||||
} else {
|
||||
// --- 3. Xac nhan theo thoi gian ---
|
||||
// Mot voxel phai duoc thay `min_hits` lan trong `temporal_window` moi
|
||||
// duoc xuat. Loc nhieu nhap nhay cua depth, doi lai mot do tre bang
|
||||
// (min_hits - 1) khung.
|
||||
for (auto& kv : frame_voxels) {
|
||||
VoxelHit& cur = kv.second;
|
||||
VoxelHit& acc = voxels_[kv.first];
|
||||
if (acc.hits > 0 &&
|
||||
SecondsBetween(stamp, acc.last_seen) > cfg_.temporal_window) {
|
||||
acc.hits = 0; // qua cu -> bat dau dem lai
|
||||
}
|
||||
acc.hits++;
|
||||
acc.last_seen = stamp;
|
||||
const float inv = 1.0f / static_cast<float>(cur.count);
|
||||
acc.centroid = PointXYZ{cur.centroid.x * inv, cur.centroid.y * inv,
|
||||
cur.centroid.z * inv};
|
||||
if (acc.hits >= cfg_.min_hits) {
|
||||
out->points.push_back(acc.centroid);
|
||||
}
|
||||
}
|
||||
|
||||
// Don voxel qua han. Khong don thi map phinh vo han khi robot di xa.
|
||||
for (auto it = voxels_.begin(); it != voxels_.end();) {
|
||||
if (SecondsBetween(stamp, it->second.last_seen) > cfg_.temporal_window * 2.0) {
|
||||
it = voxels_.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stats_.points_out += out->points.size();
|
||||
stats_.frames_out++;
|
||||
last_output_ = stamp;
|
||||
have_output_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
93
libs/vslam_perception/src/depth_to_cloud.cc
Normal file
93
libs/vslam_perception/src/depth_to_cloud.cc
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "vslam/depth_to_cloud.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
|
||||
namespace vslam {
|
||||
|
||||
// Doc mot diem anh depth ra met. Tra NaN neu khong hop le.
|
||||
//
|
||||
// Quy uoc cua RealSense va cua depth_image_proc:
|
||||
// uint16 : 0 = khong do duoc
|
||||
// float32: NaN = khong do duoc
|
||||
double DepthAtMeters(const Image& img, uint32_t u, uint32_t v) {
|
||||
const size_t row = static_cast<size_t>(v) * img.step;
|
||||
if (img.format == PixelFormat::kMono16) {
|
||||
const size_t off = row + static_cast<size_t>(u) * 2;
|
||||
const uint16_t raw = static_cast<uint16_t>(img.data[off]) |
|
||||
(static_cast<uint16_t>(img.data[off + 1]) << 8);
|
||||
if (raw == 0) return std::numeric_limits<double>::quiet_NaN();
|
||||
return raw * static_cast<double>(img.depth_scale);
|
||||
}
|
||||
if (img.format == PixelFormat::kFloat32) {
|
||||
float f;
|
||||
std::memcpy(&f, img.data + row + static_cast<size_t>(u) * 4, 4);
|
||||
return f;
|
||||
}
|
||||
return std::numeric_limits<double>::quiet_NaN();
|
||||
}
|
||||
|
||||
void DepthToCloud(const Image& depth, const CameraInfo& info, PointCloudXYZ* out) {
|
||||
out->header = depth.header;
|
||||
out->points.assign(static_cast<size_t>(depth.width) * depth.height, PointXYZ{});
|
||||
out->is_dense = false;
|
||||
|
||||
if (depth.data == nullptr) return;
|
||||
|
||||
// fx/cx DA BU BINNING. crop_decimate cap nhat binning chu khong chia lai K;
|
||||
// quen bu la moi diem lech theo he so decimate, va cloud van "trong nhu
|
||||
// that" -- chi la sai ti le.
|
||||
const double fx = info.fx_binned();
|
||||
const double fy = info.fy_binned();
|
||||
const double cx = info.cx_binned();
|
||||
const double cy = info.cy_binned();
|
||||
if (!(fx > 0.0) || !(fy > 0.0)) return;
|
||||
|
||||
const float nan_v = std::numeric_limits<float>::quiet_NaN();
|
||||
|
||||
for (uint32_t v = 0; v < depth.height; ++v) {
|
||||
for (uint32_t u = 0; u < depth.width; ++u) {
|
||||
PointXYZ& p = out->points[static_cast<size_t>(v) * depth.width + u];
|
||||
const double d = DepthAtMeters(depth, u, v);
|
||||
if (!std::isfinite(d) || d <= 0.0) {
|
||||
p.x = p.y = p.z = nan_v;
|
||||
continue;
|
||||
}
|
||||
p.x = static_cast<float>((u - cx) * d / fx);
|
||||
p.y = static_cast<float>((v - cy) * d / fy);
|
||||
p.z = static_cast<float>(d);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DepthToCloudDense(const Image& depth, const CameraInfo& info,
|
||||
PointCloudXYZ* out) {
|
||||
out->header = depth.header;
|
||||
out->points.clear();
|
||||
out->is_dense = true;
|
||||
|
||||
if (depth.data == nullptr) return;
|
||||
|
||||
const double fx = info.fx_binned();
|
||||
const double fy = info.fy_binned();
|
||||
const double cx = info.cx_binned();
|
||||
const double cy = info.cy_binned();
|
||||
if (!(fx > 0.0) || !(fy > 0.0)) return;
|
||||
|
||||
out->points.reserve(static_cast<size_t>(depth.width) * depth.height / 2);
|
||||
|
||||
for (uint32_t v = 0; v < depth.height; ++v) {
|
||||
for (uint32_t u = 0; u < depth.width; ++u) {
|
||||
const double d = DepthAtMeters(depth, u, v);
|
||||
if (!std::isfinite(d) || d <= 0.0) continue;
|
||||
PointXYZ p;
|
||||
p.x = static_cast<float>((u - cx) * d / fx);
|
||||
p.y = static_cast<float>((v - cy) * d / fy);
|
||||
p.z = static_cast<float>(d);
|
||||
out->points.push_back(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
167
libs/vslam_perception/src/gravity_align.cc
Normal file
167
libs/vslam_perception/src/gravity_align.cc
Normal file
@@ -0,0 +1,167 @@
|
||||
#include "vslam/gravity_align.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "vslam/geometry.h"
|
||||
#include "vslam/log.h"
|
||||
#include "vslam/node_constants.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
GravityAlign::GravityAlign(GravityAlignConfig cfg) : cfg_(std::move(cfg)) {}
|
||||
|
||||
void GravityAlign::Reset() {
|
||||
initialized_ = false;
|
||||
roll_ = pitch_ = 0.0;
|
||||
lpf_init_ = false;
|
||||
have_odom_ = false;
|
||||
accel_ok_ = false;
|
||||
stats_ = GravityAlignStats{};
|
||||
}
|
||||
|
||||
void GravityAlign::FeedOdom(UnixTime stamp, double vx, double vyaw) {
|
||||
if (have_odom_) {
|
||||
const double dt = SecondsBetween(stamp, last_odom_);
|
||||
if (dt > 0.0 && dt < 1.0) {
|
||||
// Dao ham van toc, loc thong thap. Dao ham tho qua nhieu de tru truc
|
||||
// tiep khoi gia toc ke.
|
||||
const double a = (vx - v_prev_) / dt;
|
||||
a_lin_ = cfg_.odom_accel_alpha * a + (1.0 - cfg_.odom_accel_alpha) * a_lin_;
|
||||
}
|
||||
}
|
||||
v_prev_ = vx;
|
||||
w_ = vyaw;
|
||||
last_odom_ = stamp;
|
||||
have_odom_ = true;
|
||||
}
|
||||
|
||||
void GravityAlign::FeedImu(const Imu& imu) {
|
||||
const UnixTime now = imu.header.stamp;
|
||||
stats_.imu_samples++;
|
||||
|
||||
if (!initialized_) {
|
||||
last_imu_ = now;
|
||||
initialized_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const double dt = SecondsBetween(now, last_imu_);
|
||||
last_imu_ = now;
|
||||
if (dt <= 0.0 || dt > 1.0) {
|
||||
// Nhay thoi gian (reset mo phong / phat lai ban ghi) -> bo qua mau nay.
|
||||
return;
|
||||
}
|
||||
|
||||
// --- Du doan bang gyro ---
|
||||
roll_ += imu.angular_velocity.x * dt;
|
||||
pitch_ += imu.angular_velocity.y * dt;
|
||||
|
||||
// --- Loc thong thap accel TRUOC khi dung ---
|
||||
// Accel tho dao dong manh do rung tiep xuc banh-san; ap cong |a| ~ g len
|
||||
// mau tho se tu choi gan nhu moi mau, va bo loc chi con lai tich phan gyro
|
||||
// -- tuc la troi tu do.
|
||||
const double ax_raw = imu.linear_acceleration.x;
|
||||
const double ay_raw = imu.linear_acceleration.y;
|
||||
const double az_raw = imu.linear_acceleration.z;
|
||||
|
||||
if (!lpf_init_) {
|
||||
ax_f_ = ax_raw;
|
||||
ay_f_ = ay_raw;
|
||||
az_f_ = az_raw;
|
||||
lpf_init_ = true;
|
||||
} else {
|
||||
const double a = cfg_.accel_lpf_alpha;
|
||||
ax_f_ = a * ax_raw + (1.0 - a) * ax_f_;
|
||||
ay_f_ = a * ay_raw + (1.0 - a) * ay_f_;
|
||||
az_f_ = a * az_raw + (1.0 - a) * az_f_;
|
||||
}
|
||||
|
||||
// --- Hieu chinh bang accel, CHI khi dang gan trang thai tinh ---
|
||||
const double ax = ax_f_, ay = ay_f_, az = az_f_;
|
||||
|
||||
// TRU gia toc cua CHINH ROBOT truoc khi coi phan con lai la trong luc.
|
||||
// axc: gia toc tinh tien (dv/dt)
|
||||
// ayc: gia toc huong tam khi quay (v * omega)
|
||||
double axc = ax, ayc = ay;
|
||||
const bool odom_fresh =
|
||||
have_odom_ && SecondsBetween(now, last_odom_) < cfg_.odom_timeout;
|
||||
if (cfg_.use_odom_accel && odom_fresh) {
|
||||
axc = ax - a_lin_;
|
||||
ayc = ay - v_prev_ * w_;
|
||||
}
|
||||
|
||||
const double norm = std::sqrt(axc * axc + ayc * ayc + az * az);
|
||||
stats_.last_accel_norm = norm;
|
||||
|
||||
if (std::fabs(norm - kGravity) < cfg_.accel_gate && norm > 1e-3) {
|
||||
const double roll_acc = std::atan2(ayc, az);
|
||||
const double pitch_acc = std::atan2(-axc, std::sqrt(ayc * ayc + az * az));
|
||||
|
||||
// Bo loc bu: gyro cho ngan han, accel keo ve dai han.
|
||||
roll_ = cfg_.alpha * roll_ + (1.0 - cfg_.alpha) * roll_acc;
|
||||
pitch_ = cfg_.alpha * pitch_ + (1.0 - cfg_.alpha) * pitch_acc;
|
||||
accel_ok_ = true;
|
||||
stats_.accel_accepted++;
|
||||
} else {
|
||||
// Dang tang toc / phanh / va cham -> khong biet dau la trong luc ->
|
||||
// chi tich phan gyro. Troi cham con hon nghieng sai.
|
||||
accel_ok_ = false;
|
||||
stats_.accel_rejected++;
|
||||
}
|
||||
}
|
||||
|
||||
Transform TiltLift(double roll, double pitch, double wheel_radius) {
|
||||
// Ghep hai phep bien doi:
|
||||
// tilt : Ry(pitch) * Rx(roll) -- DUNG dau duong.
|
||||
// Bo loc bu cho ra pitch = atan2(-ax, |a_yz|); voi robot tinh
|
||||
// nghieng goc theta quanh +Y, gia toc ke doc ax = -g sin(theta),
|
||||
// az = g cos(theta) => pitch = theta. Do la phep quay body->the
|
||||
// gioi, tuc dung chieu de dua diem tu base_link ve mat phang ngang.
|
||||
// Phu dinh o day se lam nghieng GAP DOI thay vi trie^t tieu.
|
||||
// lift : base_link (tam truc) -> base_footprint (mat dat).
|
||||
Transform tilt = TransformIdentity();
|
||||
tilt.rotation = QuaternionFromRPY(roll, pitch, 0.0);
|
||||
|
||||
Transform lift = TransformIdentity();
|
||||
lift.translation = Vector3{0.0, 0.0, wheel_radius};
|
||||
|
||||
return TransformCompose(lift, tilt);
|
||||
}
|
||||
|
||||
bool GravityAlign::Align(const PointCloudXYZ& in, const Transform& cam_to_base,
|
||||
UnixTime stamp, PointCloudXYZ* out) const {
|
||||
if (!initialized_) return false;
|
||||
|
||||
stats_.clouds_in++;
|
||||
stats_.points_in += in.points.size();
|
||||
|
||||
// Ghep ba phep bien doi:
|
||||
// lift : base_link -> base_footprint (nang len chieu cao banh xe)
|
||||
// tilt : bu nghieng DO duoc (nghich dao cua nghieng than)
|
||||
// cam : camera -> base_link (tinh, tu robot.yaml)
|
||||
const Transform total =
|
||||
TransformCompose(TiltLift(roll_, pitch_, cfg_.wheel_radius), cam_to_base);
|
||||
|
||||
out->header = in.header;
|
||||
out->header.stamp = stamp;
|
||||
out->header.frame_id = kBaseFootprintFrame;
|
||||
out->points.clear();
|
||||
out->points.reserve(in.points.size());
|
||||
out->is_dense = true;
|
||||
|
||||
for (const PointXYZ& p : in.points) {
|
||||
if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) continue;
|
||||
const Point q = TransformPoint(total, Point{p.x, p.y, p.z});
|
||||
// Cat dai chieu cao SAU khi xoay: duoi min_height la san, tren max_height
|
||||
// la tran va den.
|
||||
if (q.z < cfg_.min_height || q.z > cfg_.max_height) continue;
|
||||
out->points.push_back(PointXYZ{static_cast<float>(q.x), static_cast<float>(q.y),
|
||||
static_cast<float>(q.z)});
|
||||
}
|
||||
|
||||
stats_.clouds_out++;
|
||||
stats_.points_out += out->points.size();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
89
libs/vslam_perception/src/perception_pipeline.cc
Normal file
89
libs/vslam_perception/src/perception_pipeline.cc
Normal file
@@ -0,0 +1,89 @@
|
||||
#include "vslam/perception_pipeline.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
|
||||
#include "vslam/depth_to_cloud.h"
|
||||
#include "vslam/geometry.h"
|
||||
#include "vslam/log.h"
|
||||
|
||||
namespace vslam {
|
||||
|
||||
PerceptionPipeline::PerceptionPipeline(PerceptionConfig cfg, const Clock& clock)
|
||||
: cfg_(std::move(cfg)),
|
||||
clock_(clock),
|
||||
cam_to_base_(TransformIdentity()),
|
||||
gravity_(cfg_.gravity),
|
||||
filter_(cfg_.filter),
|
||||
cliff_(cfg_.cliff) {}
|
||||
|
||||
void PerceptionPipeline::SetCameraExtrinsic(const Transform& cam_to_base) {
|
||||
cam_to_base_ = cam_to_base;
|
||||
}
|
||||
|
||||
void PerceptionPipeline::FeedImu(const Imu& imu) { gravity_.FeedImu(imu); }
|
||||
|
||||
void PerceptionPipeline::FeedOdom(UnixTime stamp, double vx, double vyaw) {
|
||||
gravity_.FeedOdom(stamp, vx, vyaw);
|
||||
}
|
||||
|
||||
void PerceptionPipeline::Reset() {
|
||||
gravity_.Reset();
|
||||
filter_.Reset();
|
||||
cliff_.Reset();
|
||||
stats_ = PerceptionStats{};
|
||||
}
|
||||
|
||||
PerceptionOutput PerceptionPipeline::Process(const Image& depth, const CameraInfo& info) {
|
||||
// steady_clock cho do thoi gian -- KHONG dung UnixTime. Mot buoc NTP giua
|
||||
// hai moc se cho ra "khung xu ly het -3 giay".
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
stats_.frames_in++;
|
||||
|
||||
PerceptionOutput out;
|
||||
out.stamp = depth.header.stamp;
|
||||
out.roll = gravity_.roll();
|
||||
out.pitch = gravity_.pitch();
|
||||
out.tilt_valid = gravity_.valid();
|
||||
out.accel_ok = gravity_.accel_ok();
|
||||
|
||||
// --- Duong cliff: chay tren ANH DEPTH, khong qua cloud ---
|
||||
// Dat TRUOC duong cloud vi no la duong an toan. Neu duong cloud nem hay
|
||||
// cham, cliff van da chay xong.
|
||||
if (cfg_.enable_cliff) {
|
||||
cliff_.SetTilt(depth.header.stamp, gravity_.roll(), gravity_.pitch(),
|
||||
gravity_.accel_ok());
|
||||
CliffResult cr = cliff_.Process(depth, info, cam_to_base_, clock_.Now());
|
||||
out.cliff_detected = cr.detected;
|
||||
out.cliff_detail = cr.judged ? "judged" : CliffBailName(cr.bail);
|
||||
if (!cr.edge_cloud.points.empty()) {
|
||||
out.cloud_cliff = std::make_shared<const PointCloudXYZ>(std::move(cr.edge_cloud));
|
||||
}
|
||||
}
|
||||
|
||||
// --- Duong cloud: cho SLAM va cho hien thi ---
|
||||
if (cfg_.enable_cloud) {
|
||||
DepthToCloudDense(depth, info, &buf_raw_);
|
||||
out.cloud_raw = std::make_shared<const PointCloudXYZ>(buf_raw_);
|
||||
|
||||
if (filter_.Apply(buf_raw_, depth.header.stamp, &buf_filt_)) {
|
||||
out.cloud_filtered = std::make_shared<const PointCloudXYZ>(buf_filt_);
|
||||
|
||||
if (gravity_.Align(buf_filt_, cam_to_base_, depth.header.stamp, &buf_align_)) {
|
||||
out.cloud_aligned = std::make_shared<const PointCloudXYZ>(buf_align_);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const double ms = std::chrono::duration<double, std::milli>(
|
||||
std::chrono::steady_clock::now() - t0)
|
||||
.count();
|
||||
stats_.frames_out++;
|
||||
stats_.last_ms = ms;
|
||||
stats_.max_ms = std::max(stats_.max_ms, ms);
|
||||
// Trung binh truot: du de bao cao, khong giu lich su.
|
||||
stats_.avg_ms += (ms - stats_.avg_ms) * 0.05;
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace vslam
|
||||
Reference in New Issue
Block a user