kinematic motion controller

This commit is contained in:
osiu97
2026-07-08 18:14:17 +02:00
parent e1416f6979
commit 57ea3f7b4a
6 changed files with 220 additions and 138 deletions
+7 -24
View File
@@ -15,6 +15,7 @@
import BLE from '../ble.js';
import IK from '../kinematics.js';
import UI from '../ui.js';
import Motion from '../motion.js';
// ── SVG viewport ──────────────────────────────────────────────────
// We map the real workspace onto the SVG canvas.
@@ -59,6 +60,7 @@ function syncStateFromStorage() {
steps1 = 1024;
localStorage.setItem('wiji_steps1', 1024);
}
Motion.setSteps(steps1, steps2);
}
function saveSteps() {
@@ -186,36 +188,14 @@ function showTargetMarker(sx, sy) {
// ─────────────────────────────────────────────────────────────────
async function moveToXY(targetX, targetY) {
// Check workspace limits first
// Check workspace limits first (quick check before queue)
const wsCheck = IK.checkWorkspace(targetX, targetY);
if (!wsCheck.ok) {
UI.log(`${wsCheck.reason}`, 'warn');
return false;
}
const { theta1, theta2 } = IK.solve(targetX, targetY);
const newSteps1 = IK.radToStepsAbsolute(theta1, 1);
const newSteps2 = IK.radToStepsAbsolute(theta2, 2);
const cmd = `X:${newSteps1},${newSteps2}`;
if (BLE.isConnected()) {
try {
await BLE.write(cmd);
// Do not update steps1/steps2 here; let the P: notify handler update the UI smoothly
} catch (e) {
UI.log(`BLE error: ${e.message}`, 'error');
}
} else {
steps1 = newSteps1;
steps2 = newSteps2;
renderArm(theta1, theta2);
const simMsg = `[sim] IK→ θ1=${IK.radToDeg(theta1).toFixed(1)}° θ2=${IK.radToDeg(theta2).toFixed(1)}° | ${cmd}`;
console.log(simMsg);
UI.log(simMsg, 'info');
}
return true;
return await Motion.goto(targetX, targetY);
}
// ─────────────────────────────────────────────────────────────────
@@ -229,6 +209,7 @@ async function jogMotor(motor, delta) {
} else {
if (motor === 1) steps1 += delta;
else steps2 += delta;
Motion.setSteps(steps1, steps2);
renderArmFromSteps();
console.log(`[sim] ${cmd}`);
UI.log(`[sim] ${cmd}`, 'info');
@@ -249,6 +230,7 @@ async function zeroMotor(motor) {
} else {
steps2 = IK.ARM.HOME_STEPS.m2;
}
Motion.setSteps(steps1, steps2);
renderArmFromSteps();
console.log(`[sim] ${cmd}`);
UI.log(`[sim] ${cmd}`, 'info');
@@ -752,6 +734,7 @@ const StepperTestSection = {
} else {
steps1 = IK.ARM.HOME_STEPS.m1;
steps2 = IK.ARM.HOME_STEPS.m2;
Motion.setSteps(steps1, steps2);
renderArmFromSteps();
console.log(`[sim] ZEROALL`);
UI.log(`[sim] ZEROALL (Reset Position)`, 'info');