reloc,web: dinh vi lai TREN DOC (slope.enabled) -- loc gia thuyet bang do nghieng IMU vs do doc ban do, chan mu doc truc (ShiftScoreRatio), vong DUNG YEN khong xoay (ty le nho nhat >= 3 vong / hai cho cung pose), tu chay bo thang tren doc; camera VPR+PnP tren doc (>= 2 anh trung, khop nghieng, LiDAR khong phan doi, pose = trung binh camera) -- sim 10 cho chot dung 1.4-2.9 cm, 0 chot sai; web 3D: ban do uon theo san (FLH1), scan/quy dao theo z, het nen lung, sua ro GPU; CMake: TensorRT tren CMake < 3.17

This commit is contained in:
trung09122002
2026-10-06 10:17:17 +07:00
parent b1ab04c51e
commit f0c5ddb6b8
19 changed files with 1947 additions and 68 deletions

View File

@@ -456,8 +456,16 @@ function v3dSetScan(arr, pose, r) {
const n = arr.length / 2;
const pos = new Float32Array(n * 3);
// Scan nam o do cao cua LiDAR, khong phai o san. Ve o san se lam nguoi xem
// tuong LiDAR thay duoc vat thap -- no KHONG thay.
const z = r.laser_z + r.wheel_radius;
// tuong LiDAR thay duoc vat thap -- no KHONG thay. Cong cao do san duoi
// robot (che do doc), khong thi o tang tren scan ve chui duoi san.
const z0 = pose.z || 0;
const zl = r.laser_z + r.wheel_radius; // goc LiDAR tren san, he footprint
// Web nhan scan THO: tia nam trong mat phang LiDAR, NGHIENG theo than robot.
// Tren doc 15 do ve phang thi diem cach 3 m lech ~0.8 m (chui vao mat doc)
// -> xoay theo roll/pitch quanh footprint, nhu v3dSetPose xoay hinh robot.
// ROS R = Rz(yaw) Ry(pitch) Rx(roll); khong co tilt thi y het cu.
const cr = Math.cos(pose.roll || 0), sr = Math.sin(pose.roll || 0);
const cp = Math.cos(pose.pitch || 0), sp = Math.sin(pose.pitch || 0);
// ⚠️ Scan den trong frame LASER, khong phai frame BASE.
//
// `laser_link` cach `base_link` 0.24 m ve phia truoc (robot.yaml). Dat thang
@@ -469,9 +477,12 @@ function v3dSetScan(arr, pose, r) {
const lx = r.laser_x || 0, ly = r.laser_y || 0;
for (let i = 0; i < n; ++i) {
const x = arr[i * 2] + lx, y = arr[i * 2 + 1] + ly;
pos[i * 3] = pose.x + c * x - s * y;
pos[i * 3 + 1] = z;
pos[i * 3 + 2] = -(pose.y + s * x + c * y);
// Rx(roll) roi Ry(pitch), trong he footprint (truoc khi quay yaw).
const y1 = y * cr - zl * sr, z1 = y * sr + zl * cr;
const x2 = x * cp + z1 * sp, z2 = -x * sp + z1 * cp;
pos[i * 3] = pose.x + c * x2 - s * y1;
pos[i * 3 + 1] = z0 + z2;
pos[i * 3 + 2] = -(pose.y + s * x2 + c * y1);
}
V3D.scanPts.geometry.dispose();
V3D.scanPts.geometry = new THREE.BufferGeometry();
@@ -493,10 +504,11 @@ function v3dSetCloud(arr, pose) {
const c = Math.cos(pose.yaw), s = Math.sin(pose.yaw);
const n = arr.length / 3;
const pos = new Float32Array(n * 3), col = new Float32Array(n * 3);
const z0 = pose.z || 0; // cao do san duoi robot (che do doc)
for (let i = 0; i < n; ++i) {
const x = arr[i * 3], y = arr[i * 3 + 1], z = arr[i * 3 + 2];
pos[i * 3] = pose.x + c * x - s * y;
pos[i * 3 + 1] = z;
pos[i * 3 + 1] = z0 + z;
pos[i * 3 + 2] = -(pose.y + s * x + c * y);
// To mau theo do cao, cung bang mau voi khung 2D.
const t = Math.max(0, Math.min(1, z / 1.2));
@@ -618,12 +630,22 @@ function v3dSetMapCloud(v, visible) {
// San dat o TOA DO THE GIOI chu khong theo robot. Dat theo robot la dung cai
// bay "sàn troi theo robot": robot dung yen giua man hinh con ca the gioi
// truot qua, trong y het loi dinh vi.
//
// ⚠️ Chi dung lai khi BAO the gioi doi. Chay tu `.world`, v3dSetWorld() goi
// ham nay MOI khung hinh (nhanh mesh that thoat som truoc khoa worldKey), va
// ban cu dung lai mat san + GridHelper moi khung ma khong giai phong cai cu.
// Do tren slope_test.world: geometry tren GPU tang ~1 moi khung (20 -> 203
// sau 183 khung), tuc ~3600/phut o 60 fps -- trinh duyet cham dan.
function v3dSetGround(grid) {
if (!V3D.ready || !grid) return;
const key = grid.w + 'x' + grid.h + '@' + grid.res + ':' + grid.ox + ',' + grid.oy;
if (V3D.groundKey === key && V3D.floorMesh) return;
V3D.groundKey = key;
const r = grid.res;
if (V3D.floorMesh) {
V3D.root.remove(V3D.floorMesh);
V3D.floorMesh.geometry.dispose();
V3D.floorMesh.material.dispose();
}
const fw = grid.w * r, fh = grid.h * r;
V3D.floorMesh = new THREE.Mesh(
@@ -640,7 +662,11 @@ function v3dSetGround(grid) {
const span = Math.max(fw, fh);
V3D.scene.fog.near = span * 0.35;
V3D.scene.fog.far = span * 1.4;
if (V3D.gridHelper) V3D.root.remove(V3D.gridHelper);
if (V3D.gridHelper) {
V3D.root.remove(V3D.gridHelper);
V3D.gridHelper.geometry.dispose();
V3D.gridHelper.material.dispose();
}
V3D.gridHelper = new THREE.GridHelper(Math.ceil(span), Math.ceil(span),
0x93a2b0, 0xa6b3c0);
V3D.root.add(V3D.gridHelper);
@@ -844,11 +870,69 @@ function v3dSetWorld(grid, wallHeight) {
v3dSetGround(grid);
}
// Ban do occupancy -> mot mat phang co texture, dat o san.
// --- Do cao SAN cua ban do (che do doc) ---
//
// Luoi tho (0.2 m, mm, -32768 = chua biet) di kem khung ban do SLAM. O chua
// biet (xa moi node > 4 m: tuong LiDAR thay tu xa) lay gia tri o DA BIET gan
// nhat -- de 0 thi mat phang tu tang tren cam xuong z = 0 thanh mot tam xien.
const FLOOR_UNKNOWN = -32768;
// Mat ban do uon dat CAO hon san uoc luong 4 cm. Trong khung gop, mesh san
// THAT cua world (doc, san) la mat duc trung cho voi no: thap hon du chi 2 mm
// la bi che, ngang nhau la nhap nhay rang cua -- chinh la "nen bi lung". Da do
// (sim, map 645 node): sai uoc - that p1 -2.3 cm, p50 +3.3 cm; nang 3 cm con
// 0.3% o chim, 4 cm con 0%. Robot that khong co mesh world: chi noi 4 cm.
const FLOOR_LIFT = 0.04;
function v3dFloorFill(fl) {
const n = fl.w * fl.h;
const out = new Int16Array(fl.mm);
const q = new Int32Array(n);
let head = 0, tail = 0;
for (let i = 0; i < n; ++i) if (out[i] !== FLOOR_UNKNOWN) q[tail++] = i;
if (tail === 0) return null;
while (head < tail) {
const i = q[head++], c = i % fl.w, r = (i - c) / fl.w;
const nb = [c > 0 ? i - 1 : -1, c + 1 < fl.w ? i + 1 : -1,
r > 0 ? i - fl.w : -1, r + 1 < fl.h ? i + fl.w : -1];
for (const j of nb) {
if (j >= 0 && out[j] === FLOOR_UNKNOWN) { out[j] = out[i]; q[tail++] = j; }
}
}
return { ...fl, mm: out };
}
// Do cao san (m) tai (x, y) ROS: noi suy song tuyen giua TAM 4 o quanh diem
// (dinh mat phang nam dung GOC o -- lay o gan nhat thi tren doc 15 do lech
// ~3 cm theo phia lam tron). null neu ngoai luoi / chua biet.
function v3dFloorAt(fl, x, y) {
if (!fl) return null;
const gx = (x - fl.ox) / fl.res - 0.5, gy = (y - fl.oy) / fl.res - 0.5;
const c0 = Math.floor(gx), r0 = Math.floor(gy);
const fx = gx - c0, fy = gy - r0;
let sw = 0, sz = 0;
for (let k = 0; k < 4; ++k) {
const c = c0 + (k & 1), r = r0 + (k >> 1);
if (c < 0 || r < 0 || c >= fl.w || r >= fl.h) continue;
const v = fl.mm[r * fl.w + c];
if (v === FLOOR_UNKNOWN) continue;
const wgt = ((k & 1) ? fx : 1 - fx) * ((k >> 1) ? fy : 1 - fy);
sw += wgt;
sz += wgt * v;
}
return sw > 1e-6 ? (sz / sw) * 0.001 : null;
}
// Ban do occupancy -> mot mat phang co texture, dat o san -- UON theo do cao
// san khi ban do co (che do doc): mat doc va san tang tren o dung cao do,
// khong nam bep o z = 0 duoi tuong voxel.
function v3dSetMap(map) {
if (!V3D.ready || !map || !map.w || !map.h) return;
if (V3D.mapKey === map.version) return;
V3D.mapKey = map.version;
// Khoa theo MANG O, khong theo so phien ban: khung JSON mang phien ban moi
// toi TRUOC khung ban do, nen khoa theo phien ban co the chot luoi CU voi
// phien ban MOI roi khong bao gio ve lai -- ma o che do dinh vi phien ban
// chi doi khi co lien ket moi.
if (V3D.mapData === map.data) return;
V3D.mapData = map.data;
const cv = document.createElement('canvas');
cv.width = map.w; cv.height = map.h;
@@ -880,23 +964,57 @@ function v3dSetMap(map) {
tex.magFilter = THREE.NearestFilter;
tex.minFilter = THREE.LinearMipmapLinearFilter;
if (V3D.mapMesh) { V3D.root.remove(V3D.mapMesh); V3D.mapMesh.geometry.dispose(); }
if (V3D.mapMesh) {
V3D.root.remove(V3D.mapMesh);
V3D.mapMesh.geometry.dispose();
if (V3D.mapMesh.material.map) V3D.mapMesh.material.map.dispose();
V3D.mapMesh.material.dispose();
}
const wM = map.w * map.res, hM = map.h * map.res;
const fl = map.floor ? v3dFloorFill(map.floor) : null;
let geom;
if (fl) {
// Mot dinh moi o luoi san, tran 256 doan moi chieu (ban do 100 m van
// chi ~66k dinh).
const sx = Math.max(1, Math.min(256, Math.round(wM / fl.res)));
const sy = Math.max(1, Math.min(256, Math.round(hM / fl.res)));
geom = new THREE.PlaneGeometry(wM, hM, sx, sy);
// Dinh cuc bo (x, y, z) -> sau rotation.x = -PI/2 la ROS (x, y, z) quanh
// tam mat phang: cuc bo +y = ROS +y, cuc bo +z = LEN.
const p = geom.attributes.position;
for (let i = 0; i < p.count; ++i) {
const z = v3dFloorAt(fl, map.ox + wM / 2 + p.getX(i), map.oy + hM / 2 + p.getY(i));
p.setZ(i, z === null ? 0 : z);
}
p.needsUpdate = true;
} else {
geom = new THREE.PlaneGeometry(wM, hM);
}
V3D.mapMesh = new THREE.Mesh(
new THREE.PlaneGeometry(wM, hM),
geom,
new THREE.MeshBasicMaterial({ map: tex, transparent: true, depthWrite: false }));
V3D.mapMesh.rotation.x = -Math.PI / 2;
// Hoi thap hon san mot chut de khong z-fight voi luoi va vanh cliff.
V3D.mapMesh.position.set(map.ox + wM / 2, -0.002, -(map.oy + hM / 2));
// Phang: hoi thap hon san mot chut de khong z-fight voi luoi va vanh cliff.
// Uon: nang FLOOR_LIFT len tren mesh san that cua world (xem tren).
V3D.mapMesh.position.set(map.ox + wM / 2, fl ? FLOOR_LIFT : -0.002, -(map.oy + hM / 2));
V3D.mapHasFloor = !!fl;
V3D.root.add(V3D.mapMesh);
}
function v3dSetPath(pts) {
// `floor` (tuy chon): luoi do cao san cua ban do -- quy dao di theo mat doc /
// tang tren thay vi chui duoi san.
function v3dSetPath(pts, floor) {
if (!V3D.ready) return;
if (V3D.pathLine) { V3D.root.remove(V3D.pathLine); V3D.pathLine.geometry.dispose(); }
if (!pts || pts.length < 4) return;
// Quy dao luon nam sat node nen khong can lap o chua biet nhu v3dSetMap.
// Ve TREN mat ban do uon (nang FLOOR_LIFT), khong thi lop trong suot phu len
// lam quy dao mo di.
const v = [];
for (let i = 0; i + 1 < pts.length; i += 2) v.push(toScene(pts[i], pts[i + 1], 0.01));
for (let i = 0; i + 1 < pts.length; i += 2) {
const z = v3dFloorAt(floor, pts[i], pts[i + 1]);
v.push(toScene(pts[i], pts[i + 1], z === null ? 0.01 : z + FLOOR_LIFT + 0.01));
}
V3D.pathLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(v),
new THREE.LineBasicMaterial({ color: 0xffb300 }));
@@ -912,6 +1030,10 @@ function v3dSetPath(pts) {
function v3dSetWorldVisible(on) {
if (V3D.meshWorld) V3D.meshWorld.visible = on;
if (V3D.worldMesh) V3D.worldMesh.visible = on;
// Mat san the gioi (z = 0) o lai lam moc khi tat the gioi -- tru khi ban do
// co do cao san: luc do no nam duoi tang tren nhu mot san sai cho (xem nua
// SLAM trong v3dRender).
if (V3D.floorMesh) V3D.floorMesh.visible = on || !V3D.mapHasFloor;
}
// Dat khung nhin ve mot goc chuan.
@@ -989,17 +1111,21 @@ function v3dRender(canvas, follow, splitSlam) {
}
// Tam quay: bam theo robot neu bat, khong thi theo diem pan cua nguoi dung.
let cx = V3D.orbit.tx, cz = V3D.orbit.ty;
// Bam ca CAO DO (che do doc): o tang tren 1.5 m ma tam quay van o san duoi
// thi camera xoay quanh mot diem chui duoi chan robot. Pose 2D (khong z)
// thi robotGroup o y = 0, y het cu.
let cx = V3D.orbit.tx, cz = V3D.orbit.ty, cy = 0;
if (follow && V3D.robotGroup) {
cx = V3D.robotGroup.position.x + V3D.orbit.tx;
cz = V3D.robotGroup.position.z + V3D.orbit.ty;
cy = V3D.robotGroup.position.y;
}
const o = V3D.orbit;
V3D.camera.position.set(
cx + o.dist * Math.cos(o.el) * Math.cos(o.az),
o.dist * Math.sin(o.el),
cy + o.dist * Math.sin(o.el),
cz + o.dist * Math.cos(o.el) * Math.sin(o.az));
V3D.camera.lookAt(cx, 0.2, cz);
V3D.camera.lookAt(cx, cy + 0.2, cz);
const r = V3D.renderer;
if (!splitSlam) {
@@ -1038,7 +1164,10 @@ function v3dRender(canvas, follow, splitSlam) {
show(hid);
// --- Nua PHAI: chi SLAM, khong co the gioi ---
hid = hide([V3D.meshWorld, V3D.worldMesh]);
// Ban do co do cao san (che do doc) thi an ca mat san THE GIOI (z = 0, tu
// v3dSetGround): duoi tang tren 1.5 m no hien qua lop ban do trong suot
// nhu mot "san" sai cho -- SLAM da tu mang mat san cua no.
hid = hide([V3D.meshWorld, V3D.worldMesh, V3D.mapHasFloor ? V3D.floorMesh : null]);
r.setViewport(half, 0, w - half, h);
r.setScissor(half, 0, w - half, h);
r.render(V3D.scene, V3D.camera);