import { connectToHost } from '@pollen-robotics/reachy-mini-sdk/host/embed'; import { safelyReturnToPose } from '@pollen-robotics/reachy-mini-sdk/animation'; import { Gate, Motion, idle, readPad, type Input } from './control'; import './style.css'; async function main() { const handle = await connectToHost(); const { reachy } = handle; // External configs are deliberately unused: they cannot override motion limits. document.documentElement.dataset.theme = handle.theme; const offTheme = handle.onThemeChange(theme => { document.documentElement.dataset.theme = theme; }); document.querySelector('#root')!.innerHTML = `
REACHY MINI

Joystick control

1. Choose your controller. 2. Check the input indicators. 3. Center the sticks and start.

Waiting for telemetry

Pair your controller with this device, then press any controller button.

Move a stick to test detection. Testing while paused does not move the robot.

Left stick 0.00 / 0.00Right stick 0.00 / 0.00Triggers L / R 0% / 0%
01

Look

LEFT STICK

Head yaw & pitch, relative to the body.

← Look →↑ Tilt ↓
02

Turn & stretch

RIGHT STICK

Body rotation & head height.

← Turn →↑ Raise ↓
03

Antennas

TRIGGERS / SHOULDERS

Hold to move. Release to keep the expression.

LEFT
RIGHT
HEIGHT—
LEFT ANTENNA—
RIGHT ANTENNA—

Make a little room. Start with centered sticks and released triggers. D-pad ↑ / ↓ switches Turbo / Precision.

Stop centers the robot; use the host’s Leave button to end the session. Changing tabs or losing the controller pauses controls. No camera or microphone is requested.

Readouts show commanded positions. Software limits are not collision sensors.

`; const $ = (id: string) => document.getElementById(id) as T; const source = $('source'), turbo = $('turbo'), start = $('start'); const controllerSelect = $('controller'); if (matchMedia('(pointer: coarse)').matches) { source.value = 'touch'; $('pad-name').textContent = 'Drag the circles; hold antenna buttons. Controls stay on this device.'; } const gate = new Gate(); let motion: Motion | null = null, lastState = -Infinity, lastTick = performance.now(), restUntil = 0; let leaving = false, selectedPad = -1, worker: Worker | null = null; let sourceChosen = false, padSignature = '', inputMessage = '', preview = idle(); let touch = idle(); const held = new Set(); const pointerIds = new Map(); const abort = new AbortController(); const signal = abort.signal; function clearInput() { touch = idle(); held.clear(); pointerIds.clear(); document.querySelectorAll('.puck').forEach(p => { p.style.transform = 'translate(0,0)'; }); document.querySelectorAll('[data-hold]').forEach(b => b.classList.remove('held')); } const fresh = () => performance.now() - lastState < 2500; function stop(reason: string, returnHome = true, forceHome = false) { const wasActive = gate.active; gate.pause(reason); clearInput(); motion = null; if (returnHome && (wasActive || forceHome) && performance.now() >= restUntil && reachy.state === 'streaming' && fresh()) { try { const plan = safelyReturnToPose(reachy); restUntil = performance.now() + plan.duration * 1000 + 400; } catch (error) { gate.reason = `Paused; centering failed: ${String(error)}`; } } } function pads() { try { return Array.from(navigator.getGamepads?.() ?? []).filter((pad): pad is Gamepad => Boolean(pad?.connected)); } catch { throw new Error('Gamepad access is blocked here. Open the direct Space URL or choose Touch.'); } } function scanControllers() { const all = pads(); const signature = all.map(p => `${p.index}:${p.id}:${p.mapping}`).join('|'); if (signature !== padSignature) { padSignature = signature; controllerSelect.replaceChildren(); const placeholder = new Option(all.length ? 'Choose your controller' : 'Press a controller button to detect it', ''); controllerSelect.add(placeholder); for (const pad of all) { const option = new Option(`${pad.id}${pad.mapping === 'standard' ? '' : ' (unsupported mapping)'}`, String(pad.index)); option.disabled = pad.mapping !== 'standard'; controllerSelect.add(option); } const supported = all.filter(p => p.mapping === 'standard'); if (!gate.active && !all.some(p => p.index === selectedPad)) selectedPad = supported.length === 1 ? supported[0].index : -1; controllerSelect.value = selectedPad < 0 ? '' : String(selectedPad); // A paired controller takes priority over a touch-capable screen until the // user explicitly chooses an input. Otherwise a touchscreen laptop can // silently ignore a working physical gamepad in Touch mode. if (!sourceChosen && supported.length) source.value = 'gamepad'; } } function getInput(): Input { if (source.value === 'gamepad') { const pad = pads().find(p => p.index === selectedPad); if (!pad) throw new Error(gate.active ? 'Controller disconnected — reconnect and press Start.' : 'No mapped gamepad. Press a controller button or choose Touch / mouse.'); $('pad-name').textContent = pad.id; return readPad(pad); } return { ...touch, triggers: [Number(held.has('left-out')), Number(held.has('right-out'))], shoulders: [held.has('left-in'), held.has('right-in')] }; } start.addEventListener('click', () => { try { if (!worker) throw new Error('Control clock unavailable — reload the app in a browser that supports Web Workers.'); if (leaving || document.hidden || performance.now() < restUntil || reachy.robotState.is_move_running) { throw new Error('Wait for centering to finish, then press Start.'); } const input = getInput(); const next = new Motion(reachy.robotState); gate.arm(input, performance.now(), lastState, reachy.state === 'streaming'); if (!reachy.setMotorMode('enabled')) throw new Error('Robot connection is unavailable.'); motion = next; lastTick = performance.now(); } catch (error) { gate.pause(String(error instanceof Error ? error.message : error)); } }, {signal}); $('stop').addEventListener('click', () => stop('Paused — press Start after centering to resume.', true, true), {signal}); source.addEventListener('change', () => { sourceChosen = true; stop('Input changed — press Start to resume.'); $('pad-name').textContent = source.value === 'touch' ? 'Drag the circles; hold antenna buttons. Controls stay on this device.' : 'Press a gamepad button, then Start.'; }, {signal}); controllerSelect.addEventListener('change', () => { stop('Controller changed — test the sticks, then press Start.'); selectedPad = controllerSelect.value === '' ? -1 : Number(controllerSelect.value); sourceChosen = true; source.value = 'gamepad'; }, {signal}); function makeStick(id: string, offset: number) { const el = $(id), puck = el.querySelector('.puck')!; const reset = () => { touch.axes[offset] = touch.axes[offset+1] = 0; puck.style.transform = 'translate(0,0)'; }; const move = (event: PointerEvent) => { if (pointerIds.get(id) !== event.pointerId) return; const box = el.getBoundingClientRect(), radius = box.width * .34; let x = (event.clientX-box.left-box.width/2)/radius, y = (event.clientY-box.top-box.height/2)/radius; const magnitude = Math.max(1, Math.hypot(x,y)); x /= magnitude; y /= magnitude; touch.axes[offset] = x; touch.axes[offset+1] = y; puck.style.transform = `translate(${x*radius}px,${y*radius}px)`; }; el.addEventListener('pointerdown', event => { if (source.value !== 'touch' || !gate.active || pointerIds.has(id)) return; event.preventDefault(); pointerIds.set(id, event.pointerId); el.setPointerCapture(event.pointerId); move(event); }, {signal}); el.addEventListener('pointermove', move, {signal}); el.addEventListener('pointerup', event => { if (pointerIds.get(id) === event.pointerId) { pointerIds.delete(id); reset(); } }, {signal}); for (const name of ['pointercancel', 'lostpointercapture'] as const) el.addEventListener(name, () => { if (pointerIds.has(id)) { reset(); stop('Touch interrupted — press Start to resume.'); } }, {signal}); } makeStick('look', 0); makeStick('body', 2); document.querySelectorAll('[data-hold]').forEach(button => { const name = button.dataset.hold!; button.addEventListener('pointerdown', event => { if (source.value !== 'touch' || !gate.active) return; event.preventDefault(); held.add(name); button.classList.add('held'); button.setPointerCapture(event.pointerId); }, {signal}); const release = () => { held.delete(name); button.classList.remove('held'); }; button.addEventListener('pointerup', release, {signal}); button.addEventListener('pointercancel', () => { release(); stop('Touch interrupted — press Start to resume.'); }, {signal}); button.addEventListener('lostpointercapture', release, {signal}); button.addEventListener('keydown', event => { if ((event.key === ' ' || event.key === 'Enter') && source.value === 'touch' && gate.active) { event.preventDefault(); held.add(name); button.classList.add('held'); } }, {signal}); button.addEventListener('keyup', release, {signal}); button.addEventListener('blur', release, {signal}); }); const onState = () => { lastState = performance.now(); }; reachy.addEventListener('state', onState); reachy.requestState(); const lost = () => stop('Robot session ended — reconnect through the host.', false); for (const event of ['disconnected','sessionStopped','error'] as const) reachy.addEventListener(event, lost); document.addEventListener('visibilitychange', () => { if (document.hidden) stop('Tab hidden — press Start to resume.'); else reachy.requestState(); }, {signal}); window.addEventListener('blur', () => stop('Window lost focus — press Start to resume.'), {signal}); window.addEventListener('gamepaddisconnected', event => { if (source.value === 'gamepad' && event.gamepad.index === selectedPad) stop('Controller disconnected — reconnect and press Start.'); }, {signal}); try { worker = new Worker(new URL('./heartbeat.worker.ts', import.meta.url), {type: 'module'}); } catch { gate.pause('Control clock unavailable — this browser cannot start Web Workers.'); } if (worker) worker.onmessage = () => { const now = performance.now(), dt = (now-lastTick)/1000; lastTick = now; try { scanControllers(); preview = getInput(); inputMessage = ''; if (!gate.active || !motion) return; if (document.hidden || !fresh() || reachy.state !== 'streaming') { stop('Connection stale or tab hidden — press Start after reconnecting.', false); return; } if (dt > .25) { stop('Controls stalled — press Start to resume.'); return; } const input = preview; if (input.turbo !== undefined) turbo.checked = input.turbo; if (!reachy.setTarget(motion.step(input, dt, turbo.checked))) stop('Robot connection interrupted.', false); } catch (error) { preview = idle(); inputMessage = String(error instanceof Error ? error.message : error); if (gate.active) stop(inputMessage); } }; if (worker) worker.onerror = () => { stop('Control clock failed — reload this app.'); worker?.terminate(); worker = null; }; let frame = 0; function render() { $('phase').textContent = gate.reason; $('signal').classList.toggle('live', gate.active); start.disabled = gate.active || leaving || !worker || performance.now() < restUntil; start.textContent = performance.now() < restUntil ? 'Centering…' : 'Start controls'; document.body.dataset.input = source.value; controllerSelect.disabled = gate.active; $('left-input').textContent = `${preview.axes[0].toFixed(2)} / ${preview.axes[1].toFixed(2)}`; $('right-input').textContent = `${preview.axes[2].toFixed(2)} / ${preview.axes[3].toFixed(2)}`; $('trigger-input').textContent = `${Math.round(preview.triggers[0]*100)}% / ${Math.round(preview.triggers[1]*100)}%`; const moving = preview.axes.some(n => Math.abs(n) > .15) || preview.triggers.some(n => n > .15) || preview.shoulders.some(Boolean); $('input-status').textContent = inputMessage || (source.value === 'touch' ? 'Touch mode: the physical gamepad is ignored. Choose Gamepad to use your controller.' : moving ? (gate.active ? 'Controller input received — sending movement commands.' : 'Controller input received. Release controls, then press Start.') : 'Controller connected. Move a stick or trigger to check the indicators below.'); if (source.value === 'gamepad') { for (const [id, offset] of [['look',0],['body',2]] as const) { const el = $(id), puck = el.querySelector('.puck')!; const radius = el.clientWidth * .34; puck.style.transform = `translate(${preview.axes[offset]*radius}px,${preview.axes[offset+1]*radius}px)`; } for (const [name, active] of [['left-out',preview.triggers[0]>.15],['right-out',preview.triggers[1]>.15],['left-in',preview.shoulders[0]],['right-in',preview.shoulders[1]]] as const) { document.querySelector(`[data-hold="${name}"]`)?.classList.toggle('held',active); } } const values = motion?.current; $('height').textContent = values ? `${(values[2]*1000).toFixed(0)} mm` : '—'; $('left-angle').textContent = values ? `${(values[5]*180/Math.PI).toFixed(0)}°` : '—'; $('right-angle').textContent = values ? `${(values[4]*180/Math.PI).toFixed(0)}°` : '—'; frame = requestAnimationFrame(render); } frame = requestAnimationFrame(render); handle.onLeave(() => { leaving = true; gate.pause('Leaving'); clearInput(); worker?.terminate(); worker = null; cancelAnimationFrame(frame); abort.abort(); offTheme(); reachy.removeEventListener('state', onState); for (const event of ['disconnected','sessionStopped','error'] as const) reachy.removeEventListener(event, lost); safelyReturnToPose(reachy); }); } void main().catch(error => { console.error('[teleop] startup failed', error); const root = document.querySelector('#root'); if (root) root.textContent = `Unable to open Teleop: ${String(error)}`; window.parent.postMessage({source:'reachy-mini',type:'embed:error',version:1,message:String(error),fatal:true}, location.origin); });