Wire AXIS controls through operation gates
This commit is contained in:
@@ -5,6 +5,7 @@ import { ThreeMachineView } from "./ThreeMachineView";
|
||||
import { SIM_CASE_CATALOG } from "../../workspace/sim-case-catalog.mjs";
|
||||
import { loadSimulationCase, toWorkerConfiguration } from "../../workspace/sim-case-loader.mjs";
|
||||
import { axisIcon } from "./sourceAssets";
|
||||
import { AxisGateError, AxisOperationController } from "../../app/axis-operation-controller.mjs";
|
||||
|
||||
const DEFAULT_CASE = "linuxcnc/configs/sim/axis/minimal_xyz.ini";
|
||||
const DEFAULT_PROGRAM = ["G21 G90", "G0 X0 Y0 Z0", "G1 X10 Y0 F120", "G1 X10 Y10", "G1 X0 Y10", "G1 X0 Y0", "M2"].join("\n");
|
||||
@@ -68,6 +69,7 @@ function ProgramPane({ text, setText, activeGcodes, onLoad, onRun, onStep, ready
|
||||
|
||||
export function App() {
|
||||
const client = useMemo(() => new ReactWorkerClient(), []);
|
||||
const operationController = useMemo(() => new AxisOperationController(client), [client]);
|
||||
const [state, setState] = useState(INITIAL_STATE);
|
||||
const [casePath, setCasePath] = useState(DEFAULT_CASE);
|
||||
const [caseStatus, setCaseStatus] = useState("ready to load");
|
||||
@@ -87,10 +89,36 @@ export function App() {
|
||||
useEffect(() => { const unsubscribe = client.subscribe((message) => setState((previous) => reduceFrontendState(previous, message))); client.ensureSession().catch((error: Error) => setCommandStatus(`Error: ${error.message}`)); return () => { unsubscribe(); if (runTimer.current) clearInterval(runTimer.current); client.close(); }; }, [client]);
|
||||
const axes = state.snapshot?.motion?.axes ?? [0, 0, 0];
|
||||
const ready = ["ready", "running", "paused"].includes(state.status);
|
||||
const execute: Action = (kind, payload = {}) => { setCommandStatus(`${kind}...`); client.send(kind, payload, `axis-${kind.toLowerCase()}-${Date.now()}`).then(() => setCommandStatus("Acknowledged")).catch((error: Error) => setCommandStatus(`Blocked: ${error.message}`)); };
|
||||
const runProgram = () => { execute("PROGRAM_RUN"); if (runTimer.current) clearInterval(runTimer.current); runTimer.current = setInterval(() => { client.send("SIM_STEP", { cycles: 1 }, `axis-tick-${Date.now()}`).catch(() => undefined); }, 50); };
|
||||
const axisState = { task: { ...(state.task ?? {}), state: state.task?.state ?? (ready ? "ON" : "OFF"), mode: state.task?.mode ?? "MANUAL", interp_state: state.task?.interp_state ?? (state.status === "running" ? "READING" : state.status === "paused" ? "PAUSED" : "IDLE") }, program: state.program ?? {}, motion: state.snapshot?.motion ?? state.snapshot?.motion ?? {}, devices: state.devices ?? {} };
|
||||
const execute: Action = (kind, payload = {}) => {
|
||||
setCommandStatus(`${kind} pending`);
|
||||
operationController.execute(kind, payload, { state: axisState }).then(() => setCommandStatus(`${kind} acknowledged`)).catch((error: Error) => {
|
||||
const code = error instanceof AxisGateError ? error.code : error.message;
|
||||
setCommandStatus(`${kind} blocked: ${code}`);
|
||||
});
|
||||
};
|
||||
const runProgram = () => { execute("PROGRAM_RUN"); if (runTimer.current) clearInterval(runTimer.current); runTimer.current = setInterval(() => { operationController.execute("SIM_STEP", { cycles: 1 }, { state: axisState, gate: false }).catch(() => undefined); }, 50); };
|
||||
const stopProgram = () => { if (runTimer.current) { clearInterval(runTimer.current); runTimer.current = null; } execute("PROGRAM_ABORT"); };
|
||||
const loadCase = async () => { setCaseStatus("loading..."); try { await client.ensureSession(); const bundle = await loadSimulationCase({ path: casePath }); if (bundle.status !== "ready") { setCaseStatus(`blocked: ${bundle.missing.join(", ")}`); return; } const result = await client.send("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }, "axis-load-case"); const resultPayload = result.payload as { configuration?: { state?: string } }; setCaseStatus(`${bundle.entrypoint} -> ${resultPayload.configuration?.state ?? "READY"}`); } catch (error) { setCaseStatus(`blocked: ${error instanceof Error ? error.message : "case_load_failed"}`); } };
|
||||
const loadCase = async () => { setCaseStatus("loading..."); try { await client.ensureSession(); const bundle = await loadSimulationCase({ path: casePath }); if (bundle.status !== "ready") { setCaseStatus(`blocked: ${bundle.missing.join(", ")}`); return; } const operation = await operationController.execute("LOAD_WORKSPACE", { root_path: "/workspace", configuration: toWorkerConfiguration(bundle) }, { state: axisState, gate: false }); const resultPayload = (operation.response as { payload?: { configuration?: { state?: string } } }).payload; setCaseStatus(`${bundle.entrypoint} -> ${resultPayload?.configuration?.state ?? "READY"}`); } catch (error) { setCaseStatus(`blocked: ${error instanceof Error ? error.message : "case_load_failed"}`); } };
|
||||
useEffect(() => {
|
||||
const onKeyDown = (event: KeyboardEvent) => {
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (target && ["INPUT", "TEXTAREA", "SELECT"].includes(target.tagName)) return;
|
||||
const key = event.key.toLowerCase();
|
||||
if (event.ctrlKey && key === "k") { event.preventDefault(); setCommandStatus("Live plot cleared"); return; }
|
||||
if (key === "f3") { event.preventDefault(); setLeftTab("manual"); return; }
|
||||
if (key === "f5") { event.preventDefault(); setLeftTab("mdi"); return; }
|
||||
if (key === "f1") { event.preventDefault(); execute("ESTOP", { active: true }); return; }
|
||||
if (key === "f2") { event.preventDefault(); execute("IO_RECOVER"); return; }
|
||||
if (key === "r") { event.preventDefault(); runProgram(); return; }
|
||||
if (key === "t") { event.preventDefault(); execute("PROGRAM_STEP"); return; }
|
||||
if (key === "p") { event.preventDefault(); execute("PROGRAM_PAUSE"); return; }
|
||||
if (key === "s") { event.preventDefault(); execute("PROGRAM_RESUME"); return; }
|
||||
if (key === "escape") { event.preventDefault(); stopProgram(); }
|
||||
};
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
return () => window.removeEventListener("keydown", onKeyDown);
|
||||
}, [axisState, execute, runProgram, stopProgram]);
|
||||
const onSpindle = (direction: number) => { setSpindleOn(direction !== 0); execute("SPINDLE_COMMAND", { spindle: 0, direction, rpm: direction === 0 ? 0 : 600, enabled: direction !== 0 }); };
|
||||
const onCoolant = (key: "mist" | "flood") => { const next = { ...coolant, [key]: !coolant[key] }; setCoolant(next); execute("COOLANT_COMMAND", { mist: next.mist, flood: next.flood, lube: false }); };
|
||||
const activeGcodes = state.program?.active_gcodes ? String(state.program.active_gcodes) : "G21 G90\nG17 G40 G49\nG54 G64\nM5 M9";
|
||||
|
||||
6
web/react/src/legacy.d.ts
vendored
6
web/react/src/legacy.d.ts
vendored
@@ -3,6 +3,12 @@ declare module "*.mjs" {
|
||||
export function loadSimulationCase(options: { path: string }): Promise<{ status: string; entrypoint: string; files: Record<string, string>; missing: string[]; fetched: string[]; state_hash: string; halManifest: unknown[] }>;
|
||||
export function toWorkerConfiguration(bundle: unknown): Record<string, unknown>;
|
||||
export class SimSession { contextId: string; sessionId: string; constructor(core: unknown); accept(message: unknown): unknown; }
|
||||
export class AxisOperationController {
|
||||
constructor(client: { send(kind: string, payload?: unknown, operationId?: string): Promise<unknown> });
|
||||
execute(kind: string, payload?: Record<string, unknown>, options?: { state?: unknown; action?: string | null; gate?: boolean }): Promise<{ operationId: string; response: unknown; gate: { enabled: boolean; reason: string } }>;
|
||||
exportTrace(): Array<Record<string, unknown>>;
|
||||
}
|
||||
export class AxisGateError extends Error { code: string; gate: { enabled: boolean; reason: string }; }
|
||||
}
|
||||
|
||||
declare module "*.js" { const createModule: (options?: unknown) => Promise<unknown>; export default createModule; }
|
||||
|
||||
Reference in New Issue
Block a user