From 6e8fabd230a4f5be6f30087457f3339e8bb74fa0 Mon Sep 17 00:00:00 2001 From: chk <79915315+ChKendel@users.noreply.github.com> Date: Sat, 9 May 2026 06:08:36 +0200 Subject: [PATCH] Tests CalculateAngles verpacken --- package.json | 2 +- public/calculateAngles.cjs | 3 ++ public/calculateAngles.js | 43 +++++++++++++++++++++++++ public/calculateAnglesBrowser.js | 9 ++++++ public/robot.json | 6 ++++ test/calculateAngles.test.js | 55 ++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 public/calculateAngles.cjs create mode 100644 public/calculateAngles.js create mode 100644 public/calculateAnglesBrowser.js create mode 100644 test/calculateAngles.test.js diff --git a/package.json b/package.json index d28e1da..98fca1d 100755 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "test:coverage": "jest --coverage" }, "jest": { - "testEnvironment": "jsdom" + "testEnvironment": "node" }, "dependencies": { "dotenv": "^16.4.5", diff --git a/public/calculateAngles.cjs b/public/calculateAngles.cjs new file mode 100644 index 0000000..75dd0ed --- /dev/null +++ b/public/calculateAngles.cjs @@ -0,0 +1,3 @@ + +const { calculate } = require('./calculateAngles.js'); +module.exports = { calculate }; diff --git a/public/calculateAngles.js b/public/calculateAngles.js new file mode 100644 index 0000000..1f92de1 --- /dev/null +++ b/public/calculateAngles.js @@ -0,0 +1,43 @@ +/** + * calculateAngles module + * + * Browser + Server + Jest (CJS) kompatibel + */ + +function calculate(foundMarkers, jsonRobot) { + // --- X of the Hand-Joint --- + const validOns = new Set(['Base', 'Arm1', 'Joint1']); + const foundById = new Map(foundMarkers.map(f => [f.id, f])); + + const markersMovingFixedX = jsonRobot.Marker.filter(m => validOns.has(m.on)); + const markersXPosition = new Map(); + + const xs = markersMovingFixedX + .map(m => { + const f = foundById.get(m.id); + return f ? f.position[0] - m.relPos[0] : null; + }) + .filter(x => x !== null); + + const xAvg = xs.length + ? xs.reduce((a, b) => a + b, 0) / xs.length + : 0; + + return { + meta: { + module: 'calculateAngles', + timestamp: new Date().toISOString() + }, + inputs: { + markers: foundMarkers ?? null, + robot: jsonRobot ?? null + }, + status: 'ok', + result: { + xAvg + } + }; +} + + +module.exports = { calculate }; \ No newline at end of file diff --git a/public/calculateAnglesBrowser.js b/public/calculateAnglesBrowser.js new file mode 100644 index 0000000..ad69ec9 --- /dev/null +++ b/public/calculateAnglesBrowser.js @@ -0,0 +1,9 @@ + +import { calculate } from './calculateAngles.js'; + +if (typeof window !== 'undefined') { + window.calculateAngles = window.calculateAngles || {}; + window.calculateAngles.calculate = calculate; +} + +export { calculate }; diff --git a/public/robot.json b/public/robot.json index f410734..dae1a73 100644 --- a/public/robot.json +++ b/public/robot.json @@ -1,5 +1,11 @@ { "Elements":["Board","Base","Arm1","Joint1","Arm2","Finger1","Finger2"], + "Joints":{ + "jointA":{"name":"Slider", "type":"lninear", "axis":[1,0,0],"parent":"Board","child":"Base"}, + "jointB":{"name":"Shoulder","type":"revolute","axis":[1,0,0],"parent":"Base","child":"Arm1","origin":[-89.5, 115, 61]}, + "jointC":{"name":"EllbowLift","type":"revolute","axis":[1,0,0],"parent":"Arm1","child":"Joint1"}, + "jointD":{"name":"EllbowTwist","type":"revolute","axis":[0,1,0],"parent":"Joint1","child":"Arm2"} + }, "MarkerType":"DICT_4X4_250", "Marker":[ {"id":205,"on":"Board","position":[0.80, -0.090, 0.0]}, diff --git a/test/calculateAngles.test.js b/test/calculateAngles.test.js new file mode 100644 index 0000000..9109fd3 --- /dev/null +++ b/test/calculateAngles.test.js @@ -0,0 +1,55 @@ +const fs = require('fs'); +const path = require('path'); +const { calculate } = require('../public/calculateAngles.cjs'); + +/* ------------------------- + Hilfsfunktionen +------------------------- */ + +function loadMarkersFromCSV(filePath) { + const text = fs.readFileSync(filePath, 'utf8'); + + const [header, ...lines] = text + .split('\n') + .map(l => l.trim()) + .filter(Boolean); + + return lines.map(line => { + const [ + id, + x, y, z, + roll, pitch, yaw, + seen_by + ] = line.split(','); + + return { + id: Number(id), + position: [Number(x), Number(y), Number(z)], + rotation: [Number(roll), Number(pitch), Number(yaw)], + seen_by: Number(seen_by) + }; + }); +} + +function loadJSON(filePath) { + return JSON.parse(fs.readFileSync(filePath, 'utf8')); +} + +/* ------------------------- + Tests +------------------------- */ + +describe('calculateAngles.calculate', () => { + test('berechnet X-Durchschnitt für Base / Arm1 / Joint1', async () => { + const markersPath = path.resolve('./test/snapshots/snapshot_video0_1774805028717_two_cam.csv'); + const robotPath = path.resolve('./public/robot.json'); + + const foundMarkers = loadMarkersFromCSV(markersPath); + const jsonRobot = loadJSON(robotPath); + + const result = await calculate(foundMarkers, jsonRobot); + + expect(result.status).toBe('ok'); + expect(result.result).toBeDefined(); + }); +});