Files
appRobotHoming/server/buildG92.cjs
2026-06-25 17:34:41 +02:00

49 lines
2.0 KiB
JavaScript

/**
* buildG92.cjs
* Baut aus einem Homing-State {x,y,z,a,b,c,e} einen G92-G-Code-String.
*
* G92 setzt am appRobotDriver die Motorposition OHNE Bewegung (intern als M92
* verarbeitet, siehe appRobotDriver/doc/API.md + robot/RobotController.js) —
* exakt die Homing-Semantik. Die Achsbuchstaben bilden 1:1 auf die Motorachsen
* ab: X→xMotor, Y→alpha, Z→beta, A→a, B→b, C→c, E→e.
*
* Bekannte Achsen werden immer mit ihrem realen Wert gesendet. Welche Achsen
* bekannt sind, hängt vom Pfad ab:
* - 5_pose_estimation.py (Fallback) liefert alle 7 (x,y,z,a,b,c,e),
* - die 4b-Primärkette (Arm1→y … Hand→b) liefert nur x,y,z,a,b.
* Eine Achse, die wirklich fehlt oder als unbeobachtbar `null` markiert ist,
* wird per Default WEGGELASSEN — der Driver lässt nicht genannte Achsen
* unverändert (M92 setzt nur Achsen mit endlichem Zahlenwert), statt eine
* unbekannte Position fälschlich als 0 zu behaupten. `fillMissingWithZero`
* erzwingt bei Bedarf das alte 0-Auffüllen.
*
* CommonJS, damit Jest (CJS) und der ESM-Server dieselbe Funktion nutzen
* (gleiches Muster wie spinNormalize.cjs / homingXEstimate.cjs).
*/
// Reihenfolge + Achsbuchstaben wie vom Driver erwartet.
const AXES = [
['x', 'X'], ['y', 'Y'], ['z', 'Z'],
['a', 'A'], ['b', 'B'], ['c', 'C'], ['e', 'E'],
];
/**
* @param {Record<string, number|null>} state flacher Joint-State (accumulated_state)
* @param {{decimals?: number, fillMissingWithZero?: boolean}} [opts]
* @returns {string} z.B. "G92 X164.57 Y-2.09 Z60.58 A86.75 B-46.97 C-64.91 E22.59"
*/
function buildG92(state = {}, { decimals = 2, fillMissingWithZero = false } = {}) {
const parts = [];
for (const [key, axis] of AXES) {
const num = Number(state?.[key]);
if (state?.[key] != null && Number.isFinite(num)) {
parts.push(`${axis}${num.toFixed(decimals)}`);
} else if (fillMissingWithZero) {
parts.push(`${axis}${(0).toFixed(decimals)}`);
}
}
return `G92 ${parts.join(' ')}`;
}
module.exports = { buildG92, AXES };