Files
linuxcncWebWASM/docs/user-manual/local-machine-manager-demo.mjs
wangdequan db29ae625f 按关节槽位驱动 13 个五轴案例的三维模型
关节契约扩为槽位制:以 LinuxCNC 联合号标识关节,轴字母降为槽位属性,
重复轴(bridge-mill 的两个 Y)、并联杆长(hexapod/pentapod)和被动槽位
(max5 第 6 关节)都能表达。v1 规则保持不变并合成 jointOrder,已保存
GLB 与既有测试不受影响。

13 个结构 GLB 由 scripts/generate_physical_case_glbs.mjs 统一重建,
可动关节均为按槽位命名的 Group 并写 userData.linuxcncJoint,共 71 个
关节,槽位数与运行目录的 joint_count 逐条一致。

新增 machine-visual-bindings.json 与纯函数模块,以 machine id 为键:
坐标字母不能作键,因为三个案例共用 XYZAB、三个共用 XYZABC。主界面切换
案例时自动装载对应模型,失败时保留内置模型并显式提示。

消除前端五关节截断:visual 适配器的 joints 改为数组并新增
jointsAuthoritative;allHomed 按 joint_count 判断;面板按 worker 断言的
machine id 查找机型。

管理器解锁:坐标与案例下拉以"存在完整视觉绑定"为启用条件,13 案例全部
可选;machine-urdf.ts 新增 generic-wasm/1 profile,AC/BC 严格校验路径
不变;新增 machine-kinematics-specs.json 保存逐关节限位,INI 未声明的
限位保持 null 而不编造。

修复三个真实缺陷:构建漏发 woodpecker/gmoccapy/qtdragon 配置目录(
DEFECT-02 根因);案例切换沿用"装载需要已上电"的门控,导致关机状态下
第二次切换被静默拒绝;重复轴机型上 React 列表键冲突。

验收:13 案例逐个切换装载成功、槽位键与绑定定义逐一一致、浏览器错误 0;
typecheck 与生产构建通过;视觉绑定、GLB 命名、关节契约、投影、store、
双画布、运动学规格漂移、11 案例 Worker 闭环、管理器浏览器回归全部通过。
证据在 coverage/glb-machine-manager/physical-drive/。

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-30 06:09:12 +08:00

183 lines
9.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 可见演示:打开"三维机床管理"对话框,导入机床模型并测试。
// 流程:打开网站 → 打开三维机床管理 → 查看案例库 → 导入 AC 示例机床 → 查看层级与轴绑定
// → 运动验证(手动输入各轴)→ 查看标定参数 → 应用到 Web LinuxCNC → 回到主页上电/回零/Jog 验证模型跟随。
//
// 运行:
// DISPLAY=:1 PLAYWRIGHT_MODULE=/path/to/playwright/index.mjs \
// node docs/user-manual/local-machine-manager-demo.mjs
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, "../..");
const baseUrl = process.env.DEMO_URL ?? "http://127.0.0.1:5174/";
const chromePath = process.env.CHROME_PATH ?? "/usr/bin/google-chrome";
const pace = Number(process.env.DEMO_PACE_MS ?? 2500);
// 默认用带 JT_X/JT_Y/JT_Z/JT_A/JT_C 语义节点名的真实机床模型,分析器才能自动识别五轴。
const glbPath = process.env.DEMO_GLB ?? resolve(repoRoot, "glb/m1.glb");
const shotDir = resolve(here, "local-walkthrough");
const { chromium } = await import(process.env.PLAYWRIGHT_MODULE);
const browser = await chromium.launch({
executablePath: chromePath,
headless: false,
args: ["--no-sandbox", "--start-maximized", "--disable-infobars", "--enable-unsafe-swiftshader"],
});
const page = await browser.newPage({ viewport: null });
await page.bringToFront().catch(() => {});
const wait = (ms) => page.waitForTimeout(ms);
const text = async (selector) => (await page.locator(selector).first().innerText()).replace(/\s+/g, " ").trim();
const status = async () => text(".machine-status");
const axes = async () => page.locator(".xyzac-position-grid output").allInnerTexts();
const shot = (name) => page.screenshot({ path: resolve(shotDir, name) });
const say = (step, message) => console.log(`\n[${step}] ${message}`);
const waitFor = async (check, timeout, label) => {
const deadline = Date.now() + timeout;
let last = "";
while (Date.now() < deadline) {
last = await check();
if (last) return last;
await wait(400);
}
throw new Error(`${label} 超时(最后:${last || "无"})`);
};
const waitStatus = (pattern, timeout = 60000) => waitFor(async () => {
const value = await status();
return pattern.test(value) ? value : null;
}, timeout, `等待 ${pattern}`);
const dialog = () => page.locator("dialog[open]").first();
say(1, `打开网站 ${baseUrl}`);
await page.goto(baseUrl, { waitUntil: "load", timeout: 60000 });
await page.waitForSelector('[data-axis-control="workspace-case-select"]', { timeout: 60000 });
await waitFor(async () => ((await text(".case-status")).startsWith("Program:") ? "ok" : null), 90000, "案例就绪");
await wait(2000);
console.log(` 连接:${await text(".connection-state")}|机型:${await text(".xyzac-section-title")}|状态:${await status()}`);
await shot("mgr-1-home.png");
say(2, "打开“三维机床管理”对话框(此时还没有任何机床模型)");
await page.locator('[data-axis-control="machine-manager-settings"]').click();
await dialog().waitFor({ state: "visible", timeout: 10000 });
await wait(2500);
console.log(` 草稿状态:${await text('[data-axis-control="machine-manager-draft-state"]')}|${await text('[data-axis-control="machine-manager-validation"]')}`);
await shot("mgr-2-dialog-empty.png");
await wait(pace);
say(3, "查看“文件与恢复”里的 LinuxCNC 五轴案例库");
await page.locator('[data-axis-control="machine-manager-tab-files"]').click();
await wait(2000);
const caseCount = await page.locator("[data-machine-case]").count();
const cases = await page.locator("[data-machine-case] strong").allInnerTexts();
console.log(` 案例库 ${caseCount} 项:${cases.slice(0, 4).join("、")} …`);
console.log(` 案例卡片示例:${(await text("[data-machine-case]")).slice(0, 120)}`);
await shot("mgr-3-case-library.png");
await wait(pace);
say(4, `回到“部件与层级”,导入机床模型 ${glbPath.split("/").pop()}`);
await page.locator('[data-axis-control="machine-manager-tab-structure"]').click();
await wait(1200);
const [chooser] = await Promise.all([
page.waitForEvent("filechooser"),
page.locator('[data-axis-control="machine-manager-import"]').click(),
]);
await chooser.setFiles(glbPath);
await waitFor(async () => ((await page.locator("[data-axis-control=machine-model-list]").count()) ? "ok" : null), 60000, "模型导入");
await waitFor(async () => (/[1-9]\d*\/\d+ 轴已绑定/.test(await text(".machine-manager-toolbar")) ? "ok" : null), 120000, "关节自动绑定").catch(() => {});
await wait(4000);
console.log(` 模型列表:${await text("[data-axis-control=machine-model-list]")}`);
console.log(` 工具栏:${await text(".machine-manager-toolbar")}`);
await shot("mgr-4-imported-structure.png");
await wait(pace);
say(5, "查看 GLB 五轴分析结果与层级树");
const analysis = await page.locator('[data-axis-control="glb-five-axis-analysis"]').innerText().catch(() => "(无分析卡片)");
console.log(` 分析:${analysis.replace(/\s+/g, " ").slice(0, 260)}`);
const treeNodes = await page.locator('[data-axis-control="machine-tree-node"]').count();
console.log(` 层级树节点数:${treeNodes}`);
await shot("mgr-5-analysis-tree.png");
await wait(pace);
say(6, "运动验证:手动驱动模型各轴(不发运动指令给控制器)");
await page.locator('[data-axis-control="machine-manager-tab-motion"]').click();
await wait(1500);
const poseJson = async () => {
const details = page.locator("#machine-panel-motion details").first();
await details.evaluate((element) => { element.open = true; }).catch(() => {});
await wait(300);
return text('[data-axis-control="machine-test-state"]');
};
const before = await text('[data-axis-control="machine-test-state"]');
const beforePose = await poseJson();
const inputs = { x: "25", y: "15", z: "-8", a: "30", c: "45" };
const firstField = page.locator('[data-axis-control="machine-test-x"]');
if (await firstField.isDisabled()) {
console.log(" ⚠ 运动验证输入仍为禁用:该模型没有自动识别出关节,需要手动绑定自由度后才能测试");
} else {
for (const [axis, value] of Object.entries(inputs)) {
const field = page.locator(`[data-axis-control="machine-test-${axis}"]`);
if (await field.count()) { await field.fill(value); await field.blur().catch(() => {}); await wait(600); }
}
}
await wait(2000);
const afterPose = await poseJson();
const readBack = {};
for (const axis of Object.keys(inputs)) readBack[axis.toUpperCase()] = await page.locator(`[data-axis-control="machine-test-${axis}"]`).inputValue().catch(() => "-");
console.log(` 输入前模型姿态:${beforePose || before || "(空)"}`);
console.log(` 输入后模型姿态:${afterPose}`);
console.log(` 各轴输入回读:${JSON.stringify(readBack)}`);
console.log(` 姿态已改变:${beforePose !== afterPose && afterPose ? "是" : "否"}`);
await shot("mgr-6-motion-test.png");
await wait(pace);
say(7, "复位运动验证输入,并查看机床标定参数");
await page.locator('[data-axis-control="machine-test-reset"]').click();
await wait(1500);
await page.locator('[data-axis-control="machine-manager-tab-calibration"]').click();
await wait(1500);
const calibration = await text("#machine-panel-calibration");
console.log(` 标定页:${calibration.slice(0, 220)}`);
await shot("mgr-7-calibration.png");
await wait(pace);
say(8, "把配置好的机床应用到 Web LinuxCNC");
const applyButton = page.locator('[data-axis-control="machine-manager-apply"]');
console.log(` 应用按钮:${(await applyButton.isDisabled()) ? "禁用" : "可用"}|检查:${await text('[data-axis-control="machine-manager-validation"]')}`);
if (!(await applyButton.isDisabled())) {
await applyButton.click();
await wait(6000);
console.log(` 应用反馈:${await text(".machine-manager-footer")}`);
}
await shot("mgr-8-applied.png");
await wait(pace);
say(9, "关闭对话框,回到主页确认机床模型已经是导入的那一台");
await page.locator('dialog[open] header button[aria-label^="关闭"]').click();
await wait(3000);
console.log(` 机型:${await text(".xyzac-section-title")}|程序:${await text(".case-status")}`);
await shot("mgr-9-home-after-apply.png");
await wait(pace);
say(10, "上电 → HOME ALL → Jog X,验证导入的机床模型跟随运动");
const estop = page.locator('[data-axis-control="toolbar-estop"]');
if ((await estop.getAttribute("aria-label"))?.includes("Cancel")) { await estop.click(); await wait(1500); }
await page.locator('[data-axis-control="toolbar-power"]').click();
await waitStatus(/STATE_ON|STATE_IDLE/, 40000);
console.log(` 上电:${(await status()).match(/Task state (\S+)/)?.[1]}`);
await page.locator('[data-axis-control="manual-home-all"]').click();
await waitStatus(/Machine sequence HOMED/, 60000);
console.log(` 回零:${(await status()).match(/Machine sequence (NOT HOMED|HOMED)/)?.[1]}|轴位 ${(await axes()).join(" / ")}`);
await page.locator('[data-axis-control="manual-axis-x"]').check();
await page.locator('[data-axis-control="manual-jog-positive"]').hover();
await page.mouse.down();
await wait(2000);
await page.mouse.up();
await wait(1500);
console.log(` Jog 后轴位:${(await axes()).join(" / ")}`);
await shot("mgr-10-machine-moving.png");
console.log("\n机床测试演示结束,窗口保持 20 秒后关闭。");
await wait(20000);
await browser.close();