Optimize XYZAC motion updates and persist power-off recovery

This commit is contained in:
2026-08-29 04:52:44 +08:00
parent a97971ea19
commit f6e545018e
17 changed files with 479 additions and 74 deletions

View File

@@ -5,6 +5,15 @@ const SEGMENT_BYTES = 216;
const POSE_BYTES = 72;
const AXES = ["x", "y", "z", "a", "b", "c", "u", "v", "w"];
const DEFAULT_RAPID_VELOCITY = 1000;
const PROGRAM_ERROR_REASONS = Object.freeze({
[-1100]: "wasm_program_invalid_argument", [-1101]: "wasm_program_range_error", [-1102]: "wasm_program_invalid_state",
[-1103]: "wasm_program_control_error", [-1104]: "wasm_program_interpreter_error", [-1105]: "wasm_program_motion_error",
});
function programError(errorPrefix, result) {
const reason = PROGRAM_ERROR_REASONS[result] ?? `wasm_${errorPrefix}_${result}`;
return { status: "error", code: `wasm_${errorPrefix}_${result}`, result, reason, message_key: reason, source: "linuxcnc-wasm-sim_program" };
}
function call(core, name, returnType, argTypes, args) {
const fn = core?.[`_${name}`] ?? core?.[name];
@@ -69,7 +78,7 @@ export class WasmProgramRuntime {
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 };
return result === 0 ? { status: "verified", command: name } : { status: "error", code: `wasm_${errorPrefix}_${result}`, result, reason: PROGRAM_ERROR_REASONS[result] ?? `wasm_${errorPrefix}_${result}`, message_key: PROGRAM_ERROR_REASONS[result] ?? `wasm_${errorPrefix}_${result}`, source: "linuxcnc-wasm-sim_program" };
}
load(source, path = null) {
@@ -79,7 +88,7 @@ export class WasmProgramRuntime {
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}` };
if (result !== 0) return programError("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;
@@ -191,7 +200,7 @@ export class WasmProgramRuntime {
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}` };
if (result !== 0) return programError("program_execute", result);
return { status: "verified", program: this.snapshot(), motion: this.readMotion() };
} finally { this.core._free(pointer); }
}