126 lines
5.2 KiB
JavaScript
126 lines
5.2 KiB
JavaScript
// Scan co duoc dat DUNG CHO trong khong gian khong?
|
|
//
|
|
// ================== VI SAO CAN BAI KIEM NAY ==================
|
|
// Scan di qua day tren dinh dang POLAR trong frame LASER. Trinh duyet doi sang
|
|
// Descartes roi ap pose cua BASE. Bo qua khoang gan LiDAR (0.24 m ve truoc)
|
|
// lam ca vanh quet lui 24 cm -- cac tia khong con nam tren tuong.
|
|
//
|
|
// Loi nay KHONG lam gi hong theo nghia van hanh: dung so tia, dung nhip, dung
|
|
// tam nhin, khong mot ngoai le nao. No chi khien nguoi van hanh thay robot
|
|
// "troi" va di tim loi trong odometry/SLAM -- cho khong co loi.
|
|
//
|
|
// Nen bai kiem do THU DUNG: dung phep bien doi CUA CHINH app3d.js tren mot
|
|
// canh dung san, roi kiem diem quet co roi trung tuong khong.
|
|
//
|
|
// node test_scan_frame.js
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const vm = require('vm');
|
|
|
|
// --- THREE gia ---
|
|
class V3 { constructor(x, y, z) { this.x = x; this.y = y; this.z = z; } }
|
|
class BufAttr { constructor(a, n) { this.array = a; this.itemSize = n; } }
|
|
class BufGeom {
|
|
constructor() { this.attributes = {}; }
|
|
setAttribute(n, a) { this.attributes[n] = a; }
|
|
computeVertexNormals() {}
|
|
dispose() {}
|
|
}
|
|
class Grp {
|
|
constructor() { this.children = []; }
|
|
add(c) { this.children.push(c); }
|
|
remove(c) { this.children = this.children.filter((x) => x !== c); }
|
|
traverse(f) { this.children.forEach(f); }
|
|
}
|
|
global.THREE = {
|
|
Vector3: V3, Color: class { constructor(r, g, b) { Object.assign(this, { r, g, b }); } },
|
|
Quaternion: class { setFromAxisAngle() {} }, Matrix4: class { compose() {} },
|
|
BufferAttribute: BufAttr, BufferGeometry: BufGeom, Group: Grp,
|
|
Points: class { constructor(g, m) { this.geometry = g; this.material = m; } },
|
|
PointsMaterial: class { constructor(o) { Object.assign(this, o); } },
|
|
MeshStandardMaterial: class { constructor(o) { Object.assign(this, o); } dispose() {} },
|
|
InstancedMesh: class { constructor(g, m, n) { this.geometry = g; this.count = n;
|
|
this.userData = {}; this.instanceMatrix = {}; } setMatrixAt() {} dispose() {} },
|
|
Mesh: class { constructor(g, m) { this.geometry = g; this.material = m;
|
|
this.position = { set: () => {} }; this.rotation = { x: 0 }; } },
|
|
PlaneGeometry: class { dispose() {} }, GridHelper: class {},
|
|
BoxGeometry: class { dispose() {} },
|
|
};
|
|
|
|
const wwwDir = path.resolve(__dirname, '../../libs/vslam_web/www');
|
|
const sandbox = { THREE: global.THREE, console, window: {}, document: {} };
|
|
vm.createContext(sandbox);
|
|
vm.runInContext(
|
|
fs.readFileSync(path.join(wwwDir, 'app3d.js'), 'utf8') +
|
|
'\n;this.__V3D = V3D; this.__setScan = v3dSetScan;',
|
|
sandbox);
|
|
const V3D = sandbox.__V3D;
|
|
V3D.ready = true;
|
|
V3D.root = new Grp();
|
|
|
|
// --- Canh dung san: hanh lang rong 4 m, robot o giua, quay 0 rad ---
|
|
//
|
|
// Tuong o y = +2 va y = -2, keo dai theo x. LiDAR o 0.24 m truoc base.
|
|
const LASER_X = 0.24, WALL = 2.0;
|
|
const robot = { laser_x: LASER_X, laser_y: 0, laser_z: 0.125, wheel_radius: 0.075 };
|
|
const pose = { x: 5.0, y: 1.0, yaw: 0.0 };
|
|
|
|
// Tia trong frame LASER, moi tia cham dung mot buc tuong.
|
|
// LiDAR o world y = pose.y = 1.0 (vi laser_y = 0 va yaw = 0).
|
|
// Tuong tren cach 1.0 m, tuong duoi cach 3.0 m.
|
|
const rays = [];
|
|
for (const [ang, dist] of [[Math.PI / 2, 1.0], [-Math.PI / 2, 3.0]]) {
|
|
rays.push(dist * Math.cos(ang), dist * Math.sin(ang));
|
|
}
|
|
// Them mot tia cheo 45 do len tuong tren: cham o khoang cach 1.0/sin(45).
|
|
rays.push(Math.SQRT2 * Math.cos(Math.PI / 4), Math.SQRT2 * Math.sin(Math.PI / 4));
|
|
|
|
sandbox.__setScan(new Float32Array(rays), pose, robot);
|
|
const pos = V3D.scanPts.geometry.attributes.position.array;
|
|
|
|
let bad = 0;
|
|
const rows = [];
|
|
for (let i = 0; i < pos.length / 3; ++i) {
|
|
const wx = pos[i * 3];
|
|
const wy = -pos[i * 3 + 2]; // three z = -ROS y
|
|
const wz = pos[i * 3 + 1];
|
|
// Moi diem phai nam tren mot trong hai buc tuong.
|
|
const onWall = Math.abs(Math.abs(wy - pose.y) - 0) < 1e6; // tinh o duoi
|
|
const dTop = Math.abs(wy - (pose.y + 1.0));
|
|
const dBot = Math.abs(wy - (pose.y - 3.0));
|
|
const hit = Math.min(dTop, dBot) < 1e-3;
|
|
if (!hit) bad++;
|
|
rows.push(` tia ${i}: world (${wx.toFixed(3)}, ${wy.toFixed(3)}, ${wz.toFixed(3)})` +
|
|
` ${hit ? 'tren tuong' : 'TRUOT ' + Math.min(dTop, dBot).toFixed(3) + ' m'}`);
|
|
}
|
|
rows.forEach((r) => console.log(r));
|
|
|
|
// ⚠️ Phep kiem THAT: diem quet phai dich len truoc DUNG bang laser_x.
|
|
//
|
|
// Tia thang ben (+-90 do) khong doi y khi bu laser_x, nen chung mot minh
|
|
// KHONG phan biet duoc co bu hay khong. Tia CHEO thi co.
|
|
const cheo = { x: pos[6], y: -pos[8] };
|
|
const mongDoi = {
|
|
x: pose.x + LASER_X + 1.0, // 0.24 tu laser_x + 1.0 tu tia cheo
|
|
y: pose.y + 1.0,
|
|
};
|
|
console.log(`\n tia cheo: duoc (${cheo.x.toFixed(3)}, ${cheo.y.toFixed(3)})`);
|
|
console.log(` mong (${mongDoi.x.toFixed(3)}, ${mongDoi.y.toFixed(3)})`);
|
|
const dx = Math.abs(cheo.x - mongDoi.x);
|
|
if (dx > 1e-3) {
|
|
bad++;
|
|
console.log(` LECH ${dx.toFixed(3)} m theo x` +
|
|
(Math.abs(dx - LASER_X) < 1e-3 ? ' == dung bang laser_x!' : ''));
|
|
}
|
|
|
|
// Do cao: scan phai o do cao LiDAR, khong phai o san.
|
|
const zMongDoi = robot.laser_z + robot.wheel_radius;
|
|
if (Math.abs(pos[1] - zMongDoi) > 1e-6) {
|
|
bad++;
|
|
console.log(` do cao scan ${pos[1]} != ${zMongDoi}`);
|
|
}
|
|
|
|
console.log(bad ? '\n => HONG' : '\n => DAT');
|
|
process.exit(bad ? 1 : 0);
|