File size: 10,071 Bytes
a8d792f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"use client"
import { useState, useEffect, useMemo } from "react"
import { Power, PowerOff, Keyboard } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Card } from "@/components/ui/card"
import { Badge } from "@/components/ui/badge"
import { Slider } from "@/components/ui/slider"
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"
import { cn } from "@/lib/utils"
import { lerobot } from "@/lib/mock-api"
import { useLocalStorage } from "@/hooks/use-local-storage"
import VirtualKey from "@/components/VirtualKey"
import type { RobotConnection, WebCalibrationResults, TeleoperationState } from "@/types/robot"

interface TeleoperationViewProps {
  robot: RobotConnection
}

export function TeleoperationView({ robot }: TeleoperationViewProps) {
  const [localCalibrationData] = useLocalStorage<WebCalibrationResults | null>(`calibration-${robot.robotId}`, null)
  const [teleopState, setTeleopState] = useState<TeleoperationState | null>(null)
  const [teleopProcess, setTeleopProcess] = useState<{
    start: () => void
    stop: () => void
    updateKeyState: (key: string, pressed: boolean) => void
    moveMotor: (motorName: string, position: number) => void
  } | null>(null)

  const calibrationData = useMemo(() => {
    if (localCalibrationData) return localCalibrationData
    return lerobot.MOCK_MOTOR_NAMES.reduce((acc, name) => {
      acc[name] = { min: 1000, max: 3000 }
      return acc
    }, {} as WebCalibrationResults)
  }, [localCalibrationData])

  useEffect(() => {
    let process: Awaited<ReturnType<typeof lerobot.teleoperate>>
    const setup = async () => {
      process = await lerobot.teleoperate(robot, {
        calibrationData,
        onStateUpdate: setTeleopState,
      })
      setTeleopProcess(process)
    }
    setup()

    const handleKeyDown = (e: KeyboardEvent) => process?.updateKeyState(e.key, true)
    const handleKeyUp = (e: KeyboardEvent) => process?.updateKeyState(e.key, false)

    window.addEventListener("keydown", handleKeyDown)
    window.addEventListener("keyup", handleKeyUp)

    return () => {
      process?.stop()
      window.removeEventListener("keydown", handleKeyDown)
      window.removeEventListener("keyup", handleKeyUp)
    }
  }, [robot, calibrationData])

  const motorConfigs =
    teleopState?.motorConfigs ??
    lerobot.MOCK_MOTOR_NAMES.map((name) => ({
      name,
      currentPosition: 2048,
      minPosition: calibrationData[name]?.min ?? 0,
      maxPosition: calibrationData[name]?.max ?? 4095,
    }))
  const keyStates = teleopState?.keyStates ?? {}
  const controls = lerobot.SO100_KEYBOARD_CONTROLS

  return (
    <Card className="border-0 rounded-none">
      <div className="p-4 border-b border-white/10">
        <div className="flex items-center justify-between">
          <div className="flex items-center gap-4">
            <div className="w-1 h-8 bg-primary"></div>
            <div>
              <h3 className="text-xl font-bold text-foreground font-mono tracking-wider uppercase">robot control</h3>
              <p className="text-sm text-muted-foreground font-mono">
                manual <span className="text-muted-foreground">teleoperate</span> interface
              </p>
            </div>
          </div>
          <div className="flex items-center gap-6">
            <div className="border-l border-white/10 pl-6 flex items-center gap-4">
              {teleopState?.isActive ? (
                <Button onClick={() => teleopProcess?.stop()} variant="destructive" size="lg">
                  <PowerOff className="w-5 h-5 mr-2" /> Stop Control
                </Button>
              ) : (
                <Button onClick={() => teleopProcess?.start()} size="lg">
                  <Power className="w-5 h-5 mr-2" /> Control Robot
                </Button>
              )}
              <div className="flex items-center gap-2">
                <span className="text-sm font-mono text-muted-foreground uppercase">status:</span>
                <Badge
                  variant="outline"
                  className={cn(
                    "border-primary/50 bg-primary/20 text-primary font-mono text-xs",
                    teleopState?.isActive && "animate-pulse-slow",
                  )}
                >
                  {teleopState?.isActive ? "ACTIVE" : "STOPPED"}
                </Badge>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div className="pt-6 p-6 grid md:grid-cols-2 gap-8">
        <div>
          <h3 className="font-sans font-semibold mb-4 text-xl">Motor Control</h3>
          <div className="space-y-6">
            {motorConfigs.map((motor) => (
              <div key={motor.name}>
                <label className="text-sm font-mono text-muted-foreground">{motor.name}</label>
                <div className="flex items-center gap-4">
                  <Slider
                    value={[motor.currentPosition]}
                    min={motor.minPosition}
                    max={motor.maxPosition}
                    step={1}
                    onValueChange={(val) => teleopProcess?.moveMotor(motor.name, val[0])}
                    disabled={!teleopState?.isActive}
                  />
                  <span className="text-lg font-mono w-16 text-right text-accent">
                    {Math.round(motor.currentPosition)}
                  </span>
                </div>
              </div>
            ))}
          </div>
        </div>
        <div>
          <h3 className="font-sans font-semibold mb-4 text-xl">Keyboard Layout & Status</h3>
          <div className="p-4 bg-black/30 rounded-lg space-y-4">
            <div className="flex justify-around items-end">
              <div className="flex flex-col items-center gap-2">
                <VirtualKey
                  label="↑"
                  subLabel="Lift+"
                  isPressed={!!keyStates[controls.shoulder_lift.positive]?.pressed}
                />
                <div className="flex gap-2">
                  <VirtualKey
                    label="←"
                    subLabel="Pan-"
                    isPressed={!!keyStates[controls.shoulder_pan.negative]?.pressed}
                  />
                  <VirtualKey
                    label="↓"
                    subLabel="Lift-"
                    isPressed={!!keyStates[controls.shoulder_lift.negative]?.pressed}
                  />
                  <VirtualKey
                    label="→"
                    subLabel="Pan+"
                    isPressed={!!keyStates[controls.shoulder_pan.positive]?.pressed}
                  />
                </div>
                <span className="font-bold text-sm font-sans">Shoulder</span>
              </div>
              <div className="flex flex-col items-center gap-2">
                <VirtualKey
                  label="W"
                  subLabel="Elbow+"
                  isPressed={!!keyStates[controls.elbow_flex.positive]?.pressed}
                />
                <div className="flex gap-2">
                  <VirtualKey
                    label="A"
                    subLabel="Wrist+"
                    isPressed={!!keyStates[controls.wrist_flex.positive]?.pressed}
                  />
                  <VirtualKey
                    label="S"
                    subLabel="Elbow-"
                    isPressed={!!keyStates[controls.elbow_flex.negative]?.pressed}
                  />
                  <VirtualKey
                    label="D"
                    subLabel="Wrist-"
                    isPressed={!!keyStates[controls.wrist_flex.negative]?.pressed}
                  />
                </div>
                <span className="font-bold text-sm font-sans">Elbow/Wrist</span>
              </div>
              <div className="flex flex-col items-center gap-2">
                <div className="flex gap-2">
                  <VirtualKey
                    label="Q"
                    subLabel="Roll+"
                    isPressed={!!keyStates[controls.wrist_roll.positive]?.pressed}
                  />
                  <VirtualKey
                    label="E"
                    subLabel="Roll-"
                    isPressed={!!keyStates[controls.wrist_roll.negative]?.pressed}
                  />
                </div>
                <div className="flex gap-2">
                  <VirtualKey label="O" subLabel="Grip+" isPressed={!!keyStates[controls.gripper.positive]?.pressed} />
                  <VirtualKey label="C" subLabel="Grip-" isPressed={!!keyStates[controls.gripper.negative]?.pressed} />
                </div>
                <span className="font-bold text-sm font-sans">Roll/Grip</span>
              </div>
            </div>
            <div className="pt-4 border-t border-white/10">
              <div className="flex justify-between items-center font-mono text-sm">
                <div className="flex items-center gap-2 text-muted-foreground">
                  <Keyboard className="w-4 h-4" />
                  <span>Active Keys: {Object.values(keyStates).filter((k) => k.pressed).length}</span>
                </div>
                <TooltipProvider>
                  <Tooltip>
                    <TooltipTrigger asChild>
                      <div
                        className={cn(
                          "w-10 h-6 border rounded-md flex items-center justify-center font-mono text-xs transition-all",
                          !!keyStates[controls.stop]?.pressed
                            ? "bg-destructive text-destructive-foreground border-destructive"
                            : "bg-background",
                        )}
                      >
                        ESC
                      </div>
                    </TooltipTrigger>
                    <TooltipContent>Emergency Stop</TooltipContent>
                  </Tooltip>
                </TooltipProvider>
              </div>
            </div>
          </div>
        </div>
      </div>
    </Card>
  )
}