File size: 5,986 Bytes
0d9f1af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from "react";
import { Button } from "./ui/button";
import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle,
} from "./ui/card";
import { Alert, AlertDescription, AlertTitle } from "./ui/alert";
import { Badge } from "./ui/badge";
import { calibrateWithPort } from "../../lerobot/web/calibrate";
import type { ConnectedRobot } from "../types";

interface CalibrationWizardProps {
  robot: ConnectedRobot;
  onComplete: () => void;
  onCancel: () => void;
}

interface CalibrationStep {
  id: string;
  title: string;
  description: string;
  status: "pending" | "running" | "complete" | "error";
  message?: string;
}

export function CalibrationWizard({
  robot,
  onComplete,
  onCancel,
}: CalibrationWizardProps) {
  const [currentStepIndex, setCurrentStepIndex] = useState(0);
  const [steps, setSteps] = useState<CalibrationStep[]>([
    {
      id: "init",
      title: "Initialize Robot",
      description: "Connecting to robot and checking status",
      status: "pending",
    },
    {
      id: "calibrate",
      title: "Calibrate Motors",
      description: "Running calibration sequence",
      status: "pending",
    },
    {
      id: "verify",
      title: "Verify Calibration",
      description: "Testing calibrated positions",
      status: "pending",
    },
    {
      id: "complete",
      title: "Complete",
      description: "Calibration finished successfully",
      status: "pending",
    },
  ]);

  const [isRunning, setIsRunning] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    startCalibration();
  }, []);

  const updateStep = (
    stepId: string,
    status: CalibrationStep["status"],
    message?: string
  ) => {
    setSteps((prev) =>
      prev.map((step) =>
        step.id === stepId ? { ...step, status, message } : step
      )
    );
  };

  const startCalibration = async () => {
    if (!robot.port || !robot.robotType) {
      setError("Invalid robot configuration");
      return;
    }

    setIsRunning(true);
    setError(null);

    try {
      // Step 1: Initialize
      setCurrentStepIndex(0);
      updateStep("init", "running");
      await new Promise((resolve) => setTimeout(resolve, 1000));
      updateStep("init", "complete", "Robot initialized successfully");

      // Step 2: Calibrate
      setCurrentStepIndex(1);
      updateStep("calibrate", "running");

      try {
        await calibrateWithPort(robot.port, robot.robotType);
        updateStep("calibrate", "complete", "Motor calibration completed");
      } catch (error) {
        updateStep(
          "calibrate",
          "error",
          error instanceof Error ? error.message : "Calibration failed"
        );
        throw error;
      }

      // Step 3: Verify
      setCurrentStepIndex(2);
      updateStep("verify", "running");
      await new Promise((resolve) => setTimeout(resolve, 1500));
      updateStep("verify", "complete", "Calibration verified");

      // Step 4: Complete
      setCurrentStepIndex(3);
      updateStep("complete", "complete", "Robot is ready for use");

      setTimeout(() => {
        onComplete();
      }, 2000);
    } catch (error) {
      setError(error instanceof Error ? error.message : "Calibration failed");
    } finally {
      setIsRunning(false);
    }
  };

  const getStepIcon = (status: CalibrationStep["status"]) => {
    switch (status) {
      case "pending":
        return "⏳";
      case "running":
        return "πŸ”„";
      case "complete":
        return "βœ…";
      case "error":
        return "❌";
      default:
        return "⏳";
    }
  };

  const getStepBadgeVariant = (status: CalibrationStep["status"]) => {
    switch (status) {
      case "pending":
        return "secondary" as const;
      case "running":
        return "default" as const;
      case "complete":
        return "default" as const;
      case "error":
        return "destructive" as const;
      default:
        return "secondary" as const;
    }
  };

  return (
    <div className="space-y-6">
      <div className="text-center">
        <h3 className="text-lg font-semibold mb-2">Calibration in Progress</h3>
        <p className="text-muted-foreground">
          Calibrating {robot.robotId} ({robot.robotType?.replace("_", " ")})
        </p>
      </div>

      {error && (
        <Alert variant="destructive">
          <AlertDescription>{error}</AlertDescription>
        </Alert>
      )}

      <div className="space-y-4">
        {steps.map((step, index) => (
          <Card
            key={step.id}
            className={index === currentStepIndex ? "ring-2 ring-blue-500" : ""}
          >
            <CardHeader className="pb-3">
              <div className="flex items-center justify-between">
                <div className="flex items-center space-x-3">
                  <span className="text-2xl">{getStepIcon(step.status)}</span>
                  <div>
                    <CardTitle className="text-base">{step.title}</CardTitle>
                    <CardDescription className="text-sm">
                      {step.description}
                    </CardDescription>
                  </div>
                </div>
                <Badge variant={getStepBadgeVariant(step.status)}>
                  {step.status.charAt(0).toUpperCase() + step.status.slice(1)}
                </Badge>
              </div>
            </CardHeader>
            {step.message && (
              <CardContent className="pt-0">
                <p className="text-sm text-muted-foreground">{step.message}</p>
              </CardContent>
            )}
          </Card>
        ))}
      </div>

      <div className="flex justify-center space-x-4">
        <Button variant="outline" onClick={onCancel} disabled={isRunning}>
          Cancel
        </Button>
        {error && <Button onClick={startCalibration}>Retry Calibration</Button>}
      </div>
    </div>
  );
}