File size: 13,179 Bytes
130ae50
 
 
 
 
 
 
 
 
 
 
 
1a7b22d
130ae50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a83670
130ae50
 
 
 
 
6a83670
130ae50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc82a28
 
 
130ae50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a7b22d
130ae50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { useState, useCallback, useMemo } from "react";
import { Button } from "./ui/button.js";
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "./ui/card.js";
import { Badge } from "./ui/badge.js";
import {
  calibrate,
  releaseMotors,
  type WebCalibrationResults,
  type LiveCalibrationData,
  type CalibrationProcess,
} from "@lerobot/web";
import { CalibrationModal } from "./CalibrationModal.js";
import type { RobotConnection } from "@lerobot/web";

interface CalibrationPanelProps {
  robot: RobotConnection;
  onFinish: () => void;
}

export function CalibrationPanel({ robot, onFinish }: CalibrationPanelProps) {
  // Simple state management
  const [isCalibrating, setIsCalibrating] = useState(false);
  const [calibrationResult, setCalibrationResult] =
    useState<WebCalibrationResults | null>(null);
  const [status, setStatus] = useState<string>("Ready to calibrate");
  const [modalOpen, setModalOpen] = useState(false);
  const [calibrationProcess, setCalibrationProcess] =
    useState<CalibrationProcess | null>(null);
  const [motorData, setMotorData] = useState<LiveCalibrationData>({});
  const [isPreparing, setIsPreparing] = useState(false);

  // Motor names for display
  const motorNames = useMemo(
    () => [
      "shoulder_pan",
      "shoulder_lift",
      "elbow_flex",
      "wrist_flex",
      "wrist_roll",
      "gripper",
    ],
    []
  );

  // Initialize motor data
  const initializeMotorData = useCallback(() => {
    const initialData: LiveCalibrationData = {};
    motorNames.forEach((name) => {
      initialData[name] = {
        current: 2047,
        min: 2047,
        max: 2047,
        range: 0,
      };
    });
    setMotorData(initialData);
  }, [motorNames]);

  // Release motor torque
  const releaseMotorTorque = useCallback(async () => {
    try {
      setIsPreparing(true);
      setStatus("πŸ”“ Releasing motor torque - joints can now be moved freely");

      await releaseMotors(robot);

      setStatus("βœ… Joints are now free to move - set your homing position");
    } catch (error) {
      console.warn("Failed to release motor torque:", error);
      setStatus("⚠️ Could not release motor torque - try moving joints gently");
    } finally {
      setIsPreparing(false);
    }
  }, [robot]);

  // Start calibration using new API
  const handleContinueCalibration = useCallback(async () => {
    setModalOpen(false);

    if (!robot.port || !robot.robotType) {
      return;
    }

    try {
      setStatus("πŸ€– Starting calibration process...");
      setIsCalibrating(true);
      initializeMotorData();

      // Use the unified config API for calibration
      const process = await calibrate({
        robot,
        onLiveUpdate: (data) => {
          setMotorData(data);
          setStatus(
            "πŸ“ Recording joint ranges - move all joints through their full range"
          );
        },
        onProgress: (message) => {
          setStatus(message);
        },
      });

      setCalibrationProcess(process);

      // Add Enter key listener for stopping (matching Node.js UX)
      const handleKeyPress = (event: KeyboardEvent) => {
        if (event.key === "Enter") {
          process.stop();
        }
      };
      document.addEventListener("keydown", handleKeyPress);

      try {
        // Wait for calibration to complete
        const result = await process.result;
        setCalibrationResult(result);

        // App-level concern: Save results to storage
        const serialNumber =
          robot.serialNumber || robot.usbMetadata?.serialNumber || "unknown";
        await saveCalibrationResults(
          result,
          robot.robotType,
          robot.robotId || `${robot.robotType}_1`,
          serialNumber
        );

        setStatus(
          "βœ… Calibration completed successfully! Configuration saved."
        );
      } finally {
        document.removeEventListener("keydown", handleKeyPress);
        setCalibrationProcess(null);
        setIsCalibrating(false);
      }
    } catch (error) {
      console.error("❌ Calibration failed:", error);
      setStatus(
        `❌ Calibration failed: ${
          error instanceof Error ? error.message : error
        }`
      );
      setIsCalibrating(false);
      setCalibrationProcess(null);
    }
  }, [robot, initializeMotorData]);

  // Stop calibration recording
  const handleStopRecording = useCallback(() => {
    if (calibrationProcess) {
      calibrationProcess.stop();
    }
  }, [calibrationProcess]);

  // App-level concern: Save calibration results
  const saveCalibrationResults = async (
    results: WebCalibrationResults,
    robotType: string,
    robotId: string,
    serialNumber: string
  ) => {
    try {
      // Save to unified storage (app-level functionality)
      const { saveCalibrationData } = await import("../lib/unified-storage.js");

      const fullCalibrationData = {
        ...results,
        device_type: robotType,
        device_id: robotId,
        calibrated_at: new Date().toISOString(),
        platform: "web",
        api: "Web Serial API",
      };

      const metadata = {
        timestamp: new Date().toISOString(),
        readCount: Object.keys(motorData).length > 0 ? 100 : 0, // Estimate
      };

      saveCalibrationData(serialNumber, fullCalibrationData, metadata);
    } catch (error) {
      console.warn("Failed to save calibration results:", error);
    }
  };

  // App-level concern: JSON export functionality
  const downloadConfigJSON = useCallback(() => {
    if (!calibrationResult) return;

    const jsonString = JSON.stringify(calibrationResult, null, 2);
    const blob = new Blob([jsonString], { type: "application/json" });
    const url = URL.createObjectURL(blob);

    const link = document.createElement("a");
    link.href = url;
    link.download = `${robot.robotId || robot.robotType}_calibration.json`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    URL.revokeObjectURL(url);
  }, [calibrationResult, robot.robotId, robot.robotType]);

  return (
    <div className="space-y-4">
      {/* Calibration Status Card */}
      <Card>
        <CardHeader>
          <div className="flex items-center justify-between">
            <div>
              <CardTitle className="text-lg">
                πŸ› οΈ Calibrating: {robot.robotId}
              </CardTitle>
              <CardDescription>
                {robot.robotType?.replace("_", " ")} β€’ {robot.name}
              </CardDescription>
            </div>
            <Badge
              variant={
                isCalibrating
                  ? "default"
                  : calibrationResult
                  ? "default"
                  : "outline"
              }
            >
              {isCalibrating
                ? "Recording"
                : calibrationResult
                ? "Complete"
                : "Ready"}
            </Badge>
          </div>
        </CardHeader>
        <CardContent>
          <div className="space-y-4">
            <div className="p-3 bg-blue-50 rounded-lg">
              <p className="text-sm font-medium text-blue-900">Status:</p>
              <p className="text-sm text-blue-800">{status}</p>
              {isCalibrating && (
                <p className="text-xs text-blue-600 mt-1">
                  Move joints through full range | Press "Finish Recording" or
                  Enter key when done
                </p>
              )}
            </div>

            <div className="flex gap-2">
              {!isCalibrating && !calibrationResult && (
                <Button
                  onClick={async () => {
                    // βœ… Release motor torque FIRST - so user can move joints immediately
                    await releaseMotorTorque();
                    // THEN open modal - user can now follow instructions right away
                    setModalOpen(true);
                  }}
                  disabled={isPreparing}
                >
                  {isPreparing ? "Preparing..." : "Start Calibration"}
                </Button>
              )}

              {isCalibrating && calibrationProcess && (
                <Button onClick={handleStopRecording} variant="default">
                  Finish Recording
                </Button>
              )}

              {calibrationResult && (
                <>
                  <Button onClick={downloadConfigJSON} variant="outline">
                    Download Config JSON
                  </Button>
                  <Button onClick={onFinish}>Done</Button>
                </>
              )}
            </div>
          </div>
        </CardContent>
      </Card>

      {/* Configuration JSON Display */}
      {calibrationResult && (
        <Card>
          <CardHeader>
            <CardTitle className="text-lg">
              🎯 Calibration Configuration
            </CardTitle>
            <CardDescription>
              Copy this JSON or download it for your robot setup
            </CardDescription>
          </CardHeader>
          <CardContent>
            <div className="space-y-3">
              <pre className="bg-gray-100 p-4 rounded-lg text-sm overflow-x-auto border">
                <code>{JSON.stringify(calibrationResult, null, 2)}</code>
              </pre>
              <div className="flex gap-2">
                <Button onClick={downloadConfigJSON} variant="outline">
                  πŸ“„ Download JSON File
                </Button>
                <Button
                  onClick={() => {
                    navigator.clipboard.writeText(
                      JSON.stringify(calibrationResult, null, 2)
                    );
                  }}
                  variant="outline"
                >
                  πŸ“‹ Copy to Clipboard
                </Button>
              </div>
            </div>
          </CardContent>
        </Card>
      )}

      {/* Live Position Recording Table */}
      <Card>
        <CardHeader>
          <CardTitle className="text-lg">Live Position Recording</CardTitle>
          <CardDescription>
            Real-time motor position feedback during calibration
          </CardDescription>
        </CardHeader>
        <CardContent>
          <div className="overflow-hidden rounded-lg border">
            <table className="w-full font-mono text-sm">
              <thead className="bg-gray-50">
                <tr>
                  <th className="px-4 py-2 text-left font-medium text-gray-900">
                    Motor Name
                  </th>
                  <th className="px-4 py-2 text-right font-medium text-gray-900">
                    Current
                  </th>
                  <th className="px-4 py-2 text-right font-medium text-gray-900">
                    Min
                  </th>
                  <th className="px-4 py-2 text-right font-medium text-gray-900">
                    Max
                  </th>
                  <th className="px-4 py-2 text-right font-medium text-gray-900">
                    Range
                  </th>
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-200">
                {motorNames.map((motorName) => {
                  const motor = motorData[motorName] || {
                    current: 2047,
                    min: 2047,
                    max: 2047,
                    range: 0,
                  };

                  return (
                    <tr key={motorName} className="hover:bg-gray-50">
                      <td className="px-4 py-2 font-medium flex items-center gap-2">
                        {motorName}
                        {motor.range > 100 && (
                          <span className="text-green-600 text-xs">βœ“</span>
                        )}
                      </td>
                      <td className="px-4 py-2 text-right">{motor.current}</td>
                      <td className="px-4 py-2 text-right">{motor.min}</td>
                      <td className="px-4 py-2 text-right">{motor.max}</td>
                      <td className="px-4 py-2 text-right font-medium">
                        <span
                          className={
                            motor.range > 100
                              ? "text-green-600"
                              : "text-gray-500"
                          }
                        >
                          {motor.range}
                        </span>
                      </td>
                    </tr>
                  );
                })}
              </tbody>
            </table>
          </div>

          {isCalibrating && (
            <div className="mt-3 text-center text-sm text-gray-600">
              Move joints through their full range of motion...
            </div>
          )}
        </CardContent>
      </Card>

      {/* Calibration Modal */}
      <CalibrationModal
        open={modalOpen}
        onOpenChange={setModalOpen}
        deviceType={robot.robotType || "robot"}
        onContinue={handleContinueCalibration}
      />
    </div>
  );
}