Complete XYZAC LinuxCNC WASM parity and smooth 3D preview
This commit is contained in:
@@ -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] } };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user