From 1efef2ef47392680cbdcfcff0a20b486506ab32f Mon Sep 17 00:00:00 2001 From: trung09122002 Date: Tue, 22 Sep 2026 09:31:16 +0700 Subject: [PATCH] update --- app/src/runtime.cc | 49 +++++++++++++++++++++ libs/vslam_web/include/vslam/binary_codec.h | 6 ++- libs/vslam_web/include/vslam/web_snapshot.h | 6 +++ libs/vslam_web/src/binary_codec.cc | 13 ++++-- libs/vslam_web/src/web_server.cc | 47 ++++++++++++++++++-- test/test_binary_codec.cc | 47 +++++++++++++++++++- 6 files changed, 157 insertions(+), 11 deletions(-) diff --git a/app/src/runtime.cc b/app/src/runtime.cc index fcd9466..79419e3 100644 --- a/app/src/runtime.cc +++ b/app/src/runtime.cc @@ -5,6 +5,7 @@ #include #include +#include #include "vslam/clock.h" #include "vslam/geometry.h" @@ -1000,6 +1001,14 @@ void Runtime::WebLoop() { // trong vong lap khi khung dau tien toi. } + // Lich su pose de bu do tre cua cloud. 5 s phu moi do tre thuc te. + constexpr double kPoseHistorySec = 5.0; + // Khong khop duoc trong 2 s thi khong con la do tre nua ma la stamp sai + // GOC THOI GIAN -- luc do bu se te hon khong bu. + constexpr double kPoseMatchMaxSec = 2.0; + struct PoseAt { UnixTime stamp; Pose2D pose; }; + std::deque pose_hist; + while (!stop_.load()) { WebSnapshot snap; snap.robot = shape; @@ -1103,6 +1112,46 @@ void Runtime::WebLoop() { } } + // ---- Bu do tre cua cloud ---- + // app.js ve cloud VA scan bang pose moi nhat ("scan va cloud o frame + // robot -> xoay theo pose truoc khi ve"). Cloud cu hon scan nhieu: no qua + // depth_filter (10 Hz), qua gravity_align, roi doi luot gui. Ve mot dam + // may 0.5 s tuoi tai pose hien tai la dat no lech dung `v * 0.5 s` -- + // 25 cm o 0.5 m/s. Do la thu nhin thay tren robot: cloud va scan tach + // nhau khi chay, khop lai 1-2 s sau khi dung. + // + // Bu bang phep bien doi TUONG DOI trong he robot, khong phai bang pose + // tuyet doi: nhu vay khung "the gioi" (ve tai pose ground truth) dung + // duoc cung mot con so, va app.js khong phai doi gi. + // + // ⚠️ Lich su giu `ekf_pose` (he ODOM), KHONG phai `pose_map`. pose_map + // NHAY moi lan SLAM dong vong -- cu nhay do se chui vao `delta` va day + // ca dam may di mot doan khong lien quan gi toi chuyen dong cua robot. + // Odom khong nhay, va thu can o day dung la chuyen dong THAN robot giua + // luc chup va bay gio. + pose_hist.push_back(PoseAt{snap.stamp, ekf_pose}); + while (pose_hist.size() > 1 && + SecondsBetween(snap.stamp, pose_hist.front().stamp) > kPoseHistorySec) { + pose_hist.pop_front(); + } + + if (snap.cloud) { + const UnixTime cs = snap.cloud->header.stamp; + const PoseAt* best = nullptr; + double best_dt = 0.0; + for (const PoseAt& e : pose_hist) { + const double d = std::fabs(SecondsBetween(cs, e.stamp)); + if (best == nullptr || d < best_dt) { best = &e; best_dt = d; } + } + // Stamp camera la TIME_OF_ARRIVAL (D410 FW khong ho tro global time, + // xem sensors.yaml) nen cung goc thoi gian voi `clock_->Now()`. Lech + // hon ca cua so lich su nghia la gia thiet do khong con dung -> khong bu. + if (best != nullptr && best_dt <= kPoseMatchMaxSec) { + snap.cloud_delta = Pose2DCompose(Pose2DInverse(ekf_pose), best->pose); + snap.have_cloud_delta = true; + } + } + // ⚠️ Quy dao phai lay SAU khi `pose_map` da duoc tinh. // // Khoi nay truoc day nam TRUOC phep tinh pose_map, nen no doc mot diff --git a/libs/vslam_web/include/vslam/binary_codec.h b/libs/vslam_web/include/vslam/binary_codec.h index 47525cb..591cd27 100644 --- a/libs/vslam_web/include/vslam/binary_codec.h +++ b/libs/vslam_web/include/vslam/binary_codec.h @@ -58,8 +58,12 @@ void EncodeScan(const LaserScan& scan, UnixTime stamp, std::string* out); // Giam mau xuong `max_points` bang cach lay buoc deu, roi luong tu hoa theo // `quant_m`. Bo diem NaN. +// +// `delta` quy diem ve he base_footprint HIEN TAI truoc khi ma hoa. Ap o day +// chu khong tren ca dam may vi o day da qua buoc deu: quy 40 k diem roi vut +// di 36 k la lang phi thuan tuy. Dong nhat = khong bu. void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points, - float quant_m, std::string* out); + float quant_m, const Pose2D& delta, std::string* out); // RLE hang-chinh. Xem rle.h. // `type` cho phep dung chung bo ma hoa cho ban do SLAM (kMapFull) va ban do diff --git a/libs/vslam_web/include/vslam/web_snapshot.h b/libs/vslam_web/include/vslam/web_snapshot.h index c7bfe7a..9c1db96 100644 --- a/libs/vslam_web/include/vslam/web_snapshot.h +++ b/libs/vslam_web/include/vslam/web_snapshot.h @@ -145,6 +145,12 @@ struct WebSnapshot { LaserScanPtr scan; PointCloudXYZPtr cloud; + // Phep bien doi quy cloud tu he base_footprint LUC CHUP ve he + // base_footprint HIEN TAI. Trinh duyet ve cloud bang pose moi nhat, nen + // mot dam may 0.5 s tuoi se bi dat lech dung `v * 0.5 s` neu khong bu. + // Dong nhat khi khong biet pose luc chup -- xem Runtime::WebLoop. + Pose2D cloud_delta; + bool have_cloud_delta = false; OccupancyGridPtr map; // ban do SLAM dung // Ban do THE GIOI cua bo mo phong (khung nhin "Gazebo"). Null khi chay // that -- luc do khong co su that nao de doi chieu. diff --git a/libs/vslam_web/src/binary_codec.cc b/libs/vslam_web/src/binary_codec.cc index 4c9690a..d2ff049 100644 --- a/libs/vslam_web/src/binary_codec.cc +++ b/libs/vslam_web/src/binary_codec.cc @@ -101,7 +101,7 @@ void EncodeScan(const LaserScan& scan, UnixTime stamp, std::string* out) { } void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points, - float quant_m, std::string* out) { + float quant_m, const Pose2D& delta, std::string* out) { if (quant_m <= 0.0f) quant_m = 0.01f; // Lay buoc deu thay vi cat duoi: cat duoi lam mat ca mot vung khong gian, @@ -118,22 +118,27 @@ void EncodeCloud(const PointCloudXYZ& cloud, UnixTime stamp, uint32_t max_points } PutHeader(out, BinaryType::kCloud, stamp); - // Goc = (0,0,0): cloud da o frame base_footprint sau gravity_align. + // Goc = (0,0,0): sau khi ap `delta` o vong duoi, diem nam trong he + // base_footprint HIEN TAI -- dung he ma trinh duyet ve. PutF32(out, 0.0f); PutF32(out, 0.0f); PutF32(out, 0.0f); PutF32(out, quant_m); PutU32(out, n_out); + const double dc = std::cos(delta.theta), ds = std::sin(delta.theta); const float inv = 1.0f / quant_m; constexpr float kLim = 32767.0f; for (size_t i = 0; i < n_in; i += stride) { const PointXYZ& p = cloud.points[i]; if (!std::isfinite(p.x) || !std::isfinite(p.y) || !std::isfinite(p.z)) continue; + // z khong doi: `delta` la SE(2), robot khong di chuyen theo phuong dung. + const double bx = delta.x + dc * p.x - ds * p.y; + const double by = delta.y + ds * p.x + dc * p.y; // Kep vao dai i16. Voi quant 0.01 m thi dai la +-327 m -- khong bao gio // cham toi trong nha, nhung kep van re hon la de tran am tham. - const float qx = std::max(-kLim, std::min(kLim, p.x * inv)); - const float qy = std::max(-kLim, std::min(kLim, p.y * inv)); + const float qx = std::max(-kLim, std::min(kLim, static_cast(bx) * inv)); + const float qy = std::max(-kLim, std::min(kLim, static_cast(by) * inv)); const float qz = std::max(-kLim, std::min(kLim, p.z * inv)); PutI16(out, static_cast(std::lround(qx))); PutI16(out, static_cast(std::lround(qy))); diff --git a/libs/vslam_web/src/web_server.cc b/libs/vslam_web/src/web_server.cc index ac7f143..d50f670 100644 --- a/libs/vslam_web/src/web_server.cc +++ b/libs/vslam_web/src/web_server.cc @@ -118,6 +118,39 @@ class WebServerImpl { mutable std::mutex world_mu; std::string world_blob; + // --- Nhip gui rieng cho tung kenh ------------------------------------- + // `status_rate_hz`, `scan_rate_hz`, `cloud_rate_hz`, `path_rate_hz` da nam + // trong system.yaml tu dau va da duoc parse vao WebConfig, nhung KHONG cho + // nao ap dung chung -- chi `tools/vslam_web_demo.cc`. Publish() gui ca ba + // kenh o MOI lan goi, tuc o status_rate_hz = 10 Hz. + // + // Do duoc: cloud 4000 diem = 24 kB, gui 10 Hz la 240 kB/s -- gap DOI ngan + // sach 120 kB/s cua ca UI. Hang doi 4 khung day lien tuc, drop khung cu + // nhat, va man hinh tut lai 1-2 giay so voi robot. Chinh cai bay ma + // system.yaml da ghi ("109 kB/s chi vi PATH gui o 9.8 Hz thay vi 1 Hz"), + // quay lai o ca ba kenh. + // + // CHI luong goi Publish() cham vao -- khong khoa. + UnixTime last_scan_at, last_cloud_at, last_path_at; + bool have_scan_at = false, have_cloud_at = false, have_path_at = false; + + // Den luot kenh nay chua? `rate <= 0` = khong gioi han. + // + // Gate theo STAMP CUA SNAPSHOT, khong theo dong ho tuong: phat lai ban ghi + // nhanh hon thoi gian that phai ra cung ket qua nhu chay live. Cung ly do + // da ghi trong DepthFilter::Apply(). + static bool Due(double rate, UnixTime now, UnixTime* last, bool* have) { + if (rate <= 0.0) return true; + if (*have) { + const double dt = SecondsBetween(now, *last); + // dt < 0 = dong ho lui (ban ghi phat lai tu dau) -> gui va dat lai moc. + if (dt >= 0.0 && dt < 1.0 / rate) return false; + } + *last = now; + *have = true; + return true; + } + WebServerImpl(WebConfig c, WebServer::CommandHandler cmd, WebServer::TeleopHandler tel) : cfg(std::move(c)), on_command(std::move(cmd)), on_teleop(std::move(tel)) {} @@ -712,20 +745,26 @@ void WebServer::Publish(const WebSnapshot& snap) { WriteStateJson(snap, state.get()); for (const auto& s : targets) s->Send(state, /*binary=*/false, /*is_map=*/false); - if (snap.scan) { + if (snap.scan && impl_->Due(impl_->cfg.scan_hz, snap.stamp, + &impl_->last_scan_at, &impl_->have_scan_at)) { auto buf = std::make_shared(); EncodeScan(*snap.scan, snap.stamp, buf.get()); for (const auto& s : targets) s->Send(buf, true, false); } - if (snap.cloud) { + if (snap.cloud && impl_->Due(impl_->cfg.cloud_hz, snap.stamp, + &impl_->last_cloud_at, &impl_->have_cloud_at)) { auto buf = std::make_shared(); EncodeCloud(*snap.cloud, snap.stamp, impl_->cfg.cloud_max_points, - impl_->cfg.cloud_quant_m, buf.get()); + impl_->cfg.cloud_quant_m, + snap.have_cloud_delta ? snap.cloud_delta : Pose2D{}, + buf.get()); for (const auto& s : targets) s->Send(buf, true, false); } - if (!snap.trajectory.empty()) { + if (!snap.trajectory.empty() && + impl_->Due(impl_->cfg.path_hz, snap.stamp, &impl_->last_path_at, + &impl_->have_path_at)) { auto buf = std::make_shared(); EncodePath(snap.trajectory, snap.stamp, impl_->cfg.path_min_step_m, buf.get()); for (const auto& s : targets) s->Send(buf, true, false); diff --git a/test/test_binary_codec.cc b/test/test_binary_codec.cc index 94c1dbd..71d5a1a 100644 --- a/test/test_binary_codec.cc +++ b/test/test_binary_codec.cc @@ -184,7 +184,7 @@ TEST(BinaryCodec, CloudGiamMauVaBoNaN) { c.points[5] = PointXYZ{std::numeric_limits::quiet_NaN(), 0.0f, 0.0f}; std::string buf; - EncodeCloud(c, UnixTime::fromSec(1.0), /*max_points=*/1000, 0.01f, &buf); + EncodeCloud(c, UnixTime::fromSec(1.0), /*max_points=*/1000, 0.01f, Pose2D{}, &buf); // 16 header + 20 (origin3 + scale + n) + n*6 ASSERT_GE(buf.size(), 36u); @@ -196,13 +196,56 @@ TEST(BinaryCodec, CloudGiamMauVaBoNaN) { TEST(BinaryCodec, CloudRongVanHopLe) { PointCloudXYZ c; std::string buf; - EncodeCloud(c, UnixTime::fromSec(1.0), 1000, 0.01f, &buf); + EncodeCloud(c, UnixTime::fromSec(1.0), 1000, 0.01f, Pose2D{}, &buf); EXPECT_EQ(buf.size(), 36u); BinaryHeader h; ASSERT_TRUE(DecodeHeader(buf, 0, &h)); EXPECT_EQ(h.type, BinaryType::kCloud); } +// Diem phai DI THEO delta, neu khong thi phep bu do tre khong ton tai that. +// +// `delta` la `pose_now^-1 * pose_capture` (xem Runtime::WebLoop), nen robot +// TIEN 0.5 m ke tu luc chup cho ra delta.x = -0.5: diem tung o 1.0 m truoc +// mui gio chi con cach 0.5 m. +TEST(BinaryCodec, CloudApDeltaTinhTien) { + PointCloudXYZ c; + c.points.push_back(PointXYZ{1.0f, 0.0f, 0.0f}); + + std::string buf; + EncodeCloud(c, UnixTime::fromSec(1.0), 100, 0.01f, Pose2D{-0.5, 0.0, 0.0}, &buf); + + ASSERT_EQ(buf.size(), 42u); // 16 header + 20 + 1*6 + auto i16 = [&buf](size_t off) { + return static_cast(static_cast(buf[off]) | + (static_cast(buf[off + 1]) << 8)); + }; + EXPECT_NEAR(i16(36) * 0.01, 0.5, 1e-6); + EXPECT_NEAR(i16(38) * 0.01, 0.0, 1e-6); +} + +// Xoay, khong chi tinh tien: quay tai cho la truong hop lam cloud va scan +// tach nhau ro nhat, va mot delta chi co tinh tien se khong bat duoc no. +TEST(BinaryCodec, CloudApDeltaXoay) { + PointCloudXYZ c; + c.points.push_back(PointXYZ{1.0f, 0.0f, 0.5f}); + + std::string buf; + // Robot da quay PHAI 90 do ke tu luc chup -> diem truoc mui gio o ben TRAI. + EncodeCloud(c, UnixTime::fromSec(1.0), 100, 0.01f, + Pose2D{0.0, 0.0, M_PI / 2.0}, &buf); + + ASSERT_EQ(buf.size(), 42u); + auto i16 = [&buf](size_t off) { + return static_cast(static_cast(buf[off]) | + (static_cast(buf[off + 1]) << 8)); + }; + EXPECT_NEAR(i16(36) * 0.01, 0.0, 1e-6); + EXPECT_NEAR(i16(38) * 0.01, 1.0, 1e-6); + // z khong bi delta cham vao. + EXPECT_NEAR(i16(40) * 0.01, 0.5, 1e-6); +} + TEST(BinaryCodec, PathThuaHoa) { std::vector path; for (int i = 0; i < 1000; ++i) {