Files

92 lines
3.5 KiB
JavaScript

/**
* 验证 A7 装配系 → URDF L 链的对齐。
* 用 7 对连杆↔Link 对应点,候选旋转 R_x(90°),解平移 t,打印残差。
*/
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import * as THREE from 'three';
import { normalizeKeys, buildPlans, collectPlacedModels } from './lib/compile-assembly.mjs';
import { parseUrdf, computeRosWorldMatrices } from './lib/urdf.mjs';
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, '..');
const cfg = normalizeKeys(JSON.parse(readFileSync(resolve(ROOT, 'src/data/A7/assembly.json'), 'utf-8')));
const { products, finalOutput } = buildPlans(cfg);
const placed = collectPlacedModels(products.get(finalOutput), { position: [0, 0, 0], rotation: [0, 0, 0] });
const urdf = readFileSync(resolve(ROOT, 'src/assets/A7/urdf/urdf/lkls73_i1_dual_arm_description.urdf'), 'utf-8');
const { linkMap } = parseUrdf(urdf);
const worldMats = computeRosWorldMatrices(linkMap);
function linkPos(name) {
const m = worldMats.get(name);
const p = new THREE.Vector3();
m.decompose(p, new THREE.Quaternion(), new THREE.Vector3());
return p;
}
// 取装配里某零件名第一实例的位置
const byName = new Map();
for (const p of placed) if (!byName.has(p.name)) byName.set(p.name, p.position);
function asmPos(name) {
const v = byName.get(name);
return v ? new THREE.Vector3(...v) : null;
}
// 对应点:连杆N → LN
const pairs = [
['机械臂连杆1A', 'L1_Link'],
['机械臂连杆2A', 'L2_Link'],
['机械臂连杆3A', 'L3_Link'],
['机械臂连杆4A', 'L4_Link'],
['机械臂连杆5A', 'L5_Link'],
['机械臂连杆6A', 'L6_Link'],
['机械臂连杆7A', 'L7_Link'],
['机械臂末端连杆A', 'L8_Link'],
];
// 候选旋转 R_x(+90°): (x,y,z)->(x,-z,y)
const R = new THREE.Matrix4().makeRotationX(Math.PI / 2);
const A = [], B = [];
for (const [an, ln] of pairs) {
const a = asmPos(an);
if (!a) { console.log(`缺装配件 ${an}`); continue; }
A.push(a.clone().applyMatrix4(R));
B.push(linkPos(ln));
}
// t = mean(B) - mean(RA)
const meanA = A.reduce((s, v) => s.add(v), new THREE.Vector3()).multiplyScalar(1 / A.length);
const meanB = B.reduce((s, v) => s.add(v), new THREE.Vector3()).multiplyScalar(1 / B.length);
const t = meanB.clone().sub(meanA);
console.log('R = R_x(+90°), t =', t.toArray().map((v) => v.toFixed(4)).join(', '));
console.log('\n=== 残差(R·asm + t 对比 urdf link)===');
let maxErr = 0;
for (let i = 0; i < A.length; i++) {
const pred = A[i].clone().add(t);
const err = pred.clone().sub(B[i]).length();
maxErr = Math.max(maxErr, err);
console.log(
`${pairs[i][1].padEnd(8)} urdf=[${B[i].toArray().map((v)=>v.toFixed(3)).join(', ')}] ` +
`pred=[${pred.toArray().map((v)=>v.toFixed(3)).join(', ')}] err=${(err*1000).toFixed(1)}mm`,
);
}
console.log(`\n最大残差 ${(maxErr*1000).toFixed(1)}mm`);
// 也试 R_x(-90°): (x,y,z)->(x,z,-y)
console.log('\n--- 对比 R_x(-90°) ---');
const R2 = new THREE.Matrix4().makeRotationX(-Math.PI / 2);
const A2 = pairs.map(([an]) => asmPos(an)?.clone().applyMatrix4(R2)).filter(Boolean);
const meanA2 = A2.reduce((s, v) => s.add(v), new THREE.Vector3()).multiplyScalar(1 / A2.length);
const t2 = meanB.clone().sub(meanA2);
let maxErr2 = 0;
for (let i = 0; i < A2.length; i++) {
const pred = A2[i].clone().add(t2);
maxErr2 = Math.max(maxErr2, pred.clone().sub(B[i]).length());
}
console.log(`R_x(-90°) t=[${t2.toArray().map((v)=>v.toFixed(4)).join(', ')}] 最大残差 ${(maxErr2*1000).toFixed(1)}mm`);