From 523f1ff1fcec83d33904b38a9ac04955cb8d369e Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Thu, 1 Oct 2026 10:53:00 +0700 Subject: [PATCH] reloc: tra node ve ban chieu 29/09 (2c6c839) -- tam go camera VPR khoi node Mo phong lech tu 30/09 la do thieu GAZEBO_MODEL_PATH (khong nap tuong benh vien), khong phai do code. Thu vien VPR / VisualVerifier van giu trong libs/, gan lai vao node sau khi chay on trong mo phong. Co-Authored-By: Claude Opus 5.5 --- app/src/main.cc | 9 -- app/src/runtime.cc | 252 +++------------------------------------------ app/src/runtime.h | 31 ------ 3 files changed, 13 insertions(+), 279 deletions(-) diff --git a/app/src/main.cc b/app/src/main.cc index 9e81b26..16782b7 100644 --- a/app/src/main.cc +++ b/app/src/main.cc @@ -76,10 +76,6 @@ void Usage(const char* argv0) { " 0.4 rad/s, toi da 6 vong / 12 m) toi khi chot.\n" " Lenh van qua lop an toan; tay cam/web ra lenh\n" " la tu chay nhuong ngay. THU TREN MO PHONG TRUOC.\n" - " --slam-reloc-vpr [ENGINE.plan]\n" - " kem --slam-reloc: CAMERA cung dinh vi lai (VPR + PnP,\n" - " chot khi trung LiDAR). Can build -DVSLAM_WITH_TENSORRT=ON;\n" - " mac dinh $VSLAM_RESOURCE_DIR/vpr/eigenplaces_r50_fp16.plan\n" " --check nap va KIEM cau hinh roi thoat, khong chay gi\n", argv0, vslam::ConfigDir().c_str()); } @@ -178,11 +174,6 @@ int main(int argc, char** argv) { } else if (a == "--slam-reloc") opts.slam_reloc = true; else if (a == "--slam-reloc-auto") opts.slam_reloc_auto = true; - else if (a == "--slam-reloc-vpr") { - // Duong dan engine la TUY CHON (giong --gamepad [DEV]). - opts.slam_reloc_vpr = true; - if (i + 1 < argc && argv[i + 1][0] != '-') opts.vpr_engine = argv[++i]; - } else if (a == "--check") check_only = true; else if (a == "--help" || a == "-h") { Usage(argv[0]); return 0; } else { diff --git a/app/src/runtime.cc b/app/src/runtime.cc index 427d0ab..cc61b8b 100644 --- a/app/src/runtime.cc +++ b/app/src/runtime.cc @@ -20,7 +20,6 @@ #include "vslam/sim_source.h" #ifdef VSLAM_HAVE_SLAM #include "vslam/occupancy_export.h" -#include #endif namespace vslam { @@ -393,10 +392,6 @@ StatusResponse Runtime::Start() { return StatusResponse::Error(StatusCode::kInvalidArgument, "--slam-reloc-auto chi di kem --slam-reloc"); } - if (opts_.slam_reloc_vpr && !opts_.slam_reloc) { - return StatusResponse::Error(StatusCode::kInvalidArgument, - "--slam-reloc-vpr chi di kem --slam-reloc"); - } slam_.reset(new RtabmapSlam); const StatusResponse st = slam_->Init(sc); if (!st.ok()) { @@ -517,45 +512,6 @@ StatusResponse Runtime::Start() { } reloc_field_.reset(new LikelihoodField(LikelihoodField::FromGrid(g, reloc_cfg_.sigma))); reloc_tracker_.reset(new HypothesisTracker(reloc_cfg_)); - // --- Camera cho dinh vi lai (--slam-reloc-vpr) --- - // Mo ta VPR cho MOI anh trong DB, tinh MOT lan luc mo ban do (TensorRT ~10 ms/anh ca - // giai nen). Khong bat duoc thi CANH BAO roi chay bang LiDAR nhu cu -- khong tu choi. - if (opts_.slam_reloc_vpr) { - VprConfig vc; - vc.engine_path = !opts_.vpr_engine.empty() - ? opts_.vpr_engine - : ResourceDir() + "/vpr/eigenplaces_r50_fp16.plan"; - std::unique_ptr enc; - const StatusResponse vs = CreateVprEncoder(vc, &enc); - if (!vs.ok()) { - VSLAM_WARN("slam: --slam-reloc-vpr: " << vs.message << " -- dinh vi lai CHI bang LiDAR."); - } else if (!slam_use_depth_ || camera_ == nullptr) { - VSLAM_WARN("slam: --slam-reloc-vpr can camera va slam.yaml camera_3d: true (de co anh IR)" - " -- dinh vi lai CHI bang LiDAR."); - } else { - const auto tv = std::chrono::steady_clock::now(); - for (int id : slam_->MapNodeIds()) { - NodeView nv; - std::vector d; - if (!slam_->GetNodeView(id, false, &nv).ok() || !enc->Describe(nv.gray, &d).ok()) { - continue; - } - vpr_ids_.push_back(id); - vpr_desc_.insert(vpr_desc_.end(), d.begin(), d.end()); - } - const double dv = - std::chrono::duration(std::chrono::steady_clock::now() - tv).count(); - if (vpr_ids_.empty()) { - VSLAM_WARN("slam: --slam-reloc-vpr: DB khong co anh nao -- dinh vi lai CHI bang LiDAR."); - } else { - vpr_ = std::move(enc); - vverif_.reset(new VisualVerifier); - VSLAM_INFO("slam: camera cho dinh vi lai -- mo ta VPR " << vpr_ids_.size() - << " anh ban do trong " << dv << " s; chot khi PnP >= 12 inlier TRUNG" - " LiDAR (0.5 m / 10 do)."); - } - } - } // Ban do DB len web NGAY: chua chot thi SlamLoop chua xuat ban do lan nao, // va robot that khong co ban do the gioi -> web chi con scan troi giua nen // trong. Luoi khop `g` giu THUAN LiDAR; ban hien thi moi gop vat can camera. @@ -1481,11 +1437,6 @@ void Runtime::RelocStep(const LaserScan& scan, const Pose2D& odom) { const auto now = std::chrono::steady_clock::now(); const bool running = opts_.slam_reloc_auto && reloc_exec_ != RelocExec::kHalted && reloc_exec_ != RelocExec::kOff; - auto cam_note = [this]() -> std::string { - if (!vpr_) return ""; - return " | camera: " + std::to_string(reloc_cam_shots_) + " anh, tot nhat " + - std::to_string(reloc_cam_best_inl_) + " diem khop"; - }; // --- Tu chay: NGUOI LUON THANG --- bool yielding = false; @@ -1547,9 +1498,6 @@ void Runtime::RelocStep(const LaserScan& scan, const Pose2D& odom) { if (!reloc_have_prev_) { reloc_t0_ = now; reloc_swept_ = 0.0; - reloc_cam_have_last_ = false; // chup ngay anh dau cua vong gom - reloc_cam_shots_ = 0; - reloc_cam_best_inl_ = 0; } else { // Goc da quet do bang odom: xoay tai cho thi odometry rat chinh xac (ban // ROS do: 0.1 do troi tren 1086 do xoay). @@ -1559,11 +1507,6 @@ void Runtime::RelocStep(const LaserScan& scan, const Pose2D& odom) { reloc_have_prev_ = true; const std::vector p = ScanToFrame(scan, odom); reloc_pts_.insert(reloc_pts_.end(), p.begin(), p.end()); - // Camera: chot SOM ngay giua vong xoay neu anh + LiDAR cung chi mot pose. - if (vpr_ && RelocCamera(scan, odom, now)) { - if (running && !yielding) RelocCommand(0.0, 0.0); - return; - } // 300 do = yaw_sweep cua ban ROS: mot scan thieu ~124 do (vung mu), xoay // 300 do de phu kin co du. 30 s = timeout cua ban ROS: dung yen thi van @@ -1578,14 +1521,14 @@ void Runtime::RelocStep(const LaserScan& scan, const Pose2D& odom) { SetRelocStatus("TU XOAY " + std::to_string(swept_deg) + "/300 do (vong " + std::to_string(reloc_exec_cycles_ + 1) + "/" + std::to_string(kRelocMaxCycles) + ")" + - (yielding ? " -- NHUONG NGUOI" : "") + cam_note(), + (yielding ? " -- NHUONG NGUOI" : ""), true); } else { // Tu chay da DUNG HAN thi giu chu do tren web -- nguoi van hanh phai // biet vi sao robot dung. SetRelocStatus(std::string(reloc_exec_ == RelocExec::kHalted ? "DUNG HAN tu chay -- " : "") + "XOAY TAI CHO " + std::to_string(swept_deg) + "/300 do (vong " + - std::to_string(reloc_cycle_ + 1) + ")" + cam_note(), + std::to_string(reloc_cycle_ + 1) + ")", true); } return; @@ -1649,187 +1592,19 @@ void Runtime::RelocStep(const LaserScan& scan, const Pose2D& odom) { // --- CHOT -> dung yen roi tinh chinh (RelocSettle) --- if (running && !yielding) RelocCommand(0.0, 0.0); - char src[48]; - std::snprintf(src, sizeof(src), "LiDAR, ty le %.2f", m.ratio()); - RelocBeginSettle(m.map_from_odom, m.ratio(), odom, now, src); -} - -void Runtime::RelocBeginSettle(const Pose2D& map_from_odom, double ratio, const Pose2D& odom, - std::chrono::steady_clock::time_point now, - const std::string& src) { reloc_settle_ = true; - reloc_settle_T_ = map_from_odom; - reloc_settle_ratio_ = ratio; - reloc_settle_src_ = src; + reloc_settle_T_ = m.map_from_odom; + reloc_settle_ratio_ = m.ratio(); reloc_settle_t0_ = now; reloc_still_ = false; reloc_settle_prev_odom_ = odom; reloc_settle_pts_.clear(); reloc_settle_scans_ = 0; - const Pose2D est = Pose2DCompose(map_from_odom, odom); - VSLAM_INFO("slam: reloc TIM THAY bang " << src << " (" << est.x << ", " << est.y << ", " - << est.theta * 180.0 / M_PI << " do) -- cho dung yen de tinh chinh truoc khi dat pose."); - SetRelocStatus("TIM THAY bang " + src + " -- dung yen de tinh chinh", true); -} - -// ---------------- Camera: VPR + PnP, CHOT khi TRUNG LiDAR ---------------- -// -// Luat da do offline (tools/reloc_pnp_eval.py + reloc_fusion_analyze.py, 2026-09-30): top-5 -// VPR, PnP >= 12 inlier, trung khop LiDAR cuc bo quanh node anh VPR < 0.5 m / 10 do, cac ung -// vien duoc chot khong mau thuan -> 25/09: 85 -> 159 dung, 28/09: 29 -> 40 dung, 0 SAI ca hai. -// LiDAR mot minh bi lua o hanh lang doi xung (lat 180 do); diem dac trung anh nhin nguoc lai -// thi khac han -> hai nguon sai o NHUNG CHO KHAC NHAU. -namespace { -constexpr double kCamEveryRad = 30.0 * M_PI / 180.0; // chup moi 30 do xoay -constexpr double kCamSlopSec = 0.05; // anh va scan coi la cung luc -constexpr int kCamTopK = 5; -constexpr int kCamMinInliers = 12; -constexpr double kCamMaxDz = 0.3; // robot tren san: nghiem PnP cao/nghieng -constexpr double kCamMaxTiltDeg = 10.0; // bat thuong la nghiem suy bien -constexpr double kCamAgreeM = 0.5; -constexpr double kCamAgreeRad = 10.0 * M_PI / 180.0; -constexpr double kCamLidarRadius = 2.5; // LiDAR cuc bo quanh node anh VPR -constexpr double kCamLidarYaw = 45.0 * M_PI / 180.0; - -cv::Matx44d MatxFromTransform(const Transform& t) { - const Vector3 ex = RotateVector(t.rotation, Vector3{1.0, 0.0, 0.0}); - const Vector3 ey = RotateVector(t.rotation, Vector3{0.0, 1.0, 0.0}); - const Vector3 ez = RotateVector(t.rotation, Vector3{0.0, 0.0, 1.0}); - return cv::Matx44d(ex.x, ey.x, ez.x, t.translation.x, ex.y, ey.y, ez.y, t.translation.y, - ex.z, ey.z, ez.z, t.translation.z, 0.0, 0.0, 0.0, 1.0); -} - -bool CamAgree(const Pose2D& a, const Pose2D& b) { - return std::hypot(a.x - b.x, a.y - b.y) < kCamAgreeM && - std::fabs(AngleDiff(a.theta, b.theta)) < kCamAgreeRad; -} -} // namespace - -bool Runtime::RelocCamera(const LaserScan& scan, const Pose2D& odom, - std::chrono::steady_clock::time_point now) { - if (reloc_cam_have_last_ && - std::fabs(AngleDiff(odom.theta, reloc_cam_last_theta_)) < kCamEveryRad) { - return false; - } - ImagePtr ir; - std::shared_ptr info; - UnixTime stamp; - double roll = 0.0, pitch = 0.0; - { - std::lock_guard lk(depth_mu_); - if (!have_depth_ || !latest_infra1_ || !latest_depth_info_) return false; - ir = latest_infra1_; - info = latest_depth_info_; - stamp = latest_depth_stamp_; - roll = latest_depth_roll_; - pitch = latest_depth_pitch_; - } - // Anh phai CUNG luc voi scan (odom cua scan dung lam odom luc chup) va MOI. - if (ir->format != PixelFormat::kMono8 || ir->data == nullptr || - std::fabs(SecondsBetween(scan.header.stamp, stamp)) > kCamSlopSec || - (reloc_cam_shots_ > 0 && stamp.toSec() <= reloc_cam_last_stamp_.toSec())) { - return false; - } - reloc_cam_have_last_ = true; - reloc_cam_last_theta_ = odom.theta; - reloc_cam_last_stamp_ = stamp; - ++reloc_cam_shots_; - - // Anh song -> JPEG -> giai nen: CUNG xu ly voi anh ban do (RTAB-Map luu anh JPEG). Do - // 2026-09-30: mot lan nen JPEG da lech mo ta VPR ~0.0065 cosine. - const cv::Mat raw(static_cast(ir->height), static_cast(ir->width), CV_8UC1, - const_cast(ir->data), static_cast(ir->step)); - std::vector jpg; - if (!cv::imencode(".jpg", raw, jpg)) return false; - const cv::Mat gray = cv::imdecode(jpg, cv::IMREAD_GRAYSCALE); - std::vector q; - if (gray.empty() || !vpr_->Describe(gray, &q).ok()) return false; - - // Top-K anh ban do giong nhat. - const size_t dim = q.size(); - std::vector> sc(vpr_ids_.size()); - for (size_t j = 0; j < vpr_ids_.size(); ++j) { - const float* d = vpr_desc_.data() + j * dim; - float s = 0.0f; - for (size_t k = 0; k < dim; ++k) s += q[k] * d[k]; - sc[j] = {s, j}; - } - const size_t topk = std::min(kCamTopK, sc.size()); - std::partial_sort(sc.begin(), sc.begin() + topk, sc.end(), - [](const std::pair& a, const std::pair& b) { - return a.first > b.first; - }); - - VisualFrame qf = vverif_->Extract(gray); - qf.K = cv::Matx33d(info->fx_binned(), 0.0, info->cx_binned(), 0.0, info->fy_binned(), - info->cy_binned(), 0.0, 0.0, 1.0); - // CUNG hinh hoc voi anh dua vao SLAM (SlamLoop: cam_now) -> cung quy uoc voi anh ban do. - qf.T_base_optical = MatxFromTransform(TransformCompose( - TiltLift(roll, pitch, cfg_->robot.geometry().wheel_radius), slam_cam_to_link_)); - - std::vector acc; // T_map_odom (pose LiDAR) cua ung vien DUOC CHOT - std::string why; - for (size_t k = 0; k < topk; ++k) { - const int id = vpr_ids_[sc[k].second]; - auto it = vpr_shots_.find(id); - if (it == vpr_shots_.end()) { - NodeView nv; - if (!slam_->GetNodeView(id, true, &nv).ok()) continue; - MapShot ms; - ms.f = vverif_->Extract(nv.gray); - ms.f.depth = nv.depth; - ms.f.K = nv.K; - ms.f.T_base_optical = nv.T_base_optical; - ms.T_map_base = nv.T_map_base; - it = vpr_shots_.emplace(id, std::move(ms)).first; - } - const PnpResult r = vverif_->Solve(qf, it->second.f, it->second.T_map_base); - reloc_cam_best_inl_ = std::max(reloc_cam_best_inl_, r.inliers); - if (r.inliers < kCamMinInliers || std::fabs(r.dz) > kCamMaxDz || r.tilt_deg > kCamMaxTiltDeg) { - continue; - } - Pose2D cam; - cam.x = r.x; - cam.y = r.y; - cam.theta = r.yaw; - // LiDAR cuc bo quanh node anh VPR: gioi han huong truoc, tu do huong sau (nhu da do). - const cv::Matx44d& Tn = it->second.T_map_base; - Point center; - center.x = Tn(0, 3); - center.y = Tn(1, 3); - const double node_yaw = std::atan2(Tn(1, 0), Tn(0, 0)); - const RelocHypothesis hv = LocalMatch(*reloc_field_, reloc_pts_, odom, center, - kCamLidarRadius, reloc_cfg_, node_yaw, kCamLidarYaw); - const RelocHypothesis hl = - LocalMatch(*reloc_field_, reloc_pts_, odom, center, kCamLidarRadius, reloc_cfg_); - for (const RelocHypothesis* h : {&hv, &hl}) { - if (h->score > 0.0 && CamAgree(Pose2DCompose(h->map_from_odom, odom), cam)) { - acc.push_back(h->map_from_odom); - if (why.empty()) { - char b[96]; - std::snprintf(b, sizeof(b), "node %d, giong %.2f, PnP %d inlier", id, sc[k].first, - r.inliers); - why = b; - } - break; - } - } - } - if (acc.empty()) return false; - for (const Pose2D& T : acc) { - if (!CamAgree(Pose2DCompose(T, odom), Pose2DCompose(acc[0], odom))) { - VSLAM_WARN("slam: reloc camera -- " << acc.size() - << " ung vien trung LiDAR nhung MAU THUAN nhau, khong chot."); - return false; - } - } - // Chot: vong gom nay coi nhu xong. - ++reloc_cycle_; - reloc_pts_.clear(); - reloc_have_prev_ = false; - reloc_swept_ = 0.0; - RelocBeginSettle(acc[0], 0.0, odom, now, "camera+LiDAR (" + why + ")"); - return true; + const Pose2D est = Pose2DCompose(m.map_from_odom, odom); + VSLAM_INFO("slam: reloc TIM THAY (" << est.x << ", " << est.y << ", " + << est.theta * 180.0 / M_PI << " do), ty le " << m.ratio() + << " -- cho dung yen de tinh chinh truoc khi dat pose."); + SetRelocStatus("TIM THAY -- dung yen de tinh chinh", true); } void Runtime::RelocSettle(const LaserScan& scan, const Pose2D& odom, @@ -1858,7 +1633,7 @@ void Runtime::RelocSettle(const LaserScan& scan, const Pose2D& odom, } if (!reloc_still_ || std::chrono::duration(now - reloc_still_since_).count() < 0.5) { - SetRelocStatus("TIM THAY bang " + reloc_settle_src_ + " -- dung yen de tinh chinh", true); + SetRelocStatus("TIM THAY -- dung yen de tinh chinh", true); return; } @@ -1931,10 +1706,9 @@ void Runtime::RelocFinish(const Pose2D& map_from_odom, const Pose2D& odom, reloc_ref_score_ = reloc_field_->MeanScore(ScanToFrame(scan, pose)); reloc_lost_pending_ = false; reloc_watch_next_ = now + std::chrono::seconds(1); - char buf[224]; - std::snprintf(buf, sizeof(buf), "DA DINH VI (%.2f, %.2f, %.0f do) sau %d vong, bang %s", - pose.x, pose.y, pose.theta * 180.0 / M_PI, reloc_cycle_, - reloc_settle_src_.c_str()); + char buf[160]; + std::snprintf(buf, sizeof(buf), "DA DINH VI (%.2f, %.2f, %.0f do) sau %d vong, ty le %.2f", + pose.x, pose.y, pose.theta * 180.0 / M_PI, reloc_cycle_, reloc_settle_ratio_); SetRelocStatus(buf, false); VSLAM_INFO("slam: " << buf << " (" << how << ") -- dat pose ban dau cho RTAB-Map; diem " "tham chieu " << reloc_ref_score_ << ", se dinh vi lai neu tut duoi " diff --git a/app/src/runtime.h b/app/src/runtime.h index b117d8e..dd13fe9 100644 --- a/app/src/runtime.h +++ b/app/src/runtime.h @@ -51,9 +51,7 @@ #ifdef VSLAM_HAVE_SLAM #include "vslam/global_localizer.h" #include "vslam/rtabmap_slam.h" -#include "vslam/visual_verifier.h" #include "vslam/voxel_export.h" -#include "vslam/vpr_encoder.h" #endif namespace vslam { @@ -104,11 +102,6 @@ struct RuntimeOptions { // --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; - // --slam-reloc-vpr [ENGINE]: CAMERA cung dinh vi lai -- VPR (TensorRT) goi y cho, PnP cho - // pose, chot khi TRUNG khop LiDAR. Can -DVSLAM_WITH_TENSORRT=ON; khong co thi canh bao - // roi chay bang LiDAR nhu cu. - bool slam_reloc_vpr = false; - std::string vpr_engine; // rong = $VSLAM_RESOURCE_DIR/vpr/eigenplaces_r50_fp16.plan }; class Runtime { @@ -174,13 +167,6 @@ class Runtime { std::chrono::steady_clock::time_point now); void RelocFinish(const Pose2D& map_from_odom, const Pose2D& odom, const LaserScan& scan, const std::string& how); - // Bat dau pha dung yen + tinh chinh sau khi CHOT -- chung cho LiDAR va camera. - void RelocBeginSettle(const Pose2D& map_from_odom, double ratio, const Pose2D& odom, - std::chrono::steady_clock::time_point now, const std::string& src); - // Camera: cu 30 do xoay chup mot anh -> VPR top-5 -> PnP -> so voi LiDAR cuc bo quanh - // node anh. true = da CHOT (da vao RelocBeginSettle). - bool RelocCamera(const LaserScan& scan, const Pose2D& odom, - std::chrono::steady_clock::time_point now); #endif void ApplyThreadPriority(std::thread* t, int prio, const char* name); @@ -390,23 +376,6 @@ class Runtime { Pose2D reloc_settle_prev_odom_; std::vector reloc_settle_pts_; int reloc_settle_scans_ = 0; - std::string reloc_settle_src_; // "LiDAR, ty le .." | "camera+LiDAR (..)" -- log/web - - // --- Camera trong dinh vi lai (--slam-reloc-vpr). CHI luong slam cham. --- - std::unique_ptr vpr_; // null = khong dung camera - std::unique_ptr vverif_; - std::vector vpr_ids_; // node ban do co mo ta - std::vector vpr_desc_; // vpr_ids_.size() x dim, da chuan hoa - struct MapShot { - VisualFrame f; // ORB + depth + K + T_base_optical - cv::Matx44d T_map_base; - }; - std::map vpr_shots_; // node ban do, nap khi can roi giu - bool reloc_cam_have_last_ = false; - double reloc_cam_last_theta_ = 0.0; // odom.theta lan chup gan nhat - UnixTime reloc_cam_last_stamp_; - int reloc_cam_shots_ = 0; // anh da thu trong vong gom nay - int reloc_cam_best_inl_ = 0; // inlier PnP tot nhat vong nay #endif // Doc-only sau Start(), nen khong can mutex.