diff --git a/app/src/runtime.cc b/app/src/runtime.cc index 2e27121..94d2e74 100644 --- a/app/src/runtime.cc +++ b/app/src/runtime.cc @@ -1135,32 +1135,50 @@ void Runtime::WebLoop() { pose_hist.pop_front(); } - if (snap.cloud) { - const UnixTime cs = snap.cloud->header.stamp; - const PoseAt* best = nullptr; - double best_dt = 0.0; + // Pose tai mot moc thoi gian, NOI SUY giua hai mau gan nhat. + // + // Lay mau gan nhat de lai sai so bang nua chu ky lich su (50 ms o 10 Hz). + // Di thang 0.4 m/s thi do la 2 cm, bo qua duoc. Quay 50 do/s thi do la + // 2.5 do, o 3 m la 13 cm -- nhin thay tren robot. + // + // Stamp cam bien cung goc thoi gian voi `clock_->Now()` (camera: domain + // SYSTEM/GLOBAL_TIME, hoac thoi diem NHAN neu lui ve TIME_OF_ARRIVAL). + // Lech hon ca cua so bu nghia la gia thiet do sai -> khong bu. + auto pose_at = [&pose_hist](UnixTime t, Pose2D* out) -> bool { + const PoseAt* lo = nullptr; + const PoseAt* hi = nullptr; 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; } + if (SecondsBetween(t, e.stamp) >= 0.0) { lo = &e; continue; } + hi = &e; + break; } - // 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 = Pose2DBetween(ekf_pose, best->pose); + if (lo != nullptr && hi != nullptr) { + const double span = SecondsBetween(hi->stamp, lo->stamp); + const double u = span > 1e-9 ? SecondsBetween(t, lo->stamp) / span : 0.0; + out->x = lo->pose.x + u * (hi->pose.x - lo->pose.x); + out->y = lo->pose.y + u * (hi->pose.y - lo->pose.y); + out->theta = NormalizeAngle( + lo->pose.theta + u * AngleDiff(hi->pose.theta, lo->pose.theta)); + return true; + } + // Ngoai dai: nhan mau bien neu con trong cua so bu, khong thi thoi. + const PoseAt* edge = (lo != nullptr) ? lo : hi; + if (edge == nullptr) return false; + if (std::fabs(SecondsBetween(t, edge->stamp)) > kPoseMatchMaxSec) return false; + *out = edge->pose; + return true; + }; + + double cloud_age = -1.0, scan_age = -1.0; + + if (snap.cloud) { + cloud_age = SecondsBetween(snap.stamp, snap.cloud->header.stamp); + Pose2D at; + if (pose_at(snap.cloud->header.stamp, &at)) { + snap.cloud_delta = Pose2DBetween(ekf_pose, at); snap.have_cloud_delta = true; } - // ⚠️ Khong co dong nay thi cau hoi "cloud gia bao nhieu" chi tra loi - // duoc bang cach nhin man hinh roi doan -- da mat vai vong nhu vay. - VSLAM_INFO_THROTTLE(2.0, - "cloud: tuoi " << SecondsBetween(snap.stamp, cs) << " s" - << ", best_dt=" << best_dt - << ", bu=" << (snap.have_cloud_delta ? "co" : "KHONG") - << " (d=" << std::hypot(snap.cloud_delta.x, snap.cloud_delta.y) - << " m, " << (snap.cloud_delta.theta * 180.0 / M_PI) << " do)" - << ", stamp=" << cs.toSec() << " now=" << snap.stamp.toSec()); - // So do CU khong duoc hien nhu so do MOI -- cung nguyen tac voi khoi // cliff o tren. Gio `latest_cloud_` giu lai dam may cuoi cung thay vi bi // xoa (xem wiring.cc), nen phai co han: qua cua so bu thi khong dat dung @@ -1168,6 +1186,34 @@ void Runtime::WebLoop() { if (!snap.have_cloud_delta) snap.cloud = nullptr; } + // Scan KHONG bu duoc o server: do la du lieu CUC, khong ap mot phep doi + // SE(2) thang len (range, goc) duoc nhu voi cloud. Gui delta len trinh + // duyet -- noi scan da duoc doi sang Descartes de ve. + // + // Khong bu thi scan la lop lech NHIEU NHAT khi quay: tuoi 0-140 ms (gui + // 10 Hz cong voi trinh duyet giu giua hai khung), o 50 do/s la 3-7 do, o + // 3 m la 18-36 cm. Cloud da bu roi nen chinh scan moi la cai troi ra. + if (snap.scan) { + scan_age = SecondsBetween(snap.stamp, snap.scan->header.stamp); + Pose2D at; + if (pose_at(snap.scan->header.stamp, &at)) { + snap.scan_delta = Pose2DBetween(ekf_pose, at); + snap.have_scan_delta = true; + } + } + + // ⚠️ Khong co dong nay thi cau hoi "lop nay gia bao nhieu" chi tra loi + // duoc bang cach nhin man hinh roi doan -- da mat vai vong nhu vay. + if (snap.cloud != nullptr || snap.scan != nullptr) { + VSLAM_INFO_THROTTLE(2.0, + "bu do tre: cloud tuoi " << cloud_age << " s (" + << (snap.have_cloud_delta ? snap.cloud_delta.theta * 180.0 / M_PI : 0.0) + << " do, " << (snap.have_cloud_delta ? "bu" : "KHONG bu") << ")" + << ", scan tuoi " << scan_age << " s (" + << (snap.have_scan_delta ? snap.scan_delta.theta * 180.0 / M_PI : 0.0) + << " do, " << (snap.have_scan_delta ? "bu" : "KHONG bu") << ")"); + } + // ⚠️ 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/web_snapshot.h b/libs/vslam_web/include/vslam/web_snapshot.h index 9c1db96..140b32f 100644 --- a/libs/vslam_web/include/vslam/web_snapshot.h +++ b/libs/vslam_web/include/vslam/web_snapshot.h @@ -144,6 +144,13 @@ struct WebSnapshot { TeleopSnapshot teleop; LaserScanPtr scan; + // Phep bien doi quy scan tu he laser LUC QUET ve pose HIEN TAI. Khac + // `cloud_delta` o cho no KHONG duoc ap o server: scan la du lieu cuc + // (range, goc) va mot phep doi SE(2) khong ap thang len do duoc. Gui len + // trinh duyet, noi scan da thanh diem Descartes. + Pose2D scan_delta; + bool have_scan_delta = false; + 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 diff --git a/libs/vslam_web/src/json_writer.cc b/libs/vslam_web/src/json_writer.cc index 66d197f..58c3283 100644 --- a/libs/vslam_web/src/json_writer.cc +++ b/libs/vslam_web/src/json_writer.cc @@ -117,6 +117,20 @@ void WriteStateJson(const WebSnapshot& s, std::string* out) { Num("yaw", s.pose_map.theta, 4, out); out->push_back('}'); Comma(out); + // Bu do tre cua scan: quy tia tu pose HIEN TAI ve pose LUC QUET. Cloud + // duoc bu san o server (EncodeCloud), scan thi khong -- no la du lieu cuc. + // + // Chi xuat khi CO. Khong co thi trinh duyet ve bang pose hien tai nhu truoc, + // chu khong phai ve bang mot delta 0 gia mao la "da bu". + if (s.have_scan_delta) { + Key("scan_delta", out); + out->push_back('{'); + Num("x", s.scan_delta.x, 4, out); Comma(out); + Num("y", s.scan_delta.y, 4, out); Comma(out); + Num("yaw", s.scan_delta.theta, 5, out); + out->push_back('}'); Comma(out); + } + // Ground truth chi xuat khi CO. Xuat 0 khi khong co la noi doi: client // khong phan biet duoc "dung o goc" voi "khong biet". if (s.have_ground_truth) { diff --git a/libs/vslam_web/www/app.js b/libs/vslam_web/www/app.js index 09524d4..662200c 100644 --- a/libs/vslam_web/www/app.js +++ b/libs/vslam_web/www/app.js @@ -73,8 +73,9 @@ const R_BLANKED = 0xFFFD; // bi sector mask cat (than robot che) const state = { pose: null, gt: null, scan: null, cloud: null, path: null, - // Pose tai thoi diem khung CLOUD toi. Xem cloudAnchorPose(). - cloudPose: null, + // Pose ma tung lop du lieu THUOC VE, chot luc khung cua no toi. Xem + // anchorPose(). `scanDelta` den tu khung state, dung ngay khung scan ke tiep. + cloudPose: null, scanPose: null, scanDelta: null, map: null, mapImage: null, mapVersion: 0, bytes: 0, lastBytes: 0, wireRate: 0, }; @@ -149,6 +150,10 @@ function onBinary(dv) { pts[m++] = r * Math.sin(a); } state.scan = pts.subarray(0, m); + // Tia van o he laser LUC QUET -- server khong quy duoc, du lieu cuc. + // `scan_delta` tu khung state cua cung lan Publish() cho biet pose luc + // quet; neo scan vao do thay vi vao pose hien tai. + state.scanPose = composePose(state.pose, state.scanDelta); return; } @@ -165,6 +170,7 @@ function onBinary(dv) { state.cloud = pts; // Publish() gui khung STATE truoc khung CLOUD trong cung mot lan goi, nen // `state.pose` o day dung la pose ma server da dung de tinh `cloud_delta`. + // Diem DA duoc quy ve pose do, nen neo thang vao no. state.cloudPose = state.pose; return; } @@ -256,6 +262,10 @@ function rasterize(map, pal) { function onState(s) { state.pose = s.pose; + // Khong co truong nay = server khong bu duoc do tre scan. Dat null chu + // khong dat {0,0,0}: mot delta 0 gia mao la "da bu", con null thi lop ve + // biet la chua co gi va dung pose hien tai nhu truoc. + state.scanDelta = s.scan_delta || null; state.mapVersion = s.slam.map_version; // Hinh hoc robot den TU SERVER (robot.yaml). File nay khong duoc chua mot // hang so kich thuoc nao -- xem ghi chu trong app3d.js. @@ -434,19 +444,31 @@ function drawRobot(ctx, x, y, yaw, color, alpha, pal) { ctx.restore(); } -// Pose de ve CLOUD, khong phai pose robot. +// a ⊕ d -- dat `d` (he cua a) vao he the gioi. +function composePose(a, d) { + if (!a) return a; + if (!d) return a; + const c = Math.cos(a.yaw), s = Math.sin(a.yaw); + return { + x: a.x + c * d.x - s * d.y, + y: a.y + s * d.x + c * d.y, + yaw: a.yaw + d.yaw, + }; +} + +// Pose de ve MOT LOP DU LIEU, khong phai pose robot. // -// Server da quy cloud ve he base_footprint tai thoi diem GUI (EncodeCloud + -// cloud_delta). Nhung cloud chi toi 2 Hz con trang ve lai moi khung hinh: ve -// bang pose moi nhat thi ngay sau khi nhan, sai so moc lai tu 0 len -// `v * 0.5 s`. Phai neo dam may vao pose LUC KHUNG DO TOI. +// Moi lop toi theo nhip rieng (cloud 2 Hz, scan 10 Hz) con trang ve lai moi +// khung hinh. Ve bang pose moi nhat thi ngay sau khi nhan, sai so moc lai tu +// 0 den `v * chu ky lop do`. Phai neo lop vao pose no THUOC VE. // -// Tra ve pose TUONG DOI so voi `robotPose` dang ve, khong phai pose tuyet -// doi: khung "the gioi" ve tai pose ground truth va no can cung phep bu nay. -function cloudAnchorPose(robotPose) { - if (!state.cloudPose || !state.pose || !robotPose) return robotPose; - const dx = state.cloudPose.x - state.pose.x; - const dy = state.cloudPose.y - state.pose.y; +// `anchoredAt` la pose tuyet doi cua lop, chot luc khung cua no toi. Ham tra +// ve pose TUONG DOI so voi `robotPose` dang ve chu khong phai pose tuyet doi: +// khung "the gioi" ve tai pose ground truth va no can cung phep bu nay. +function anchorPose(robotPose, anchoredAt) { + if (!anchoredAt || !state.pose || !robotPose) return robotPose; + const dx = anchoredAt.x - state.pose.x; + const dy = anchoredAt.y - state.pose.y; const cn = Math.cos(state.pose.yaw), sn = Math.sin(state.pose.yaw); const lx = cn * dx + sn * dy; const ly = -sn * dx + cn * dy; @@ -454,7 +476,7 @@ function cloudAnchorPose(robotPose) { return { x: robotPose.x + c * lx - s * ly, y: robotPose.y + s * lx + c * ly, - yaw: robotPose.yaw + (state.cloudPose.yaw - state.pose.yaw), + yaw: robotPose.yaw + (anchoredAt.yaw - state.pose.yaw), }; } @@ -515,10 +537,10 @@ function drawScene(targetCv, targetCtx, grid, gridImage, robotPose, showGt, mapT ctx.stroke(); } - // scan va cloud o frame robot -> xoay theo pose truoc khi ve. + // scan va cloud o frame robot -> xoay theo pose truoc khi ve. Nhung KHONG + // phai pose hien tai: moi lop neo vao pose no thuoc ve, xem anchorPose(). const p = robotPose; if (p) { - const c = Math.cos(p.yaw), s = Math.sin(p.yaw); // Ground truth ve TRUOC pose uoc luong, de pose ve de len tren: cai ta // dang danh gia phai nhin ro hon cai lam chuan. @@ -527,8 +549,8 @@ function drawScene(targetCv, targetCtx, grid, gridImage, robotPose, showGt, mapT } if (state.cloud && on('lyCloud')) { - // Cloud KHONG dung `p` -- xem cloudAnchorPose(). - const cp = cloudAnchorPose(p); + // Cloud KHONG dung `p` -- xem anchorPose(). + const cp = anchorPose(p, state.cloudPose); const cc = Math.cos(cp.yaw), cs = Math.sin(cp.yaw); const pts = state.cloud; for (let i = 0; i < pts.length; i += 3) { @@ -551,10 +573,14 @@ function drawScene(targetCv, targetCtx, grid, gridImage, robotPose, showGt, mapT // vao pose cua base. Xem ghi chu trong v3dSetScan(). const lx = (state.robot && state.robot.laser_x) || 0; const ly = (state.robot && state.robot.laser_y) || 0; + // Neo vao pose LUC QUET, khong phai `p` -- xem anchorPose(). Khi quay + // 50 do/s thi 100 ms giu giua hai khung la 5 do, o 3 m la 26 cm. + const sp = anchorPose(p, state.scanPose); + const sc = Math.cos(sp.yaw), ss = Math.sin(sp.yaw); for (let i = 0; i < pts.length; i += 2) { const bx = pts[i] + lx, by = pts[i + 1] + ly; - const x = p.x + c * bx - s * by; - const y = p.y + s * bx + c * by; + const x = sp.x + sc * bx - ss * by; + const y = sp.y + ss * bx + sc * by; ctx.fillRect(sx(x, y) - 1.5, sy(x, y) - 1.5, 3, 3); } } @@ -1023,8 +1049,10 @@ function draw3d() { V3D.gtGroup.visible = false; } - v3dSetScan(on('lyScan') ? state.scan : null, state.pose, state.robot); - v3dSetCloud(on('lyCloud') ? state.cloud : null, cloudAnchorPose(state.pose)); + v3dSetScan(on('lyScan') ? state.scan : null, + anchorPose(state.pose, state.scanPose), state.robot); + v3dSetCloud(on('lyCloud') ? state.cloud : null, + anchorPose(state.pose, state.cloudPose)); // THE GIOI: tuong 3D dung tu ban do mo phong -- day la phan "Gazebo". // Dung MOT lan (ban do the gioi la tinh), roi chi bat/tat.