File size: 5,619 Bytes
aa77a8f
 
 
 
 
eb24e49
aa77a8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb24e49
f5375f2
 
eb24e49
aa77a8f
 
 
 
 
eb24e49
aa77a8f
 
 
 
 
 
 
eb24e49
aa77a8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb24e49
f5375f2
 
aa77a8f
f5375f2
eb24e49
 
aa77a8f
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { useState } from "react";
import { gameApi, useGame } from "../store.js";
import { HOME } from "../game/reachy/reachyKinematics.js";
import { POSES, SEQUENCES } from "../game/reachy/reachyMoves.js";
import SimulatorPanel from "./SimulatorPanel.jsx";
import { SPEECH_LANGUAGES } from "../game/reachy/voiceProfile.js";

const FIELDS = [
  ["x", "x (mm)", -20, 20], ["y", "y (mm)", -20, 20], ["z", "z (mm)", -30, 20],
  ["roll", "Roll (°)", -25, 25], ["pitch", "Pitch (°)", -25, 25], ["yaw", "Yaw (°)", -45, 45],
  ["leftAntenna", "Left antenna (°)", -90, 90], ["rightAntenna", "Right antenna (°)", -90, 90],
];

export default function ReachyControls() {
  const [pose, setPose] = useState({ ...HOME });
  const [duration, setDuration] = useState(1);
  const [error, setError] = useState(null);
  const reachy = useGame((s) => s.reachy);
  const locked = useGame((s) => s.emotionBusy || s.scriptPlaying);
  const recording = useGame((s) => s.scriptRecording);
  const [speech, setSpeech] = useState("");
  const language = useGame((s) => s.speechLanguage);
  const personality = useGame((s) => s.speechPersonality);
  const provider = useGame((s) => s.speechProvider);
  const voice = useGame((s) => s.speech);
  const act = async (fn) => {
    setError(null);
    try { await fn(gameApi.reachy); }
    catch (e) { if (e.name !== "AbortError") setError(e.message); }
  };
  const action = (label, fn, intent) => recording ? gameApi.script?.add(label, () => fn(gameApi.reachy), { intent }) : act(fn);
  return <SimulatorPanel side="left" title="Reachy Mini">
    <fieldset disabled={locked || (reachy.playing && !recording)}>
      <legend>Poses</legend>
      <div className="sim-buttons">{Object.entries(POSES).map(([name, p]) => <button key={name} onClick={() => action(name, (r) => r.playSequence([{ pose: { ...HOME, ...p }, duration }]))}>{name}</button>)}</div>
      <legend>Sequences</legend>
      <div className="sim-buttons">{Object.entries(SEQUENCES).map(([name, sequence]) => <button key={name} onClick={() => action(name, (r) => r.playSequence(sequence))}>{name}</button>)}</div>
      <legend>Emotions</legend>
      <div className="sim-buttons">{[["joy", "Happy"], ["sadness", "Sad"], ["anger", "Angry"]].map(([id, label]) => <button key={id} onClick={() => action(`Reachy ${label}`, () => gameApi.playReachyEmotion?.(id), { robot: "reachy", kind: "emotion", emotion: id })}>{label}</button>)}</div>
    </fieldset>
    <fieldset disabled={locked}>
      <legend>Custom pose</legend>
      {FIELDS.map(([key, label, min, max]) => <label className="sim-field" key={key}><span>{label}</span><input type="number" min={min} max={max} step="1" value={pose[key]} onChange={(e) => setPose({ ...pose, [key]: Number(e.target.value) })} /></label>)}
      <label className="sim-field"><span>Duration (s)</span><input type="number" min="0.1" max="30" step="0.1" value={duration} onChange={(e) => setDuration(Number(e.target.value))} /></label>
      <label className="sim-field"><span>Playback speed</span><select value={reachy.speed} onChange={(e) => act((r) => r.setSpeed(Number(e.target.value)))}>{[0.25, 0.5, 1, 1.5, 2].map((s) => <option key={s} value={s}>{s}×</option>)}</select></label>
      <div className="sim-buttons">
        <button onClick={() => action("Reachy custom pose", (r) => r.enqueue(pose, duration))}>Queue</button>
        <button disabled={reachy.playing && !recording} onClick={() => action("Play Reachy queue", (r) => reachy.queue.length ? r.playQueue() : r.playSequence([{ pose, duration }]))}>Play</button>
        <button onClick={() => gameApi.reachy?.stop()}>Stop</button>
        <button onClick={() => gameApi.reachy?.clear()}>Clear</button>
      </div>
    </fieldset>
    <fieldset disabled={locked}>
      <legend>Text to speech</legend>
      <label>Voice language <select aria-label="Reachy voice language" value={language} onChange={(e) => useGame.setState({ speechLanguage: e.target.value })}>{Object.entries(SPEECH_LANGUAGES).map(([code, item]) => <option key={code} value={code}>{item.label}</option>)}</select></label>
      <label>Reachy personality <select aria-label="Reachy personality" value={personality} onChange={(e) => useGame.setState({ speechPersonality: e.target.value })}><option value="sympa">Warm</option><option value="blagueur">Playful</option><option value="cassant">Blunt</option></select></label>
      <label>Voice engine <select aria-label="Reachy voice engine" value={provider} onChange={(e) => useGame.setState({ speechProvider: e.target.value })}><option value="browser">Local · instant (free/offline)</option><option value="qwen">Qwen expressive (online)</option></select></label>
      <input className="sim-speech" value={speech} placeholder="What should Reachy say?" onChange={(e) => setSpeech(e.target.value)} />
      <button disabled={!speech.trim() || voice.busy} onClick={() => action(`Reachy says: ${speech.trim()}`, () => gameApi.reachySay?.(speech.trim(), { language, personality, provider }), { robot: "reachy", kind: "speech", text: speech.trim(), options: { language, personality, provider } })}>Say / add to script</button>
      {voice.status && <p role="status">{voice.status}</p>}
      {voice.error && <p role="alert" className="sim-error">{voice.error}</p>}
    </fieldset>
    <p role="status">{reachy.playing ? "Playing · " : ""}{reachy.queue.length} queued</p>
    {reachy.queue.length > 0 && <ol className="sim-queue">{reachy.queue.map((label, i) => <li key={i}>{label}</li>)}</ol>}
    {(error || reachy.error) && <p className="sim-error" role="alert">{error || reachy.error}</p>}
  </SimulatorPanel>;
}