arbeiten am callibration

This commit is contained in:
chk
2026-06-13 00:00:18 +02:00
parent e0e4212a90
commit 1762a771cf
51 changed files with 30343 additions and 5 deletions

69
server/robotActions.js Normal file
View File

@@ -0,0 +1,69 @@
/**
* robotActions.js Roboter-Bewegungsaktionen
*
* Alle Bewegungsbefehle laufen hier durch, bevor sie ans Roboter-Backend
* weitergeleitet werden (ROBOT_URL). Solange ROBOT_URL nicht konfiguriert
* ist, werden die Aktionen nur geloggt und eine Stub-Antwort zurückgegeben.
*
* Joint-Namen: 'x-axis' | 'arm1' | 'arm2' | 'elbow' | 'hand'
* Directions: 'left' | 'right' (x-axis, linear)
* 'cw' | 'ccw' (alle Gelenke, rotatorisch)
*/
const ROBOT_URL = process.env.ROBOT_URL || '';
// ── Validierung ───────────────────────────────────────────────────────────────
const VALID_JOINTS = new Set(['x-axis', 'arm1', 'arm2', 'elbow', 'hand']);
const VALID_DIRECTIONS = new Set(['left', 'right', 'cw', 'ccw']);
function validateMove({ joint, direction, steps = 1 }) {
if (!joint) return '"joint" fehlt';
if (!direction) return '"direction" fehlt';
if (!VALID_JOINTS.has(joint)) return `Unbekanntes Joint: "${joint}"`;
if (!VALID_DIRECTIONS.has(direction)) return `Unbekannte Richtung: "${direction}"`;
if (typeof steps !== 'number' || steps < 1 || steps > 100)
return '"steps" muss eine Zahl zwischen 1 und 100 sein';
return null; // ok
}
// ── Ausführung ────────────────────────────────────────────────────────────────
/**
* Sendet einen Bewegungsbefehl.
* Gibt { ok, joint, direction, steps, message } zurück oder wirft einen Fehler.
*/
export async function executeMove({ joint, direction, steps = 1 }) {
const err = validateMove({ joint, direction, steps });
if (err) throw Object.assign(new Error(err), { statusCode: 400 });
const payload = { joint, direction, steps };
console.log(`[robotActions] move: ${JSON.stringify(payload)}`);
if (ROBOT_URL) {
// ── Weiterleitung an das Roboter-Backend ─────────────────────────────────
const url = new URL('/api/move', ROBOT_URL).toString();
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) {
const text = await res.text().catch(() => '');
throw Object.assign(
new Error(`Robot-Backend: ${res.status} ${text}`),
{ statusCode: 502 }
);
}
const data = await res.json();
return { ok: true, joint, direction, steps, ...data };
}
// ── Stub (ROBOT_URL nicht konfiguriert) ───────────────────────────────────
const label = joint === 'x-axis'
? `X-Achse ${direction === 'left' ? '⬅' : '➡'}`
: `${joint} ${direction === 'ccw' ? '↺ Rauf' : 'Runter ↻'}`;
const message = `[Stub] ${label} ${steps} Schritt(e). ROBOT_URL nicht konfiguriert.`;
console.warn(`[robotActions] ${message}`);
return { ok: true, stub: true, joint, direction, steps, message };
}

View File

@@ -937,6 +937,54 @@ app.post('/api/calibration/upload-npz', async (req, res) => {
}
});
// ── X-Achse / Rotations-Detektion ────────────────────────────────────────────
const xaxisDataDir = path.join(__dirname, '..', 'data', 'xaxis');
const ROTATION_DETECTION_FILE = path.join(xaxisDataDir, 'rotation_detection.json');
/** POST /api/xaxis/save-rotation-detection
* Speichert eine Achsmessung an rotation_detection.json (append-Modus). */
app.post('/api/xaxis/save-rotation-detection', express.json(), async (req, res) => {
try {
const { axis, runs, numMarkers, markers } = req.body ?? {};
if (!axis || !axis.dir || !axis.referencePoint) {
return res.status(400).json({ error: 'Ungültige Nutzlast: axis.dir und axis.referencePoint erwartet' });
}
// Verzeichnis anlegen falls nötig
await fsPromises.mkdir(xaxisDataDir, { recursive: true });
// Bestehende Einträge lesen oder leer beginnen
let entries = [];
try {
const raw = await fsPromises.readFile(ROTATION_DETECTION_FILE, 'utf-8');
entries = JSON.parse(raw);
if (!Array.isArray(entries)) entries = [];
} catch {
// Datei existiert noch nicht kein Fehler
}
const newEntry = {
timestamp: new Date().toISOString(),
runs: runs ?? {},
axis,
numMarkers: numMarkers ?? null,
markers: markers ?? [],
};
entries.push(newEntry);
await fsPromises.writeFile(ROTATION_DETECTION_FILE, JSON.stringify(entries, null, 2), 'utf-8');
return res.json({
file: 'data/xaxis/rotation_detection.json',
total: entries.length,
});
} catch (err) {
console.error('save-rotation-detection error:', err);
return res.status(500).json({ error: String(err) });
}
});
async function checkServiceReachability(name, url) {
try {
const controller = new AbortController();