CalculateAngles verpacken
This commit is contained in:
chk
2026-05-09 06:08:36 +02:00
parent 07e1378309
commit 6e8fabd230
6 changed files with 117 additions and 1 deletions

View File

@@ -13,7 +13,7 @@
"test:coverage": "jest --coverage"
},
"jest": {
"testEnvironment": "jsdom"
"testEnvironment": "node"
},
"dependencies": {
"dotenv": "^16.4.5",

View File

@@ -0,0 +1,3 @@
const { calculate } = require('./calculateAngles.js');
module.exports = { calculate };

43
public/calculateAngles.js Normal file
View File

@@ -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 };

View File

@@ -0,0 +1,9 @@
import { calculate } from './calculateAngles.js';
if (typeof window !== 'undefined') {
window.calculateAngles = window.calculateAngles || {};
window.calculateAngles.calculate = calculate;
}
export { calculate };

View File

@@ -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]},

View File

@@ -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();
});
});