import * as THREE from 'three'; import { poseMatrix } from './kinematics'; import { APPROACH_Z, DEMO_SLOT, PICK_SOFT_POINT, PICK_Z, SCAN_PASSES, SCAN_POSE, SLOTS } from './cellLayout'; const JOINT_MOVE_ITERATIONS = 200; const LINEAR_MOVE_ITERATIONS = 6; // Motion durations are computed from the distance and rotation between poses const TRANSIT_SPEED = 320; // mm/s const SCAN_SPEED = 170; // mm/s, during laser recording const ROTATION_SPEED = 110; // °/s // Initial estimate for joint movements (°): keeps the IK settling on the same arm configuration (elbow up, J4 ≈ -210), // otherwise the numerical solution can fall into another branch and put the robot in odd poses const SEEDS = Object.fromEntries( Object.entries({ soft: [40, -80, 110, -210, -130, 0], up: [30, -60, 90, -120, -90, 30], scrc: [-10, -80, 110, -210, 10, 90], horizontalFrom: [-50, -80, 90, -190, 50, -90], horizontal2From: [-10, -90, 110, -190, 10, -90], verticalFrom: [-64, -89, 124, -215, -26, -1], }).map(([key, angles]) => [key, angles.map(THREE.MathUtils.degToRad)]) ); const slotPose = (index, z) => { const slot = SLOTS[index]; return [slot.x, slot.y, z, 180, 0, slot.rz]; }; const smooth = (t) => t * t * (3 - 2 * t); // Flow of scan.py's run_scan_cycle for the first part (config: pick, put_back): // pick_object → scrc → small / horizontal / horizontal2 / vertical scans → placed back in its slot via after_scan // joint: MoveCart (in joint space), linear: endpoint moves in a straight line (approach and scan passes) function buildCycle(slot) { const up = slotPose(slot, APPROACH_Z); const down = slotPose(slot, PICK_Z); const steps = [ { key: 'soft', move: 'joint', pose: PICK_SOFT_POINT }, { key: 'up', move: 'joint', pose: up }, { key: 'down', move: 'linear', pose: down, then: (cell) => cell.pickFromSlot(slot) }, { move: 'wait', duration: 1.0 }, { key: 'up', move: 'linear', pose: up }, { key: 'soft', move: 'joint', pose: PICK_SOFT_POINT }, { key: 'scrc', move: 'joint', pose: SCAN_POSE }, ]; SCAN_PASSES.forEach(({ name, from, to }) => { if (from !== SCAN_POSE) steps.push({ key: `${name}From`, move: 'joint', pose: from }); steps.push({ move: 'wait', duration: 0.3, laser: 'on' }); steps.push({ key: `${name}To`, move: 'linear', pose: to, laser: 'record', speed: SCAN_SPEED }); }); steps.push( { key: 'soft', move: 'joint', pose: PICK_SOFT_POINT }, { key: 'up', move: 'joint', pose: up }, { key: 'down', move: 'linear', pose: down, then: (cell) => cell.placeHeld() }, { move: 'wait', duration: 1.5 }, { key: 'up', move: 'linear', pose: up }, { move: 'wait', duration: 1.0 } ); return steps.map((step) => (step.pose ? { ...step, matrix: poseMatrix(step.pose) } : step)); } export function createDemoScan({ ik, cell }) { const steps = buildCycle(DEMO_SLOT); // The solution for each intermediate pose is cached: later cycles pick the same joint configuration const solved = new Map(); let index = 0; let time = 0; let duration = 1; let current = null; let fromAngles = null; let toAngles = null; const fromPose = new THREE.Matrix4(); const target = new THREE.Matrix4(); const fromPos = new THREE.Vector3(); const toPos = new THREE.Vector3(); const fromQuat = new THREE.Quaternion(); const toQuat = new THREE.Quaternion(); const pos = new THREE.Vector3(); const quat = new THREE.Quaternion(); const scale = new THREE.Vector3(); const unit = new THREE.Vector3(1, 1, 1); const beginStep = () => { const step = steps[index]; time = 0; cell.setLaser(Boolean(step.laser), step.laser === 'record'); if (step.move === 'wait') { duration = step.duration; return; } ik.forward(current, fromPose).decompose(fromPos, fromQuat, scale); step.matrix.decompose(toPos, toQuat, scale); const distance = fromPos.distanceTo(toPos) * 1000; const angle = THREE.MathUtils.radToDeg(fromQuat.angleTo(toQuat)); duration = Math.max(0.6, distance / (step.speed ?? TRANSIT_SPEED) + angle / ROTATION_SPEED); if (step.move === 'joint') { fromAngles = current.slice(); toAngles = ik.solve(step.matrix, solved.get(step.key) ?? SEEDS[step.key] ?? current, JOINT_MOVE_ITERATIONS); solved.set(step.key, toAngles); } }; const finishStep = () => { const step = steps[index]; if (step.key) solved.set(step.key, current.slice()); step.then?.(cell); index = (index + 1) % steps.length; beginStep(); }; return { start(angles) { current = angles.slice(); index = 0; cell.placeHeld(); beginStep(); }, stop() { cell.setLaser(false); cell.placeHeld(); }, // Joint angles for this frame (radians) update(dt) { const step = steps[index]; time += dt; const t = Math.min(1, time / duration); const k = smooth(t); if (step.move === 'joint') { current = fromAngles.map((from, j) => from + (toAngles[j] - from) * k); } else if (step.move === 'linear') { pos.lerpVectors(fromPos, toPos, k); quat.slerpQuaternions(fromQuat, toQuat, k); current = ik.solve(target.compose(pos, quat, unit), current, LINEAR_MOVE_ITERATIONS); } if (t >= 1) finishStep(); return current; }, }; }