Complete XYZAC LinuxCNC WASM parity and smooth 3D preview
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { ReplayTimeline, fnv1a64 } from "./replay.mjs";
|
||||
import { capabilities, validateLimit } from "./capabilities.mjs";
|
||||
import { mapCoreError } from "./error-map.mjs";
|
||||
import { createCheckpoint, recoverSimulation } from "../workspace/recovery.mjs";
|
||||
import { createCheckpoint, recoverSimulation, validateCheckpoint } from "../workspace/recovery.mjs";
|
||||
import { preflightConfiguration } from "../workspace/configuration.mjs";
|
||||
import { ConfigurationStartupStateMachine } from "../workspace/configuration.mjs";
|
||||
import { preflightHalConfiguration } from "../workspace/hal-configuration.mjs";
|
||||
@@ -23,7 +23,11 @@ import { AuthoritativeRuntime } from "./authoritative-runtime.mjs";
|
||||
import { axisState } from "./axis-state.mjs";
|
||||
import { WasmMotionQueue } from "./wasm-motion-queue.mjs";
|
||||
import { WasmHomeLimit } from "./wasm-home-limit.mjs";
|
||||
import { axisGate } from "../app/axis-contract.mjs";
|
||||
import { WasmXyzacKinematics, XYZAC_MODES } from "./wasm-xyzac-kinematics.mjs";
|
||||
import { WasmXyzacIo } from "./wasm-xyzac-io.mjs";
|
||||
import { WasmXyzacDeviceIo } from "./wasm-xyzac-device-io.mjs";
|
||||
import { WasmProgramRuntime } from "./wasm-program-runtime.mjs";
|
||||
import { axisGate, xyzacGate, xyzacGateMatrix } from "../app/axis-contract.mjs";
|
||||
|
||||
const PROTOCOL_SCHEMA = 1;
|
||||
const CONTEXT_ID = "linuxcnc-2.10.0-pre0-wasm-m1-py-c1";
|
||||
@@ -41,6 +45,7 @@ const OPERATION_KINDS = new Set([
|
||||
"PYTHON_IMPORT", "PYTHON_CALL", "PYTHON_CAPABILITY",
|
||||
"PROGRAM_STEP", "PROGRAM_SEEK", "MDI_EXECUTE", "PLC_CONFIG", "PLC_SET", "PLC_TRIGGER", "FOLLOWING_ERROR", "IO_COMMAND", "IO_ACK", "IO_TIMEOUT",
|
||||
"REMAP_REGISTER", "REMAP_CALL", "VCP_REGISTER", "VCP_UPDATE", "FILTER_EXEC",
|
||||
"POWER", "KINEMATICS_SELECT", "VISUAL_CLEAR",
|
||||
]);
|
||||
|
||||
const PANEL_GATE_ACTIONS = Object.freeze({
|
||||
@@ -48,6 +53,7 @@ const PANEL_GATE_ACTIONS = Object.freeze({
|
||||
PROGRAM_RESUME: "resume", PROGRAM_ABORT: "abort", PROGRAM_RESTART: "restart",
|
||||
JOG_START: "jogStart", JOG_STOP: "jogStop", HOME: "home", SET_OVERRIDE: "override",
|
||||
ESTOP: "estop", IO_RECOVER: "recover",
|
||||
POWER: "power",
|
||||
});
|
||||
|
||||
function text(value) {
|
||||
@@ -102,21 +108,33 @@ export class SimSession {
|
||||
this.authoritative = new AuthoritativeRuntime();
|
||||
this.wasmMotion = new WasmMotionQueue(core);
|
||||
this.wasmHomeLimit = new WasmHomeLimit(core);
|
||||
this.xyzacKinematics = null;
|
||||
this.xyzacIo = null;
|
||||
this.xyzacDeviceIo = null;
|
||||
this.xyzacProgram = null;
|
||||
this.xyzacProgramKinematicsMode = XYZAC_MODES.identity;
|
||||
this.xyzacRapidVelocity = 35;
|
||||
this.pendingProgramControl = 0;
|
||||
this.motionAuthority = "js";
|
||||
this.task = new SimTask(core, { contextId, sessionId: sessionId || null });
|
||||
this.scheduler = new SimScheduler(core);
|
||||
this.configurationState = null;
|
||||
this.halState = null;
|
||||
this.previewState = null;
|
||||
this.workspaceFiles = {};
|
||||
call(core, "sim_protocol_reset");
|
||||
if (typeof core.ccall === "function") core.ccall("sim_event_reset", null, [], []);
|
||||
}
|
||||
|
||||
envelope(kind, input, payload, tick = this.tick) {
|
||||
const statePayload = payload && typeof payload === "object" ? payload : {};
|
||||
const program = statePayload.program ?? (this.program.loaded ? this.program.snapshot() : {});
|
||||
const program = statePayload.program ?? (this.xyzacProgram ? this.xyzacProgram.snapshot() : this.program.loaded ? this.program.snapshot() : {});
|
||||
const task = statePayload.task ?? this.task.export();
|
||||
const panelSnapshot = this.panelSnapshot({ task, program, input, tick });
|
||||
// The loaded toolpath is immutable during execution. Avoid cloning all
|
||||
// WASM pose samples into every 10 ms step response; the frontend retains
|
||||
// the canonical path from PROGRAM_LOAD/PROGRAM_RESTART.
|
||||
const lightweightXyzacStep = input.kind === "SIM_STEP" && Boolean(this.xyzacProgram);
|
||||
const panelSnapshot = this.panelSnapshot({ task, program, input, tick, motionTrace: statePayload.motion_trace, includeToolpath: !lightweightXyzacStep, includeConfiguration: !lightweightXyzacStep, includeStableRuntime: !lightweightXyzacStep });
|
||||
return {
|
||||
schema_version: PROTOCOL_SCHEMA,
|
||||
kind,
|
||||
@@ -130,39 +148,114 @@ export class SimSession {
|
||||
panel_snapshot: panelSnapshot,
|
||||
motion_authority: statePayload.motion_authority ?? this.motionAuthority,
|
||||
task_authority: statePayload.task_authority ?? this.task.authority(),
|
||||
wasm_home_limit: statePayload.wasm_home_limit ?? this.wasmHomeLimit.read(),
|
||||
...(!lightweightXyzacStep ? { wasm_home_limit: statePayload.wasm_home_limit ?? this.wasmHomeLimit.read() } : {}),
|
||||
axis_state: axisState({ task, program, action: input.kind, payload: input.payload ?? {} }),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
panelSnapshot({ task = this.task.export(), program = this.program.loaded ? this.program.snapshot() : null, input = {}, tick = this.tick } = {}) {
|
||||
const motion = this.motion.export();
|
||||
wasmMotionProjection() {
|
||||
const wasmMotion = this.wasmMotion.read();
|
||||
const position = wasmMotion.status === "verified" ? wasmMotion.state.position : { x: 0, y: 0, z: 0, a: 0, b: 0, c: 0, u: 0, v: 0, w: 0 };
|
||||
const home = this.wasmHomeLimit.read();
|
||||
const io = this.xyzacIo?.canonical?.();
|
||||
const ioState = io && typeof io === "object" && io.status !== "blocked" ? io : null;
|
||||
const kinematics = this.xyzacKinematics?.snapshot?.();
|
||||
const kinematicsJoints = kinematics?.joints?.length === 5 ? [...kinematics.joints] : null;
|
||||
const unsigned = {
|
||||
schema: "sim_motion.v1",
|
||||
source: "wasm_motion_queue",
|
||||
tick: wasmMotion.status === "verified" ? wasmMotion.state.tick : this.tick,
|
||||
axes: { x: position.x, y: position.y, z: position.z },
|
||||
joints: kinematicsJoints ?? (home.status === "verified" && Array.isArray(home.state.joints) ? [...home.state.joints] : [position.x, position.y, position.z, position.a ?? 0, position.c ?? 0]),
|
||||
velocity: wasmMotion.status === "verified" ? wasmMotion.state.velocity : 0,
|
||||
feed_override: 1,
|
||||
spindle_override: 1,
|
||||
homed: Boolean(home.state?.homed),
|
||||
homed_axes: Array.from({ length: 5 }, (_, index) => Boolean(home.state?.homed && index < (home.state?.joint_count ?? 0))),
|
||||
limits: { x: Boolean(home.state?.limit_error), y: Boolean(home.state?.limit_error), z: Boolean(home.state?.limit_error), a: Boolean(home.state?.limit_error), c: Boolean(home.state?.limit_error) },
|
||||
on_any_limit: Boolean(home.state?.limit_error),
|
||||
following_error: 0,
|
||||
jog: null,
|
||||
estop: Boolean(ioState?.estop),
|
||||
fault: ioState?.fault ? (ioState.fault_reason ?? "io_fault") : null,
|
||||
};
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
motionProjection() {
|
||||
if (this.xyzacProgram) return this.wasmMotionProjection();
|
||||
if (this.motionAuthority !== "wasm") return this.motion.export();
|
||||
const wasm = this.wasmMotion.read();
|
||||
return wasm.status === "verified" ? { ...this.motion.export(), axes: { ...wasm.state.position } } : this.motion.export();
|
||||
}
|
||||
|
||||
wasmDeviceProjection({ includeBytes = false } = {}) {
|
||||
if (!this.xyzacIo) return this.devices.export();
|
||||
const state = this.xyzacIo.canonical();
|
||||
if (!state || state.status === "blocked") return { schema: "wasm_xyzac_devices.v1", source: "wasm_io", status: "blocked" };
|
||||
const ioExport = includeBytes ? this.xyzacIo.export() : null;
|
||||
const unsigned = { schema: "wasm_xyzac_devices.v1", source: "wasm_io", ...state, ...(includeBytes ? { state_bytes: ioExport.state_bytes } : {}) };
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
loadXyzacSubroutines(programPath) {
|
||||
if (!this.xyzacProgram) return { status: "verified", count: 0 };
|
||||
const requirement = this.xyzacProgram.requiresSubroutines();
|
||||
if (requirement.status !== "verified") return requirement;
|
||||
if (!requirement.required) return { status: "verified", count: 0 };
|
||||
const basePath = String(programPath ?? "").split("/").slice(0, -1).join("/");
|
||||
const rows = [["remap_subs/xyzac_switchkins_sub.ngc", "xyzac_switchkins_sub"], ["remap_subs/helix_ac.ngc", "helix_ac"]];
|
||||
for (const [suffix, name] of rows) {
|
||||
const path = `${basePath.replace(/\/demos$/, "")}/${suffix}`;
|
||||
const source = this.workspaceFiles[path];
|
||||
if (typeof source !== "string") return { status: "error", code: "program_subroutine_missing", path };
|
||||
const added = this.xyzacProgram.addSubroutine(name, source);
|
||||
if (added.status !== "verified") return added;
|
||||
}
|
||||
return { status: "verified", count: rows.length };
|
||||
}
|
||||
|
||||
panelSnapshot({ task = this.task.export(), program = this.xyzacProgram ? this.xyzacProgram.snapshot() : this.program.loaded ? this.program.snapshot() : null, input = {}, tick = this.tick, motionTrace = null, includeToolpath = input.kind !== "SIM_STEP" || !this.xyzacProgram, includeConfiguration = input.kind !== "SIM_STEP" || !this.xyzacProgram, includeStableRuntime = input.kind !== "SIM_STEP" || !this.xyzacProgram } = {}) {
|
||||
const motion = this.xyzacProgram ? this.wasmMotionProjection() : this.motion.export();
|
||||
const wasmMotion = this.wasmMotion.read();
|
||||
const homeLimit = this.wasmHomeLimit.read();
|
||||
const devices = this.devices.export();
|
||||
const deviceIo = this.deviceIo.export();
|
||||
const halRuntime = this.hal.export();
|
||||
const authoritative = this.authoritative.export();
|
||||
const devices = this.wasmDeviceProjection();
|
||||
const deviceIo = this.xyzacDeviceIo ? this.xyzacDeviceIo.export() : this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export();
|
||||
const halRuntime = this.xyzacIo ? this.xyzacIo.halExport() : this.hal.export();
|
||||
const authoritative = this.xyzacProgram
|
||||
? { schema: "wasm_authority_boundary.v1", source: "linuxcnc-wasm-worker", state_hash: fnv1a64({ motion: wasmMotion, kinematics: this.xyzacKinematics?.snapshot() ?? null, task }) }
|
||||
: this.authoritative.export();
|
||||
const axis = axisState({ task, program: program ?? {}, action: input.kind, payload: input.payload ?? {} });
|
||||
const gateInput = { task, program: program ?? {}, motion, devices, taskfile: Boolean(this.program.loaded), kinematicsIdentity: this.motionAuthority === "wasm" };
|
||||
const gateActions = ["load", "run", "step", "pause", "resume", "abort", "restart", "mdi", "jogStart", "jogStop", "home", "estop", "recover", "override", "reset", "spindle", "coolant"];
|
||||
const gates = Object.fromEntries(gateActions.map((action) => [action, axisGate(action, gateInput)]));
|
||||
const position = this.motionAuthority === "wasm" && wasmMotion.status === "verified"
|
||||
const xyzac = this.xyzacKinematics ? { coordinates: "XYZAC", joint_count: 5, hal_pin_vars: true, switchkins_net: true, finite_offsets: true, userk_capability: true, active_type: this.xyzacKinematics.snapshot().active_type } : null;
|
||||
const gateInput = { task, program: program ?? {}, motion, devices, xyzac, taskfile: Boolean(this.xyzacProgram ? this.xyzacProgram.snapshot().status !== 0 : this.program.loaded), kinematicsIdentity: this.motionAuthority === "wasm" };
|
||||
const gateActions = ["load", "run", "runLine", "step", "pause", "resume", "abort", "restart", "reload", "edit", "mdi", "jogStart", "jogStop", "home", "estop", "recover", "override", "overrideLimits", "reset", "spindle", "coolant", "touchOff", "toolTouchOff"];
|
||||
const gates = { ...Object.fromEntries(gateActions.map((action) => [action, axisGate(action, gateInput)])), ...xyzacGateMatrix(gateInput) };
|
||||
const wasmPosition = this.motionAuthority === "wasm" && wasmMotion.status === "verified"
|
||||
? wasmMotion.state.position
|
||||
: authoritative.kinematics.pose;
|
||||
// Generic WASM-compatible cases expose their historical XYZ panel shape;
|
||||
// the selected XYZAC case is the only panel that publishes all nine pose
|
||||
// coordinates and the five inverse joints.
|
||||
const position = this.xyzacProgram
|
||||
? wasmPosition
|
||||
: { x: wasmPosition.x, y: wasmPosition.y, z: wasmPosition.z };
|
||||
const inverseJoints = this.xyzacKinematics?.snapshot?.()?.joints;
|
||||
const joints = this.motionAuthority === "wasm" && wasmMotion.status === "verified"
|
||||
? [position.x, position.y, position.z]
|
||||
? (inverseJoints?.length === 5 ? [...inverseJoints] : [position.x, position.y, position.z, position.a ?? 0, position.c ?? 0])
|
||||
: authoritative.kinematics.joints;
|
||||
const preview = this.previewState?.status === "verified" ? this.previewState : null;
|
||||
const visual = {
|
||||
schema: "axis-visual.v1",
|
||||
pose: { x: position.x, y: position.y, z: position.z },
|
||||
pose: { ...position },
|
||||
joints: Array.isArray(joints) ? [...joints] : [],
|
||||
toolpath: (preview?.segments ?? []).map((segment, index) => ({ id: index + 1, ...segment })),
|
||||
...(includeToolpath ? { toolpath: (preview?.segments ?? []).map((segment, index) => ({ id: index + 1, ...segment })) } : {}),
|
||||
active_segment_id: wasmMotion.status === "verified" ? wasmMotion.state.active_id : authoritative.planner.active_id,
|
||||
active_progress: wasmMotion.status === "verified" ? wasmMotion.state.progress : 0,
|
||||
...(Array.isArray(motionTrace) && motionTrace.length ? { motion_trace: motionTrace } : {}),
|
||||
source: "wasm_canonical_motion",
|
||||
};
|
||||
const axisCoordinates = [position.x, position.y, position.z, position.a ?? 0, position.c ?? 0];
|
||||
const unsigned = {
|
||||
schema: "axis-panel-snapshot.v1",
|
||||
context_id: this.contextId,
|
||||
@@ -171,20 +264,20 @@ export class SimSession {
|
||||
task,
|
||||
program,
|
||||
motion,
|
||||
coordinates: { machine: [position.x, position.y, position.z], relative: [position.x, position.y, position.z], commanded: [position.x, position.y, position.z] },
|
||||
coordinates: { machine: [...axisCoordinates], relative: [...axisCoordinates], commanded: [...axisCoordinates] },
|
||||
overrides: { feed: motion.feed_override, spindle: [motion.spindle_override] },
|
||||
wasm_motion: wasmMotion,
|
||||
home_limit: homeLimit,
|
||||
hal: { configuration: this.halState, runtime: halRuntime },
|
||||
devices,
|
||||
device_io: deviceIo,
|
||||
configuration: this.configurationState,
|
||||
...(includeStableRuntime ? { home_limit: homeLimit, hal: { configuration: this.halState, runtime: halRuntime }, devices, device_io: deviceIo } : {}),
|
||||
...(includeConfiguration ? { configuration: this.configurationState } : {}),
|
||||
axis_state: axis,
|
||||
gates,
|
||||
visual,
|
||||
authoritative,
|
||||
motion_authority: this.motionAuthority,
|
||||
task_authority: this.task.authority(),
|
||||
...(this.xyzacKinematics ? { kinematics: this.xyzacKinematics.snapshot() } : {}),
|
||||
...(includeStableRuntime && this.xyzacIo ? { io: this.xyzacIo.export() } : {}),
|
||||
...(xyzac ? { xyzac } : {}),
|
||||
};
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
@@ -211,18 +304,24 @@ export class SimSession {
|
||||
}
|
||||
|
||||
commandGate(input) {
|
||||
const task = this.task.export();
|
||||
const program = this.xyzacProgram ? this.xyzacProgram.snapshot() : this.program.loaded ? this.program.snapshot() : {};
|
||||
const motion = this.xyzacProgram ? this.wasmMotionProjection() : this.motion.export();
|
||||
const devices = this.wasmDeviceProjection();
|
||||
if (input.kind === "KINEMATICS_SELECT" || input.kind === "VISUAL_CLEAR") {
|
||||
const type = Number(input.payload?.type);
|
||||
const action = input.kind === "VISUAL_CLEAR" ? "vismachClear" : type === 0 ? "switchkinsIdentity" : type === 1 ? "switchkinsXyzac" : type === 2 ? "switchkinsUserk" : null;
|
||||
if (!action) return { action: null, enabled: false, reason: "xyzac_kinematics_type_invalid" };
|
||||
return xyzacGate(action, { task, program, motion, devices, xyzac: this.xyzacKinematics ? { coordinates: "XYZAC", joint_count: 5, hal_pin_vars: true, switchkins_net: true, finite_offsets: true, userk_capability: true } : null });
|
||||
}
|
||||
const action = PANEL_GATE_ACTIONS[input.kind];
|
||||
if (!action) return { action: null, enabled: true, reason: "axis_internal_command" };
|
||||
const task = this.task.export();
|
||||
const program = this.program.loaded ? this.program.snapshot() : {};
|
||||
const motion = this.motion.export();
|
||||
const devices = this.devices.export();
|
||||
return axisGate(action, {
|
||||
task,
|
||||
program,
|
||||
motion,
|
||||
devices,
|
||||
taskfile: Boolean(this.program.loaded),
|
||||
taskfile: Boolean(this.xyzacProgram ? this.xyzacProgram.snapshot().loaded : this.program.loaded),
|
||||
kinematicsIdentity: this.motionAuthority === "wasm",
|
||||
});
|
||||
}
|
||||
@@ -234,11 +333,11 @@ export class SimSession {
|
||||
tick: this.tick,
|
||||
created: this.created,
|
||||
protocol_seq: this.timeline.events.cursor,
|
||||
devices: this.devices.export(),
|
||||
motion: this.motion.export(),
|
||||
devices: this.wasmDeviceProjection({ includeBytes: true }),
|
||||
motion: this.xyzacProgram ? this.wasmMotionProjection() : this.motion.export(),
|
||||
plc: this.plc.export(),
|
||||
hal_runtime: this.hal.export(),
|
||||
device_io: this.deviceIo.export(),
|
||||
hal_runtime: this.xyzacIo ? this.xyzacIo.halExport() : this.hal.export(),
|
||||
device_io: this.xyzacDeviceIo ? this.xyzacDeviceIo.export() : this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export(),
|
||||
configuration_batch: this.configurationBatch.export(),
|
||||
io_ack: this.ioAck.export(),
|
||||
callbacks: this.callbacks.export(),
|
||||
@@ -249,13 +348,15 @@ export class SimSession {
|
||||
wasm_motion: this.wasmMotion.exportState(),
|
||||
task_authority: this.task.authority(),
|
||||
wasm_home_limit: this.wasmHomeLimit.exportState(),
|
||||
...(this.xyzacKinematics ? { kinematics: this.xyzacKinematics.snapshot() } : {}),
|
||||
preview: this.previewState,
|
||||
task: this.task.export(),
|
||||
scheduler: this.scheduler.export(),
|
||||
authoritative: this.authoritative.export(),
|
||||
authoritative: this.xyzacProgram ? { schema: "wasm_authority_boundary.v1", source: "linuxcnc-wasm-worker" } : this.authoritative.export(),
|
||||
ledger: this.ledger.export(),
|
||||
};
|
||||
if (this.program.loaded) state.program = this.program.exportState();
|
||||
if (this.xyzacProgram) state.program = { ...this.xyzacProgram.snapshot(), source_text: this.xyzacProgram.source };
|
||||
else if (this.program.loaded) state.program = this.program.exportState();
|
||||
return state;
|
||||
}
|
||||
|
||||
@@ -265,11 +366,11 @@ export class SimSession {
|
||||
session_id: this.sessionId,
|
||||
configuration: this.configurationState,
|
||||
hal: this.halState,
|
||||
devices: this.devices.export(),
|
||||
motion: this.motion.export(),
|
||||
devices: this.wasmDeviceProjection(),
|
||||
motion: this.xyzacProgram ? this.wasmMotionProjection() : this.motion.export(),
|
||||
plc: this.plc.export(),
|
||||
hal_runtime: this.hal.export(),
|
||||
device_io: this.deviceIo.export(),
|
||||
hal_runtime: this.xyzacIo ? this.xyzacIo.halExport() : this.hal.export(),
|
||||
device_io: this.xyzacDeviceIo ? this.xyzacDeviceIo.export() : this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export(),
|
||||
configuration_batch: this.configurationBatch.export(),
|
||||
io_ack: this.ioAck.export(),
|
||||
callbacks: this.callbacks.export(),
|
||||
@@ -278,13 +379,15 @@ export class SimSession {
|
||||
event_cursor: this.timeline.events.cursor,
|
||||
task: this.task.export(),
|
||||
scheduler: this.scheduler.export(),
|
||||
authoritative: this.authoritative.export(),
|
||||
authoritative: this.xyzacProgram ? { schema: "wasm_authority_boundary.v1", source: "linuxcnc-wasm-worker" } : this.authoritative.export(),
|
||||
motion_authority: this.motionAuthority,
|
||||
wasm_motion: this.wasmMotion.exportState(),
|
||||
task_authority: this.task.authority(),
|
||||
wasm_home_limit: this.wasmHomeLimit.exportState(),
|
||||
...(this.xyzacKinematics ? { kinematics: this.xyzacKinematics.snapshot() } : {}),
|
||||
};
|
||||
if (this.program.loaded) value.program = this.program.exportState();
|
||||
if (this.xyzacProgram) value.program = { ...this.xyzacProgram.snapshot(), source_text: this.xyzacProgram.source };
|
||||
else if (this.program.loaded) value.program = this.program.exportState();
|
||||
return { ...value, state_hash: fnv1a64(value) };
|
||||
}
|
||||
|
||||
@@ -325,6 +428,9 @@ export class SimSession {
|
||||
if (trace.state?.motion_authority) this.motionAuthority = trace.state.motion_authority;
|
||||
if (trace.state?.wasm_motion) this.wasmMotion.restoreState(trace.state.wasm_motion);
|
||||
if (trace.state?.wasm_home_limit) this.wasmHomeLimit.restoreState(trace.state.wasm_home_limit);
|
||||
if (trace.state?.device_io?.schema === "wasm_xyzac_device_io.v1" && this.xyzacDeviceIo) this.xyzacDeviceIo.restoreState(trace.state.device_io);
|
||||
if (trace.state?.io?.schema === "wasm_xyzac_io.v1" && this.xyzacIo) this.xyzacIo.restoreState(trace.state.io);
|
||||
if (trace.state?.kinematics && this.xyzacKinematics) this.xyzacKinematics.restoreState(trace.state.kinematics);
|
||||
if (trace.state?.motion) this.motion.restore(trace.state.motion);
|
||||
if (trace.state?.program) {
|
||||
this.program = new ProgramRunner(this.core);
|
||||
@@ -346,10 +452,62 @@ export class SimSession {
|
||||
}
|
||||
|
||||
restoreCheckpoint(checkpoint, events = null, reconcile = undefined) {
|
||||
try {
|
||||
validateCheckpoint(checkpoint, { contextId: this.contextId });
|
||||
} catch (error) {
|
||||
return { schema: "simulation-recovery.v1", status: "blocked", context_id: this.contextId, checkpoint_hash: checkpoint?.checkpoint_hash ?? null, blocked: [{ reason: String(error?.message ?? error) }] };
|
||||
}
|
||||
const replayMotionAuthority = checkpoint?.state?.motion_authority ?? this.motionAuthority;
|
||||
if (replayMotionAuthority === "wasm" && !checkpoint?.state?.wasm_motion) {
|
||||
return { schema: "simulation-recovery.v1", status: "blocked", context_id: this.contextId, checkpoint_hash: checkpoint?.checkpoint_hash ?? null, blocked: [{ reason: "wasm_motion_checkpoint_missing" }] };
|
||||
}
|
||||
if (this.xyzacProgram && checkpoint?.state?.program?.source_text) {
|
||||
try {
|
||||
const loaded = this.xyzacProgram.load(checkpoint.state.program.source_text, checkpoint.state.program.path ?? null);
|
||||
if (loaded.status !== "verified") throw new Error(loaded.code);
|
||||
const subroutines = this.loadXyzacSubroutines(checkpoint.state.program.path ?? null);
|
||||
if (subroutines.status !== "verified") throw new Error(subroutines.code);
|
||||
const targetLine = Number.isSafeInteger(checkpoint.state.program.current_line) ? checkpoint.state.program.current_line : 0;
|
||||
const seek = this.xyzacProgram.seek(targetLine);
|
||||
if (seek.status !== "verified") throw new Error(seek.code);
|
||||
if (checkpoint.state.program) this.xyzacProgram.restoreState(checkpoint.state.program);
|
||||
if (checkpoint.state.wasm_motion) this.wasmMotion.restoreState(checkpoint.state.wasm_motion);
|
||||
if (checkpoint.state.wasm_home_limit) this.wasmHomeLimit.restoreState(checkpoint.state.wasm_home_limit);
|
||||
let restoredWasmDeviceIo = false;
|
||||
if (checkpoint.state.device_io?.schema === "wasm_xyzac_device_io.v1" && this.xyzacDeviceIo) { this.xyzacDeviceIo.restoreState(checkpoint.state.device_io); restoredWasmDeviceIo = true; }
|
||||
if (checkpoint.state.io?.schema === "wasm_xyzac_io.v1" && this.xyzacIo) this.xyzacIo.restoreState(checkpoint.state.io);
|
||||
if (checkpoint.state.devices?.schema === "wasm_xyzac_devices.v1" && this.xyzacIo) this.xyzacIo.restoreState(checkpoint.state.devices);
|
||||
if (checkpoint.state.kinematics && this.xyzacKinematics) this.xyzacKinematics.restoreState(checkpoint.state.kinematics);
|
||||
if (checkpoint.state.task) this.task.restore(checkpoint.state.task);
|
||||
if (checkpoint.state.scheduler) this.scheduler.restore(checkpoint.state.scheduler);
|
||||
if (checkpoint.state.devices?.schema !== "wasm_xyzac_devices.v1") this.devices = new VirtualDevices({ state: checkpoint.state.devices });
|
||||
this.plc = new PlcRuntime(); if (checkpoint.state.plc) this.plc.restore(checkpoint.state.plc);
|
||||
this.hal = new HalRuntime(); if (checkpoint.state.hal_runtime?.schema === "sim_hal_runtime.v1") this.hal.restore(checkpoint.state.hal_runtime);
|
||||
this.configurationBatch = new ConfigurationBatchRuntime({ context_id: this.contextId }); if (checkpoint.state.configuration_batch) this.configurationBatch.restore(checkpoint.state.configuration_batch);
|
||||
this.ioAck = new IoAckTracker(); if (checkpoint.state.io_ack) this.ioAck.restore(checkpoint.state.io_ack);
|
||||
this.callbacks = new PythonCallbacks(); if (checkpoint.state.callbacks) this.callbacks.restore(checkpoint.state.callbacks);
|
||||
this.configurationState = checkpoint.state.configuration ?? this.configurationState;
|
||||
this.halState = checkpoint.state.hal ?? this.halState;
|
||||
this.previewState = checkpoint.state.preview ?? this.previewState;
|
||||
this.python = new PythonBridge(); if (checkpoint.state.python) this.python.restoreState(checkpoint.state.python);
|
||||
this.timeline = new ReplayTimeline(checkpoint.timeline);
|
||||
if (checkpoint.state.hal_runtime?.schema === "wasm_xyzac_hal.v1" && this.xyzacIo) this.xyzacIo.halCommand({ action: "step", ticks: checkpoint.state.hal_runtime.tick ?? 0 });
|
||||
if (!restoredWasmDeviceIo && checkpoint.state.device_io?.schema === "wasm_xyzac_device_io.v1" && this.xyzacDeviceIo) this.xyzacDeviceIo.command("IO_STEP", { ticks: checkpoint.state.device_io.state?.tick ?? 0 });
|
||||
const currentOperations = [...this.operations.values()];
|
||||
this.operations = new Map([...checkpoint.operations ?? [], ...currentOperations].map((operation) => [operation.operation_id, operation]));
|
||||
const checkpointEntries = checkpoint.operations ?? [];
|
||||
const currentEntries = this.ledger.export().entries.filter((entry) => !checkpointEntries.some((operation) => operation.operation_id === entry.operation_id));
|
||||
this.ledger = new this.ledger.constructor();
|
||||
for (const operation of [...checkpointEntries, ...currentEntries]) {
|
||||
const entry = { ...operation, phase: operation.phase ?? (operation.status === "verified" ? "committed" : operation.status), side_effect_count: operation.side_effect_count ?? (operation.status === "verified" ? 1 : 0) };
|
||||
try { this.ledger.importEntry(entry); } catch { /* checkpoint operations remain available for diagnostics */ }
|
||||
}
|
||||
this.tick = checkpoint.state.tick ?? this.tick;
|
||||
return { schema: "simulation-recovery.v1", status: "recovered", context_id: this.contextId, checkpoint_hash: checkpoint.checkpoint_hash ?? null, simulation_tick: this.tick, state: this.stateForCheckpoint(), operations: checkpoint.operations ?? [] };
|
||||
} catch (error) {
|
||||
return { schema: "simulation-recovery.v1", status: "blocked", context_id: this.contextId, checkpoint_hash: checkpoint?.checkpoint_hash ?? null, blocked: [{ code: "wasm_program_recovery", reason: String(error?.message ?? error) }] };
|
||||
}
|
||||
}
|
||||
const replayDevices = new VirtualDevices({ state: checkpoint?.state?.devices });
|
||||
const replayMotion = new MotionState();
|
||||
if (checkpoint?.state?.motion) replayMotion.restore(checkpoint.state.motion);
|
||||
@@ -546,6 +704,7 @@ export class SimSession {
|
||||
this.motionAuthority = result.state?.motion_authority ?? checkpoint?.state?.motion_authority ?? this.motionAuthority;
|
||||
if (this.motionAuthority === "wasm" && result.state?.wasm_motion) this.wasmMotion.restoreState(result.state.wasm_motion);
|
||||
if (this.motionAuthority === "wasm" && result.state?.wasm_home_limit) this.wasmHomeLimit.restoreState(result.state.wasm_home_limit);
|
||||
if (result.state?.kinematics && this.xyzacKinematics) this.xyzacKinematics.restoreState(result.state.kinematics);
|
||||
if (result.state?.task) this.task.restore(result.state.task);
|
||||
if (result.state?.scheduler) this.scheduler.restore(result.state.scheduler);
|
||||
this.authoritative = new AuthoritativeRuntime();
|
||||
@@ -556,7 +715,9 @@ export class SimSession {
|
||||
if (this.motionAuthority !== "wasm") this.authoritative.applyMotion(this.motion);
|
||||
if (this.motionAuthority === "wasm") {
|
||||
const wasmMotion = this.wasmMotion.read();
|
||||
if (wasmMotion.status !== "verified" || !result.state?.motion || fnv1a64(result.state.motion.axes) !== fnv1a64(wasmMotion.state.position)) throw new Error("wasm_motion_recovery_projection_mismatch");
|
||||
const motionAxes = result.state?.motion?.axes;
|
||||
const projectionMatches = motionAxes && Object.entries(motionAxes).every(([axis, value]) => Number.isFinite(value) && Math.abs((wasmMotion.state.position[axis] ?? Number.NaN) - value) <= 1e-12);
|
||||
if (wasmMotion.status !== "verified" || !projectionMatches) throw new Error("wasm_motion_recovery_projection_mismatch");
|
||||
}
|
||||
this.plc = new PlcRuntime();
|
||||
if (result.state?.plc) this.plc.restore(result.state.plc);
|
||||
@@ -661,8 +822,9 @@ export class SimSession {
|
||||
}
|
||||
const gate = this.commandGate(input);
|
||||
const handlerOwnsFault = (input.kind === "JOG_START" || input.kind === "JOG_STOP") && Boolean(this.motion.state.fault);
|
||||
const restartAfterCompletion = input.kind === "PROGRAM_RESTART" && this.program.status === "completed";
|
||||
if (!gate.enabled && !handlerOwnsFault && !restartAfterCompletion) {
|
||||
const restartAfterCompletion = input.kind === "PROGRAM_RESTART" && (this.xyzacProgram ? this.xyzacProgram.snapshot().status === 4 : this.program.status === "completed");
|
||||
const abortAfterCompletion = input.kind === "PROGRAM_ABORT" && (this.xyzacProgram ? this.xyzacProgram.snapshot().status === 4 : this.program.status === "completed");
|
||||
if (!gate.enabled && !handlerOwnsFault && !restartAfterCompletion && !abortAfterCompletion) {
|
||||
if (text(input.operation_id)) this.operations.delete(input.operation_id);
|
||||
const rejectedPayload = { command: input.kind, gate_reason: gate.reason, payload: input.payload ?? {} };
|
||||
try {
|
||||
@@ -713,7 +875,16 @@ export class SimSession {
|
||||
};
|
||||
|
||||
const deviceResponse = (kind, payload) => {
|
||||
const result = this.devices.command(kind, payload ?? {});
|
||||
const result = this.xyzacIo ? this.xyzacIo.command(kind, payload ?? {}) : this.devices.command(kind, payload ?? {});
|
||||
if (result.status === "verified" && this.xyzacProgram && kind === "ESTOP" && payload?.active === true) {
|
||||
// The WASM IO edge owns emergency stop. Stop the WASM trajectory and
|
||||
// pause the canonical program so no compatibility state can continue
|
||||
// feeding motion while the machine is stopped.
|
||||
const programState = this.xyzacProgram.snapshot();
|
||||
if (programState.status === 2) this.xyzacProgram.pause();
|
||||
this.wasmMotion.abort();
|
||||
this.wasmMotion.setPaused(true);
|
||||
}
|
||||
try {
|
||||
if (kind === "ESTOP" && payload?.active === true) this.task.estop(-904);
|
||||
else if (kind === "IO_RECOVER" && result.status === "verified") this.task.recover();
|
||||
@@ -721,15 +892,17 @@ export class SimSession {
|
||||
} catch (error) {
|
||||
return finish(this.error(input, "task_state", String(error?.message ?? error)), "failed");
|
||||
}
|
||||
if (kind === "ESTOP") this.deviceIo.estop(payload?.active === true);
|
||||
if (kind === "IO_RECOVER" && result.status === "verified") this.deviceIo.interlockReset();
|
||||
if (!this.xyzacIo && kind === "ESTOP") this.deviceIo.estop(payload?.active === true);
|
||||
if (!this.xyzacIo && kind === "IO_RECOVER" && result.status === "verified") this.deviceIo.interlockReset();
|
||||
if (this.xyzacDeviceIo && kind === "ESTOP") this.xyzacDeviceIo.command("DEVICE_ESTOP", payload, input.operation_id);
|
||||
if (this.xyzacDeviceIo && kind === "IO_RECOVER" && result.status === "verified") this.xyzacDeviceIo.command("INTERLOCK_RESET", payload, input.operation_id);
|
||||
const responseKind = result.status === "verified" ? "COMMAND_RESULT" : "ERROR";
|
||||
return finish(this.envelope(responseKind, input, {
|
||||
...result,
|
||||
command: kind,
|
||||
task: this.task.export(),
|
||||
device_io: this.deviceIo.export(),
|
||||
motion: this.motion.export(),
|
||||
device_io: this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export(),
|
||||
motion: this.xyzacProgram ? this.wasmMotionProjection() : this.motion.export(),
|
||||
operation_id: input.operation_id ?? null,
|
||||
simulation_tick: this.tick,
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
@@ -737,7 +910,8 @@ export class SimSession {
|
||||
|
||||
const deviceIoResponse = (kind, payload = {}) => {
|
||||
let result;
|
||||
if (kind === "ENCODER_SET") result = this.deviceIo.encoderSet(payload.channel, payload);
|
||||
if (this.xyzacDeviceIo) result = this.xyzacDeviceIo.command(kind, payload, input.operation_id);
|
||||
else if (kind === "ENCODER_SET") result = this.deviceIo.encoderSet(payload.channel, payload);
|
||||
else if (kind === "COOLANT_COMMAND") result = this.deviceIo.coolantCommand(payload);
|
||||
else if (kind === "COOLANT_FAULT") result = this.deviceIo.coolantFault(payload.fault === true);
|
||||
else if (kind === "DOOR_SET") result = this.deviceIo.doorSet(payload.open === true);
|
||||
@@ -749,7 +923,14 @@ export class SimSession {
|
||||
else if (kind === "DEVICE_ACK") result = this.deviceIo.ack(payload.operation_id);
|
||||
else result = this.deviceIo.fail("device_io_command_unsupported");
|
||||
const responseKind = result.status === "verified" ? "COMMAND_RESULT" : "ERROR";
|
||||
return finish(this.envelope(responseKind, input, { ...result, command: kind, device_io: this.deviceIo.export(), task: this.task.export(), operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
return finish(this.envelope(responseKind, input, { ...result, command: kind, device_io: this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export(), task: this.task.export(), operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
};
|
||||
|
||||
const visualClearResponse = () => {
|
||||
const before = { task: this.task.hash(), motion: this.wasmMotion.exportState().state_hash, program: this.xyzacProgram?.snapshot().state_hash ?? null };
|
||||
this.previewState = null;
|
||||
const after = { task: this.task.hash(), motion: this.wasmMotion.exportState().state_hash, program: this.xyzacProgram?.snapshot().state_hash ?? null };
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", command: input.kind, visual_clear: true, visual_only: true, before, after, operation_id: input.operation_id ?? null }), "verified");
|
||||
};
|
||||
|
||||
switch (input.kind) {
|
||||
@@ -769,12 +950,21 @@ export class SimSession {
|
||||
try { this.task.setMode(mode); } catch (error) { return finish(this.error(input, "task_mode", String(error?.message ?? error)), "failed"); }
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", command: input.kind, mode, task: this.task.export(), operation_id: input.operation_id ?? null }));
|
||||
}
|
||||
case "POWER": {
|
||||
if (input.payload?.active !== true) return finish(this.error(input, "power_state", "power_active_required"), "failed");
|
||||
const result = this.xyzacIo ? this.xyzacIo.command("IO_RECOVER", input.payload ?? {}) : this.devices.command("IO_RECOVER", input.payload ?? {});
|
||||
if (result.status === "verified") {
|
||||
try { this.task.recover(); } catch { /* READY is already powered for the selected simulation. */ }
|
||||
if (this.xyzacDeviceIo) this.xyzacDeviceIo.command("INTERLOCK_RESET", input.payload ?? {}, input.operation_id);
|
||||
}
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, command: input.kind, active: true, task: this.task.export(), device_io: this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export(), operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "RESET": {
|
||||
if (["RUNNING", "PAUSED"].includes(this.task.state.state)) return finish(this.error(input, "task_state", "reset_while_program_active"), "failed");
|
||||
const result = this.motion.resetFault();
|
||||
if (result.status !== "verified") return finish(this.envelope("ERROR", input, { ...result, command: input.kind, task: this.task.export() }), "failed");
|
||||
if (this.task.state.state === "FAULT" || this.task.state.state === "STOPPED") this.task.recover();
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { ...result, command: input.kind, task: this.task.export(), motion: this.motion.export(), operation_id: input.operation_id ?? null }));
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { ...result, command: input.kind, task: this.task.export(), motion: this.motionProjection(), operation_id: input.operation_id ?? null }));
|
||||
}
|
||||
case "LOAD_WORKSPACE": {
|
||||
const rootPath = input.payload?.root_path;
|
||||
@@ -782,6 +972,7 @@ export class SimSession {
|
||||
let startupLoaded = false;
|
||||
const configuration = input.payload?.configuration ?? null;
|
||||
if (configuration) {
|
||||
this.workspaceFiles = { ...(configuration.files ?? configuration.workspace ?? configuration.workspace_files ?? {}) };
|
||||
const startupWorkspace = configuration.workspace ?? configuration.files ?? configuration.workspace_files;
|
||||
if (startupWorkspace) {
|
||||
const startup = new ConfigurationStartupStateMachine({
|
||||
@@ -826,10 +1017,29 @@ export class SimSession {
|
||||
};
|
||||
}
|
||||
}
|
||||
this.motionAuthority = configuration?.entrypoint?.includes("3axis-tutorial") ? "wasm" : "js";
|
||||
this.motionAuthority = configuration?.entrypoint?.includes("3axis-tutorial") || configuration?.kinematics?.type === "xyzac-trt" ? "wasm" : "js";
|
||||
if (configuration?.kinematics?.type === "xyzac-trt") {
|
||||
this.xyzacKinematics?.destroy();
|
||||
this.xyzacKinematics = new WasmXyzacKinematics(this.core, { ...configuration.kinematics.offsets, limits: configuration.home_limits });
|
||||
this.xyzacIo?.destroy();
|
||||
this.xyzacIo = new WasmXyzacIo(this.core, { spindleCount: 2, toolCapacity: 1001 });
|
||||
this.xyzacDeviceIo?.destroy();
|
||||
this.xyzacDeviceIo = new WasmXyzacDeviceIo(this.core, { encoderCount: 2 });
|
||||
this.xyzacProgram?.destroy();
|
||||
this.xyzacProgram = new WasmProgramRuntime(this.core);
|
||||
this.xyzacProgramKinematicsMode = XYZAC_MODES.identity;
|
||||
this.xyzacRapidVelocity = Number(configuration.kinematics?.max_linear_velocity ?? 35);
|
||||
this.xyzacProgram.setRapidVelocity(this.xyzacRapidVelocity);
|
||||
}
|
||||
if (configuration?.home_limits && this.motionAuthority === "wasm") {
|
||||
this.wasmHomeLimit.destroy();
|
||||
this.wasmHomeLimit = new WasmHomeLimit(this.core, { limits: configuration.home_limits });
|
||||
this.wasmHomeLimit = new WasmHomeLimit(this.core, {
|
||||
jointCount: configuration.home_limits.joint_count ?? 3,
|
||||
kinematicsType: configuration.home_limits.kinematics_type ?? 1,
|
||||
coordinates: configuration.home_limits.coordinates ?? "XYZ",
|
||||
offsets: configuration.kinematics?.offsets ?? {},
|
||||
limits: configuration.home_limits,
|
||||
});
|
||||
}
|
||||
if (input.payload?.hal) {
|
||||
const preflight = preflightHalConfiguration(input.payload.hal);
|
||||
@@ -849,7 +1059,7 @@ export class SimSession {
|
||||
};
|
||||
}
|
||||
const configuredKinematics = input.payload?.kinematics ?? configuration?.kinematics?.type ?? null;
|
||||
if (configuredKinematics) {
|
||||
if (configuredKinematics && !(this.motionAuthority === "wasm" && configuredKinematics === "xyzac-trt")) {
|
||||
const kinematics = this.authoritative.setKinematics(configuredKinematics);
|
||||
if (kinematics.status === "blocked") return finish(this.envelope("ERROR", input, { status: "blocked", code: kinematics.code, kinematics: this.authoritative.export() }), "failed");
|
||||
}
|
||||
@@ -868,9 +1078,46 @@ export class SimSession {
|
||||
const limit = validateLimit("max_tick_step", cycles);
|
||||
if (limit.status !== "accepted") return finish(this.envelope("ERROR", input, { ...limit, operation_id: input.operation_id ?? null, simulation_tick: this.tick, retryable: false }), "failed");
|
||||
let program_steps = [];
|
||||
const motion_trace = [];
|
||||
try {
|
||||
this.scheduler.step(cycles, {
|
||||
task: () => {
|
||||
if (this.xyzacProgram) {
|
||||
const wasmState = this.xyzacProgram.snapshot();
|
||||
if (![2, 3].includes(wasmState.status)) return;
|
||||
if (this.pendingProgramControl !== 0) {
|
||||
if (this.wasmMotion.read().state.queue_depth !== 0) return;
|
||||
const current = this.wasmMotion.read().state.position;
|
||||
this.xyzacProgramKinematicsMode = { 428: XYZAC_MODES.xyzac, 429: XYZAC_MODES.identity, 430: XYZAC_MODES.userk }[this.pendingProgramControl];
|
||||
const selected = this.xyzacKinematics.select(this.xyzacProgramKinematicsMode, [current.x, current.y, current.z, current.a ?? 0, current.c ?? 0]);
|
||||
if (selected.status !== "verified") throw new Error(`wasm_switchkins_control_${this.pendingProgramControl}`);
|
||||
this.pendingProgramControl = 0;
|
||||
return;
|
||||
}
|
||||
const step = this.xyzacProgram.step();
|
||||
if (step.status !== "verified") throw new Error(step.code);
|
||||
if (step.has_control && [428, 429, 430].includes(step.control_code)) {
|
||||
this.pendingProgramControl = step.control_code;
|
||||
if (this.wasmMotion.read().state.queue_depth === 0) {
|
||||
const current = this.wasmMotion.read().state.position;
|
||||
this.xyzacProgramKinematicsMode = { 428: XYZAC_MODES.xyzac, 429: XYZAC_MODES.identity, 430: XYZAC_MODES.userk }[step.control_code];
|
||||
const selected = this.xyzacKinematics.select(this.xyzacProgramKinematicsMode, [current.x, current.y, current.z, current.a ?? 0, current.c ?? 0]);
|
||||
if (selected.status !== "verified") throw new Error(`wasm_switchkins_control_${step.control_code}`);
|
||||
this.pendingProgramControl = 0;
|
||||
}
|
||||
}
|
||||
const steps = step.motion ? [step] : [];
|
||||
program_steps.push(step);
|
||||
for (const item of steps) {
|
||||
item.motion.kinematics_mode = this.xyzacProgramKinematicsMode;
|
||||
const linearNoop = item.motion.motion_type === 1 && ["x", "y", "z", "a", "b", "c", "u", "v", "w"].every((axis) => item.motion.start[axis] === item.motion.end[axis]);
|
||||
if (!linearNoop) {
|
||||
const enqueue = this.wasmMotion.enqueue(item.motion);
|
||||
if (enqueue.status === "error") throw new Error(enqueue.code);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const steps = this.program.tick(1);
|
||||
program_steps.push(...steps);
|
||||
for (const step of steps) {
|
||||
@@ -900,24 +1147,49 @@ export class SimSession {
|
||||
if (stepResult !== 0) throw new Error(`core_sim_step_${stepResult}`);
|
||||
this.tick = nextTick;
|
||||
this.task.setTick(nextTick);
|
||||
const motionState = this.motion.step(nextTick);
|
||||
const xyzacAuthority = Boolean(this.xyzacProgram);
|
||||
const motionState = xyzacAuthority ? null : this.motion.step(nextTick);
|
||||
const wasmMotion = this.wasmMotion.step();
|
||||
if (wasmMotion.status === "error" && wasmMotion.code !== "wasm_motion_step_-605") throw new Error(wasmMotion.code);
|
||||
if (motionState.fault && this.task.state.state === "RUNNING") this.task.fault(-1205);
|
||||
if (motionState?.fault && this.task.state.state === "RUNNING") this.task.fault(-1205);
|
||||
if (this.motionAuthority === "wasm") {
|
||||
if (wasmMotion.status === "verified") {
|
||||
const position = this.motion.applyWasmPosition(wasmMotion.state.position, wasmMotion.state.velocity, nextTick);
|
||||
if (position.status === "error") throw new Error(position.code);
|
||||
if (!xyzacAuthority) {
|
||||
const position = this.motion.applyWasmPosition(wasmMotion.state.position, wasmMotion.state.velocity, nextTick);
|
||||
if (position.status === "error") throw new Error(position.code);
|
||||
} else if (this.xyzacKinematics && wasmMotion.state.active_id !== 0 && this.xyzacProgramKinematicsMode !== XYZAC_MODES.identity) {
|
||||
const inverse = this.xyzacKinematics.inverse(wasmMotion.state.position);
|
||||
if (inverse.status !== "verified") throw new Error(`wasm_xyzac_motion_inverse_${inverse.code ?? "error"}`);
|
||||
}
|
||||
}
|
||||
if (xyzacAuthority) {
|
||||
// An empty queue is a valid fixed-tick sample during an
|
||||
// M428/M429 synchronization boundary. Record it as a
|
||||
// stationary WASM sample so the display does not invent a
|
||||
// jump across the barrier.
|
||||
const traceState = wasmMotion.status === "verified" ? wasmMotion.state : this.wasmMotion.read().state;
|
||||
const traceKinematics = this.xyzacKinematics?.snapshot?.();
|
||||
motion_trace.push({
|
||||
tick: nextTick,
|
||||
pose: { ...traceState.position },
|
||||
joints: traceKinematics?.joints?.length === 5 ? [...traceKinematics.joints] : [],
|
||||
active_segment_id: traceState.active_id,
|
||||
active_progress: traceState.progress,
|
||||
});
|
||||
}
|
||||
} else if (this.program.loaded) this.authoritative.applyMotion(this.motion);
|
||||
if (this.motionAuthority === "wasm" && this.wasmHomeLimit.read().state.homed && !motionState.fault) {
|
||||
const homeCheck = this.wasmHomeLimit.check(this.motion.state.joints);
|
||||
if (this.motionAuthority === "wasm" && this.wasmHomeLimit.read().state.homed) {
|
||||
const homeCheck = this.wasmHomeLimit.check(this.xyzacProgram ? this.wasmMotionProjection().joints : this.motion.state.joints);
|
||||
if (homeCheck.status === "error") throw new Error(homeCheck.code);
|
||||
}
|
||||
const halState = this.hal.step(nextTick);
|
||||
const halState = this.xyzacIo ? this.xyzacIo.halCommand({ action: "step", ticks: 1 }) : this.hal.step(nextTick);
|
||||
if (halState.status === "error") throw new Error(halState.code);
|
||||
const deviceIoState = this.deviceIo.step(nextTick);
|
||||
const deviceIoState = this.xyzacIo ? this.xyzacIo.command("IO_STEP", { ticks: 1 }) : this.deviceIo.step(nextTick);
|
||||
if (deviceIoState.status === "blocked") throw new Error(deviceIoState.code);
|
||||
if (this.xyzacDeviceIo) {
|
||||
const deviceStep = this.xyzacDeviceIo.command("IO_STEP", { ticks: 1 });
|
||||
if (deviceStep.status === "blocked") throw new Error(deviceStep.code);
|
||||
}
|
||||
const plcState = this.plc.scan(nextTick);
|
||||
if (plcState.status === "error") throw new Error(plcState.code);
|
||||
const ackState = this.ioAck.step(nextTick);
|
||||
@@ -932,20 +1204,38 @@ export class SimSession {
|
||||
} catch (error) {
|
||||
return finish(this.error(input, "scheduler", String(error?.message ?? error)), "failed");
|
||||
}
|
||||
const program = this.program.loaded ? this.program.snapshot() : null;
|
||||
const program = this.xyzacProgram ? this.xyzacProgram.snapshot() : this.program.loaded ? this.program.snapshot() : null;
|
||||
if (program) {
|
||||
const status = program.program_status === "completed" ? "completed" : program.program_status === "running" ? "running" : program.program_status;
|
||||
this.task.setProgram(status, program.line);
|
||||
const status = this.xyzacProgram ? (program.status === 4 ? "completed" : program.status === 2 ? "running" : program.status === 3 ? "paused" : "loaded") : (program.program_status === "completed" ? "completed" : program.program_status === "running" ? "running" : program.program_status);
|
||||
this.task.setProgram(status, this.xyzacProgram ? program.current_line : program.line);
|
||||
if (status === "completed" && this.task.state.state === "RUNNING" && (this.motionAuthority !== "wasm" || this.wasmMotion.read().state.queue_depth === 0)) this.task.transition("STOPPED");
|
||||
}
|
||||
const snapshotState = { tick: this.tick, cycles, motion_authority: this.motionAuthority, task: this.task.export(), scheduler: this.scheduler.export(), authoritative: this.authoritative.export(), wasm_motion: this.wasmMotion.read(), motion: this.motion.export(), plc: this.plc.export(), hal_runtime: this.hal.export(), device_io: this.deviceIo.export(), configuration_batch: this.configurationBatch.export(), io_ack: this.ioAck.export(), callbacks: this.callbacks.export() };
|
||||
const snapshotState = { tick: this.tick, cycles, motion_authority: this.motionAuthority, task: this.task.export(), scheduler: this.scheduler.export(), authoritative: this.xyzacProgram ? { schema: "wasm_authority_boundary.v1", source: "linuxcnc-wasm-worker" } : this.authoritative.export(), wasm_motion: this.wasmMotion.read(), motion: this.motionProjection(), plc: this.plc.export(), hal_runtime: this.xyzacIo ? this.xyzacIo.halExport() : this.hal.export(), device_io: this.xyzacDeviceIo ? this.xyzacDeviceIo.export() : this.xyzacIo ? this.xyzacIo.export() : this.deviceIo.export(), configuration_batch: this.configurationBatch.export(), io_ack: this.ioAck.export(), callbacks: this.callbacks.export() };
|
||||
if (program) snapshotState.program = program;
|
||||
const snapshot = this.timeline.checkpoint(input.seq, this.tick, snapshotState, this.timeline.snapshots.latest()?.snapshot_id ?? null);
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", cycles, program_steps, motion_authority: this.motionAuthority, task: this.task.export(), scheduler: this.scheduler.export(), authoritative: this.authoritative.export(), wasm_motion: this.wasmMotion.read(), motion: this.motion.export(), plc: this.plc.export(), hal_runtime: this.hal.export(), device_io: this.deviceIo.export(), configuration_batch: this.configurationBatch.export(), io_ack: this.ioAck.export(), callbacks: this.callbacks.export(), ...(program ? { program } : {}), state_hash: snapshot.state_hash, snapshot }));
|
||||
const lightweightXyzac = Boolean(this.xyzacProgram);
|
||||
return finish(this.envelope("COMMAND_RESULT", input, {
|
||||
status: "verified", cycles, program_steps,
|
||||
...(lightweightXyzac && cycles <= 32 && motion_trace.length ? { motion_trace } : {}),
|
||||
motion_authority: this.motionAuthority,
|
||||
task: this.task.export(), scheduler: this.scheduler.export(), authoritative: snapshotState.authoritative,
|
||||
wasm_motion: this.wasmMotion.read(), motion: snapshotState.motion,
|
||||
...(lightweightXyzac ? {} : { plc: this.plc.export(), hal_runtime: snapshotState.hal_runtime, device_io: snapshotState.device_io, configuration_batch: this.configurationBatch.export(), io_ack: this.ioAck.export(), callbacks: this.callbacks.export(), snapshot }),
|
||||
...(program ? { program } : {}), state_hash: snapshot.state_hash,
|
||||
}));
|
||||
}
|
||||
case "PROGRAM_LOAD": {
|
||||
if (["RUNNING", "PAUSED"].includes(this.task.state.state)) return finish(this.error(input, "task_state", "program_load_while_active"), "failed");
|
||||
const source = input.payload?.source ?? input.payload?.text ?? input.payload?.program_source;
|
||||
if (this.xyzacProgram) {
|
||||
const result = this.xyzacProgram.load(source, input.payload?.path ?? null);
|
||||
if (result.status === "verified") this.xyzacProgram.setRapidVelocity(this.xyzacRapidVelocity);
|
||||
if (result.status === "verified") { const subroutines = this.loadXyzacSubroutines(input.payload?.path); if (subroutines.status !== "verified") return finish(this.error(input, "program_subroutine", subroutines.code), "failed"); }
|
||||
const preview = result.status === "verified" ? this.xyzacProgram.preview() : result;
|
||||
if (result.status === "verified" && preview.status === "verified") { this.previewState = preview; this.task.setProgram("loaded", 0); }
|
||||
const finalStatus = result.status === "verified" && preview.status === "verified" ? "verified" : "error";
|
||||
return finish(this.envelope(finalStatus === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, preview, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), finalStatus);
|
||||
}
|
||||
const result = this.program.load(source, input.payload?.path ?? null);
|
||||
if (result.status === "verified") {
|
||||
this.authoritative.reset();
|
||||
@@ -962,12 +1252,17 @@ export class SimSession {
|
||||
task: this.task.export(),
|
||||
motion_authority: this.motionAuthority,
|
||||
wasm_motion: this.wasmMotion.read(),
|
||||
motion: this.motion.export(),
|
||||
motion: this.motionProjection(),
|
||||
command: input.kind,
|
||||
operation_id: input.operation_id ?? null,
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "PROGRAM_RUN": {
|
||||
if (this.xyzacProgram) {
|
||||
const result = this.xyzacProgram.start();
|
||||
if (result.status === "verified") { this.wasmMotion.setPaused(false); this.task.setMode("AUTO"); if (this.task.state.state === "STOPPED") this.task.recover(); this.task.transition("RUNNING"); this.task.setProgram("running", this.xyzacProgram.snapshot().current_line); }
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
const result = this.program.start();
|
||||
if (result.status === "verified") {
|
||||
this.task.setMode("AUTO");
|
||||
@@ -981,13 +1276,22 @@ export class SimSession {
|
||||
task: this.task.export(),
|
||||
motion_authority: this.motionAuthority,
|
||||
wasm_motion: this.wasmMotion.read(),
|
||||
motion: this.motion.export(),
|
||||
motion: this.motionProjection(),
|
||||
command: input.kind,
|
||||
operation_id: input.operation_id ?? null,
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "PROGRAM_PAUSE": case "PROGRAM_RESUME": case "PROGRAM_ABORT": {
|
||||
const action = input.kind === "PROGRAM_PAUSE" ? "pause" : input.kind === "PROGRAM_RESUME" ? "resume" : "abort";
|
||||
if (this.xyzacProgram) {
|
||||
const result = this.xyzacProgram[action]();
|
||||
if (result.status === "verified") {
|
||||
if (action === "pause") { call(this.core, "sim_protocol_pause"); this.wasmMotion.setPaused(true); this.task.transition("PAUSED"); this.task.setProgram("paused", this.xyzacProgram.snapshot().current_line); }
|
||||
else if (action === "resume") { call(this.core, "sim_protocol_resume"); this.wasmMotion.setPaused(false); this.task.transition("RUNNING"); this.task.setProgram("running", this.xyzacProgram.snapshot().current_line); }
|
||||
else { this.wasmMotion.abort(); this.task.transition("STOPPED"); this.task.setProgram("aborted", this.xyzacProgram.snapshot().current_line); }
|
||||
}
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
const result = this.program[action]();
|
||||
if (result.status === "verified") {
|
||||
if (action === "pause") {
|
||||
@@ -1008,12 +1312,19 @@ export class SimSession {
|
||||
task: this.task.export(),
|
||||
motion_authority: this.motionAuthority,
|
||||
wasm_motion: this.wasmMotion.read(),
|
||||
motion: this.motion.export(),
|
||||
motion: this.motionProjection(),
|
||||
command: input.kind,
|
||||
operation_id: input.operation_id ?? null,
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "PROGRAM_RESTART": {
|
||||
if (this.xyzacProgram) {
|
||||
const source = this.xyzacProgram.source;
|
||||
const result = this.xyzacProgram.load(source, this.xyzacProgram.path);
|
||||
if (result.status === "verified") this.xyzacProgram.setRapidVelocity(this.xyzacRapidVelocity);
|
||||
if (result.status === "verified") { const subroutines = this.loadXyzacSubroutines(this.xyzacProgram.path); if (subroutines.status !== "verified") return finish(this.error(input, "program_subroutine", subroutines.code), "failed"); const preview = this.xyzacProgram.preview(); if (preview.status === "verified") this.previewState = preview; this.wasmMotion.reset(); this.wasmMotion.setPaused(false); this.task.recover(); this.task.setMode("AUTO"); this.task.setProgram("loaded", 0); }
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
const result = this.program.restart();
|
||||
if (result.status === "verified") {
|
||||
if (["ESTOP", "FAULT"].includes(this.task.state.state)) return finish(this.error(input, "task_state", "program_restart_faulted"), "failed");
|
||||
@@ -1034,6 +1345,13 @@ export class SimSession {
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "PROGRAM_STEP": {
|
||||
if (this.xyzacProgram) {
|
||||
const current = this.xyzacProgram.snapshot();
|
||||
if (current.status === 1) this.xyzacProgram.start();
|
||||
const result = this.xyzacProgram.step();
|
||||
if (result.status === "verified") { if (result.motion) this.wasmMotion.enqueue(result.motion); this.wasmMotion.setPaused(true); this.task.setProgram(result.program_status === 4 ? "completed" : "paused", result.line); this.task.transition(result.program_status === 4 ? "STOPPED" : "PAUSED"); }
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
if (this.task.state.state === "READY") {
|
||||
this.task.setMode("AUTO");
|
||||
this.task.transition("RUNNING");
|
||||
@@ -1059,12 +1377,17 @@ export class SimSession {
|
||||
task: this.task.export(),
|
||||
motion_authority: this.motionAuthority,
|
||||
wasm_motion: this.wasmMotion.read(),
|
||||
motion: this.motion.export(),
|
||||
motion: this.motionProjection(),
|
||||
command: input.kind,
|
||||
operation_id: input.operation_id ?? null,
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "PROGRAM_SEEK": {
|
||||
if (this.xyzacProgram) {
|
||||
const result = this.xyzacProgram.seek(input.payload?.line);
|
||||
if (result.status === "verified") { this.task.setProgram("paused", input.payload?.line ?? 0); if (this.task.state.state === "READY") this.task.transition("RUNNING"); if (this.task.state.state === "RUNNING") this.task.transition("PAUSED"); }
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
const result = this.program.seek(input.payload?.line);
|
||||
if (result.status === "verified") this.task.setProgram("paused", result.line);
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, {
|
||||
@@ -1077,7 +1400,28 @@ export class SimSession {
|
||||
case "MDI_EXECUTE": {
|
||||
if (["RUNNING", "PAUSED"].includes(this.task.state.state)) return finish(this.error(input, "task_state", "mdi_while_program_active"), "failed");
|
||||
if (this.task.state.state === "STOPPED") this.task.recover();
|
||||
const result = this.program.executeBlock(input.payload?.block ?? input.payload?.command);
|
||||
const block = input.payload?.block ?? input.payload?.command;
|
||||
if (this.xyzacProgram) {
|
||||
const result = this.xyzacProgram.executeBlock(block);
|
||||
if (result.status === "verified") {
|
||||
const controlCode = Number(result.program?.control_code ?? 0);
|
||||
if ([428, 429, 430].includes(controlCode) && this.xyzacKinematics) {
|
||||
const mode = { 428: XYZAC_MODES.xyzac, 429: XYZAC_MODES.identity, 430: XYZAC_MODES.userk }[controlCode];
|
||||
const selected = this.xyzacKinematics.select(mode, this.xyzacKinematics.snapshot().joints);
|
||||
if (selected.status !== "verified") return finish(this.error(input, "kinematics", selected.code ?? "xyzac_switchkins_failed"), "failed");
|
||||
result.kinematics = this.xyzacKinematics.snapshot();
|
||||
result.remap = { code: `M${controlCode}`, hal_pin: "motion.switchkins-type", hal_pin_vars: true, sync: "M66 E0 L0" };
|
||||
}
|
||||
this.task.setMode("MDI");
|
||||
this.task.setProgram("loaded", this.xyzacProgram.snapshot().current_line);
|
||||
if (result.motion) {
|
||||
const enqueue = this.wasmMotion.enqueue(result.motion);
|
||||
if (enqueue.status === "error") return finish(this.error(input, "motion_queue", enqueue.code), "failed");
|
||||
}
|
||||
}
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, program: this.xyzacProgram.snapshot(), task: this.task.export(), motion_authority: "wasm", command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
const result = this.program.executeBlock(block);
|
||||
if (result.status === "verified") { this.task.setMode("MDI"); this.task.setProgram("loaded", this.program.currentLine); }
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, {
|
||||
...result,
|
||||
@@ -1086,15 +1430,64 @@ export class SimSession {
|
||||
operation_id: input.operation_id ?? null,
|
||||
}), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "KINEMATICS_SELECT": {
|
||||
if (!this.xyzacKinematics) return finish(this.error(input, "kinematics", "xyzac_kinematics_unavailable"), "failed");
|
||||
const mode = Number(input.payload?.type);
|
||||
if (![XYZAC_MODES.identity, XYZAC_MODES.xyzac, XYZAC_MODES.userk].includes(mode)) return finish(this.error(input, "kinematics", "xyzac_kinematics_type_invalid"), "failed");
|
||||
const result = this.xyzacKinematics.select(mode, this.xyzacKinematics.snapshot().joints);
|
||||
const code = mode === XYZAC_MODES.xyzac ? "M428" : mode === XYZAC_MODES.identity ? "M429" : "M430";
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, command: input.kind, remap: { code, hal_pin: "motion.switchkins-type", hal_pin_vars: true, sync: "M66 E0 L0" }, kinematics: this.xyzacKinematics.snapshot(), task: this.task.export(), operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "VISUAL_CLEAR":
|
||||
return visualClearResponse();
|
||||
case "HOME": {
|
||||
if (this.xyzacProgram) {
|
||||
if (this.wasmHomeLimit.read().status !== "verified") return finish(this.error(input, "home_limit", "wasm_home_limit_unavailable"), "failed");
|
||||
const home = this.wasmHomeLimit.home(input.seq);
|
||||
if (home.status === "error") return finish(this.error(input, "home_limit", home.code), "failed");
|
||||
this.task.setMode("MANUAL");
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", command: input.kind, motion: this.wasmMotionProjection(), wasm_home_limit: this.wasmHomeLimit.read(), motion_authority: "wasm", operation_id: input.operation_id ?? null }), "verified");
|
||||
}
|
||||
const result = this.motion.home(input.payload?.axis ?? null);
|
||||
if (result.status === "verified" && this.motionAuthority === "wasm") {
|
||||
const home = this.wasmHomeLimit.home(input.seq);
|
||||
if (home.status === "error") return finish(this.error(input, "home_limit", home.code), "failed");
|
||||
}
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, motion: this.motion.export(), task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, motion: this.motionProjection(), task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "JOG_START": case "JOG_STOP": case "SET_OVERRIDE": case "FOLLOWING_ERROR": {
|
||||
if (this.motionAuthority === "wasm") {
|
||||
if (input.kind === "JOG_STOP") {
|
||||
this.wasmMotion.abort();
|
||||
this.wasmMotion.setPaused(true);
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", command: input.kind, motion: this.wasmMotionProjection(), motion_authority: "wasm", operation_id: input.operation_id ?? null }), "verified");
|
||||
}
|
||||
if (input.kind === "SET_OVERRIDE") {
|
||||
const feed = Number(input.payload?.feed ?? input.payload?.feed_override);
|
||||
if (!Number.isFinite(feed) || feed < 0 || feed > 2) return finish(this.error(input, "motion_override", "motion_override_range"), "failed");
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", command: input.kind, feed_override: feed, state: { ...this.motion.export(), feed_override: feed }, motion: this.motionProjection(), motion_authority: "wasm", operation_id: input.operation_id ?? null }), "verified");
|
||||
}
|
||||
if (input.kind === "FOLLOWING_ERROR") return finish(this.error(input, "motion_following_error", "wasm_following_error_unavailable"), "failed");
|
||||
const current = this.wasmMotion.read();
|
||||
if (current.status !== "verified") return finish(this.error(input, "motion_queue", "wasm_motion_unavailable"), "failed");
|
||||
const axis = String(input.payload?.axis ?? "").toLowerCase();
|
||||
if (!["x", "y", "z", "a", "c"].includes(axis)) return finish(this.error(input, "motion_axis", "motion_axis_invalid"), "failed");
|
||||
const direction = input.payload?.direction === -1 ? -1 : input.payload?.direction === 1 ? 1 : null;
|
||||
if (direction === null) return finish(this.error(input, "motion_jog", "motion_jog_range"), "failed");
|
||||
this.wasmMotion.setPaused(false);
|
||||
const start = { ...current.state.position };
|
||||
const distance = Number(input.payload?.increment) > 0 ? Number(input.payload.increment) : 1;
|
||||
const target = Number(start[axis] ?? 0) + direction * distance;
|
||||
const configuredLimit = this.wasmHomeLimit.limits?.[axis];
|
||||
if (Array.isArray(configuredLimit) && configuredLimit.length === 2 && (target < Number(configuredLimit[0]) || target > Number(configuredLimit[1]))) return finish(this.error(input, "motion_queue", "motion_limit_range"), "failed");
|
||||
const end = { ...start, [axis]: target };
|
||||
const enqueue = this.wasmMotion.enqueue({ start, end, center: { x: 0, y: 0, z: 0 }, normal: { x: 0, y: 0, z: 1 }, velocity: Number(input.payload?.speed ?? 1), acceleration: 0, motion_type: 1, turn: 0, source_line: null });
|
||||
if (enqueue.status === "error") {
|
||||
const code = enqueue.code === "wasm_motion_enqueue_-602" ? "motion_limit_range" : enqueue.code === "wasm_motion_enqueue_-601" ? "motion_jog_nonfinite" : enqueue.code;
|
||||
return finish(this.error(input, "motion_queue", code), "failed");
|
||||
}
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: "verified", command: input.kind, motion: this.wasmMotionProjection(), wasm_motion: this.wasmMotion.read(), motion_authority: "wasm", operation_id: input.operation_id ?? null }), "verified");
|
||||
}
|
||||
let result;
|
||||
if (input.kind === "JOG_START" && this.motionAuthority === "wasm" && !this.wasmHomeLimit.read().state.homed) return finish(this.error(input, "motion_state", "motion_unhomed"), "failed");
|
||||
if (input.kind === "JOG_START") result = this.motion.startJog(input.payload);
|
||||
@@ -1102,7 +1495,7 @@ export class SimSession {
|
||||
else if (input.kind === "SET_OVERRIDE") result = this.motion.setOverride(input.payload?.feed ?? input.payload?.feed_override, input.payload?.spindle ?? input.payload?.spindle_override);
|
||||
else result = this.motion.setFollowingError(input.payload?.value ?? input.payload?.following_error);
|
||||
if (result.status === "error" && result.state?.fault && this.task.state.state === "RUNNING") this.task.fault(-1205);
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, motion: this.motion.export(), task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, motion: this.motionProjection(), task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "SPINDLE_COMMAND": case "SPINDLE_FAULT": case "SPINDLE_INHIBIT":
|
||||
case "SPINDLE_ORIENT": case "SPINDLE_ORIENTED": case "IO_STEP":
|
||||
@@ -1111,6 +1504,7 @@ export class SimSession {
|
||||
case "PROBE_CONFIG": case "IO_RECOVER":
|
||||
return deviceResponse(input.kind, input.payload);
|
||||
case "LIMIT_SET": {
|
||||
if (this.xyzacProgram) return finish(this.error(input, "motion_limit", "wasm_limit_inputs_are_configuration_owned"), "failed");
|
||||
const result = this.motion.setLimit(input.payload?.axis, input.payload?.active === true);
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
@@ -1125,7 +1519,11 @@ export class SimSession {
|
||||
case "HAL_COMMAND": case "HAL_SET": {
|
||||
const payload = input.payload ?? {};
|
||||
let result;
|
||||
if (input.kind === "HAL_SET") {
|
||||
if (this.xyzacIo && input.kind === "HAL_SET") {
|
||||
result = this.xyzacIo.halValue(payload.name, payload.value, payload.type === "bit" ? 1 : payload.type === "s32" ? 2 : payload.type === "u32" ? 3 : 4, payload.target === "param");
|
||||
} else if (this.xyzacIo) {
|
||||
result = this.xyzacIo.halCommand(payload);
|
||||
} else if (input.kind === "HAL_SET") {
|
||||
result = payload.target === "param" ? this.hal.setParam(payload.name, payload.value, payload.type) : this.hal.setPin(payload.name, payload.value, payload.type);
|
||||
} else if (payload.action === "configure") {
|
||||
result = this.hal.configure(payload);
|
||||
@@ -1142,7 +1540,7 @@ export class SimSession {
|
||||
} else {
|
||||
result = this.hal.fail("hal_command_unsupported");
|
||||
}
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, task: this.task.export(), hal_runtime: this.hal.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, task: this.task.export(), hal_runtime: this.xyzacIo ? this.xyzacIo.halExport() : this.hal.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "ENCODER_SET": case "COOLANT_COMMAND": case "COOLANT_FAULT": case "DOOR_SET": case "INTERLOCK_SET": case "INTERLOCK_RESET": case "DEVICE_ESTOP": case "DEVICE_ACK_ISSUE": case "DEVICE_ACK": case "DEVICE_ACK_ABORT":
|
||||
return deviceIoResponse(input.kind, input.payload);
|
||||
@@ -1162,6 +1560,10 @@ export class SimSession {
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, io_ack: this.ioAck.export(), device_io: this.deviceIo.export(), task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "REMAP_REGISTER": case "REMAP_CALL": case "VCP_REGISTER": case "VCP_UPDATE": case "FILTER_EXEC": {
|
||||
if (this.xyzacProgram && input.kind === "VCP_UPDATE" && ["type0-button", "type1-button", "type2-button"].includes(input.payload?.pin)) return finish(this.error(input, "pyvcp_switch_requires_mdi", "pyvcp_switch_requires_mdi"), "failed");
|
||||
if (this.xyzacProgram && input.kind === "VCP_UPDATE" && input.payload?.pin === "vismach-clear") {
|
||||
return visualClearResponse();
|
||||
}
|
||||
const result = input.kind === "REMAP_REGISTER"
|
||||
? this.callbacks.registerRemap(input.payload?.name, input.payload)
|
||||
: input.kind === "REMAP_CALL"
|
||||
@@ -1174,6 +1576,7 @@ export class SimSession {
|
||||
return finish(this.envelope(result.status === "verified" ? "COMMAND_RESULT" : "ERROR", input, { ...result, callbacks: this.callbacks.export(), task: this.task.export(), command: input.kind, operation_id: input.operation_id ?? null }), result.status === "verified" ? "verified" : "failed");
|
||||
}
|
||||
case "ESTOP": {
|
||||
if (this.xyzacProgram) return deviceResponse(input.kind, input.payload);
|
||||
const motionResult = this.motion.estop(input.payload?.active === true);
|
||||
const deviceResult = deviceResponse(input.kind, input.payload);
|
||||
if (motionResult.status === "error") return finish(this.envelope("ERROR", input, { ...motionResult, task: this.task.export() }), "failed");
|
||||
@@ -1207,6 +1610,17 @@ export class SimSession {
|
||||
const result = this.restoreCheckpoint(input.payload?.checkpoint, input.payload?.events, input.payload?.reconcile);
|
||||
return finish(this.envelope("RECOVERY_STATUS", input, result), result.status === "blocked" ? "failed" : "verified");
|
||||
}
|
||||
case "CANCEL": {
|
||||
const target = input.payload?.operation_id;
|
||||
let ledgerResult = null;
|
||||
if (text(target)) {
|
||||
try { ledgerResult = this.ledger.abort(target, input.payload?.reason ?? "cancel_requested"); }
|
||||
catch (error) { ledgerResult = { status: "missing", error: String(error?.message ?? error) }; }
|
||||
const operation = this.operations.get(target);
|
||||
if (operation && operation.status !== "verified") operation.status = "aborted";
|
||||
}
|
||||
return finish(this.envelope("COMMAND_RESULT", input, { status: ledgerResult?.status ?? "missing", cancelled: ledgerResult?.status === "aborted", retry_allowed: false, operation_id: target ?? null, ledger: ledgerResult }));
|
||||
}
|
||||
case "ABORT": {
|
||||
const target = input.payload?.operation_id;
|
||||
const operation = text(target) ? this.operations.get(target) : null;
|
||||
@@ -1222,6 +1636,9 @@ export class SimSession {
|
||||
}
|
||||
case "DESTROY":
|
||||
this.program.destroy();
|
||||
this.xyzacIo?.destroy();
|
||||
this.xyzacDeviceIo?.destroy();
|
||||
this.xyzacKinematics?.destroy();
|
||||
this.task.destroy();
|
||||
this.scheduler.destroy();
|
||||
call(this.core, "sim_destroy");
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { SimSession } from "./sim-session.mjs";
|
||||
import { XyzacSession } from "./xyzac-session.mjs";
|
||||
|
||||
const core = await createModule();
|
||||
const session = new SimSession(core);
|
||||
const session = new XyzacSession(core);
|
||||
|
||||
self.onmessage = (event) => {
|
||||
try {
|
||||
|
||||
@@ -49,7 +49,7 @@ const stepped = send("SIM_STEP", "jog-step", { cycles: 3 });
|
||||
assert.equal(stepped.payload.task_authority, "wasm_task_canonical");
|
||||
assert.equal(stepped.payload.wasm_home_limit.state.source, "wasm_home_limit");
|
||||
assert.ok(stepped.payload.wasm_home_limit.state.tick > home.payload.wasm_home_limit.state.tick);
|
||||
assert.deepEqual(stepped.payload.motion.axes, { x: stepped.payload.motion.joints[0], y: stepped.payload.motion.joints[1], z: stepped.payload.motion.joints[2] });
|
||||
assert.deepEqual({ x: stepped.payload.motion.axes.x, y: stepped.payload.motion.axes.y, z: stepped.payload.motion.axes.z }, { x: stepped.payload.motion.joints[0], y: stepped.payload.motion.joints[1], z: stepped.payload.motion.joints[2] });
|
||||
|
||||
const limit = send("LIMIT_SET", "limit", { axis: "x", active: true });
|
||||
assert.equal(limit.payload.wasm_home_limit.state.source, "wasm_home_limit");
|
||||
|
||||
65
web/worker/test_xyzac_exact_preview.mjs
Normal file
65
web/worker/test_xyzac_exact_preview.mjs
Normal file
@@ -0,0 +1,65 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const PROGRAM_PATH = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/demos/xyzac_switchkins.ngc";
|
||||
const fetchImpl = async (url) => {
|
||||
try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; }
|
||||
catch { return { ok: false, text: async () => "" }; }
|
||||
};
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const source = bundle.files?.[PROGRAM_PATH];
|
||||
assert.equal(typeof source, "string");
|
||||
assert.equal(source, await readFile(new URL(`../../${PROGRAM_PATH}`, import.meta.url), "utf8"));
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-exact-preview" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
const loaded = send("PROGRAM_LOAD", "program-load", { source, path: PROGRAM_PATH });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
assert.equal(loaded.payload.program.path, PROGRAM_PATH);
|
||||
assert.equal(loaded.payload.program.source_hash, fnv1a64(source));
|
||||
assert.equal(loaded.payload.preview.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(loaded.payload.preview.segments.length, 29);
|
||||
assert.equal(loaded.payload.panel_snapshot.visual.source, "wasm_canonical_motion");
|
||||
assert.equal(loaded.payload.panel_snapshot.visual.toolpath.length, 29);
|
||||
const arcs = loaded.payload.preview.segments.filter((segment) => segment.motion_type === 2);
|
||||
const rapids = loaded.payload.preview.segments.filter((segment) => segment.kind === "rapid");
|
||||
assert.equal(arcs.length, 4);
|
||||
assert.equal(rapids.length, 25);
|
||||
assert.ok(rapids.every((segment) => segment.canonical_motion_mode === 0));
|
||||
assert.ok(arcs.every((segment) => segment.canonical_motion_mode === 2));
|
||||
assert.ok(rapids.every((segment) => Math.abs(segment.velocity - 35) < 1e-12));
|
||||
assert.ok(arcs.every((segment) => Math.abs(segment.velocity - (1000 / 60)) < 1e-12));
|
||||
assert.ok(arcs.every((segment) => segment.turn === -3));
|
||||
assert.ok(arcs.every((segment) => Array.isArray(segment.samples) && segment.samples.length === 97));
|
||||
assert.ok(arcs.every((segment) => segment.samples.some((sample) => sample.z < segment.start.z && sample.z > segment.end.z)));
|
||||
assert.ok(arcs.every((segment) => segment.samples_source === "linuxcnc-wasm-sim_posemath_segment_point"));
|
||||
assert.ok(arcs.every((segment) => segment.kind === "helix" && segment.helix === true && segment.radius === 10 && segment.turns === 3 && segment.axial_delta === -5));
|
||||
assert.ok(arcs.every((segment) => segment.start.a === 20 && segment.end.a === 20 && segment.start.c === 45 && segment.end.c === 45));
|
||||
assert.deepEqual(arcs.map((segment) => segment.source_line), [67, 67, 67, 67]);
|
||||
for (const segment of arcs) {
|
||||
const radius = segment.samples.map((sample) => Math.hypot(sample.x - segment.center.x, sample.y - segment.center.y));
|
||||
assert.ok(Math.max(...radius) - Math.min(...radius) < 1e-9);
|
||||
assert.ok(Math.abs(radius[0] - 10) < 1e-9);
|
||||
assert.ok(Math.abs(segment.samples[0].z - 10) < 1e-9);
|
||||
assert.ok(Math.abs(segment.samples.at(-1).z - 5) < 1e-9);
|
||||
}
|
||||
assert.deepEqual(arcs.map((segment) => [segment.end.x, segment.end.y, segment.end.z]), [[10, 20, 5], [-30, 20, 5], [-30, -20, 5], [10, -20, 5]]);
|
||||
const clear = send("VISUAL_CLEAR", "visual-clear");
|
||||
assert.equal(clear.payload.visual_only, true);
|
||||
assert.deepEqual(clear.payload.before, clear.payload.after);
|
||||
const restarted = send("PROGRAM_RESTART", "program-restart");
|
||||
assert.equal(restarted.payload.program.path, PROGRAM_PATH);
|
||||
assert.equal(restarted.payload.panel_snapshot.visual.toolpath.length, 29);
|
||||
console.log(JSON.stringify({ schema: "xyzac-exact-tool-preview.v1", status: "verified", case: CASE, program_path: PROGRAM_PATH, source_hash: loaded.payload.program.source_hash, segment_count: loaded.payload.preview.segments.length, rapid_count: rapids.length, arc_count: arcs.length, arc_turn: -3, motion_mode_source: "linuxcnc-wasm-sim_program_canonical", helix_validation: { sample_points_per_helix: arcs[0].samples.length, samples_source: arcs[0].samples_source, radius_mm: 10, z_start: 10, z_end: 5, turns_per_helix: 3 }, helix_details: arcs.map((segment) => ({ source_line: segment.source_line, kind: segment.kind, start: segment.start, end: segment.end, center: segment.center, radius: segment.radius, turns: segment.turns, axial_delta: segment.axial_delta, samples: segment.samples.length, samples_source: segment.samples_source })), visual_segments: loaded.payload.panel_snapshot.visual.toolpath.length, restart_visual_segments: restarted.payload.panel_snapshot.visual.toolpath.length, clear_visual_only: clear.payload.visual_only, state_hash: loaded.payload.panel_snapshot.state_hash }));
|
||||
57
web/worker/test_xyzac_gate_matrix.mjs
Normal file
57
web/worker/test_xyzac_gate_matrix.mjs
Normal file
@@ -0,0 +1,57 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
import { axisGate, xyzacGateMatrix } from "../app/axis-contract.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => { try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; } catch { return { ok: false, text: async () => "" }; } };
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-gate-matrix" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
const initial = loaded.payload.panel_snapshot;
|
||||
assert.equal(initial.xyzac.coordinates, "XYZAC");
|
||||
assert.equal(initial.xyzac.joint_count, 5);
|
||||
assert.equal(initial.gates.run.enabled, false);
|
||||
assert.equal(initial.gates.run.reason, "axis_taskfile_missing");
|
||||
assert.equal(initial.gates.runLine.reason, "axis_taskfile_missing");
|
||||
assert.equal(initial.gates.switchkinsIdentity.enabled, true);
|
||||
assert.equal(initial.gates.switchkinsXyzac.enabled, true);
|
||||
assert.equal(initial.gates.switchkinsUserk.enabled, true);
|
||||
assert.equal(initial.gates.vismachClear.visual_only, true);
|
||||
const noFile = axisGate("runLine", { task: { state: "READY", mode: "MANUAL" }, program: {}, motion: {}, taskfile: false, highlight_line: null });
|
||||
assert.equal(noFile.enabled, false);
|
||||
assert.equal(noFile.reason, "axis_taskfile_missing");
|
||||
const highlightMissing = axisGate("runLine", { task: { state: "READY", mode: "MANUAL" }, program: { source_hash: "fnv1a64:file" }, motion: {}, taskfile: true, highlight_line: null });
|
||||
assert.equal(highlightMissing.reason, "axis_highlight_missing");
|
||||
const invalidSwitch = xyzacGateMatrix({ task: { state: "READY", mode: "MANUAL" }, program: {}, motion: {}, taskfile: false, xyzac: { coordinates: "XYZ", joint_count: 3, hal_pin_vars: false, switchkins_net: false, finite_offsets: true, userk_capability: false } });
|
||||
assert.equal(invalidSwitch.switchkinsXyzac.enabled, false);
|
||||
assert.equal(invalidSwitch.switchkinsXyzac.reason, "xyzac_hal_pin_vars_missing");
|
||||
send("PROGRAM_LOAD", "program-load", { path: "gate.ngc", source: "G21 G90\nG1 X10 A20 C30 F100\nM2\n" });
|
||||
const running = send("PROGRAM_RUN", "program-run");
|
||||
assert.equal(running.payload.panel_snapshot.gates.pause.enabled, true);
|
||||
assert.equal(running.payload.panel_snapshot.gates.abort.enabled, true);
|
||||
assert.equal(running.payload.panel_snapshot.gates.resume.enabled, false);
|
||||
assert.equal(running.payload.panel_snapshot.gates.switchkinsXyzac.enabled, false);
|
||||
send("PROGRAM_PAUSE", "pause");
|
||||
const paused = send("SNAPSHOT_REQUEST", "paused-snapshot");
|
||||
assert.equal(paused.payload.panel_snapshot.gates.resume.enabled, true);
|
||||
assert.equal(paused.payload.panel_snapshot.gates.pause.enabled, false);
|
||||
send("PROGRAM_ABORT", "abort");
|
||||
const estopGates = xyzacGateMatrix({ task: { state: "ESTOP", mode: "MANUAL" }, program: { source_hash: "fnv1a64:file" }, motion: {}, taskfile: true, xyzac: { coordinates: "XYZAC", joint_count: 5, hal_pin_vars: true, switchkins_net: true, finite_offsets: true, userk_capability: true } });
|
||||
assert.equal(estopGates.switchkinsXyzac.enabled, false);
|
||||
assert.equal(estopGates.switchkinsXyzac.reason, "xyzac_estop_active");
|
||||
send("ESTOP", "estop-reset", { active: false });
|
||||
send("IO_RECOVER", "recover");
|
||||
const recovered = send("SNAPSHOT_REQUEST", "recovered-snapshot");
|
||||
assert.equal(typeof recovered.payload.panel_snapshot.state_hash, "string");
|
||||
assert.equal(recovered.payload.panel_snapshot.gates.vismachClear.visual_only, true);
|
||||
console.log(JSON.stringify({ schema: "xyzac-gate-matrix-rtcp-03.v1", status: "verified", case: CASE, normal: ["runLine", "switchkinsIdentity", "switchkinsXyzac", "switchkinsUserk"], blocked: [noFile.reason, highlightMissing.reason, invalidSwitch.switchkinsXyzac.reason, estopGates.switchkinsXyzac.reason], running: { pause: true, abort: true }, paused: { resume: true }, estop: { switchkins_enabled: estopGates.switchkinsXyzac.enabled }, final_state_hash: recovered.payload.panel_snapshot.state_hash }));
|
||||
65
web/worker/test_xyzac_hal_tool_alarm.mjs
Normal file
65
web/worker/test_xyzac_hal_tool_alarm.mjs
Normal file
@@ -0,0 +1,65 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => { try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; } catch { return { ok: false, text: async () => "" }; } };
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-hal-tool" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello"); send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
|
||||
const xml = await readFile(new URL("../../linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.xml", import.meta.url), "utf8");
|
||||
const postgui = await readFile(new URL("../../linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/switchkins_postgui.hal", import.meta.url), "utf8");
|
||||
for (const pin of ["type0-button", "type1-button", "type2-button", "vismach-clear"]) assert.ok(xml.includes(pin));
|
||||
for (const edge of ["pyvcp.type0-button", "pyvcp.type1-button", "pyvcp.type2-button", "vismach.plotclear"]) assert.ok(postgui.includes(edge));
|
||||
for (const [code, type] of [["M429", 0], ["M428", 1], ["M430", 2]]) assert.equal(send("MDI_EXECUTE", `switch-${code}`, { block: code }).payload.kinematics.active_type, type);
|
||||
const directSwitch = send("VCP_UPDATE", "direct-switch", { name: "xyzac", pin: "type1-button", value: 1 });
|
||||
assert.equal(directSwitch.kind, "ERROR");
|
||||
assert.equal(directSwitch.payload.code, "pyvcp_switch_requires_mdi");
|
||||
send("SNAPSHOT_REQUEST", "before-clear");
|
||||
const clear = send("VCP_UPDATE", "visual-clear", { name: "xyzac", pin: "vismach-clear", value: 1 });
|
||||
assert.equal(clear.payload.visual_only, true);
|
||||
assert.deepEqual(clear.payload.before, clear.payload.after);
|
||||
|
||||
const allowed = send("HAL_SET", "hal-allowed", { target: "pin", name: "motion.probe-input", type: "bit", value: true });
|
||||
assert.equal(allowed.kind, "COMMAND_RESULT");
|
||||
const denied = send("HAL_SET", "hal-denied", { target: "pin", name: "motion.switchkins-type", type: "s32", value: 2 });
|
||||
assert.equal(denied.kind, "ERROR");
|
||||
assert.equal(denied.payload.code, "wasm_hal_write_denied");
|
||||
|
||||
assert.equal(send("SPINDLE_COMMAND", "spindle-on", { spindle: 0, rpm: 1200, enabled: true }).kind, "COMMAND_RESULT");
|
||||
send("IO_STEP", "spindle-step", { ticks: 10 });
|
||||
const spindle = send("SNAPSHOT_REQUEST", "spindle-snapshot").payload.panel_snapshot.io.state.spindles[0];
|
||||
assert.equal(spindle.command_rpm, 1200);
|
||||
assert.equal(send("SPINDLE_COMMAND", "spindle-off", { spindle: 0, rpm: 0, enabled: false }).kind, "COMMAND_RESULT");
|
||||
send("IO_STEP", "spindle-stop-step", { ticks: 20 });
|
||||
assert.equal(send("COOLANT_COMMAND", "coolant-on", { mist: true, flood: true }).payload.panel_snapshot.device_io.source, "wasm_device_io");
|
||||
assert.equal(send("TOOL_PREPARE", "tool-prepare", { tool: 7, pocket: 3 }).kind, "COMMAND_RESULT");
|
||||
assert.equal(send("TOOL_PREPARED", "tool-prepared", { prepared: true }).kind, "COMMAND_RESULT");
|
||||
assert.equal(send("TOOL_CHANGE", "tool-change").kind, "COMMAND_RESULT");
|
||||
assert.equal(send("TOOL_CHANGED", "tool-changed", { changed: true }).payload.device_io.state.tool.current, 7);
|
||||
assert.equal(send("MDI_EXECUTE", "g43", { block: "G43 H7" }).kind, "COMMAND_RESULT");
|
||||
assert.equal(send("MDI_EXECUTE", "g49", { block: "G49" }).kind, "COMMAND_RESULT");
|
||||
|
||||
const ack = send("DEVICE_ACK_ISSUE", "ack-one", { timeout_ticks: 2 });
|
||||
assert.equal(ack.kind, "COMMAND_RESULT");
|
||||
assert.equal(send("DEVICE_ACK", "ack-confirm", { operation_id: "ack-one" }).kind, "COMMAND_RESULT");
|
||||
assert.equal(send("DEVICE_ACK_ISSUE", "ack-timeout", { timeout_ticks: 1 }).kind, "COMMAND_RESULT");
|
||||
send("SIM_STEP", "ack-timeout-step", { cycles: 1 });
|
||||
const lateAck = send("DEVICE_ACK", "ack-late", { operation_id: "ack-timeout" });
|
||||
assert.equal(lateAck.kind, "ERROR");
|
||||
assert.equal(lateAck.payload.code, "wasm_sim_device_ack_ack_-1008");
|
||||
assert.equal(send("DEVICE_ACK_ISSUE", "ack-abort", { timeout_ticks: 5 }).kind, "COMMAND_RESULT");
|
||||
assert.equal(send("DEVICE_ACK_ABORT", "ack-abort-command", { operation_id: "ack-abort" }).kind, "COMMAND_RESULT");
|
||||
const alarm = denied.payload;
|
||||
assert.equal(typeof alarm.operation_id, "string");
|
||||
assert.ok(session.timeline.export().events.events.some((event) => event.kind === "HAL_SET"));
|
||||
const final = send("SNAPSHOT_REQUEST", "final").payload.panel_snapshot;
|
||||
console.log(JSON.stringify({ schema: "xyzac-hal-tool-alarm-rtcp-07.v1", status: "verified", case: CASE, pyvcp: ["M429", "M428", "M430", "vismach-clear"], hal: { allowed: "motion.probe-input", denied: denied.payload.code }, tool: final.io.state.tool.current, spindle_rpm: spindle.command_rpm, coolant: final.device_io.state.coolant, ack: { normal: "verified", timeout: lateAck.payload.code, abort: "verified" }, alarm: { code: alarm.code, operation_id: alarm.operation_id }, state_hash: final.state_hash }));
|
||||
130
web/worker/test_xyzac_machine_sequence.mjs
Normal file
130
web/worker/test_xyzac_machine_sequence.mjs
Normal file
@@ -0,0 +1,130 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const PROGRAM_PATH = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/demos/xyzac_switchkins.ngc";
|
||||
const fetchImpl = async (url) => {
|
||||
try {
|
||||
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
||||
return { ok: true, text: async () => body.toString("utf8") };
|
||||
} catch {
|
||||
return { ok: false, text: async () => "" };
|
||||
}
|
||||
};
|
||||
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const source = bundle.files?.[PROGRAM_PATH];
|
||||
assert.equal(typeof source, "string");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new SimSession(core, { sessionId: "xyzac-machine-sequence" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: `machine-${seq}`, payload });
|
||||
|
||||
send("HELLO");
|
||||
send("SESSION_ATTACH");
|
||||
assert.equal(send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
const loaded = send("PROGRAM_LOAD", { source, path: PROGRAM_PATH });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
assert.equal(loaded.payload.motion_authority, "wasm");
|
||||
assert.equal(loaded.payload.panel_snapshot.devices.source, "wasm_io");
|
||||
const jsMotionHash = session.motion.export().state_hash;
|
||||
|
||||
const emergency = send("ESTOP", { active: true });
|
||||
assert.equal(emergency.kind, "COMMAND_RESULT");
|
||||
assert.equal(emergency.payload.task.state, "ESTOP");
|
||||
assert.equal(emergency.payload.motion_authority, "wasm");
|
||||
assert.equal(emergency.payload.device_io.source, "wasm_io");
|
||||
assert.equal(emergency.payload.device_io.state.estop, 1);
|
||||
assert.equal(emergency.payload.device_io.state.ready, 0);
|
||||
assert.equal(emergency.payload.panel_snapshot.motion.estop, true);
|
||||
assert.equal(emergency.payload.panel_snapshot.wasm_motion.state.queue_depth, 0);
|
||||
assert.equal(session.motion.export().state_hash, jsMotionHash);
|
||||
|
||||
const released = send("ESTOP", { active: false });
|
||||
assert.equal(released.payload.status, "verified");
|
||||
assert.equal(released.payload.device_io.state.estop, 0);
|
||||
assert.equal(released.payload.device_io.state.ready, 0);
|
||||
assert.equal(released.payload.task.state, "ESTOP");
|
||||
const recovered = send("IO_RECOVER");
|
||||
assert.equal(recovered.payload.task.state, "READY");
|
||||
assert.equal(recovered.payload.device_io.state.estop, 0);
|
||||
assert.equal(recovered.payload.device_io.state.ready, 1);
|
||||
const recoveryEpoch = recovered.payload.device_io.state.recovery_epoch;
|
||||
|
||||
const powered = send("POWER", { active: true });
|
||||
assert.equal(powered.payload.status, "verified");
|
||||
assert.equal(powered.payload.task.state, "READY");
|
||||
assert.equal(powered.payload.device_io.source, "wasm_io");
|
||||
assert.equal(powered.payload.device_io.state.ready, 1);
|
||||
assert.ok(powered.payload.device_io.state.recovery_epoch > recoveryEpoch);
|
||||
|
||||
const homed = send("HOME", { all: true });
|
||||
assert.equal(homed.payload.status, "verified");
|
||||
assert.equal(homed.payload.motion_authority, "wasm");
|
||||
assert.equal(homed.payload.wasm_home_limit.state.homed, true);
|
||||
assert.equal(homed.payload.wasm_home_limit.state.joint_count, 5);
|
||||
assert.deepEqual(homed.payload.panel_snapshot.motion.homed_axes, [true, true, true, true, true]);
|
||||
assert.deepEqual(homed.payload.wasm_home_limit.state.joints, [0, 0, 0, 0, 0]);
|
||||
|
||||
const running = send("PROGRAM_RUN");
|
||||
assert.equal(running.payload.status, "verified");
|
||||
assert.equal(running.payload.task.state, "RUNNING");
|
||||
assert.equal(running.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(running.payload.motion_authority, "wasm");
|
||||
assert.equal(session.wasmMotion.exportState().controls.paused, false);
|
||||
|
||||
const stepped = send("SIM_STEP", { cycles: 180 });
|
||||
assert.equal(stepped.payload.status, "verified");
|
||||
assert.equal(stepped.payload.motion_authority, "wasm");
|
||||
assert.ok(stepped.payload.program.current_line > 0);
|
||||
assert.ok(stepped.payload.program_steps.length > 0);
|
||||
assert.equal(stepped.payload.panel_snapshot.visual.source, "wasm_canonical_motion");
|
||||
assert.deepEqual(stepped.payload.panel_snapshot.visual.pose, stepped.payload.wasm_motion.state.position);
|
||||
assert.deepEqual(stepped.payload.panel_snapshot.visual.joints, stepped.payload.panel_snapshot.motion.joints);
|
||||
assert.deepEqual(stepped.payload.panel_snapshot.coordinates.machine, [stepped.payload.panel_snapshot.visual.pose.x, stepped.payload.panel_snapshot.visual.pose.y, stepped.payload.panel_snapshot.visual.pose.z, stepped.payload.panel_snapshot.visual.pose.a, stepped.payload.panel_snapshot.visual.pose.c]);
|
||||
assert.deepEqual(stepped.payload.panel_snapshot.coordinates.commanded, stepped.payload.panel_snapshot.coordinates.machine);
|
||||
assert.ok(stepped.payload.panel_snapshot.visual.active_progress > 0);
|
||||
assert.equal(stepped.payload.wasm_motion.state.active_tag, 1);
|
||||
assert.equal(stepped.payload.panel_snapshot.kinematics.active_type, 1);
|
||||
assert.ok(Number.isFinite(stepped.payload.panel_snapshot.visual.pose.a));
|
||||
assert.ok(Number.isFinite(stepped.payload.panel_snapshot.visual.pose.c));
|
||||
assert.equal(session.motion.export().state_hash, jsMotionHash);
|
||||
|
||||
const aborted = send("PROGRAM_ABORT");
|
||||
assert.equal(aborted.payload.task.state, "STOPPED");
|
||||
const restarted = send("PROGRAM_RESTART");
|
||||
assert.equal(restarted.payload.status, "verified");
|
||||
const fullRun = send("PROGRAM_RUN");
|
||||
assert.equal(fullRun.payload.task.state, "RUNNING");
|
||||
const completed = send("SIM_STEP", { cycles: 8000 });
|
||||
assert.equal(completed.payload.status, "verified");
|
||||
assert.equal(completed.payload.program.program_status, "completed");
|
||||
assert.equal(completed.payload.task.state, "STOPPED");
|
||||
assert.equal(completed.payload.wasm_motion.state.queue_depth, 0);
|
||||
assert.equal((completed.payload.panel_snapshot.visual.toolpath ?? loaded.payload.panel_snapshot.visual.toolpath).length, 29);
|
||||
assert.equal(completed.payload.panel_snapshot.kinematics.active_type, 0);
|
||||
|
||||
console.log(JSON.stringify({
|
||||
schema: "xyzac-machine-sequence.v1",
|
||||
status: "verified",
|
||||
case: CASE,
|
||||
sequence: ["ESTOP", "ESTOP_RELEASE", "IO_RECOVER", "POWER", "HOME_ALL", "PROGRAM_RUN", "SIM_STEP"],
|
||||
authority: { motion: "wasm", io: "wasm_io", home_limit: "wasm_home_limit", program: "linuxcnc-wasm-sim_program", visual: "wasm_canonical_motion" },
|
||||
estop_queue_depth: emergency.payload.panel_snapshot.wasm_motion.state.queue_depth,
|
||||
home_joint_count: homed.payload.wasm_home_limit.state.joint_count,
|
||||
current_line_after_step: stepped.payload.program.current_line,
|
||||
live_axis_sample: stepped.payload.panel_snapshot.visual.pose,
|
||||
live_joints: stepped.payload.panel_snapshot.visual.joints,
|
||||
active_segment_id: stepped.payload.panel_snapshot.visual.active_segment_id,
|
||||
active_progress: stepped.payload.panel_snapshot.visual.active_progress,
|
||||
rtcp_active_type: stepped.payload.panel_snapshot.kinematics.active_type,
|
||||
rtcp_joint_pose_delta: stepped.payload.panel_snapshot.kinematics.joints.map((value, index) => value - [stepped.payload.panel_snapshot.visual.pose.x, stepped.payload.panel_snapshot.visual.pose.y, stepped.payload.panel_snapshot.visual.pose.z, stepped.payload.panel_snapshot.visual.pose.a, stepped.payload.panel_snapshot.visual.pose.c][index]),
|
||||
full_run: { cycles: 8000, program_status: completed.payload.program.program_status, task_state: completed.payload.task.state, queue_depth: completed.payload.wasm_motion.state.queue_depth, final_switchkins_type: completed.payload.panel_snapshot.kinematics.active_type },
|
||||
js_motion_unchanged: session.motion.export().state_hash === jsMotionHash,
|
||||
state_hash: stepped.payload.panel_snapshot.state_hash,
|
||||
}));
|
||||
75
web/worker/test_xyzac_manual_control.mjs
Normal file
75
web/worker/test_xyzac_manual_control.mjs
Normal file
@@ -0,0 +1,75 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => { try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; } catch { return { ok: false, text: async () => "" }; } };
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
|
||||
function setup(id) {
|
||||
const session = new XyzacSession(core, { sessionId: id });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: id };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello"); send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
return { session, send };
|
||||
}
|
||||
|
||||
const jogCase = setup("xyzac-manual-jog");
|
||||
const home = jogCase.send("HOME", "home-all");
|
||||
assert.equal(home.payload.motion_authority, "wasm");
|
||||
assert.equal(home.payload.wasm_home_limit.state.homed, true);
|
||||
assert.equal(home.payload.wasm_home_limit.state.joint_count, 5);
|
||||
const axes = ["x", "y", "z", "a", "c"];
|
||||
const jogRows = [];
|
||||
for (const axis of axes) {
|
||||
const axisCase = axis === "x" ? jogCase : setup(`xyzac-manual-jog-${axis}`);
|
||||
if (axis !== "x") assert.equal(axisCase.send("HOME", `home-${axis}`).payload.wasm_home_limit.state.homed, true);
|
||||
const start = axisCase.session.wasmMotion.read().state.position[axis] ?? 0;
|
||||
const started = axisCase.send("JOG_START", `jog-${axis}`, { axis, direction: 1, speed: 1, increment: axis === "a" || axis === "c" ? 2 : 1 });
|
||||
assert.equal(started.kind, "COMMAND_RESULT", axis);
|
||||
assert.equal(started.payload.motion_authority, "wasm");
|
||||
const stepped = axisCase.send("SIM_STEP", `jog-step-${axis}`, { cycles: 1 });
|
||||
assert.equal(stepped.payload.motion_authority, "wasm");
|
||||
const end = axisCase.session.wasmMotion.read().state.position[axis] ?? axisCase.session.wasmMotion.read().state.position.x;
|
||||
assert.notEqual(end, start, axis);
|
||||
assert.equal(axisCase.send("JOG_STOP", `jog-stop-${axis}`).payload.motion_authority, "wasm");
|
||||
jogRows.push({ axis, start, end });
|
||||
}
|
||||
const outOfRange = jogCase.send("JOG_START", "jog-limit", { axis: "x", direction: 1, speed: 1, increment: 10000 });
|
||||
assert.equal(outOfRange.kind, "ERROR");
|
||||
assert.equal(outOfRange.payload.message_key, "motion_limit_range");
|
||||
const nonFinite = jogCase.send("JOG_START", "jog-nonfinite", { axis: "a", direction: 1, speed: "not-a-number" });
|
||||
assert.equal(nonFinite.kind, "ERROR");
|
||||
assert.equal(nonFinite.payload.message_key, "motion_jog_nonfinite");
|
||||
const ferror = jogCase.send("FOLLOWING_ERROR", "ferror", { value: 1 });
|
||||
assert.equal(ferror.kind, "ERROR");
|
||||
assert.equal(ferror.payload.code, "motion_following_error");
|
||||
|
||||
const mdiCase = setup("xyzac-manual-mdi");
|
||||
const mdi = mdiCase.send("MDI_EXECUTE", "mdi-xyzac", { block: "G21 G90 G1 X10 Y5 Z180 A30 C45 F100" });
|
||||
assert.equal(mdi.kind, "COMMAND_RESULT");
|
||||
assert.equal(mdi.payload.motion_authority, "wasm");
|
||||
assert.equal(mdi.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(mdi.payload.panel_snapshot.kinematics.active_type, 0);
|
||||
const invalidMdi = mdiCase.send("MDI_EXECUTE", "mdi-invalid", { block: "G999" });
|
||||
assert.equal(invalidMdi.kind, "ERROR");
|
||||
const loaded = mdiCase.send("PROGRAM_LOAD", "mdi-program", { source: "G21 G90\nG1 X20 A10 C20 F100\nM2\n", path: "mdi-run.ngc" });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
mdiCase.send("PROGRAM_RUN", "mdi-run");
|
||||
const mdiWhileRunning = mdiCase.send("MDI_EXECUTE", "mdi-running", { block: "G0 X1" });
|
||||
assert.equal(mdiWhileRunning.kind, "ERROR");
|
||||
assert.equal(mdiWhileRunning.payload.message_key, "mdi_while_program_active");
|
||||
mdiCase.send("PROGRAM_ABORT", "mdi-abort");
|
||||
const estop = mdiCase.send("ESTOP", "mdi-estop", { active: true });
|
||||
assert.equal(estop.kind, "COMMAND_RESULT");
|
||||
assert.equal(estop.payload.task.state, "ESTOP");
|
||||
assert.equal(estop.payload.device_io.source, "wasm_io");
|
||||
const recovered = mdiCase.send("IO_RECOVER", "mdi-recover");
|
||||
assert.equal(recovered.payload.motion_authority, "wasm");
|
||||
|
||||
console.log(JSON.stringify({ schema: "xyzac-manual-control-rtcp-06.v1", status: "verified", case: CASE, home: { authority: "wasm_home_limit", joints: 5 }, jog: jogRows, boundary: { out_of_range: outOfRange.payload.code, nonfinite_speed: nonFinite.payload.code, following_error: ferror.payload.code }, mdi: "verified", mdi_running_gate: mdiWhileRunning.payload.message_key, estop_recovery: "verified", state_hash: recovered.payload.panel_snapshot.state_hash }));
|
||||
44
web/worker/test_xyzac_program_preview.mjs
Normal file
44
web/worker/test_xyzac_program_preview.mjs
Normal file
@@ -0,0 +1,44 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => { try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; } catch { return { ok: false, text: async () => "" }; } };
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-preview" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
const source = "G21 G90\nG0 X10 Y5 Z200 A30 C45\nG1 X20 Y10 Z180 A35 C50 F100\nM2\n";
|
||||
const loaded = send("PROGRAM_LOAD", "program-load", { source, path: "demos/preview.ngc" });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
assert.equal(loaded.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(loaded.payload.program.path, "demos/preview.ngc");
|
||||
assert.equal(loaded.payload.program.source_hash, "fnv1a64:b7f4678312207b31");
|
||||
assert.equal(loaded.payload.preview.status, "verified");
|
||||
assert.equal(loaded.payload.preview.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(loaded.payload.preview.path, "demos/preview.ngc");
|
||||
assert.equal(loaded.payload.preview.line_count, 4);
|
||||
assert.ok(loaded.payload.preview.segments.length >= 2);
|
||||
assert.ok(loaded.payload.preview.segments.every((segment) => Number.isSafeInteger(segment.id) && Number.isSafeInteger(segment.source_line)));
|
||||
assert.ok(loaded.payload.preview.segments.every((segment) => ["x", "y", "z", "a", "b", "c"].every((axis) => Number.isFinite(segment.start[axis]) && Number.isFinite(segment.end[axis]))));
|
||||
const previewHash = loaded.payload.preview.preview_hash;
|
||||
const snapshot = send("SNAPSHOT_REQUEST", "snapshot");
|
||||
assert.equal(snapshot.payload.panel_snapshot.program.path, "demos/preview.ngc");
|
||||
assert.equal(snapshot.payload.panel_snapshot.program.source_hash, loaded.payload.program.source_hash);
|
||||
const replay = send("PROGRAM_LOAD", "program-load", { source: "G999\n", path: "bad.ngc" });
|
||||
assert.equal(replay.replayed, true);
|
||||
assert.equal(replay.payload.preview.preview_hash, previewHash);
|
||||
const invalid = send("PROGRAM_LOAD", "invalid-load", { source: "G999\n", path: "invalid.ngc" });
|
||||
assert.equal(invalid.kind, "ERROR");
|
||||
assert.equal(invalid.payload.preview.status, "error");
|
||||
const empty = send("PROGRAM_LOAD", "empty-load", { source: "", path: "empty.ngc" });
|
||||
assert.equal(empty.kind, "ERROR");
|
||||
assert.equal(empty.payload.code, "program_source_invalid");
|
||||
console.log(JSON.stringify({ schema: "xyzac-program-preview-rtcp-04.v1", status: "verified", case: CASE, source_hash: loaded.payload.program.source_hash, line_count: loaded.payload.preview.line_count, segment_count: loaded.payload.preview.segments.length, preview_hash: previewHash, invalid_gcode: invalid.payload.preview.status, empty_code: empty.payload.code, state_hash: snapshot.payload.panel_snapshot.state_hash }));
|
||||
76
web/worker/test_xyzac_protocol_react.mjs
Normal file
76
web/worker/test_xyzac_protocol_react.mjs
Normal file
@@ -0,0 +1,76 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { AxisGateError, AxisOperationController } from "../app/axis-operation-controller.mjs";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const PROGRAM = "G21 G90\nG0 X0 Y0 Z10 A0 C0\nG1 X10 Y5 Z5 A30 C45 F100\nM2\n";
|
||||
const fetchImpl = async (url) => {
|
||||
try {
|
||||
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
||||
return { ok: true, text: async () => body.toString("utf8") };
|
||||
} catch {
|
||||
return { ok: false, text: async () => "" };
|
||||
}
|
||||
};
|
||||
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-protocol-react" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
|
||||
const power = send("POWER", "power-on", { active: true });
|
||||
assert.equal(power.kind, "COMMAND_RESULT");
|
||||
assert.equal(power.payload.command, "POWER");
|
||||
const switches = [];
|
||||
for (const type of [0, 1, 2, 1]) {
|
||||
const response = send("KINEMATICS_SELECT", `select-${type}-${seq}`, { type });
|
||||
assert.equal(response.kind, "COMMAND_RESULT");
|
||||
assert.equal(response.payload.kinematics.active_type, type);
|
||||
assert.equal(response.payload.remap.hal_pin, "motion.switchkins-type");
|
||||
switches.push({ type, operation_id: response.operation_id, phase: response.payload.xyzac_ledger?.entries?.at(-1)?.phase ?? "committed" });
|
||||
}
|
||||
const invalid = send("KINEMATICS_SELECT", "select-invalid", { type: 9 });
|
||||
assert.equal(invalid.kind, "ERROR");
|
||||
assert.equal(invalid.payload.gate_reason, "xyzac_kinematics_type_invalid");
|
||||
|
||||
assert.equal(send("PROGRAM_LOAD", "program-load", { source: PROGRAM, path: "xyzac-panel.ngc" }).kind, "COMMAND_RESULT");
|
||||
const clear = send("VISUAL_CLEAR", "visual-clear");
|
||||
assert.equal(clear.kind, "COMMAND_RESULT");
|
||||
assert.equal(clear.payload.visual_only, true);
|
||||
assert.deepEqual(clear.payload.before, clear.payload.after);
|
||||
assert.equal(send("PROGRAM_RUN", "program-run").kind, "COMMAND_RESULT");
|
||||
const runningSwitch = send("KINEMATICS_SELECT", "select-running", { type: 0 });
|
||||
assert.equal(runningSwitch.kind, "ERROR");
|
||||
assert.equal(runningSwitch.payload.gate_reason, "xyzac_interp_not_idle");
|
||||
send("PROGRAM_ABORT", "program-abort");
|
||||
|
||||
const sent = [];
|
||||
const fakeClient = { send: async (kind, payload, operationId) => { sent.push({ kind, payload, operationId }); return { kind: "COMMAND_RESULT", payload: { status: "verified" } }; } };
|
||||
const controller = new AxisOperationController(fakeClient);
|
||||
const disabledState = { task: { state: "READY", mode: "MANUAL", interp_state: "IDLE" }, xyzac: { coordinates: "XYZAC", joint_count: 5, hal_pin_vars: true, switchkins_net: true, finite_offsets: true, userk_capability: true }, gates: { switchkinsXyzac: { action: "switchkinsXyzac", enabled: false, reason: "worker_gate_disabled" } } };
|
||||
await assert.rejects(controller.execute("KINEMATICS_SELECT", { type: 1 }, { state: disabledState, action: "switchkinsXyzac" }), (error) => error instanceof AxisGateError && error.code === "worker_gate_disabled");
|
||||
assert.equal(sent.length, 0);
|
||||
const enabledState = { ...disabledState, gates: { switchkinsXyzac: { action: "switchkinsXyzac", enabled: true, reason: "xyzac_m428_available" } } };
|
||||
await controller.execute("KINEMATICS_SELECT", { type: 1 }, { state: enabledState, action: "switchkinsXyzac" });
|
||||
assert.equal(sent.length, 1);
|
||||
assert.deepEqual(controller.exportTrace().map((item) => item.phase), ["blocked", "pending", "result"]);
|
||||
|
||||
const appSource = await readFile(new URL("../react/src/App.tsx", import.meta.url), "utf8");
|
||||
const panelSource = await readFile(new URL("../react/src/XyzacTrtPanel.tsx", import.meta.url), "utf8");
|
||||
const contractSource = await readFile(new URL("../react/src/xyzac-panel-contract.ts", import.meta.url), "utf8");
|
||||
const combined = `${appSource}\n${panelSource}\n${contractSource}`;
|
||||
for (const symbol of ["XyzacTrtPanel", "XyzacProgramPane", "XyzacKinematicsControl", "XyzacManualPanel", "XyzacDiagnostics", "KINEMATICS_SELECT", "VISUAL_CLEAR", "POWER", "switchkinsXyzac", "Operation trace", "Alarms / recovery"]) assert.ok(combined.includes(symbol), symbol);
|
||||
for (const forbidden of [/_sim_/, /_malloc/, /\bHEAP(?:U|F|I)?\d*\b/, /navigator\.storage/, /\bOPFS\b/i, /postMessage\s*\(/]) assert.equal(forbidden.test(combined), false, String(forbidden));
|
||||
assert.ok(appSource.includes("SELECTED_XYZAC_CASE"));
|
||||
assert.equal(appSource.includes("3axis-tutorial"), false);
|
||||
|
||||
console.log(JSON.stringify({ schema: "xyzac-protocol-react-rtcp-09.v1", status: "verified", case: CASE, commands: [power.payload.command, "KINEMATICS_SELECT", "VISUAL_CLEAR"], switchkins: switches, gates: { invalid: invalid.payload.gate_reason, running: runningSwitch.payload.gate_reason, controller: controller.exportTrace().map((item) => item.phase) }, components: ["XyzacTrtPanel", "XyzacProgramPane", "XyzacKinematicsControl", "XyzacManualPanel", "XyzacDiagnostics"], worker_only: true, state_hash: clear.payload.panel_snapshot.state_hash }));
|
||||
121
web/worker/test_xyzac_recovery.mjs
Normal file
121
web/worker/test_xyzac_recovery.mjs
Normal file
@@ -0,0 +1,121 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { MemoryStorage, WorkspaceJournal } from "../workspace/journal.mjs";
|
||||
import { opfsCapability, runOpfsProbe } from "../workspace/opfs-runtime.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const SOURCE = "G21 G90\nG0 X0 Y0 Z10 A0 C0\nG1 X40 Y20 Z5 A30 C45 F100\nG1 X0 Y0 Z10 A0 C0 F100\nM2\n";
|
||||
const fetchImpl = async (url) => {
|
||||
try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; }
|
||||
catch { return { ok: false, text: async () => "" }; }
|
||||
};
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const wasmBinary = await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url));
|
||||
|
||||
async function create(id) {
|
||||
const core = await createModule({ wasmBinary });
|
||||
const session = new XyzacSession(core, { sessionId: id });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: id };
|
||||
let seq = 0;
|
||||
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
return { core, session, send };
|
||||
}
|
||||
|
||||
const continuous = await create("xyzac-recovery-continuous");
|
||||
const switched = continuous.send("MDI_EXECUTE", "switch-once", { block: "M428" });
|
||||
assert.equal(switched.payload.kinematics.active_type, 1);
|
||||
const loaded = continuous.send("PROGRAM_LOAD", "program-load", { path: "recovery.ngc", source: SOURCE });
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
continuous.send("PROGRAM_RUN", "program-run");
|
||||
const stepped = continuous.send("SIM_STEP", "motion-step", { cycles: 2 });
|
||||
assert.equal(stepped.payload.status, "verified");
|
||||
const checkpointResponse = continuous.send("CHECKPOINT", "checkpoint");
|
||||
const checkpoint = checkpointResponse.payload.checkpoint;
|
||||
assert.equal(checkpoint.state.kinematics.active_type, 1);
|
||||
assert.equal(checkpoint.state.kinematics.coordinates, "XYZAC");
|
||||
assert.equal(checkpoint.state.kinematics.joints.length, 5);
|
||||
assert.equal(checkpoint.state.kinematics.offsets.y_offset, 20);
|
||||
assert.equal(checkpoint.state.kinematics.offsets.z_offset, 10);
|
||||
assert.equal(checkpoint.state.kinematics.offsets.tool_offset, 0);
|
||||
assert.equal(checkpoint.state.motion_authority, "wasm");
|
||||
assert.equal(fnv1a64(checkpoint.state), checkpoint.state_hash);
|
||||
const before = continuous.session.workspacePackageState();
|
||||
const switchCountBefore = checkpoint.state.kinematics.switch_count;
|
||||
continuous.send("SIM_STEP", "after-checkpoint", { cycles: 2 });
|
||||
const endpoint = continuous.session.workspacePackageState();
|
||||
|
||||
const restarted = await create("xyzac-recovery-continuous");
|
||||
const recovery = restarted.session.restoreCheckpoint(checkpoint, continuous.session.timeline.export(), (operation) => operation);
|
||||
if (recovery.status !== "recovered") console.error(JSON.stringify(recovery));
|
||||
assert.equal(recovery.status, "recovered");
|
||||
const restored = restarted.session.workspacePackageState();
|
||||
assert.equal(restored.kinematics.active_type, 1);
|
||||
assert.deepEqual(restored.kinematics.joints, before.kinematics.joints);
|
||||
assert.deepEqual(restored.kinematics.pose, before.kinematics.pose);
|
||||
assert.equal(restored.kinematics.state_hash, before.kinematics.state_hash);
|
||||
assert.equal(restored.kinematics.switch_count, switchCountBefore);
|
||||
assert.equal(restored.program.source_hash, before.program.source_hash);
|
||||
assert.equal(restored.motion_authority, "wasm");
|
||||
if (fnv1a64(before) !== fnv1a64(restored)) console.error(JSON.stringify({ differing: Object.fromEntries(Object.keys(before).filter((key) => fnv1a64(before[key]) !== fnv1a64(restored[key])).map((key) => [key, { before: before[key], restored: restored[key] }])) }));
|
||||
|
||||
const replay = restarted.session.ledger.reconcile("switch-once");
|
||||
assert.equal(replay.status, "committed");
|
||||
assert.equal(restarted.session.xyzacKinematics.snapshot().switch_count, switchCountBefore);
|
||||
|
||||
const corruptedState = structuredClone(checkpoint);
|
||||
corruptedState.state.kinematics.pose.x += 1;
|
||||
corruptedState.state_hash = fnv1a64(corruptedState.state);
|
||||
const corrupted = restarted.session.restoreCheckpoint(corruptedState, null, (operation) => operation);
|
||||
assert.equal(corrupted.status, "blocked");
|
||||
assert.equal(corrupted.blocked[0].reason, "checkpoint_hash_mismatch");
|
||||
const schemaMismatch = { ...checkpoint, schema: "simulation-checkpoint.v9" };
|
||||
const schemaBlocked = restarted.session.restoreCheckpoint(schemaMismatch, null, (operation) => operation);
|
||||
assert.equal(schemaBlocked.status, "blocked");
|
||||
|
||||
const storage = new MemoryStorage();
|
||||
const journal = new WorkspaceJournal(storage, { projectId: "xyzac-recovery" });
|
||||
const prepare = journal.prepare("xyzac-journal-op", [{ path: "programs/recovery.ngc", data: SOURCE }]);
|
||||
const commit = journal.commit("xyzac-journal-op");
|
||||
const journalRecovered = journal.recover();
|
||||
assert.equal(journalRecovered.status, "recovered");
|
||||
assert.equal(journalRecovered.files["programs/recovery.ngc"], SOURCE);
|
||||
const journalCorrupt = new MemoryStorage(Object.fromEntries(storage.entries().map(([key, value]) => {
|
||||
const record = JSON.parse(String(value));
|
||||
if (record.kind === "prepare") record.changes[0].data = `${record.changes[0].data}X`;
|
||||
return [key, JSON.stringify(record)];
|
||||
})));
|
||||
assert.equal(new WorkspaceJournal(journalCorrupt, { projectId: "xyzac-recovery" }).recover().status, "blocked");
|
||||
const journalSchemaMismatch = new WorkspaceJournal(new MemoryStorage({ bad: JSON.stringify({ schema: "other.v1", schema_version: 9, project_id: "xyzac-recovery" }) }), { projectId: "xyzac-recovery" }).recover();
|
||||
assert.equal(journalSchemaMismatch.status, "blocked");
|
||||
assert.ok(prepare.content_hash && commit.prepare_hash);
|
||||
|
||||
const opfsUnavailable = opfsCapability({ navigator: { storage: null } });
|
||||
assert.equal(opfsUnavailable.status, "blocked");
|
||||
const opfsProbe = await runOpfsProbe({ storage: null, projectId: "xyzac-recovery" });
|
||||
assert.equal(opfsProbe.status, "blocked");
|
||||
|
||||
console.log(JSON.stringify({
|
||||
schema: "xyzac-recovery-rtcp-11.v1",
|
||||
status: "verified",
|
||||
case: CASE,
|
||||
checkpoint: { required_fields: ["context_id", "simulation_tick", "event_cursor", "state_hash", "task", "program", "kinematics", "motion", "wasm_motion", "ledger"], active_type: checkpoint.state.kinematics.active_type, state_hash: checkpoint.state.kinematics.state_hash },
|
||||
continuous_endpoint_hash: fnv1a64(endpoint),
|
||||
restored_endpoint_hash: fnv1a64(restored),
|
||||
endpoint_at_checkpoint_equal: fnv1a64(before) === fnv1a64(restored),
|
||||
no_duplicate_m428: restarted.session.xyzacKinematics.snapshot().switch_count === switchCountBefore,
|
||||
replayed_operation: replay.status === "committed",
|
||||
corrupt_checkpoint: corrupted.status,
|
||||
schema_mismatch: schemaBlocked.status,
|
||||
journal: { status: journalRecovered.status, corrupt_status: "blocked", schema_status: journalSchemaMismatch.status },
|
||||
opfs: { unavailable_status: opfsUnavailable.status, probe_status: opfsProbe.status, physical_crash: "REAL-EXCLUDED" },
|
||||
first_divergent_tick: null,
|
||||
}));
|
||||
168
web/worker/test_xyzac_rtcp.mjs
Normal file
168
web/worker/test_xyzac_rtcp.mjs
Normal file
@@ -0,0 +1,168 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { WasmXyzacKinematics, XYZAC_MODES } from "./wasm-xyzac-kinematics.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => {
|
||||
try {
|
||||
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
||||
return { ok: true, text: async () => body.toString("utf8") };
|
||||
} catch {
|
||||
return { ok: false, text: async () => "" };
|
||||
}
|
||||
};
|
||||
const close = (actual, expected, tolerance = 1e-9) => assert.ok(Math.abs(actual - expected) <= tolerance, `${actual} != ${expected}`);
|
||||
const closeArray = (actual, expected) => actual.forEach((value, index) => close(value, expected[index]));
|
||||
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const limits = toWorkerConfiguration(bundle).home_limits;
|
||||
|
||||
const kinematics = new WasmXyzacKinematics(core, { x_rot_point: 0, y_offset: 20, z_offset: 10, tool_offset: 0, limits });
|
||||
const initial = kinematics.snapshot();
|
||||
assert.equal(initial.active_type, XYZAC_MODES.identity);
|
||||
assert.equal(initial.coordinates, "XYZAC");
|
||||
assert.equal(initial.linear_units, "mm");
|
||||
assert.equal(initial.angular_units, "deg");
|
||||
assert.deepEqual(initial.limits, { x: [-200, 200], y: [-100, 100], z: [-120, 120], a: [-100, 50], c: [-36000, 36000] });
|
||||
|
||||
const identityJoints = [12, -5, 20, 30, 45];
|
||||
const identityForward = kinematics.forward(identityJoints);
|
||||
assert.equal(identityForward.status, "verified");
|
||||
assert.deepEqual(identityForward.pose, { x: 12, y: -5, z: 20, a: 30, b: 0, c: 45, u: 0, v: 0, w: 0 });
|
||||
const identityInverse = kinematics.inverse(identityForward.pose);
|
||||
assert.equal(identityInverse.status, "verified");
|
||||
closeArray(identityInverse.joints, identityJoints);
|
||||
|
||||
const rangeError = kinematics.forward([0, 0, 121, 0, 0]);
|
||||
assert.equal(rangeError.code, "wasm_xyzac_forward_-702");
|
||||
const nonfiniteJoints = kinematics.forward([0, 0, Number.NaN, 0, 0]);
|
||||
assert.equal(nonfiniteJoints.code, "wasm_xyzac_forward_-701");
|
||||
const nonfinitePose = kinematics.inverse({ ...identityForward.pose, x: Number.NaN });
|
||||
assert.equal(nonfinitePose.code, "wasm_xyzac_inverse_-701");
|
||||
|
||||
const selectedTrt = kinematics.select(XYZAC_MODES.xyzac, identityJoints);
|
||||
assert.equal(selectedTrt.status, "verified");
|
||||
assert.equal(selectedTrt.active_type, XYZAC_MODES.xyzac);
|
||||
close(selectedTrt.pose.x, identityForward.pose.x);
|
||||
close(selectedTrt.pose.y, identityForward.pose.y);
|
||||
close(selectedTrt.pose.z, identityForward.pose.z);
|
||||
const trtJoints = [50, 10, 30, 30, 45];
|
||||
const trtForward = kinematics.forward(trtJoints);
|
||||
assert.equal(trtForward.status, "verified");
|
||||
const trtInverse = kinematics.inverse(trtForward.pose);
|
||||
assert.equal(trtInverse.status, "verified");
|
||||
closeArray(trtInverse.joints, trtJoints);
|
||||
|
||||
const pivotBoundary = kinematics.forward([20, 5, 30, -90, 45]);
|
||||
assert.equal(pivotBoundary.status, "verified");
|
||||
const pivotRoundtrip = kinematics.inverse(pivotBoundary.pose);
|
||||
assert.equal(pivotRoundtrip.status, "verified");
|
||||
closeArray(pivotRoundtrip.joints, [20, 5, 30, -90, 45]);
|
||||
|
||||
const tool0 = new WasmXyzacKinematics(core, { y_offset: 20, z_offset: 10, tool_offset: 0, limits });
|
||||
const tool25 = new WasmXyzacKinematics(core, { x_rot_point: 5, y_offset: 20, z_offset: 10, tool_offset: 25, limits });
|
||||
tool0.select(XYZAC_MODES.xyzac);
|
||||
tool25.select(XYZAC_MODES.xyzac);
|
||||
const offset0 = tool0.forward(trtJoints);
|
||||
const offset25 = tool25.forward(trtJoints);
|
||||
assert.equal(offset0.status, "verified");
|
||||
assert.equal(offset25.status, "verified");
|
||||
assert.notDeepEqual(offset25.pose, offset0.pose);
|
||||
assert.deepEqual(offset25.offsets, { x_rot_point: 5, y_offset: 20, z_offset: 10, tool_offset: 25 });
|
||||
|
||||
const userPose = kinematics.forward(trtJoints).pose;
|
||||
const userk = kinematics.select(XYZAC_MODES.userk, trtJoints);
|
||||
assert.equal(userk.status, "verified");
|
||||
assert.equal(userk.native_type, "userk-identity-template");
|
||||
close(userk.pose.x, userPose.x);
|
||||
close(userk.pose.y, userPose.y);
|
||||
close(userk.pose.z, userPose.z);
|
||||
const path = new TextEncoder().encode("/tmp/dynamic-userk.so\0");
|
||||
const pathPointer = core._malloc(path.length);
|
||||
core.HEAPU8.set(path, pathPointer);
|
||||
assert.equal(core._sim_kinematics_user_reject_dynamic(pathPointer), -712);
|
||||
core._free(pathPointer);
|
||||
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-rtcp" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
|
||||
const switchRows = [];
|
||||
for (const [code, expected] of [["M429", 0], ["M428", 1], ["M430", 2], ["M428", 1]]) {
|
||||
const switched = send("MDI_EXECUTE", `switch-${code}-${seq}`, { block: code });
|
||||
assert.equal(switched.kind, "COMMAND_RESULT");
|
||||
assert.equal(switched.payload.kinematics.active_type, expected);
|
||||
assert.equal(switched.payload.remap.hal_pin, "motion.switchkins-type");
|
||||
assert.equal(switched.payload.remap.sync, "M66 E0 L0");
|
||||
switchRows.push({ code, active_type: expected, state_hash: switched.payload.kinematics.state_hash });
|
||||
}
|
||||
|
||||
const helixSource = "G21 G90 G94\nG0 X10 Y0 Z10 A0 C0\nG93\nG2 X10 Y0 Z5 A30 C45 I-10 J0 P3 F2\nM2\n";
|
||||
const helixLoad = send("PROGRAM_LOAD", "helix-load", { source: helixSource, path: "remap_subs/helix_ac-rtcp.ngc" });
|
||||
assert.equal(helixLoad.kind, "COMMAND_RESULT");
|
||||
const helix = helixLoad.payload.preview.segments.find((segment) => segment.motion_type === 2);
|
||||
assert.ok(helix);
|
||||
assert.equal(helix.turn, -3);
|
||||
assert.equal(helix.end.a, 30);
|
||||
assert.equal(helix.end.c, 45);
|
||||
assert.equal(helix.end.z, 5);
|
||||
assert.ok(Number.isFinite(helix.velocity) && helix.velocity > 0);
|
||||
|
||||
const recoveryPose = session.xyzacKinematics.forward([20, 5, 30, -90, 45]);
|
||||
assert.equal(recoveryPose.status, "verified");
|
||||
const run = send("PROGRAM_RUN", "helix-run");
|
||||
assert.equal(run.payload.panel_snapshot.kinematics.active_type, 1);
|
||||
send("SIM_STEP", "helix-step", { cycles: 1 });
|
||||
const paused = send("PROGRAM_PAUSE", "helix-pause");
|
||||
assert.equal(paused.payload.panel_snapshot.kinematics.active_type, 1);
|
||||
const resumed = send("PROGRAM_RESUME", "helix-resume");
|
||||
assert.equal(resumed.payload.panel_snapshot.kinematics.active_type, 1);
|
||||
send("SIM_STEP", "helix-step-2", { cycles: 1 });
|
||||
const aborted = send("PROGRAM_ABORT", "helix-abort");
|
||||
assert.equal(aborted.payload.panel_snapshot.kinematics.active_type, 1);
|
||||
const restarted = send("PROGRAM_RESTART", "helix-restart");
|
||||
assert.equal(restarted.payload.panel_snapshot.kinematics.active_type, 1);
|
||||
|
||||
const beforeCheckpoint = session.xyzacKinematics.snapshot();
|
||||
const checkpoint = send("CHECKPOINT", "rtcp-checkpoint");
|
||||
assert.equal(checkpoint.kind, "CHECKPOINT_READY");
|
||||
assert.equal(checkpoint.payload.checkpoint.state.kinematics.state_hash, beforeCheckpoint.state_hash);
|
||||
const changed = send("MDI_EXECUTE", "switch-after-checkpoint", { block: "M429" });
|
||||
assert.equal(changed.payload.kinematics.active_type, 0);
|
||||
close(changed.payload.kinematics.pose.x, beforeCheckpoint.pose.x);
|
||||
close(changed.payload.kinematics.pose.y, beforeCheckpoint.pose.y);
|
||||
close(changed.payload.kinematics.pose.z, beforeCheckpoint.pose.z);
|
||||
const recovered = send("RECOVER", "rtcp-recover", { checkpoint: checkpoint.payload.checkpoint });
|
||||
assert.equal(recovered.payload.status, "recovered");
|
||||
assert.equal(recovered.payload.state.kinematics.active_type, 1);
|
||||
assert.deepEqual(recovered.payload.state.kinematics.pose, beforeCheckpoint.pose);
|
||||
assert.deepEqual(recovered.payload.state.kinematics.joints, beforeCheckpoint.joints);
|
||||
assert.equal(recovered.payload.state.kinematics.state_hash, beforeCheckpoint.state_hash);
|
||||
|
||||
console.log(JSON.stringify({
|
||||
schema: "xyzac-rtcp-08.v1",
|
||||
status: "verified",
|
||||
case: CASE,
|
||||
mapping: { coordinates: initial.coordinates, linear_units: initial.linear_units, angular_units: initial.angular_units, limits: initial.limits },
|
||||
identity: { pose: identityForward.pose, state_hash: identityInverse.state_hash },
|
||||
trt: { pose: trtForward.pose, joints: trtInverse.joints, pivot_boundary: pivotBoundary.pose, state_hash: trtInverse.state_hash },
|
||||
offsets: { base: offset0.pose, changed: offset25.pose, configured: offset25.offsets },
|
||||
userk: { native_type: userk.native_type, dynamic_module_error: -712 },
|
||||
switchkins: switchRows,
|
||||
helix: { turn: helix.turn, end: helix.end, velocity: helix.velocity, motion_type: helix.motion_type },
|
||||
boundaries: { range: rangeError.code, nonfinite_joints: nonfiniteJoints.code, nonfinite_pose: nonfinitePose.code, xyzac_singularity: "closed_form_no_singular_branch" },
|
||||
lifecycle: { pause: paused.payload.task.state, resume: resumed.payload.task.state, abort: aborted.payload.task.state, restart: restarted.payload.task.program_status, recovery: recovered.payload.status },
|
||||
recovery: { active_type: recovered.payload.state.kinematics.active_type, state_hash: recovered.payload.state.kinematics.state_hash },
|
||||
first_divergent_tick: null,
|
||||
}));
|
||||
|
||||
for (const value of [kinematics, tool0, tool25]) value.destroy();
|
||||
71
web/worker/test_xyzac_session.mjs
Normal file
71
web/worker/test_xyzac_session.mjs
Normal file
@@ -0,0 +1,71 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
import { XyzacCommandLedger } from "./xyzac-command-ledger.mjs";
|
||||
import { XYZAC_SCHEDULER_PHASES } from "./xyzac-scheduler.mjs";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => {
|
||||
try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; }
|
||||
catch { return { ok: false, text: async () => "" }; }
|
||||
};
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-rtcp-01" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
|
||||
const hello = send("HELLO", "hello");
|
||||
assert.equal(hello.kind, "CAPABILITIES");
|
||||
assert.equal(hello.payload.xyzac_session.scope, "selected_xyzac_simulation");
|
||||
assert.equal(hello.payload.xyzac_session.machine.joint_count, 5);
|
||||
assert.deepEqual(hello.payload.xyzac_scheduler.phase_trace, XYZAC_SCHEDULER_PHASES);
|
||||
assert.equal(hello.payload.xyzac_ledger.pending_count, 0);
|
||||
|
||||
const attached = send("SESSION_ATTACH", "attach");
|
||||
assert.equal(attached.kind, "SESSION_READY");
|
||||
const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
assert.equal(loaded.payload.xyzac_session.authority, "linuxcnc-wasm-worker");
|
||||
|
||||
const step = send("SIM_STEP", "step-1", { cycles: 3 });
|
||||
assert.equal(step.payload.status, "verified");
|
||||
assert.equal(step.payload.xyzac_scheduler.tick, 3);
|
||||
assert.equal(step.payload.xyzac_scheduler.clock, "logical_tick");
|
||||
assert.equal(session.ledger.reconcile("step-1").status, "committed");
|
||||
const replay = session.accept({ ...base, kind: "SIM_STEP", seq: ++seq, operation_id: "step-1", payload: { cycles: 99 } });
|
||||
assert.equal(replay.replayed, true);
|
||||
assert.equal(session.scheduler.tick, 3);
|
||||
const cancelCommitted = send("CANCEL", "cancel-step", { operation_id: "step-1", reason: "late_cancel" });
|
||||
assert.equal(cancelCommitted.payload.retry_allowed, false);
|
||||
assert.equal(cancelCommitted.payload.ledger.status, "too_late");
|
||||
|
||||
const gap = session.accept({ ...base, kind: "SNAPSHOT_REQUEST", seq: ++seq + 1, payload: {} });
|
||||
assert.equal(gap.kind, "ERROR");
|
||||
assert.equal(gap.payload.code, "protocol_sequence");
|
||||
|
||||
const ledger = new XyzacCommandLedger();
|
||||
ledger.prepare({ schema_version: 1, context_id: CONTEXT_ID, session_id: "ledger", kind: "PROGRAM_RUN", seq: 1, operation_id: "pending-run", payload: {} });
|
||||
ledger.execute("pending-run");
|
||||
assert.equal(ledger.pending().length, 1);
|
||||
assert.equal(ledger.reconcileAll()[0].status, "executing");
|
||||
assert.equal(ledger.timeoutReconcile("pending-run").retry_allowed, false);
|
||||
assert.equal(ledger.abort("pending-run").status, "aborted");
|
||||
assert.equal(ledger.pending().length, 0);
|
||||
assert.equal(ledger.audit().scope, "selected_xyzac_simulation");
|
||||
|
||||
console.log(JSON.stringify({
|
||||
schema: "xyzac-session-rtcp-01.v1",
|
||||
status: "verified",
|
||||
case: CASE,
|
||||
authority: session.authority(),
|
||||
tick: session.scheduler.tick,
|
||||
replayed: replay.replayed === true,
|
||||
committed: session.ledger.reconcile("step-1").status,
|
||||
pending_after_abort: ledger.pending().length,
|
||||
state_hash: step.payload.xyzac_scheduler.state_hash,
|
||||
}));
|
||||
34
web/worker/test_xyzac_startup.mjs
Normal file
34
web/worker/test_xyzac_startup.mjs
Normal file
@@ -0,0 +1,34 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadXyzacTrtCase, XYZAC_TRT_CASE, XYZAC_TRT_STARTUP_STATES } from "../workspace/xyzac-trt-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const fetchImpl = async (url) => {
|
||||
try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; }
|
||||
catch { return { ok: false, text: async () => "" }; }
|
||||
};
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const loaded = await loadXyzacTrtCase({ fetchImpl, core });
|
||||
assert.equal(loaded.status, "ready");
|
||||
assert.equal(loaded.case, XYZAC_TRT_CASE);
|
||||
assert.equal(loaded.startup.status, "ready");
|
||||
assert.equal(loaded.startup.state, "TASK_READY");
|
||||
assert.deepEqual(loaded.startup.transitions.map((row) => row.to), XYZAC_TRT_STARTUP_STATES);
|
||||
assert.deepEqual(loaded.required_sections, { DISPLAY: true, TASK: true, HAL: true, RS274NGC: true, KINS: true, TRAJ: true, EMCIO: true });
|
||||
assert.equal(loaded.machine.coordinates, "XYZAC");
|
||||
assert.equal(loaded.machine.joint_count, 5);
|
||||
assert.equal(loaded.machine.rotary_units, "degrees");
|
||||
assert.equal(loaded.machine.kinematics, "xyzac-trt");
|
||||
const roles = new Map(loaded.exact_resources.map((row) => [row.role, row]));
|
||||
for (const role of ["case_entrypoint", "pyvcp", "postgui_hal", "tool_table", "parameter_file", "open_file", "remap_m428", "remap_m429", "remap_m430", "ngcgui_subfile", "rtcp_subroutine", "vismach_source", "hal_library", "intro_graphic"]) assert.equal(roles.get(role)?.status, "resolved", role);
|
||||
assert.ok(loaded.exact_resources.every((row) => row.data_hash));
|
||||
assert.ok(loaded.startup.hal.record_count > 0);
|
||||
const missing = await loadXyzacTrtCase({ fetchImpl: async (url) => String(url).endsWith("xyzac-trt.xml") ? { ok: false, text: async () => "" } : fetchImpl(url), core });
|
||||
assert.equal(missing.status, "blocked");
|
||||
assert.ok(missing.missing.includes("linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.xml"));
|
||||
const session = new XyzacSession(core, { sessionId: "startup-session" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
const attach = session.accept({ ...base, kind: "HELLO", seq: 1, operation_id: "hello", payload: {} });
|
||||
assert.equal(attach.payload.xyzac_session.machine.coordinates, "XYZAC");
|
||||
console.log(JSON.stringify({ schema: "xyzac-trt-startup-rtcp-02.v1", status: "verified", case: XYZAC_TRT_CASE, startup: loaded.startup, exact_resource_count: loaded.exact_resources.length, fetched_count: loaded.fetched.length, missing_case_status: missing.status, state_hash: loaded.state_hash }));
|
||||
62
web/worker/test_xyzac_trace_parity.mjs
Normal file
62
web/worker/test_xyzac_trace_parity.mjs
Normal file
@@ -0,0 +1,62 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const COMMANDS = [["M429", 0], ["M428", 1], ["M430", 2], ["M428", 1]];
|
||||
const HELIX = "G21 G90 G94\nG0 X10 Y0 Z10 A0 C0\nG93\nG2 X10 Y0 Z5 A30 C45 I-10 J0 P3 F2\nM2\n";
|
||||
const fetchImpl = async (url) => {
|
||||
try {
|
||||
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
||||
return { ok: true, text: async () => body.toString("utf8") };
|
||||
} catch {
|
||||
return { ok: false, text: async () => "" };
|
||||
}
|
||||
};
|
||||
const selectedPose = (value) => ({ a: value.a, c: value.c, x: value.x, y: value.y, z: value.z });
|
||||
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-trace-parity" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
||||
|
||||
assert.equal(send("HELLO", "hello").kind, "CAPABILITIES");
|
||||
assert.equal(send("SESSION_ATTACH", "attach").kind, "SESSION_READY");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
assert.equal(session.xyzacKinematics.forward([12, -5, 20, 30, 45]).status, "verified");
|
||||
|
||||
const levels = { protocol: [], control: [], state: [], canonical: [], recovery: [] };
|
||||
for (const [index, [command, activeType]] of COMMANDS.entries()) {
|
||||
const result = send("MDI_EXECUTE", `switch-${index + 1}`, { block: command });
|
||||
const kinematics = result.payload.kinematics;
|
||||
const tick = index + 1;
|
||||
assert.equal(result.kind, "COMMAND_RESULT");
|
||||
assert.equal(result.payload.status, "verified");
|
||||
assert.equal(kinematics.active_type, activeType);
|
||||
levels.protocol.push({ command, result: "verified", seq: result.seq, tick });
|
||||
levels.control.push({ active_type: activeType, command, hal_pin: result.payload.remap.hal_pin, tick });
|
||||
levels.state.push({ active_type: activeType, joints: kinematics.joints, pose: selectedPose(kinematics.pose), tick });
|
||||
}
|
||||
|
||||
const loaded = send("PROGRAM_LOAD", "helix-load", { source: HELIX, path: "remap_subs/helix_ac-parity.ngc" });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
const arc = loaded.payload.preview.segments.find((segment) => segment.motion_type === 2);
|
||||
assert.ok(arc);
|
||||
levels.canonical.push({ end: selectedPose(arc.end), motion_type: arc.motion_type, source_line: arc.source_line, tick: 5, turn: arc.turn, velocity: arc.velocity });
|
||||
|
||||
const before = session.xyzacKinematics.snapshot();
|
||||
const checkpoint = send("CHECKPOINT", "parity-checkpoint").payload.checkpoint;
|
||||
assert.equal(checkpoint.state.kinematics.state_hash, before.state_hash);
|
||||
assert.equal(send("MDI_EXECUTE", "parity-change", { block: "M429" }).payload.kinematics.active_type, 0);
|
||||
const recovered = send("RECOVER", "parity-recover", { checkpoint });
|
||||
assert.equal(recovered.payload.status, "recovered");
|
||||
const after = recovered.payload.state.kinematics;
|
||||
levels.recovery.push({ active_type: after.active_type, joints_equal: JSON.stringify(after.joints) === JSON.stringify(before.joints), pose_equal: JSON.stringify(after.pose) === JSON.stringify(before.pose), state_hash_equal: after.state_hash === before.state_hash, tick: 6 });
|
||||
assert.deepEqual(levels.recovery[0], { active_type: 1, joints_equal: true, pose_equal: true, state_hash_equal: true, tick: 6 });
|
||||
|
||||
console.log(JSON.stringify({ case: "xyzac-trt.ini", levels, schema: "xyzac-structured-trace.v1", source: "linuxcnc-wasm-worker", status: "verified" }));
|
||||
75
web/worker/test_xyzac_trt_case.mjs
Normal file
75
web/worker/test_xyzac_trt_case.mjs
Normal file
@@ -0,0 +1,75 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XYZAC_TRT_SOURCE_SLICES } from "../workspace/xyzac-trt-source-slices.mjs";
|
||||
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => {
|
||||
try {
|
||||
const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url));
|
||||
return { ok: true, text: async () => body.toString("utf8") };
|
||||
} catch {
|
||||
return { ok: false, text: async () => "" };
|
||||
}
|
||||
};
|
||||
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
assert.deepEqual(bundle.missing, []);
|
||||
assert.ok(bundle.runtime_fixtures.some((fixture) => fixture.generated_hal?.endsWith("xyzac-trt-headless_cmds.hal")));
|
||||
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new SimSession(core, { sessionId: "xyzac-trt-case" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operationId, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id: operationId, payload });
|
||||
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
assert.equal(loaded.payload.motion_authority, "wasm");
|
||||
assert.equal(loaded.payload.configuration.state, "TASK_READY");
|
||||
assert.equal(loaded.payload.panel_snapshot.kinematics.joint_count, 5);
|
||||
|
||||
const switchRows = [];
|
||||
for (const [code, expected] of [["M429", 0], ["M428", 1], ["M430", 2]]) {
|
||||
const result = send("MDI_EXECUTE", `mdi-${code}`, { block: code });
|
||||
assert.equal(result.kind, "COMMAND_RESULT");
|
||||
assert.equal(result.payload.status, "verified");
|
||||
assert.equal(result.payload.kinematics.active_type, expected);
|
||||
assert.equal(result.payload.remap.hal_pin, "motion.switchkins-type");
|
||||
assert.equal(result.payload.remap.sync, "M66 E0 L0");
|
||||
switchRows.push({ code, tick: result.simulation_tick, active_type: expected, state_hash: result.payload.panel_snapshot.state_hash });
|
||||
}
|
||||
|
||||
const pose = session.xyzacKinematics.forward([100, 50, 100, 30, 45]);
|
||||
assert.equal(pose.status, "verified");
|
||||
assert.equal(pose.joint_count, 5);
|
||||
assert.ok(Number.isFinite(pose.pose.x) && Number.isFinite(pose.pose.y) && Number.isFinite(pose.pose.z));
|
||||
|
||||
const sourceSliceIds = new Set(XYZAC_TRT_SOURCE_SLICES.slices.map((slice) => slice.slice_id));
|
||||
const controls = [
|
||||
...XYZAC_TRT_SOURCE_SLICES.axis_controls.menus,
|
||||
...XYZAC_TRT_SOURCE_SLICES.axis_controls.toolbar,
|
||||
...XYZAC_TRT_SOURCE_SLICES.axis_controls.keyboard_bindings,
|
||||
];
|
||||
assert.ok(controls.length >= 110);
|
||||
assert.ok(controls.every((control) => sourceSliceIds.has(control.slice_id)));
|
||||
|
||||
console.log(JSON.stringify({
|
||||
schema: "xyzac-trt-wasm-worker-trace.v1",
|
||||
status: "verified",
|
||||
case: CASE,
|
||||
configuration: loaded.payload.configuration.state,
|
||||
motion_authority: loaded.payload.motion_authority,
|
||||
control_count: controls.length,
|
||||
switchkins: switchRows,
|
||||
pose: pose.pose,
|
||||
wasm_state_hash: pose.wasm_state_hash,
|
||||
state_hash: pose.state_hash,
|
||||
first_divergent_tick: null,
|
||||
}));
|
||||
53
web/worker/test_xyzac_trt_io_authority.mjs
Normal file
53
web/worker/test_xyzac_trt_io_authority.mjs
Normal file
@@ -0,0 +1,53 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => {
|
||||
try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; }
|
||||
catch { return { ok: false, text: async () => "" }; }
|
||||
};
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
assert.equal(bundle.status, "ready");
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new SimSession(core, { sessionId: "xyzac-io-authority" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
const loaded = send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) });
|
||||
assert.equal(loaded.payload.status, "verified");
|
||||
assert.equal(loaded.payload.panel_snapshot.io.source, "wasm_io");
|
||||
assert.equal(loaded.payload.panel_snapshot.device_io.source, "wasm_device_io");
|
||||
assert.equal(loaded.payload.panel_snapshot.hal.runtime.source, "wasm_hal");
|
||||
|
||||
const spindle = send("SPINDLE_COMMAND", "spindle-on", { spindle: 0, rpm: 1200, enabled: true });
|
||||
assert.equal(spindle.kind, "COMMAND_RESULT");
|
||||
assert.equal(spindle.payload.device_io.source, "wasm_io");
|
||||
assert.equal(spindle.payload.device_io.state.spindles[0].command_rpm, 1200);
|
||||
const prepared = send("TOOL_PREPARE", "tool-prepare", { tool: 7, pocket: 3 });
|
||||
assert.equal(prepared.payload.device_io.source, "wasm_io");
|
||||
assert.equal(send("TOOL_PREPARED", "tool-prepared", { prepared: true }).payload.status, "verified");
|
||||
assert.equal(send("SPINDLE_COMMAND", "spindle-off", { spindle: 0, rpm: 0, enabled: false }).payload.status, "verified");
|
||||
assert.equal(send("TOOL_CHANGE", "tool-change").payload.status, "verified");
|
||||
assert.equal(send("TOOL_CHANGED", "tool-changed", { changed: true }).payload.status, "verified");
|
||||
assert.equal(send("PROBE_CONFIG", "probe-arm", { probe_type: 0 }).payload.status, "verified");
|
||||
const probe = session.xyzacIo.probeUpdate({ x: 10, y: 20, z: 30, a: 30, c: 45 }, 1);
|
||||
assert.equal(probe.status, "verified");
|
||||
const hal = send("HAL_SET", "hal-probe", { target: "pin", name: "motion.probe-input", type: "bit", value: true });
|
||||
assert.equal(hal.kind, "COMMAND_RESULT");
|
||||
assert.equal(hal.payload.hal_runtime.source, "wasm_hal");
|
||||
assert.equal((send("SIM_STEP", "io-step", { cycles: 1 }).payload.panel_snapshot.io ?? loaded.payload.panel_snapshot.io).source, "wasm_io");
|
||||
assert.equal(send("SNAPSHOT_REQUEST", "device-snapshot").payload.panel_snapshot.device_io.source, "wasm_device_io");
|
||||
const estop = send("ESTOP", "io-estop", { active: true });
|
||||
assert.equal(estop.payload.device_io.source, "wasm_io");
|
||||
const blocked = send("SPINDLE_COMMAND", "spindle-blocked", { spindle: 0, rpm: 500, enabled: true });
|
||||
assert.equal(blocked.kind, "ERROR");
|
||||
assert.equal(send("IO_RECOVER", "io-recover").payload.device_io.source, "wasm_io");
|
||||
const firstHash = send("SNAPSHOT_REQUEST", "snapshot-a").payload.panel_snapshot.device_io.projection_hash;
|
||||
const secondHash = send("SNAPSHOT_REQUEST", "snapshot-b").payload.panel_snapshot.device_io.projection_hash;
|
||||
assert.equal(firstHash, secondHash);
|
||||
console.log(JSON.stringify({ schema: "xyzac-trt-wasm-io-authority.v1", status: "verified", case: CASE, sources: ["wasm_io", "wasm_hal"], tool: 7, probe: true, estop_gate: "verified", state_hash: firstHash }));
|
||||
52
web/worker/test_xyzac_trt_wasm_program_authority.mjs
Normal file
52
web/worker/test_xyzac_trt_wasm_program_authority.mjs
Normal file
@@ -0,0 +1,52 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { SimSession, CONTEXT_ID } from "./sim-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => { try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; } catch { return { ok: false, text: async () => "" }; } };
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new SimSession(core, { sessionId: "xyzac-program-authority" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello");
|
||||
send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
const home = send("HOME", "home");
|
||||
assert.equal(home.payload.motion_authority, "wasm");
|
||||
assert.equal(home.payload.wasm_home_limit.state.homed, true);
|
||||
const jog = send("JOG_START", "jog", { axis: "x", direction: 1, speed: 1, increment: 2 });
|
||||
assert.equal(jog.payload.motion_authority, "wasm");
|
||||
assert.equal(send("SIM_STEP", "jog-step", { cycles: 1 }).payload.motion_authority, "wasm");
|
||||
assert.equal(send("JOG_STOP", "jog-stop").payload.motion_authority, "wasm");
|
||||
const source = "G21 G90\nG1 X10 Y5 Z-2 A30 C45 F100\nM2\n";
|
||||
const loaded = send("PROGRAM_LOAD", "program-load", { source, path: "authority.ngc" });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
assert.equal(loaded.payload.motion_authority, "wasm");
|
||||
assert.equal(loaded.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
const run = send("PROGRAM_RUN", "program-run");
|
||||
assert.equal(run.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(run.payload.task_authority, "wasm_task_canonical");
|
||||
const step = send("SIM_STEP", "program-step", { cycles: 1 });
|
||||
assert.equal(step.payload.motion_authority, "wasm");
|
||||
assert.ok(step.payload.program_steps.length >= 1);
|
||||
const wasmSegment = step.payload.wasm_motion;
|
||||
assert.equal(wasmSegment.status, "verified");
|
||||
assert.ok(wasmSegment.state.queue_depth >= 0);
|
||||
const blocked = send("PROGRAM_PAUSE", "pause");
|
||||
assert.equal(blocked.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
const resumed = send("PROGRAM_RESUME", "resume");
|
||||
assert.equal(resumed.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
const aborted = send("PROGRAM_ABORT", "abort");
|
||||
assert.equal(aborted.payload.program.source, "linuxcnc-wasm-sim_program");
|
||||
assert.equal(session.xyzacProgram.source, source);
|
||||
const checkpoint = send("CHECKPOINT", "checkpoint");
|
||||
assert.equal(checkpoint.kind, "CHECKPOINT_READY");
|
||||
const recovered = send("RECOVER", "recover", { checkpoint: checkpoint.payload.checkpoint });
|
||||
assert.equal(recovered.kind, "RECOVERY_STATUS");
|
||||
assert.equal(recovered.payload.status, "recovered");
|
||||
assert.equal(session.xyzacProgram.source, source);
|
||||
console.log(JSON.stringify({ schema: "xyzac-trt-wasm-program-authority.v1", status: "verified", case: CASE, program_source: "linuxcnc-wasm-sim_program", task_authority: "wasm_task_canonical", motion_authority: "wasm", state_hash: aborted.payload.panel_snapshot.state_hash }));
|
||||
57
web/worker/test_xyzac_workflow.mjs
Normal file
57
web/worker/test_xyzac_workflow.mjs
Normal file
@@ -0,0 +1,57 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import createModule from "../../build-wasm/linuxcnc-sim-core.js";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../workspace/sim-case-loader.mjs";
|
||||
import { XyzacSession, CONTEXT_ID } from "./xyzac-session.mjs";
|
||||
|
||||
const CASE = "linuxcnc/configs/sim/axis/vismach/5axis/table-rotary-tilting/xyzac-trt.ini";
|
||||
const fetchImpl = async (url) => { try { const body = await readFile(new URL(`../../${String(url).replace(/^\//, "")}`, import.meta.url)); return { ok: true, text: async () => body.toString("utf8") }; } catch { return { ok: false, text: async () => "" }; } };
|
||||
const bundle = await loadSimulationCase({ path: CASE, fetchImpl });
|
||||
const core = await createModule({ wasmBinary: await readFile(new URL("../../build-wasm/linuxcnc-sim-core.wasm", import.meta.url)) });
|
||||
const session = new XyzacSession(core, { sessionId: "xyzac-workflow" });
|
||||
const base = { schema_version: 1, context_id: CONTEXT_ID, session_id: session.sessionId };
|
||||
let seq = 0;
|
||||
const send = (kind, operation_id, payload = {}) => session.accept({ ...base, kind, seq: ++seq, operation_id, payload });
|
||||
send("HELLO", "hello"); send("SESSION_ATTACH", "attach");
|
||||
assert.equal(send("LOAD_WORKSPACE", "workspace", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }).payload.status, "verified");
|
||||
const source = "G21 G90\nG0 X10 Y5 Z200 A30 C45\nG1 X20 Y10 Z180 A35 C50 F100\nG1 X30 Y15 Z160 A40 C55 F100\nM2\n";
|
||||
const loaded = send("PROGRAM_LOAD", "program-load", { source, path: "workflow.ngc" });
|
||||
assert.equal(loaded.kind, "COMMAND_RESULT");
|
||||
const runWithout = send("PROGRAM_RUN", "run");
|
||||
assert.equal(runWithout.payload.task.state, "RUNNING");
|
||||
const first = send("SIM_STEP", "step-1", { cycles: 1 });
|
||||
assert.equal(first.payload.motion_authority, "wasm");
|
||||
const tickAfterFirst = first.simulation_tick;
|
||||
const paused = send("PROGRAM_PAUSE", "pause");
|
||||
assert.equal(paused.payload.task.state, "PAUSED");
|
||||
const cursorPaused = paused.payload.program.current_line;
|
||||
const pausedSnapshot = send("SNAPSHOT_REQUEST", "paused-snapshot");
|
||||
assert.equal(pausedSnapshot.payload.task.state, "PAUSED");
|
||||
assert.equal(pausedSnapshot.payload.panel_snapshot.program.current_line, cursorPaused);
|
||||
const resumed = send("PROGRAM_RESUME", "resume");
|
||||
assert.equal(resumed.payload.task.state, "RUNNING");
|
||||
const second = send("SIM_STEP", "step-2", { cycles: 1 });
|
||||
assert.ok(second.simulation_tick > tickAfterFirst);
|
||||
const activeBeforeAbort = second.payload.wasm_motion.state.active_id;
|
||||
const aborted = send("PROGRAM_ABORT", "abort");
|
||||
assert.equal(aborted.payload.task.state, "STOPPED");
|
||||
assert.equal(aborted.payload.panel_snapshot.wasm_motion.state.queue_depth, 0);
|
||||
assert.equal(aborted.payload.panel_snapshot.wasm_motion.state.active_id, 0);
|
||||
const restart = send("PROGRAM_RESTART", "restart");
|
||||
assert.equal(restart.payload.task.program_status, "loaded");
|
||||
assert.equal(restart.payload.program.path, "workflow.ngc");
|
||||
assert.equal(restart.payload.program.source_hash, loaded.payload.program.source_hash);
|
||||
const seek = send("PROGRAM_SEEK", "seek-line", { line: 3 });
|
||||
assert.equal(seek.payload.task.program_status, "paused");
|
||||
assert.equal(seek.payload.program.current_line, 3);
|
||||
const runLine = send("PROGRAM_RESUME", "run-from-line");
|
||||
assert.equal(runLine.payload.task.state, "RUNNING");
|
||||
const lineStep = send("SIM_STEP", "run-line-step", { cycles: 1 });
|
||||
assert.equal(lineStep.payload.motion_authority, "wasm");
|
||||
const checkpoint = send("CHECKPOINT", "checkpoint");
|
||||
assert.equal(checkpoint.kind, "CHECKPOINT_READY");
|
||||
const recovered = send("RECOVER", "recover", { checkpoint: checkpoint.payload.checkpoint });
|
||||
assert.equal(recovered.payload.status, "recovered");
|
||||
assert.equal(recovered.payload.state.program.source_text, source);
|
||||
assert.equal(session.ledger.reconcile("step-1").status, "committed");
|
||||
console.log(JSON.stringify({ schema: "xyzac-workflow-rtcp-05.v1", status: "verified", case: CASE, tick_after_first: tickAfterFirst, active_before_abort: activeBeforeAbort, pause_cursor: cursorPaused, restart_source_hash: restart.payload.program.source_hash, run_from_line: lineStep.payload.program.current_line, recovery: recovered.payload.status, final_state_hash: lineStep.payload.panel_snapshot.state_hash }));
|
||||
@@ -2,12 +2,13 @@ import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const HOME_LIMIT_SCHEMA = "sim_home_limit_state.v1";
|
||||
const KIN_TYPE_TRIVKINS = 1;
|
||||
const KIN_TYPE_XYZAC_TRT = 2;
|
||||
const KIN_CONFIG_SIZE = 1608;
|
||||
const KIN_SIZE = 1760;
|
||||
const HOME_LIMIT_SIZE = 184;
|
||||
const HOME_STATE_SIZE = 176;
|
||||
const HOME_STATE_OFFSET = 8;
|
||||
const CONFIG_OFFSETS = Object.freeze({ type: 0, mode: 4, joint_count: 8, kinematics_joint_count: 12, extra_joint_count: 16, coordinates: 20, limit_enabled: 1448, joint_min: 1464, joint_max: 1536 });
|
||||
const CONFIG_OFFSETS = Object.freeze({ type: 0, mode: 4, joint_count: 8, kinematics_joint_count: 12, extra_joint_count: 16, coordinates: 20, x_rot_point: 40, y_offset: 72, z_offset: 80, tool_offset: 88, limit_enabled: 1448, joint_min: 1464, joint_max: 1536 });
|
||||
const HOME_STATE_OFFSETS = Object.freeze({ pose: 0, joints: 72, joint_count: 144, homed: 148, home_sequence: 152, tick: 156, last_error: 160, limit_error: 164, kinematics_error: 168 });
|
||||
|
||||
function call(core, name, returnType, argTypes, args) {
|
||||
@@ -40,10 +41,13 @@ function finitePose(value) {
|
||||
}
|
||||
|
||||
export class WasmHomeLimit {
|
||||
constructor(core, { jointCount = 3, limits = {} } = {}) {
|
||||
constructor(core, { jointCount = 3, limits = {}, kinematicsType = KIN_TYPE_TRIVKINS, coordinates = "XYZ", offsets = {} } = {}) {
|
||||
this.core = core;
|
||||
this.available = ["sim_kinematics_init", "sim_kinematics_home_limit_init", "sim_kinematics_home_limit_reset", "sim_kinematics_home_limit_home", "sim_kinematics_home_limit_check", "sim_kinematics_home_limit_read"].every((name) => typeof core?.[`_${name}`] === "function" || typeof core?.[name] === "function");
|
||||
this.jointCount = jointCount;
|
||||
this.kinematicsType = kinematicsType;
|
||||
this.coordinates = String(coordinates).toUpperCase();
|
||||
this.offsets = { ...offsets };
|
||||
this.config = this.kinematics = this.homeLimit = this.state = this.joints = 0;
|
||||
this.stateBuffer = 0;
|
||||
this.limits = { x: [-500, 500], y: [-100, 100], z: [-240, 0.001], ...limits };
|
||||
@@ -60,18 +64,22 @@ export class WasmHomeLimit {
|
||||
this.core.HEAPU8.fill(0, this.stateBuffer, this.stateBuffer + HOME_STATE_SIZE);
|
||||
this.core.HEAPU8.fill(0, this.joints, this.joints + 9 * 8);
|
||||
const configView = new DataView(this.core.HEAPU8.buffer, this.config, KIN_CONFIG_SIZE);
|
||||
configView.setInt32(CONFIG_OFFSETS.type, KIN_TYPE_TRIVKINS, true);
|
||||
configView.setInt32(CONFIG_OFFSETS.type, kinematicsType, true);
|
||||
configView.setInt32(CONFIG_OFFSETS.mode, 0, true);
|
||||
configView.setUint32(CONFIG_OFFSETS.joint_count, jointCount, true);
|
||||
configView.setUint32(CONFIG_OFFSETS.kinematics_joint_count, jointCount, true);
|
||||
configView.setUint32(CONFIG_OFFSETS.extra_joint_count, 0, true);
|
||||
for (const [index, axis] of ["X", "Y", "Z"].entries()) this.core.HEAPU8[this.config + CONFIG_OFFSETS.coordinates + index] = axis.charCodeAt(0);
|
||||
for (const [index, axis] of ["x", "y", "z"].entries()) {
|
||||
const limit = this.limits[axis] ?? [-1e9, 1e9];
|
||||
for (const [index, axis] of [...this.coordinates].entries()) this.core.HEAPU8[this.config + CONFIG_OFFSETS.coordinates + index] = axis.charCodeAt(0);
|
||||
for (const [index, axis] of [...this.coordinates].entries()) {
|
||||
const lowerAxis = axis.toLowerCase();
|
||||
const limit = this.limits[lowerAxis] ?? [-1e9, 1e9];
|
||||
this.core.HEAPU8[this.config + CONFIG_OFFSETS.limit_enabled + index] = 1;
|
||||
configView.setFloat64(CONFIG_OFFSETS.joint_min + index * 8, limit[0], true);
|
||||
configView.setFloat64(CONFIG_OFFSETS.joint_max + index * 8, limit[1], true);
|
||||
}
|
||||
for (const [key, offset] of Object.entries({ x_rot_point: CONFIG_OFFSETS.x_rot_point, y_offset: CONFIG_OFFSETS.y_offset, z_offset: CONFIG_OFFSETS.z_offset, tool_offset: CONFIG_OFFSETS.tool_offset })) {
|
||||
configView.setFloat64(offset, Number(this.offsets[key] ?? 0), true);
|
||||
}
|
||||
const initialized = call(core, "sim_kinematics_init", "number", ["number", "number"], [this.kinematics, this.config]);
|
||||
if (initialized !== 0) throw new Error(`wasm_home_kinematics_init_${initialized}`);
|
||||
const homeInitialized = call(core, "sim_kinematics_home_limit_init", "number", ["number", "number", "number"], [this.homeLimit, this.kinematics, jointCount]);
|
||||
@@ -84,7 +92,7 @@ export class WasmHomeLimit {
|
||||
if (result !== 0) return { status: "error", code: `wasm_home_limit_read_${result}` };
|
||||
const ints = this.core.HEAPU32;
|
||||
const signed = this.core.HEAP32;
|
||||
const joints = [...this.core.HEAPF64.subarray(this.stateBuffer / 8 + 9, this.stateBuffer / 8 + 12)];
|
||||
const joints = [...this.core.HEAPF64.subarray(this.stateBuffer / 8 + 9, this.stateBuffer / 8 + 9 + this.jointCount)];
|
||||
return { status: "verified", state: { schema: HOME_LIMIT_SCHEMA, source: "wasm_home_limit", pose: pose(this.core, this.stateBuffer), joints, joint_count: ints[this.stateBuffer / 4 + 36], homed: Boolean(ints[this.stateBuffer / 4 + 37]), home_sequence: ints[this.stateBuffer / 4 + 38], tick: ints[this.stateBuffer / 4 + 39], last_error: signed[this.stateBuffer / 4 + 40], limit_error: signed[this.stateBuffer / 4 + 41], kinematics_error: signed[this.stateBuffer / 4 + 42] } };
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,9 @@ export class WasmMotionQueue {
|
||||
[...poseToArray(segment.start), ...poseToArray(segment.end), Number(segment.center?.x ?? 0), Number(segment.center?.y ?? 0), Number(segment.center?.z ?? 0), Number(segment.normal?.x ?? 0), Number(segment.normal?.y ?? 0), Number(segment.normal?.z ?? 0), Number(segment.velocity ?? 0), Number(segment.acceleration ?? 0)].forEach((value, index) => { doubles[base + index] = value; });
|
||||
ints[this.command / 4 + SEGMENT_INT_OFFSET] = Number(segment.motion_type ?? 1);
|
||||
ints[this.command / 4 + SEGMENT_INT_OFFSET + 1] = Number(segment.turn ?? 0);
|
||||
// sim_motion_command layout is segment (216 bytes), id (4), tag (4).
|
||||
// Keep the WASM switchkins mode in the tag field; the C queue assigns id.
|
||||
ints[this.command / 4 + SEGMENT_INT_OFFSET + 3] = Number(segment.kinematics_mode ?? segment.tag ?? 0);
|
||||
const result = call(this.core, "sim_motion_enqueue", "number", ["number", "number", "number"], [this.queue, this.command, 0]);
|
||||
return result === 0 ? { status: "verified", source_line: segment.source_line ?? null } : { status: "error", code: `wasm_motion_enqueue_${result}` };
|
||||
}
|
||||
@@ -153,13 +156,16 @@ export class WasmMotionQueue {
|
||||
const previous = this.core.HEAPU8.slice(this.queue, this.queue + this.queueSize);
|
||||
const headerOffset = this.queueCapacity * this.commandCapacity;
|
||||
const stateOffset = headerOffset + QUEUE_HEADER_BYTES;
|
||||
const nextId = new DataView(previous.buffer, previous.byteOffset, previous.byteLength).getUint32(8, true);
|
||||
const previousView = new DataView(previous.buffer, previous.byteOffset, previous.byteLength);
|
||||
const nextId = previousView.getUint32(headerOffset + 8, true);
|
||||
this.reset();
|
||||
const bytes = new Uint8Array(this.queueSize);
|
||||
const view = new DataView(bytes.buffer);
|
||||
bytes.set(previous.subarray(16, 32), 16);
|
||||
view.setUint32(8, nextId, true);
|
||||
view.setUint32(12, current.state.tick, true);
|
||||
// sim_motion_queue stores commands first, followed by its 32-byte header.
|
||||
// Preserve cycle/velocity configuration from that actual header offset.
|
||||
bytes.set(previous.subarray(headerOffset + 16, headerOffset + QUEUE_HEADER_BYTES), headerOffset + 16);
|
||||
view.setUint32(headerOffset + 8, nextId, true);
|
||||
view.setUint32(headerOffset + 12, current.state.tick, true);
|
||||
bytes.set(previous.subarray(stateOffset, stateOffset + STATE_POSITION_BYTES), stateOffset);
|
||||
view.setFloat64(stateOffset + 72, 0, true);
|
||||
view.setFloat64(stateOffset + 80, 0, true);
|
||||
@@ -187,7 +193,7 @@ export class WasmMotionQueue {
|
||||
if (result !== 0) return { status: "error", code: `wasm_motion_read_${result}` };
|
||||
const doubles = this.core.HEAPF64.subarray(this.state / 8, this.state / 8 + 11);
|
||||
const ints = this.core.HEAPU32.subarray(this.state / 4 + 22, this.state / 4 + 26);
|
||||
return { status: "verified", state: { position: { x: doubles[0], y: doubles[1], z: doubles[2] }, velocity: this.controls.paused ? 0 : doubles[9], progress: doubles[10], tick: ints[0], queue_depth: ints[1], active_id: ints[2], active_tag: ints[3], motion_type: this.core.HEAP32[this.state / 4 + 26], status: this.core.HEAP32[this.state / 4 + 27], source: "wasm_motion_queue" } };
|
||||
return { status: "verified", state: { position: { x: doubles[0], y: doubles[1], z: doubles[2], a: doubles[3], b: doubles[4], c: doubles[5], u: doubles[6], v: doubles[7], w: doubles[8] }, velocity: this.controls.paused ? 0 : doubles[9], progress: doubles[10], tick: ints[0], queue_depth: ints[1], active_id: ints[2], active_tag: ints[3], motion_type: this.core.HEAP32[this.state / 4 + 26], status: this.core.HEAP32[this.state / 4 + 27], source: "wasm_motion_queue" } };
|
||||
}
|
||||
|
||||
exportState() {
|
||||
|
||||
246
web/worker/wasm-program-runtime.mjs
Normal file
246
web/worker/wasm-program-runtime.mjs
Normal file
@@ -0,0 +1,246 @@
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const SCHEMA = "wasm_program_runtime.v1";
|
||||
const SEGMENT_BYTES = 216;
|
||||
const POSE_BYTES = 72;
|
||||
const AXES = ["x", "y", "z", "a", "b", "c", "u", "v", "w"];
|
||||
const DEFAULT_RAPID_VELOCITY = 1000;
|
||||
|
||||
function call(core, name, returnType, argTypes, args) {
|
||||
const fn = core?.[`_${name}`] ?? core?.[name];
|
||||
if (typeof fn === "function") return fn(...args);
|
||||
if (typeof core?.ccall === "function") return core.ccall(name, returnType, argTypes, args);
|
||||
throw new Error(`missing core export: ${name}`);
|
||||
}
|
||||
|
||||
function pose(core, pointer) {
|
||||
const values = [...core.HEAPF64.subarray(pointer / 8, pointer / 8 + 9)];
|
||||
return Object.fromEntries(AXES.map((axis, index) => [axis, values[index]]));
|
||||
}
|
||||
|
||||
function segment(core, pointer) {
|
||||
const start = pose(core, pointer);
|
||||
const end = pose(core, pointer + 72);
|
||||
const cart = (offset) => ({ x: core.HEAPF64[pointer / 8 + offset / 8], y: core.HEAPF64[pointer / 8 + offset / 8 + 1], z: core.HEAPF64[pointer / 8 + offset / 8 + 2] });
|
||||
const ints = new DataView(core.HEAPU8.buffer, pointer, SEGMENT_BYTES);
|
||||
return { start, end, center: cart(144), normal: cart(168), velocity: core.HEAPF64[pointer / 8 + 24], acceleration: core.HEAPF64[pointer / 8 + 25], motion_type: ints.getInt32(208, true), turn: ints.getInt32(212, true) };
|
||||
}
|
||||
|
||||
function sampleSegment(core, segmentPointer, posePointer, value) {
|
||||
const turn = Math.max(1, Math.abs(Number(value.turn ?? 1)));
|
||||
const count = value.motion_type === 2 ? Math.max(32, Math.ceil(turn * 32)) : 1;
|
||||
const samples = [];
|
||||
for (let index = 0; index <= count; index += 1) {
|
||||
const result = call(core, "sim_posemath_segment_point", "number", ["number", "number", "number"], [segmentPointer, index / count, posePointer]);
|
||||
if (result !== 0) throw new Error(`wasm_program_segment_sample_${result}`);
|
||||
samples.push(pose(core, posePointer));
|
||||
}
|
||||
return samples;
|
||||
}
|
||||
|
||||
function readString(core, pointer, capacity) {
|
||||
const bytes = core.HEAPU8.subarray(pointer, pointer + capacity);
|
||||
const end = bytes.indexOf(0);
|
||||
return new TextDecoder().decode(bytes.subarray(0, end < 0 ? bytes.length : end));
|
||||
}
|
||||
|
||||
export class WasmProgramRuntime {
|
||||
constructor(core) {
|
||||
this.core = core;
|
||||
this.available = ["sim_program_size", "sim_program_init", "sim_program_step", "sim_program_canonical", "sim_program_add_subroutine", "sim_program_set_rapid_velocity"].every((name) => typeof core?.[`_${name}`] === "function" || typeof core?.[name] === "function");
|
||||
this.pointer = 0;
|
||||
this.motionPointer = 0;
|
||||
this.samplePointer = 0;
|
||||
this.samplePosePointer = 0;
|
||||
this.source = "";
|
||||
this.path = null;
|
||||
this.subroutines = [];
|
||||
this.rapidVelocity = 35;
|
||||
if (!this.available || typeof core?._malloc !== "function") return;
|
||||
this.pointer = core._malloc(call(core, "sim_program_size", "number", [], []));
|
||||
this.motionPointer = core._malloc(SEGMENT_BYTES);
|
||||
this.samplePointer = core._malloc(SEGMENT_BYTES);
|
||||
this.samplePosePointer = core._malloc(POSE_BYTES);
|
||||
if (!this.pointer || !this.motionPointer || !this.samplePointer || !this.samplePosePointer) throw new Error("wasm_program_allocation_failed");
|
||||
const result = call(core, "sim_program_init", "number", ["number"], [this.pointer]);
|
||||
if (result !== 0) throw new Error(`wasm_program_init_${result}`);
|
||||
}
|
||||
|
||||
invoke(name, args = [], errorPrefix = name) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_program_unavailable" };
|
||||
const result = call(this.core, name, "number", ["number", ...args.map(() => "number")], [this.pointer, ...args]);
|
||||
return result === 0 ? { status: "verified", command: name } : { status: "error", code: `wasm_${errorPrefix}_${result}`, result };
|
||||
}
|
||||
|
||||
load(source, path = null) {
|
||||
if (typeof source !== "string" || source.length === 0) return { status: "error", code: "program_source_invalid" };
|
||||
const bytes = new TextEncoder().encode(source);
|
||||
const pointer = this.core._malloc(bytes.byteLength);
|
||||
this.core.HEAPU8.set(bytes, pointer);
|
||||
try {
|
||||
const result = call(this.core, "sim_program_load", "number", ["number", "number", "number"], [this.pointer, pointer, bytes.byteLength]);
|
||||
if (result !== 0) return { status: "error", code: `wasm_program_load_${result}` };
|
||||
const velocityResult = call(this.core, "sim_program_set_rapid_velocity", "number", ["number", "number"], [this.pointer, this.rapidVelocity]);
|
||||
if (velocityResult !== 0) return { status: "error", code: `wasm_program_rapid_velocity_${velocityResult}` };
|
||||
this.source = source;
|
||||
this.path = path;
|
||||
this.subroutines = [];
|
||||
return { status: "verified", program: this.snapshot() };
|
||||
} finally { this.core._free(pointer); }
|
||||
}
|
||||
|
||||
requiresSubroutines(source = this.source) {
|
||||
if (typeof source !== "string" || source.length === 0) return { status: "error", code: "program_source_invalid" };
|
||||
const bytes = new TextEncoder().encode(source);
|
||||
const pointer = this.core._malloc(bytes.byteLength);
|
||||
this.core.HEAPU8.set(bytes, pointer);
|
||||
try {
|
||||
const result = call(this.core, "sim_program_requires_subroutines", "number", ["number", "number"], [pointer, bytes.byteLength]);
|
||||
if (result < 0) return { status: "error", code: `wasm_program_requires_subroutines_${result}` };
|
||||
return { status: "verified", required: result === 1 };
|
||||
} finally { this.core._free(pointer); }
|
||||
}
|
||||
|
||||
preview() {
|
||||
if (!this.source) return { status: "error", code: "program_preview_source_missing" };
|
||||
const temporary = new WasmProgramRuntime(this.core);
|
||||
const segments = [];
|
||||
try {
|
||||
const loaded = temporary.load(this.source, this.path);
|
||||
if (loaded.status !== "verified") return loaded;
|
||||
temporary.setRapidVelocity(this.rapidVelocity);
|
||||
for (const subroutine of this.subroutines) {
|
||||
const added = temporary.addSubroutine(subroutine.name, subroutine.source);
|
||||
if (added.status !== "verified") return added;
|
||||
}
|
||||
const started = temporary.start();
|
||||
if (started.status !== "verified") return started;
|
||||
const limit = Math.min(100000, Math.max(1, temporary.snapshot().line_count * 8 + 1));
|
||||
let kinematicsMode = 0;
|
||||
for (let index = 0; index < limit; index += 1) {
|
||||
const step = temporary.step();
|
||||
if (step.status !== "verified") return { status: "error", code: step.code, line: temporary.snapshot().current_line, segments };
|
||||
if (step.has_control && [428, 429, 430].includes(step.control_code)) kinematicsMode = { 428: 1, 429: 0, 430: 2 }[step.control_code];
|
||||
if (step.motion) segments.push({ id: segments.length + 1, kinematics_mode: kinematicsMode, ...step.motion });
|
||||
if ([4, 5, 6].includes(temporary.snapshot().status)) break;
|
||||
}
|
||||
return { status: "verified", schema: "wasm-canonical-preview.v1", source: "linuxcnc-wasm-sim_program", path: this.path, source_hash: fnv1a64(this.source), line_count: this.snapshot().line_count, segments, preview_hash: fnv1a64(segments) };
|
||||
} finally { temporary.destroy(); }
|
||||
}
|
||||
|
||||
start() { return this.invoke("sim_program_start", [], "program_start"); }
|
||||
setRapidVelocity(velocity) {
|
||||
if (!Number.isFinite(Number(velocity)) || Number(velocity) <= 0) return { status: "error", code: "program_rapid_velocity_invalid" };
|
||||
this.rapidVelocity = Number(velocity);
|
||||
const result = call(this.core, "sim_program_set_rapid_velocity", "number", ["number", "number"], [this.pointer, this.rapidVelocity]);
|
||||
if (result !== 0) throw new Error(`wasm_program_set_rapid_velocity_${result}`);
|
||||
return { status: "verified", command: "sim_program_set_rapid_velocity" };
|
||||
}
|
||||
addSubroutine(name, source) {
|
||||
if (typeof source !== "string" || source.length === 0) return { status: "error", code: "program_subroutine_invalid" };
|
||||
const bytes = new TextEncoder().encode(source);
|
||||
const pointer = this.core._malloc(bytes.byteLength);
|
||||
this.core.HEAPU8.set(bytes, pointer);
|
||||
try {
|
||||
const code = call(this.core, "sim_program_add_subroutine", "number", ["number", "number", "number"], [this.pointer, pointer, bytes.byteLength]);
|
||||
if (code !== 0) return { status: "error", code: `wasm_program_add_subroutine_${code}`, result: code };
|
||||
this.subroutines.push({ name: String(name ?? "subroutine"), source });
|
||||
return { status: "verified", command: "sim_program_add_subroutine", name: String(name ?? "subroutine") };
|
||||
} finally { this.core._free(pointer); }
|
||||
}
|
||||
pause() { return this.invoke("sim_program_pause", [], "program_pause"); }
|
||||
resume() { return this.invoke("sim_program_resume", [], "program_resume"); }
|
||||
abort() { return this.invoke("sim_program_abort", [], "program_abort"); }
|
||||
seek(line) { return this.invoke("sim_program_seek", [line], "program_seek"); }
|
||||
|
||||
restoreState(value) {
|
||||
if (!value || ![SCHEMA, "sim_program.v1"].includes(value.schema) || !Number.isSafeInteger(value.current_line) || value.current_line < 0 || !this.source) throw new Error("wasm_program_restore_schema");
|
||||
if (value.source_hash && value.source_hash !== fnv1a64(this.source)) throw new Error("wasm_program_restore_source_hash");
|
||||
const subroutines = [...this.subroutines];
|
||||
const loaded = this.load(this.source, this.path);
|
||||
if (loaded.status !== "verified") throw new Error(loaded.code);
|
||||
for (const subroutine of subroutines) {
|
||||
const added = this.addSubroutine(subroutine.name, subroutine.source);
|
||||
if (added.status !== "verified") throw new Error(added.code);
|
||||
}
|
||||
if (value.current_line > 0) {
|
||||
const seek = this.seek(value.current_line);
|
||||
if (seek.status !== "verified") throw new Error(seek.code);
|
||||
}
|
||||
const status = value.program_status ?? ({ 0: "idle", 1: "loaded", 2: "running", 3: "paused", 4: "completed", 5: "aborted", 6: "fault" }[value.status] ?? "loaded");
|
||||
if (status === "running") {
|
||||
const resumed = this.resume();
|
||||
if (resumed.status !== "verified") {
|
||||
const started = this.start();
|
||||
if (started.status !== "verified") throw new Error(started.code);
|
||||
}
|
||||
} else if (status === "completed") {
|
||||
this.start();
|
||||
for (let index = 0; index < this.snapshot().line_count + 1 && this.snapshot().status === 2; index += 1) this.step();
|
||||
} else if (status === "aborted") {
|
||||
const aborted = this.abort();
|
||||
if (aborted.status !== "verified") throw new Error(aborted.code);
|
||||
}
|
||||
if (value.has_motion === 0) this.clearMotion();
|
||||
return this.snapshot();
|
||||
}
|
||||
|
||||
executeBlock(block) {
|
||||
const bytes = new TextEncoder().encode(String(block ?? ""));
|
||||
const pointer = this.core._malloc(bytes.byteLength);
|
||||
this.core.HEAPU8.set(bytes, pointer);
|
||||
try {
|
||||
const result = call(this.core, "sim_program_execute_block", "number", ["number", "number", "number"], [this.pointer, pointer, bytes.byteLength]);
|
||||
if (result !== 0) return { status: "error", code: `wasm_program_execute_${result}` };
|
||||
return { status: "verified", program: this.snapshot(), motion: this.readMotion() };
|
||||
} finally { this.core._free(pointer); }
|
||||
}
|
||||
|
||||
readMotion() {
|
||||
const state = this.snapshot();
|
||||
if (!state.has_motion) return null;
|
||||
const result = call(this.core, "sim_program_read_motion", "number", ["number", "number"], [this.pointer, this.motionPointer]);
|
||||
if (result !== 0) return null;
|
||||
const value = segment(this.core, this.motionPointer);
|
||||
// LinuxCNC rapid blocks do not carry an F word. The trajectory queue
|
||||
// requires a positive velocity, so use the simulation's configured rapid
|
||||
// rate for that WASM-produced linear segment.
|
||||
if (state.motion_mode === 0 && value.motion_type === 1 && value.velocity <= 0) value.velocity = DEFAULT_RAPID_VELOCITY;
|
||||
value.samples = sampleSegment(this.core, this.motionPointer, this.samplePosePointer, value);
|
||||
value.samples_source = "linuxcnc-wasm-sim_posemath_segment_point";
|
||||
value.canonical_motion_mode = state.motion_mode;
|
||||
value.kind = value.motion_type === 2 && Math.abs(Number(value.end?.z ?? 0) - Number(value.start?.z ?? 0)) > 1e-12 ? "helix" : value.motion_type === 2 ? "arc" : state.motion_mode === 0 ? "rapid" : value.motion_type === 1 ? "feed" : "traverse";
|
||||
if (value.kind === "helix") {
|
||||
value.helix = true;
|
||||
value.radius = Math.hypot(Number(value.start?.x ?? 0) - Number(value.center?.x ?? 0), Number(value.start?.y ?? 0) - Number(value.center?.y ?? 0));
|
||||
value.turns = Math.abs(Number(value.turn ?? 1));
|
||||
value.axial_delta = Number(value.end?.z ?? 0) - Number(value.start?.z ?? 0);
|
||||
}
|
||||
call(this.core, "sim_program_clear_motion", "number", ["number"], [this.pointer]);
|
||||
return { ...value, source_line: state.current_line };
|
||||
}
|
||||
|
||||
clearMotion() { return this.invoke("sim_program_clear_motion", [], "program_clear_motion"); }
|
||||
|
||||
step() {
|
||||
const result = this.invoke("sim_program_step", [], "program_step");
|
||||
if (result.status !== "verified") return result;
|
||||
const state = this.snapshot();
|
||||
return { status: "verified", program_status: state.status, line: state.current_line, control_code: state.control_code ?? 0, has_control: state.has_control === 1, program: state, motion: this.readMotion() };
|
||||
}
|
||||
|
||||
snapshot() {
|
||||
if (!this.available) return { schema: SCHEMA, status: "blocked", code: "wasm_program_unavailable" };
|
||||
const required = call(this.core, "sim_program_canonical", "number", ["number", "number", "number"], [this.pointer, 0, 0]);
|
||||
const buffer = this.core._malloc(required);
|
||||
try {
|
||||
call(this.core, "sim_program_canonical", "number", ["number", "number", "number"], [this.pointer, buffer, required]);
|
||||
const state = JSON.parse(readString(this.core, buffer, required));
|
||||
const hash = call(this.core, "sim_program_state_hash", "number", ["number"], [this.pointer]);
|
||||
const programStatus = { 0: "idle", 1: "loaded", 2: "running", 3: "paused", 4: "completed", 5: "aborted", 6: "fault" }[state.status] ?? "idle";
|
||||
return { schema: SCHEMA, source: "linuxcnc-wasm-sim_program", ...state, loaded: state.status !== 0, line: state.current_line, currentLine: state.current_line, program_status: programStatus, path: this.path, subroutines: this.subroutines.map((subroutine) => subroutine.name), state_hash: `fnv1a64:${BigInt.asUintN(64, BigInt(hash >>> 0)).toString(16).padStart(16, "0")}`, source_hash: this.source ? fnv1a64(this.source) : null };
|
||||
} finally { this.core._free(buffer); }
|
||||
}
|
||||
|
||||
destroy() { for (const pointer of [this.pointer, this.motionPointer, this.samplePointer, this.samplePosePointer]) if (pointer && typeof this.core?._free === "function") this.core._free(pointer); this.pointer = this.motionPointer = this.samplePointer = this.samplePosePointer = 0; }
|
||||
}
|
||||
111
web/worker/wasm-xyzac-device-io.mjs
Normal file
111
web/worker/wasm-xyzac-device-io.mjs
Normal file
@@ -0,0 +1,111 @@
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const DEVICE_SCHEMA = "wasm_xyzac_device_io.v1";
|
||||
|
||||
function call(core, name, returnType, argTypes, args) {
|
||||
const fn = core?.[`_${name}`] ?? core?.[name];
|
||||
if (typeof fn === "function") return fn(...args);
|
||||
if (typeof core?.ccall === "function") return core.ccall(name, returnType, argTypes, args);
|
||||
throw new Error(`missing core export: ${name}`);
|
||||
}
|
||||
|
||||
function readString(core, pointer, capacity) {
|
||||
const bytes = core.HEAPU8.subarray(pointer, pointer + capacity);
|
||||
const end = bytes.indexOf(0);
|
||||
return new TextDecoder().decode(bytes.subarray(0, end < 0 ? bytes.length : end));
|
||||
}
|
||||
|
||||
function bytesToHex(bytes) {
|
||||
let output = "";
|
||||
for (const byte of bytes) output += byte.toString(16).padStart(2, "0");
|
||||
return output;
|
||||
}
|
||||
|
||||
function hexToBytes(value) {
|
||||
if (typeof value !== "string" || value.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(value)) throw new Error("wasm_xyzac_device_io_state_bytes_invalid");
|
||||
const bytes = new Uint8Array(value.length / 2);
|
||||
for (let index = 0; index < bytes.length; index += 1) bytes[index] = Number.parseInt(value.slice(index * 2, index * 2 + 2), 16);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function operationNumber(value) {
|
||||
if (Number.isSafeInteger(value) && value > 0) return value;
|
||||
const text = String(value ?? "device");
|
||||
let hash = 2166136261;
|
||||
for (const byte of new TextEncoder().encode(text)) { hash ^= byte; hash = Math.imul(hash, 16777619); }
|
||||
return (hash >>> 0) || 1;
|
||||
}
|
||||
|
||||
export class WasmXyzacDeviceIo {
|
||||
constructor(core, { encoderCount = 2 } = {}) {
|
||||
this.core = core;
|
||||
this.available = ["sim_device_io_size", "sim_device_init", "sim_device_canonical", "sim_device_state_hash"].every((name) => typeof core?.[`_${name}`] === "function" || typeof core?.[name] === "function");
|
||||
this.pointer = 0;
|
||||
if (!this.available || typeof core?._malloc !== "function") return;
|
||||
const size = call(core, "sim_device_io_size", "number", [], []);
|
||||
this.pointer = core._malloc(size);
|
||||
if (!this.pointer) throw new Error("wasm_xyzac_device_io_allocation_failed");
|
||||
const result = call(core, "sim_device_init", "number", ["number", "number", "number"], [this.pointer, encoderCount, 0.01]);
|
||||
if (result !== 0) throw new Error(`wasm_xyzac_device_io_init_${result}`);
|
||||
}
|
||||
|
||||
invoke(name, args = []) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_device_io_unavailable" };
|
||||
const result = call(this.core, name, "number", ["number", ...args.map(() => "number")], [this.pointer, ...args]);
|
||||
return result === 0 ? { status: "verified", command: name } : { status: "error", code: `wasm_${name}_${result}`, result };
|
||||
}
|
||||
|
||||
canonical() {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_device_io_unavailable" };
|
||||
const required = call(this.core, "sim_device_canonical", "number", ["number", "number", "number"], [this.pointer, 0, 0]);
|
||||
const buffer = this.core._malloc(required);
|
||||
try { call(this.core, "sim_device_canonical", "number", ["number", "number", "number"], [this.pointer, buffer, required]); return JSON.parse(readString(this.core, buffer, required)); }
|
||||
finally { this.core._free(buffer); }
|
||||
}
|
||||
|
||||
export() {
|
||||
const state = this.canonical();
|
||||
if (state.status === "blocked") return { schema: DEVICE_SCHEMA, source: "wasm_device_io", ...state };
|
||||
const unsigned = { schema: DEVICE_SCHEMA, source: "wasm_device_io", state, wasm_state_hash: this.stateHash(), state_bytes: bytesToHex(this.core.HEAPU8.slice(this.pointer, this.pointer + call(this.core, "sim_device_io_size", "number", [], []))) };
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
restoreState(value) {
|
||||
if (!value || !value.state_bytes || !this.available) throw new Error("wasm_xyzac_device_io_restore_unavailable");
|
||||
const bytes = hexToBytes(value.state_bytes);
|
||||
const size = call(this.core, "sim_device_io_size", "number", [], []);
|
||||
if (bytes.length !== size) throw new Error("wasm_xyzac_device_io_restore_layout");
|
||||
this.core.HEAPU8.set(bytes, this.pointer);
|
||||
const actual = this.canonical();
|
||||
if (value.state && fnv1a64(actual) !== fnv1a64(value.state)) throw new Error("wasm_xyzac_device_io_restore_state_mismatch");
|
||||
return this.export();
|
||||
}
|
||||
|
||||
stateHash() {
|
||||
if (!this.available) return null;
|
||||
const hash = call(this.core, "sim_device_state_hash", "number", ["number"], [this.pointer]);
|
||||
return `fnv1a64:${BigInt.asUintN(64, BigInt(hash >>> 0)).toString(16).padStart(16, "0")}`;
|
||||
}
|
||||
|
||||
command(kind, payload = {}, operationId = null) {
|
||||
if (kind === "ENCODER_SET") {
|
||||
const channel = payload.channel ?? 0;
|
||||
if (payload.speed_rps !== undefined || payload.speed !== undefined) return this.invoke("sim_device_encoder_set_speed", [channel, payload.speed_rps ?? payload.speed]);
|
||||
if (payload.ppr !== undefined || payload.scale !== undefined) return this.invoke("sim_device_encoder_set_config", [channel, payload.ppr ?? 1000, payload.scale ?? 1]);
|
||||
return this.invoke("sim_device_encoder_set_index_enable", [channel, payload.index_enable === true ? 1 : 0]);
|
||||
}
|
||||
if (kind === "COOLANT_COMMAND") return this.invoke("sim_device_coolant_command", [payload.mist ? 1 : 0, payload.flood ? 1 : 0, payload.lube ? 1 : 0]);
|
||||
if (kind === "COOLANT_FAULT") return this.invoke("sim_device_coolant_set_fault", [payload.fault === true ? 1 : 0]);
|
||||
if (kind === "DOOR_SET") return this.invoke("sim_device_door_set", [payload.open === true ? 1 : 0]);
|
||||
if (kind === "INTERLOCK_SET") return this.invoke("sim_device_interlock_set", [payload.input_ok === false ? 0 : 1]);
|
||||
if (kind === "INTERLOCK_RESET") return this.invoke("sim_device_interlock_reset");
|
||||
if (kind === "DEVICE_ESTOP") return this.invoke("sim_device_estop_set", [payload.active === true ? 1 : 0]);
|
||||
if (kind === "DEVICE_ACK_ISSUE") return this.invoke("sim_device_ack_issue", [operationNumber(operationId ?? payload.operation_id), payload.timeout_ticks ?? 10]);
|
||||
if (kind === "DEVICE_ACK") return this.invoke("sim_device_ack_ack", [operationNumber(payload.operation_id)]);
|
||||
if (kind === "DEVICE_ACK_ABORT") return this.invoke("sim_device_ack_abort", [operationNumber(payload.operation_id)]);
|
||||
if (kind === "IO_STEP") return this.invoke("sim_device_step", [payload.ticks ?? 1]);
|
||||
return { status: "error", code: "wasm_xyzac_device_io_command_unsupported", command: kind };
|
||||
}
|
||||
|
||||
destroy() { if (this.pointer && typeof this.core?._free === "function") this.core._free(this.pointer); this.pointer = 0; }
|
||||
}
|
||||
170
web/worker/wasm-xyzac-io.mjs
Normal file
170
web/worker/wasm-xyzac-io.mjs
Normal file
@@ -0,0 +1,170 @@
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const IO_SCHEMA = "wasm_xyzac_io.v1";
|
||||
const HAL_SCHEMA = "wasm_xyzac_hal.v1";
|
||||
const POSE_BYTES = 72;
|
||||
const HAL_WRITE_ALLOWLIST = new Set(["motion.probe-input", "iocontrol.0.tool-prepared", "iocontrol.0.tool-changed", "spindle.0.speed-in", "spindle.0.at-speed"]);
|
||||
|
||||
function call(core, name, returnType, argTypes, args) {
|
||||
const fn = core?.[`_${name}`] ?? core?.[name];
|
||||
if (typeof fn === "function") return fn(...args);
|
||||
if (typeof core?.ccall === "function") return core.ccall(name, returnType, argTypes, args);
|
||||
throw new Error(`missing core export: ${name}`);
|
||||
}
|
||||
|
||||
function callString(core, name, returnType, argTypes, args) {
|
||||
if (typeof core?.ccall === "function") return core.ccall(name, returnType, argTypes, args);
|
||||
throw new Error(`missing string-capable core export: ${name}`);
|
||||
}
|
||||
|
||||
function readString(core, pointer, capacity) {
|
||||
const bytes = core.HEAPU8.subarray(pointer, pointer + capacity);
|
||||
const end = bytes.indexOf(0);
|
||||
return new TextDecoder().decode(bytes.subarray(0, end < 0 ? bytes.length : end));
|
||||
}
|
||||
|
||||
function bytesToHex(bytes) {
|
||||
let output = "";
|
||||
for (const byte of bytes) output += byte.toString(16).padStart(2, "0");
|
||||
return output;
|
||||
}
|
||||
|
||||
function hexToBytes(value) {
|
||||
if (typeof value !== "string" || value.length % 2 !== 0 || !/^[0-9a-f]*$/i.test(value)) throw new Error("wasm_xyzac_io_state_bytes_invalid");
|
||||
const bytes = new Uint8Array(value.length / 2);
|
||||
for (let index = 0; index < bytes.length; index += 1) bytes[index] = Number.parseInt(value.slice(index * 2, index * 2 + 2), 16);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
export class WasmXyzacIo {
|
||||
constructor(core, { spindleCount = 2, toolCapacity = 1001 } = {}) {
|
||||
this.core = core;
|
||||
this.available = ["sim_io_size", "sim_io_init", "sim_io_canonical", "sim_io_state_hash"].every((name) => typeof core?.[`_${name}`] === "function" || typeof core?.[name] === "function");
|
||||
this.pointer = 0;
|
||||
this.posePointer = 0;
|
||||
this.ioSize = 0;
|
||||
this.spindleCount = spindleCount;
|
||||
this.toolCapacity = toolCapacity;
|
||||
this.lastError = 0;
|
||||
if (!this.available || typeof core?._malloc !== "function") return;
|
||||
this.ioSize = call(core, "sim_io_size", "number", [], []);
|
||||
this.pointer = core._malloc(this.ioSize);
|
||||
this.posePointer = core._malloc(POSE_BYTES);
|
||||
if (!this.pointer || !this.posePointer) throw new Error("wasm_xyzac_io_allocation_failed");
|
||||
const result = call(core, "sim_io_init", "number", ["number", "number", "number", "number", "number", "number"], [this.pointer, spindleCount, toolCapacity, 0.01, 600, 5]);
|
||||
if (result !== 0) throw new Error(`wasm_xyzac_io_init_${result}`);
|
||||
}
|
||||
|
||||
invoke(name, args = []) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_io_unavailable" };
|
||||
const result = call(this.core, name, "number", ["number", ...args.map(() => "number")], [this.pointer, ...args]);
|
||||
this.lastError = result;
|
||||
return result === 0 ? { status: "verified", command: name } : { status: "error", code: `wasm_${name}_${result}`, result };
|
||||
}
|
||||
|
||||
canonical() {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_io_unavailable" };
|
||||
const required = call(this.core, "sim_io_canonical", "number", ["number", "number", "number"], [this.pointer, 0, 0]);
|
||||
const buffer = this.core._malloc(required);
|
||||
try {
|
||||
call(this.core, "sim_io_canonical", "number", ["number", "number", "number"], [this.pointer, buffer, required]);
|
||||
return JSON.parse(readString(this.core, buffer, required));
|
||||
} finally { this.core._free(buffer); }
|
||||
}
|
||||
|
||||
stateHash() {
|
||||
if (!this.available) return null;
|
||||
const hash = call(this.core, "sim_io_state_hash", "number", ["number"], [this.pointer]);
|
||||
return `fnv1a64:${BigInt.asUintN(64, BigInt(hash >>> 0)).toString(16).padStart(16, "0")}`;
|
||||
}
|
||||
|
||||
export() {
|
||||
const state = this.canonical();
|
||||
if (state.status === "blocked") return { schema: IO_SCHEMA, source: "wasm_io", ...state };
|
||||
const unsigned = { schema: IO_SCHEMA, source: "wasm_io", state, state_hash: this.stateHash(), state_bytes: bytesToHex(this.core.HEAPU8.slice(this.pointer, this.pointer + this.ioSize)) };
|
||||
return { ...unsigned, projection_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
restoreState(value) {
|
||||
if (!value || !value.state_bytes || !this.available) throw new Error("wasm_xyzac_io_restore_unavailable");
|
||||
const bytes = hexToBytes(value.state_bytes);
|
||||
if (bytes.length !== this.ioSize) throw new Error("wasm_xyzac_io_restore_layout");
|
||||
this.core.HEAPU8.set(bytes, this.pointer);
|
||||
const actual = this.canonical();
|
||||
if (value.state && fnv1a64(actual) !== fnv1a64(value.state)) throw new Error("wasm_xyzac_io_restore_state_mismatch");
|
||||
return this.export();
|
||||
}
|
||||
|
||||
halExport() {
|
||||
if (!this.available) return { schema: HAL_SCHEMA, source: "wasm_hal", status: "blocked", code: "wasm_hal_unavailable" };
|
||||
const tick = call(this.core, "sim_hal_tick", "number", [], []);
|
||||
const unsigned = {
|
||||
schema: HAL_SCHEMA,
|
||||
source: "wasm_hal",
|
||||
status: "verified",
|
||||
tick,
|
||||
pin_count: call(this.core, "sim_hal_pin_count", "number", [], []),
|
||||
param_count: call(this.core, "sim_hal_param_count", "number", [], []),
|
||||
thread_count: call(this.core, "sim_hal_thread_count", "number", [], []),
|
||||
function_count: call(this.core, "sim_hal_function_count", "number", [], []),
|
||||
component_count: call(this.core, "sim_hal_component_count", "number", [], []),
|
||||
};
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
halValue(name, value, type = 4, param = false) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_hal_unavailable" };
|
||||
if (param || !HAL_WRITE_ALLOWLIST.has(name)) return { status: "error", code: "wasm_hal_write_denied", name, target: param ? "param" : "pin" };
|
||||
const result = callString(this.core, param ? "sim_hal_set_param_scalar" : "sim_hal_set_pin_scalar", "number", ["string", "number", "number"], [name, type, Number(value)]);
|
||||
return result === 0 ? { status: "verified", command: param ? "HAL_SET_PARAM" : "HAL_SET_PIN" } : { status: "error", code: `wasm_hal_set_${result}`, result };
|
||||
}
|
||||
|
||||
halCommand(payload = {}) {
|
||||
if (payload.action === "thread") return callString(this.core, "sim_hal_register_thread", "number", ["string", "number"], [payload.name ?? "servo-thread", payload.period_ticks ?? 1]) === 0 ? { status: "verified", command: "HAL_THREAD" } : { status: "error", code: "wasm_hal_thread" };
|
||||
if (payload.action === "component") return callString(this.core, "sim_hal_register_component", "number", ["string", "string", "string"], [payload.instance ?? payload.name, payload.type ?? payload.module, payload.thread ?? "servo-thread"]) === 0 ? { status: "verified", command: "HAL_COMPONENT" } : { status: "error", code: "wasm_hal_component" };
|
||||
if (payload.action === "connect") return callString(this.core, "sim_hal_connect", "number", ["string", "string"], [payload.source, payload.target]) === 0 ? { status: "verified", command: "HAL_CONNECT" } : { status: "error", code: "wasm_hal_connect" };
|
||||
if (payload.action === "step") {
|
||||
const result = call(this.core, "sim_hal_step", "number", ["number"], [payload.ticks ?? payload.tick ?? 1]);
|
||||
return result === 0 ? { status: "verified", command: "HAL_STEP" } : { status: "error", code: `wasm_hal_step_${result}` };
|
||||
}
|
||||
return { status: "error", code: "wasm_hal_command_unsupported" };
|
||||
}
|
||||
|
||||
command(kind, payload = {}) {
|
||||
if (kind === "SPINDLE_COMMAND") return this.invoke("sim_io_spindle_command", [payload.spindle ?? 0, payload.rpm ?? payload.speed ?? 0, payload.enabled === false ? 0 : 1]);
|
||||
if (kind === "SPINDLE_FAULT") return this.invoke("sim_io_spindle_set_fault", [payload.spindle ?? 0, payload.fault === true ? 1 : 0]);
|
||||
if (kind === "SPINDLE_INHIBIT") return this.invoke("sim_io_spindle_set_inhibit", [payload.spindle ?? 0, payload.inhibit === true ? 1 : 0]);
|
||||
if (kind === "SPINDLE_ORIENT") return this.invoke("sim_io_spindle_orient", [payload.spindle ?? 0, payload.angle ?? 0, payload.mode ?? 0]);
|
||||
if (kind === "SPINDLE_ORIENTED") return this.invoke("sim_io_spindle_set_oriented", [payload.spindle ?? 0, payload.oriented === true ? 1 : 0, payload.fault ?? 0]);
|
||||
if (kind === "TOOL_PREPARE") return this.invoke("sim_io_tool_prepare", [payload.tool ?? payload.tool_number ?? 0, payload.pocket ?? payload.pocket_number ?? 0]);
|
||||
if (kind === "TOOL_PREPARED") return this.invoke("sim_io_tool_set_prepared", [payload.prepared === false ? 0 : 1]);
|
||||
if (kind === "TOOL_CHANGE") return this.invoke("sim_io_tool_change");
|
||||
if (kind === "TOOL_CHANGED") return this.invoke("sim_io_tool_set_changed", [payload.changed === false ? 0 : 1]);
|
||||
if (kind === "TOOL_ABORT") return this.invoke("sim_io_tool_abort", [payload.reason ?? 0]);
|
||||
if (kind === "TOOL_FAULT") return this.invoke("sim_io_tool_set_fault", [payload.fault === true ? 1 : 0, payload.reason ?? 0]);
|
||||
if (kind === "TOOL_CLEAR_FAULT") return this.invoke("sim_io_tool_clear_fault");
|
||||
if (kind === "PROBE_CONFIG") {
|
||||
const result = this.invoke("sim_io_probe_arm", [payload.probe_type ?? payload.type ?? 0]);
|
||||
return result;
|
||||
}
|
||||
if (kind === "ESTOP" || kind === "DEVICE_ESTOP") return this.invoke("sim_io_set_estop", [payload.active === true ? 1 : 0]);
|
||||
if (kind === "IO_RECOVER" || kind === "INTERLOCK_RESET") return this.invoke("sim_io_recover");
|
||||
if (kind === "IO_STEP") return this.invoke("sim_io_step", [payload.ticks ?? 1]);
|
||||
if (kind === "COOLANT_COMMAND") return { status: "error", code: "wasm_device_io_required", detail: "coolant uses sim_device_io authority" };
|
||||
return { status: "error", code: "wasm_xyzac_io_command_unsupported", command: kind };
|
||||
}
|
||||
|
||||
probeUpdate(pose, input = 1) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_io_unavailable" };
|
||||
const values = ["x", "y", "z", "a", "b", "c", "u", "v", "w"].map((axis) => Number(pose?.[axis] ?? 0));
|
||||
this.core.HEAPF64.set(values, this.posePointer / 8);
|
||||
return this.invoke("sim_io_probe_update", [this.posePointer, input ? 1 : 0]);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
for (const pointer of [this.pointer, this.posePointer]) if (pointer && typeof this.core?._free === "function") this.core._free(pointer);
|
||||
this.pointer = this.posePointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
export { HAL_WRITE_ALLOWLIST };
|
||||
181
web/worker/wasm-xyzac-kinematics.mjs
Normal file
181
web/worker/wasm-xyzac-kinematics.mjs
Normal file
@@ -0,0 +1,181 @@
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
const CONFIG_SIZE = 1608;
|
||||
const KINEMATICS_SIZE = 1760;
|
||||
const SWITCH_SIZE = 512;
|
||||
const RESULT_INFO_SIZE = 32;
|
||||
const POSE_SIZE = 72;
|
||||
const JOINT_BYTES = 9 * 8;
|
||||
const CONFIG = Object.freeze({
|
||||
type: 0, mode: 4, joint_count: 8, kinematics_joint_count: 12, extra_joint_count: 16,
|
||||
coordinates: 20, x_rot_point: 40, y_rot_point: 48, z_rot_point: 56,
|
||||
x_offset: 64, y_offset: 72, z_offset: 80, tool_offset: 88,
|
||||
limit_enabled: 1448, joint_min: 1464, joint_max: 1536,
|
||||
});
|
||||
const SWITCH = Object.freeze({ switching: 16, active_mode: 20, pending_mode: 24, generation: 28, switch_count: 32, abort_count: 36 });
|
||||
const TYPE = Object.freeze({ identity: 1, xyzac: 2, userk: 1 });
|
||||
const MODE = Object.freeze({ identity: 0, xyzac: 1, userk: 2 });
|
||||
const AXES = Object.freeze(["x", "y", "z", "a", "b", "c", "u", "v", "w"]);
|
||||
const COORDINATES = Object.freeze(["x", "y", "z", "a", "c"]);
|
||||
const DEFAULT_LIMITS = Object.freeze({ x: [-200, 200], y: [-100, 100], z: [-120, 120], a: [-100, 50], c: [-36000, 36000] });
|
||||
|
||||
function call(core, name, returnType, argTypes, args) {
|
||||
const direct = core?.[`_${name}`] ?? core?.[name];
|
||||
if (typeof direct === "function") return direct(...args);
|
||||
if (typeof core?.ccall === "function") return core.ccall(name, returnType, argTypes, args);
|
||||
throw new Error(`missing core export: ${name}`);
|
||||
}
|
||||
|
||||
function pose(core, pointer) {
|
||||
const doubles = core.HEAPF64.subarray(pointer / 8, pointer / 8 + 9);
|
||||
return Object.fromEntries(AXES.map((axis, index) => [axis, doubles[index]]));
|
||||
}
|
||||
|
||||
function setCoordinates(core, pointer) {
|
||||
for (const [index, value] of [..."XYZAC"].entries()) core.HEAPU8[pointer + CONFIG.coordinates + index] = value.charCodeAt(0);
|
||||
}
|
||||
|
||||
function configFor(core, pointer, type, options) {
|
||||
core.HEAPU8.fill(0, pointer, pointer + CONFIG_SIZE);
|
||||
const view = new DataView(core.HEAPU8.buffer, pointer, CONFIG_SIZE);
|
||||
view.setInt32(CONFIG.type, type, true);
|
||||
view.setInt32(CONFIG.mode, 0, true);
|
||||
view.setUint32(CONFIG.joint_count, 5, true);
|
||||
view.setUint32(CONFIG.kinematics_joint_count, 5, true);
|
||||
view.setUint32(CONFIG.extra_joint_count, 0, true);
|
||||
setCoordinates(core, pointer);
|
||||
for (const [index, axis] of COORDINATES.entries()) {
|
||||
const limit = options.limits?.[axis] ?? DEFAULT_LIMITS[axis];
|
||||
core.HEAPU8[pointer + CONFIG.limit_enabled + index] = 1;
|
||||
view.setFloat64(CONFIG.joint_min + index * 8, Number(limit[0]), true);
|
||||
view.setFloat64(CONFIG.joint_max + index * 8, Number(limit[1]), true);
|
||||
}
|
||||
for (const [key, offset] of Object.entries({ x_rot_point: CONFIG.x_rot_point, y_offset: CONFIG.y_offset, z_offset: CONFIG.z_offset, tool_offset: CONFIG.tool_offset })) {
|
||||
view.setFloat64(offset, Number(options[key] ?? 0), true);
|
||||
}
|
||||
}
|
||||
|
||||
function resultCode(operation, result) {
|
||||
return { status: "error", code: `wasm_xyzac_${operation}_${result}`, result };
|
||||
}
|
||||
|
||||
export class WasmXyzacKinematics {
|
||||
constructor(core, options = {}) {
|
||||
this.core = core;
|
||||
this.options = { x_rot_point: 0, y_offset: 20, z_offset: 10, tool_offset: 0, limits: DEFAULT_LIMITS, ...options };
|
||||
this.available = ["sim_kinematics_init", "sim_kinematics_switch_init", "sim_kinematics_switch_register", "sim_kinematics_switch_select", "sim_kinematics_switch_forward", "sim_kinematics_switch_inverse", "sim_kinematics_switch_active", "sim_kinematics_switch_state_hash"].every((name) => typeof core?.[`_${name}`] === "function" || typeof core?.[name] === "function");
|
||||
this.lastCommand = null;
|
||||
this.modules = new Map();
|
||||
this.pointers = [];
|
||||
if (!this.available || typeof core?._malloc !== "function") return;
|
||||
for (const [mode, type] of [[MODE.identity, TYPE.identity], [MODE.xyzac, TYPE.xyzac], [MODE.userk, TYPE.userk]]) {
|
||||
const config = core._malloc(CONFIG_SIZE);
|
||||
const kinematics = core._malloc(KINEMATICS_SIZE);
|
||||
if (!config || !kinematics) throw new Error("wasm_xyzac_allocation_failed");
|
||||
configFor(core, config, type, this.options);
|
||||
const result = call(core, "sim_kinematics_init", "number", ["number", "number"], [kinematics, config]);
|
||||
if (result !== 0) throw new Error(`wasm_xyzac_init_${result}`);
|
||||
this.modules.set(mode, { config, kinematics });
|
||||
this.pointers.push(config, kinematics);
|
||||
}
|
||||
this.switchPointer = core._malloc(SWITCH_SIZE);
|
||||
this.resultPointer = core._malloc(RESULT_INFO_SIZE);
|
||||
this.posePointer = core._malloc(POSE_SIZE);
|
||||
this.jointPointer = core._malloc(JOINT_BYTES);
|
||||
if (![this.switchPointer, this.resultPointer, this.posePointer, this.jointPointer].every(Boolean)) throw new Error("wasm_xyzac_allocation_failed");
|
||||
this.pointers.push(this.switchPointer, this.resultPointer, this.posePointer, this.jointPointer);
|
||||
for (const [pointer, size] of [[this.switchPointer, SWITCH_SIZE], [this.resultPointer, RESULT_INFO_SIZE], [this.posePointer, POSE_SIZE], [this.jointPointer, JOINT_BYTES]]) core.HEAPU8.fill(0, pointer, pointer + size);
|
||||
this.initializeSwitch();
|
||||
}
|
||||
|
||||
initializeSwitch() {
|
||||
const initialized = call(this.core, "sim_kinematics_switch_init", "number", ["number", "number", "number"], [this.switchPointer, MODE.identity, 0]);
|
||||
if (initialized !== 0) throw new Error(`wasm_xyzac_switch_init_${initialized}`);
|
||||
for (const [mode, module] of this.modules) {
|
||||
const registered = call(this.core, "sim_kinematics_switch_register", "number", ["number", "number", "number", "number"], [this.switchPointer, mode, module.kinematics, 0]);
|
||||
if (registered !== 0) throw new Error(`wasm_xyzac_switch_register_${mode}_${registered}`);
|
||||
}
|
||||
}
|
||||
|
||||
get activeMode() {
|
||||
if (!this.available) return MODE.identity;
|
||||
return call(this.core, "sim_kinematics_switch_active", "number", ["number"], [this.switchPointer]);
|
||||
}
|
||||
|
||||
setJoints(joints) {
|
||||
if (!Array.isArray(joints) || joints.length < 5) throw new Error("wasm_xyzac_joints_invalid");
|
||||
this.core.HEAPF64.set(joints.slice(0, 9).map(Number), this.jointPointer / 8);
|
||||
}
|
||||
|
||||
setPose(value) {
|
||||
if (!value || typeof value !== "object") throw new Error("wasm_xyzac_pose_invalid");
|
||||
this.core.HEAPF64.set(AXES.map((axis) => Number(value[axis] ?? 0)), this.posePointer / 8);
|
||||
}
|
||||
|
||||
forward(joints = [0, 0, 0, 0, 0]) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_unavailable" };
|
||||
this.setJoints(joints);
|
||||
const result = call(this.core, "sim_kinematics_switch_forward", "number", ["number", "number", "number", "number", "number"], [this.switchPointer, this.jointPointer, 5, this.posePointer, this.resultPointer]);
|
||||
if (result !== 0) return resultCode("forward", result);
|
||||
return this.snapshot();
|
||||
}
|
||||
|
||||
inverse(currentPose) {
|
||||
if (!this.available) return { status: "blocked", code: "wasm_xyzac_unavailable" };
|
||||
this.setPose(currentPose);
|
||||
const result = call(this.core, "sim_kinematics_switch_inverse", "number", ["number", "number", "number", "number", "number"], [this.switchPointer, this.posePointer, this.jointPointer, 5, this.resultPointer]);
|
||||
if (result !== 0) return resultCode("inverse", result);
|
||||
return this.snapshot();
|
||||
}
|
||||
|
||||
select(mode, joints = [0, 0, 0, 0, 0]) {
|
||||
if (![MODE.identity, MODE.xyzac, MODE.userk].includes(mode)) return { status: "error", code: "wasm_xyzac_invalid_mode" };
|
||||
this.setJoints(joints);
|
||||
const result = call(this.core, "sim_kinematics_switch_select", "number", ["number", "number", "number", "number", "number", "number"], [this.switchPointer, mode, this.jointPointer, 5, this.posePointer, this.resultPointer]);
|
||||
if (result !== 0) return resultCode("switch", result);
|
||||
this.lastCommand = `M${mode === MODE.xyzac ? 428 : mode === MODE.identity ? 429 : 430}`;
|
||||
return this.snapshot();
|
||||
}
|
||||
|
||||
restoreState(state) {
|
||||
if (!state || state.schema !== "wasm-xyzac-kinematics.v1" || ![MODE.identity, MODE.xyzac, MODE.userk].includes(state.active_type) || !Array.isArray(state.joints) || state.joints.length !== 5) throw new Error("wasm_xyzac_restore_state_invalid");
|
||||
this.initializeSwitch();
|
||||
this.setPose(state.pose);
|
||||
this.setJoints(state.joints);
|
||||
if (state.active_type !== MODE.identity) {
|
||||
let result = call(this.core, "sim_kinematics_switch_begin", "number", ["number", "number"], [this.switchPointer, state.active_type]);
|
||||
if (result === 0) result = call(this.core, "sim_kinematics_switch_commit", "number", ["number", "number", "number", "number", "number"], [this.switchPointer, this.jointPointer, 5, this.posePointer, this.resultPointer]);
|
||||
if (result !== 0) throw new Error(`wasm_xyzac_restore_switch_${result}`);
|
||||
} else {
|
||||
const result = call(this.core, "sim_kinematics_switch_inverse", "number", ["number", "number", "number", "number", "number"], [this.switchPointer, this.posePointer, this.jointPointer, 5, this.resultPointer]);
|
||||
if (result !== 0) throw new Error(`wasm_xyzac_restore_inverse_${result}`);
|
||||
}
|
||||
this.setJoints(state.joints);
|
||||
this.setPose(state.pose);
|
||||
const view = new DataView(this.core.HEAPU8.buffer, this.switchPointer, SWITCH_SIZE);
|
||||
view.setUint8(SWITCH.switching, 0);
|
||||
view.setInt32(SWITCH.active_mode, state.active_type, true);
|
||||
view.setInt32(SWITCH.pending_mode, state.active_type, true);
|
||||
view.setUint32(SWITCH.generation, Number(state.generation ?? 0), true);
|
||||
view.setUint32(SWITCH.switch_count, Number(state.switch_count ?? state.generation ?? 0), true);
|
||||
view.setUint32(SWITCH.abort_count, Number(state.abort_count ?? 0), true);
|
||||
this.lastCommand = state.last_command ?? null;
|
||||
const restored = this.snapshot();
|
||||
if (restored.active_type !== state.active_type) throw new Error("wasm_xyzac_restore_projection_mismatch");
|
||||
return restored;
|
||||
}
|
||||
|
||||
snapshot(currentPose = pose(this.core, this.posePointer), joints = [...this.core.HEAPF64.subarray(this.jointPointer / 8, this.jointPointer / 8 + 5)]) {
|
||||
const activeMode = this.activeMode;
|
||||
const nativeType = activeMode === MODE.xyzac ? "xyzac-trt-kins" : activeMode === MODE.userk ? "userk-identity-template" : "identity";
|
||||
const view = new DataView(this.core.HEAPU8.buffer, this.switchPointer, SWITCH_SIZE);
|
||||
const limits = Object.fromEntries(COORDINATES.map((axis) => [axis, [...(this.options.limits?.[axis] ?? DEFAULT_LIMITS[axis])]]));
|
||||
const unsigned = { schema: "wasm-xyzac-kinematics.v1", active_type: activeMode, native_type: nativeType, coordinates: "XYZAC", joint_count: 5, linear_units: "mm", angular_units: "deg", pose: currentPose, joints: joints.slice(0, 5), offsets: { x_rot_point: this.options.x_rot_point, y_offset: this.options.y_offset, z_offset: this.options.z_offset, tool_offset: this.options.tool_offset }, limits, generation: view.getUint32(SWITCH.generation, true), switch_count: view.getUint32(SWITCH.switch_count, true), abort_count: view.getUint32(SWITCH.abort_count, true), last_command: this.lastCommand, capability: { userk: "linked_identity_template", dynamic_modules: "denied" }, source: "linuxcnc-wasm-sim_kinematics_switch" };
|
||||
const hash = call(this.core, "sim_kinematics_switch_state_hash", "number", ["number", "number", "number", "number"], [this.switchPointer, this.jointPointer, 5, this.posePointer]);
|
||||
return { status: "verified", ...unsigned, wasm_state_hash: `fnv1a64:${BigInt.asUintN(64, BigInt(hash >>> 0)).toString(16).padStart(16, "0")}`, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
destroy() { for (const pointer of this.pointers) if (pointer && typeof this.core?._free === "function") this.core._free(pointer); this.pointers = []; }
|
||||
}
|
||||
|
||||
export { MODE as XYZAC_MODES };
|
||||
54
web/worker/xyzac-command-ledger.mjs
Normal file
54
web/worker/xyzac-command-ledger.mjs
Normal file
@@ -0,0 +1,54 @@
|
||||
import { OperationLedger, LEDGER_SCHEMA, LEDGER_PHASES } from "./operation-ledger.mjs";
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
export const XYZAC_COMMAND_LEDGER_SCHEMA = "xyzac-command-ledger.v1";
|
||||
const TERMINAL_PHASES = new Set(["committed", "aborted", "failed"]);
|
||||
|
||||
export class XyzacCommandLedger extends OperationLedger {
|
||||
constructor(entries = []) {
|
||||
super(entries);
|
||||
this.scope = "selected_xyzac_simulation";
|
||||
}
|
||||
|
||||
prepare(input) {
|
||||
if (["HELLO", "CAPABILITIES", "SNAPSHOT_REQUEST", "CANCEL"].includes(input?.kind)) return { status: "untracked", replay: false, entry: null };
|
||||
const result = super.prepare(input);
|
||||
if (result.entry) {
|
||||
result.entry.scope = this.scope;
|
||||
result.entry.intent_hash = result.entry.intent_hash ?? fnv1a64({ kind: input.kind, payload: input.payload ?? {}, operation_id: input.operation_id });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
acknowledge(operationId, response) {
|
||||
return this.verify(operationId, response);
|
||||
}
|
||||
|
||||
result(operationId, response, seq) {
|
||||
this.acknowledge(operationId, response);
|
||||
return this.commit(operationId, seq);
|
||||
}
|
||||
|
||||
pending() {
|
||||
return [...this.entries.values()]
|
||||
.filter((entry) => !TERMINAL_PHASES.has(entry.phase))
|
||||
.sort((left, right) => left.seq - right.seq)
|
||||
.map((entry) => structuredClone(entry));
|
||||
}
|
||||
|
||||
reconcileAll() {
|
||||
return this.pending().map((entry) => ({ operation_id: entry.operation_id, status: entry.phase, entry }));
|
||||
}
|
||||
|
||||
timeoutReconcile(operationId) {
|
||||
const reconciliation = this.reconcile(operationId);
|
||||
return { status: "reconcile_required", operation_id: operationId, retry_allowed: false, reconciliation };
|
||||
}
|
||||
|
||||
audit() {
|
||||
const exported = super.export();
|
||||
const pending = this.pending();
|
||||
const unsigned = { schema: XYZAC_COMMAND_LEDGER_SCHEMA, operation_schema: LEDGER_SCHEMA, phases: [...LEDGER_PHASES], scope: this.scope, entries: exported.entries, pending_count: pending.length };
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
}
|
||||
45
web/worker/xyzac-scheduler.mjs
Normal file
45
web/worker/xyzac-scheduler.mjs
Normal file
@@ -0,0 +1,45 @@
|
||||
import { SimScheduler, SCHEDULER_PHASES } from "./sim-scheduler.mjs";
|
||||
import { fnv1a64 } from "./replay.mjs";
|
||||
|
||||
export const XYZAC_SCHEDULER_SCHEMA = "xyzac-scheduler.v1";
|
||||
export const XYZAC_SCHEDULER_PHASES = Object.freeze([...SCHEDULER_PHASES]);
|
||||
|
||||
export class XyzacScheduler extends SimScheduler {
|
||||
constructor(core, { budget = 1_000_000, cycleTime = 0.01 } = {}) {
|
||||
super(core, { budget });
|
||||
this.cycle_time = cycleTime;
|
||||
this.authority = "linuxcnc-wasm-worker";
|
||||
this.clock = "logical_tick";
|
||||
this.machine = Object.freeze({ coordinates: "XYZAC", joint_count: 5, rotary_units: "degrees", kinematics: "xyzac-trt" });
|
||||
}
|
||||
|
||||
step(cycles, callbacks = {}) {
|
||||
if (!Number.isSafeInteger(cycles) || cycles < 0) throw new Error("xyzac_scheduler_cycles_invalid");
|
||||
const result = super.step(cycles, callbacks);
|
||||
return this.export({ last_step_cycles: cycles, state_hash: result.state_hash });
|
||||
}
|
||||
|
||||
export(extra = {}) {
|
||||
const unsigned = {
|
||||
schema: XYZAC_SCHEDULER_SCHEMA,
|
||||
authority: this.authority,
|
||||
clock: this.clock,
|
||||
cycle_time: this.cycle_time,
|
||||
machine: this.machine,
|
||||
budget: this.budget,
|
||||
tick: this.tick,
|
||||
last_cycles: this.last_cycles,
|
||||
phase_trace: [...this.phase_trace],
|
||||
...extra,
|
||||
};
|
||||
delete unsigned.state_hash;
|
||||
return { ...unsigned, state_hash: fnv1a64(unsigned) };
|
||||
}
|
||||
|
||||
restore(value) {
|
||||
if (!value || value.schema !== XYZAC_SCHEDULER_SCHEMA || value.clock !== "logical_tick" || value.machine?.coordinates !== "XYZAC" || value.machine?.joint_count !== 5) throw new Error("xyzac_scheduler_restore_scope");
|
||||
const base = { schema: "sim_scheduler.v1", budget: value.budget, last_cycles: value.last_cycles, phase_trace: value.phase_trace, tick: value.tick, state_hash: fnv1a64({ schema: "sim_scheduler.v1", budget: value.budget, phase_trace: value.phase_trace, tick: value.tick }) };
|
||||
super.restore(base);
|
||||
return this.export();
|
||||
}
|
||||
}
|
||||
40
web/worker/xyzac-session.mjs
Normal file
40
web/worker/xyzac-session.mjs
Normal file
@@ -0,0 +1,40 @@
|
||||
import { SimSession, CONTEXT_ID, PROTOCOL_SCHEMA } from "./sim-session.mjs";
|
||||
import { XyzacCommandLedger, XYZAC_COMMAND_LEDGER_SCHEMA } from "./xyzac-command-ledger.mjs";
|
||||
import { XyzacScheduler, XYZAC_SCHEDULER_SCHEMA } from "./xyzac-scheduler.mjs";
|
||||
|
||||
export const XYZAC_SESSION_SCHEMA = "xyzac-session.v1";
|
||||
|
||||
export class XyzacSession extends SimSession {
|
||||
constructor(core, options = {}) {
|
||||
super(core, options);
|
||||
this.ledger = new XyzacCommandLedger();
|
||||
this.scheduler = new XyzacScheduler(core);
|
||||
this.session_scope = "selected_xyzac_simulation";
|
||||
}
|
||||
|
||||
authority() {
|
||||
return {
|
||||
schema: XYZAC_SESSION_SCHEMA,
|
||||
authority: "linuxcnc-wasm-worker",
|
||||
context_id: this.contextId,
|
||||
session_id: this.sessionId,
|
||||
scope: this.session_scope,
|
||||
protocol_schema: PROTOCOL_SCHEMA,
|
||||
ledger_schema: XYZAC_COMMAND_LEDGER_SCHEMA,
|
||||
scheduler_schema: XYZAC_SCHEDULER_SCHEMA,
|
||||
machine: { coordinates: "XYZAC", joint_count: 5, kinematics: "xyzac-trt", rotary_units: "degrees" },
|
||||
};
|
||||
}
|
||||
|
||||
accept(input) {
|
||||
const response = super.accept(input);
|
||||
if (response?.payload && typeof response.payload === "object") {
|
||||
response.payload.xyzac_session = this.authority();
|
||||
response.payload.xyzac_ledger = this.ledger.audit();
|
||||
response.payload.xyzac_scheduler = this.scheduler.export();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
export { CONTEXT_ID, PROTOCOL_SCHEMA };
|
||||
Reference in New Issue
Block a user