reloc: --slam-reloc(-auto) tu dinh vi lai trong node (tu xoay/tu di, nguoi luon thang); web ve robot mo o vi tri doan; sieu am cho xoay; LocalMatch gioi han huong

This commit is contained in:
trung09122002
2026-09-29 11:16:04 +07:00
parent 399a46139e
commit 60853531aa
15 changed files with 883 additions and 27 deletions

View File

@@ -49,6 +49,7 @@
#include "vslam/wheel_odometry.h"
#ifdef VSLAM_HAVE_SLAM
#include "vslam/global_localizer.h"
#include "vslam/rtabmap_slam.h"
#include "vslam/voxel_export.h"
#endif
@@ -95,6 +96,12 @@ struct RuntimeOptions {
// ROS, thu ma `dock_init.py` phat tu dock.
bool have_slam_init_pose = false;
Pose2D slam_init_pose;
// --slam-reloc: localization KHONG pose ban dau -- tu dinh vi lai bang khop
// scan LiDAR toan ban do (vslam/global_localizer.h).
bool slam_reloc = false;
// --slam-reloc-auto: robot TU XOAY va TU DI de gom scan (reloc_exec cua ban
// ROS). Mac dinh TAT: robot tu chay khi CHUA biet minh o dau.
bool slam_reloc_auto = false;
};
class Runtime {
@@ -132,6 +139,35 @@ class Runtime {
void ReportCloudScanAlign(const PointCloudXYZ& cloud);
void WebLoop();
void SlamLoop();
#ifdef VSLAM_HAVE_SLAM
// Dinh vi lai tu dong (--slam-reloc). CHI luong slam goi, TRUOC Process():
// gom scan theo goc da xoay, du thi khop toan cuc, chot thi SetInitialPose.
void RelocStep(const LaserScan& scan, const Pose2D& odom);
// Sau Process(): scan tai pose dang tin con khop ban do khong -- tut lau
// thi quay lai gom (nghi da lac).
void RelocWatch(const LaserScan& scan, const Pose2D& pose_map);
// Diem scan (frame laser) -> frame cua `pose` (odom hoac map), cat max_range.
std::vector<Point> ScanToFrame(const LaserScan& scan, const Pose2D& pose) const;
// `pending` = CHUA dinh vi xong: web khong ve robot theo pose uoc luong.
void SetRelocStatus(const std::string& s, bool pending);
// --- Tu chay (--slam-reloc-auto) ---
// Lenh di qua `raw_cmd_` NHU MOI NGUON KHAC -> van qua lop an toan.
void RelocCommand(double v, double w);
// Co ai KHAC ghi raw_cmd_ trong 0.5 s qua khong (tay cam, web).
bool RelocHumanCommanding();
// Chon huong thoang nhat trong scan, dat muc tieu QUAY + DI. false = khong
// co cho nao du rong.
bool RelocPlanDrive(const LaserScan& scan, const Pose2D& odom);
void RelocStartSweep();
void RelocHalt(const std::string& why);
// Sau khi CHOT: cho robot DUNG YEN, gom scan luc dung yen (khong nhoe do
// quay), khop cuc bo quanh pose vua chot roi moi dat pose ban dau.
void RelocSettle(const LaserScan& scan, const Pose2D& odom,
std::chrono::steady_clock::time_point now);
void RelocFinish(const Pose2D& map_from_odom, const Pose2D& odom, const LaserScan& scan,
const std::string& how);
#endif
void ApplyThreadPriority(std::thread* t, int prio, const char* name);
AppConfig* cfg_;
@@ -239,6 +275,9 @@ class Runtime {
Twist raw_cmd_;
UnixTime raw_cmd_stamp_;
bool have_raw_cmd_ = false;
// Stamp cua lan GHI GAN NHAT boi dinh vi lai tu chay. raw_cmd_stamp_ moi hon
// cai nay nghia la NGUOI (tay cam/web) vua ra lenh -> tu chay nhuong.
UnixTime reloc_cmd_stamp_;
// teleop_mu_: TeleopModel co trang thai (toc do dieu chinh duoc luc chay).
mutable std::mutex teleop_mu_;
@@ -291,6 +330,57 @@ class Runtime {
std::vector<VoxelIndex> prev_voxel_cells_;
uint32_t voxel_epoch_ = 0;
uint32_t voxel_seq_ = 0;
// ⚠️ Dinh vi lai tu dong: RIENG cua luong SlamLoop, KHONG duoi mutex nao --
// tru `reloc_status_` (duoi slam_mu_, WebLoop doc).
std::unique_ptr<LikelihoodField> reloc_field_; // null = khong bat --slam-reloc
GlobalLocalizerConfig reloc_cfg_;
std::unique_ptr<HypothesisTracker> reloc_tracker_;
bool reloc_active_ = false; // dang gom/khop: CHUA dua gi cho RTAB-Map
std::vector<Point> reloc_pts_; // diem da gom, frame odom
double reloc_swept_ = 0.0; // rad da xoay trong vong gom nay
Pose2D reloc_prev_odom_;
bool reloc_have_prev_ = false;
std::chrono::steady_clock::time_point reloc_t0_; // dau vong gom
std::chrono::steady_clock::time_point reloc_epoch_; // moc thoi gian cho tracker
int reloc_cycle_ = 0;
double reloc_ref_score_ = -1.0; // diem luc chot -- moc cho canh "da lac"
bool reloc_lost_pending_ = false;
std::chrono::steady_clock::time_point reloc_lost_since_;
std::chrono::steady_clock::time_point reloc_watch_next_;
Transform slam_laser_to_base_ = TransformIdentity(); // doc-only sau Start()
std::string reloc_status_; // duoi slam_mu_
bool reloc_pending_ = false; // duoi slam_mu_: chua dinh vi xong
// Duoi slam_mu_: ung vien SO 1 cua vong khop gan nhat (ke ca bi tu choi).
// CHI de VE: khi chua chot, web ve robot MO tai `guess * odom` -- thay cho
// odom tu 0 nam ngoai toa nha. KHONG dua cho RTAB-Map.
Pose2D reloc_guess_T_;
bool have_reloc_guess_ = false;
// Tu chay: XOAY (gom) -> khop -> chua chot thi QUAY ve cho thoang -> DI ->
// XOAY... Tran cung 6 vong / 12 m roi DUNG HAN goi nguoi (nhu ban ROS).
enum class RelocExec { kOff, kSweep, kTurn, kDrive, kHalted };
RelocExec reloc_exec_ = RelocExec::kOff;
double reloc_turn_target_ = 0.0; // huong odom can quay toi (rad)
Pose2D reloc_drive_start_;
double reloc_drive_len_ = 0.0; // quang duong du dinh cua pha DI
double reloc_total_drive_ = 0.0; // cong don cho mot lan dinh vi
int reloc_exec_cycles_ = 0;
std::chrono::steady_clock::time_point reloc_phase_t0_;
std::chrono::steady_clock::time_point reloc_yield_until_; // nguoi thang -> nhuong
// Pha "dung yen roi tinh chinh" (RelocSettle). Gom luc DANG QUAY lam dam
// diem nhoe 1-2 do moi scan (odom doc tre hon luc quet) -- mo phong
// 2026-09-29: chot lech 0.12 m va RTAB-Map khong sua vi robot dung ngay.
bool reloc_settle_ = false;
Pose2D reloc_settle_T_; // T_map_odom luc chot, chua tinh chinh
double reloc_settle_ratio_ = 0.0;
std::chrono::steady_clock::time_point reloc_settle_t0_;
std::chrono::steady_clock::time_point reloc_still_since_;
bool reloc_still_ = false;
Pose2D reloc_settle_prev_odom_;
std::vector<Point> reloc_settle_pts_;
int reloc_settle_scans_ = 0;
#endif
// Doc-only sau Start(), nen khong can mutex.