File size: 11,131 Bytes
ec936d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Web calibration functionality using Web Serial API
 * For browser environments - matches Node.js implementation
 */

import type { CalibrateConfig } from "../node/robots/config.js";

/**
 * Web Serial Port wrapper to match Node.js SerialPort interface
 */
class WebSerialPortWrapper {
  private port: SerialPort;
  private reader: ReadableStreamDefaultReader<Uint8Array> | null = null;
  private writer: WritableStreamDefaultWriter<Uint8Array> | null = null;

  constructor(port: SerialPort) {
    this.port = port;
  }

  get isOpen(): boolean {
    return this.port !== null && this.port.readable !== null;
  }

  async initialize(): Promise<void> {
    // Set up reader and writer for already opened port
    if (this.port.readable) {
      this.reader = this.port.readable.getReader();
    }
    if (this.port.writable) {
      this.writer = this.port.writable.getWriter();
    }
  }

  async write(data: Buffer): Promise<void> {
    if (!this.writer) {
      throw new Error("Port not open for writing");
    }
    await this.writer.write(new Uint8Array(data));
  }

  async read(timeout: number = 5000): Promise<Buffer> {
    if (!this.reader) {
      throw new Error("Port not open for reading");
    }

    return new Promise<Buffer>((resolve, reject) => {
      const timer = setTimeout(() => {
        reject(new Error("Read timeout"));
      }, timeout);

      this.reader!.read()
        .then(({ value, done }) => {
          clearTimeout(timer);
          if (done || !value) {
            reject(new Error("Read failed"));
          } else {
            resolve(Buffer.from(value));
          }
        })
        .catch(reject);
    });
  }

  async close(): Promise<void> {
    if (this.reader) {
      await this.reader.cancel();
      this.reader = null;
    }
    if (this.writer) {
      this.writer.releaseLock();
      this.writer = null;
    }
    // Don't close the port itself - let the UI manage that
  }
}

/**
 * SO-100 calibration configuration for web
 */
interface WebSO100CalibrationConfig {
  deviceType: "so100_follower" | "so100_leader";
  port: WebSerialPortWrapper;
  motorNames: string[];
  driveModes: number[];
  calibModes: string[];
  limits: {
    position_min: number[];
    position_max: number[];
    velocity_max: number[];
    torque_max: number[];
  };
}

/**
 * Read motor positions using Web Serial API
 */
async function readMotorPositions(
  config: WebSO100CalibrationConfig
): Promise<number[]> {
  const motorPositions: number[] = [];
  const motorIds = [1, 2, 3, 4, 5, 6]; // SO-100 uses servo IDs 1-6

  for (let i = 0; i < motorIds.length; i++) {
    const motorId = motorIds[i];

    try {
      // Create STS3215 Read Position packet
      const packet = Buffer.from([
        0xff,
        0xff,
        motorId,
        0x04,
        0x02,
        0x38,
        0x02,
        0x00,
      ]);
      const checksum = ~(motorId + 0x04 + 0x02 + 0x38 + 0x02) & 0xff;
      packet[7] = checksum;

      await config.port.write(packet);

      try {
        const response = await config.port.read(100);
        if (response.length >= 7) {
          const id = response[2];
          const error = response[4];
          if (id === motorId && error === 0) {
            const position = response[5] | (response[6] << 8);
            motorPositions.push(position);
          } else {
            motorPositions.push(2047); // Fallback to center
          }
        } else {
          motorPositions.push(2047);
        }
      } catch (readError) {
        motorPositions.push(2047);
      }
    } catch (error) {
      motorPositions.push(2047);
    }

    // Minimal delay between servo reads
    await new Promise((resolve) => setTimeout(resolve, 2));
  }

  return motorPositions;
}

/**
 * Interactive web calibration with live updates
 */
async function performWebCalibration(
  config: WebSO100CalibrationConfig
): Promise<any> {
  // Step 1: Set homing position
  alert(
    `πŸ“ STEP 1: Set Homing Position\n\nMove the SO-100 ${config.deviceType} to the MIDDLE of its range of motion and click OK...`
  );

  const currentPositions = await readMotorPositions(config);
  const homingOffsets: { [motor: string]: number } = {};
  for (let i = 0; i < config.motorNames.length; i++) {
    const motorName = config.motorNames[i];
    const position = currentPositions[i];
    const maxRes = 4095; // STS3215 resolution
    homingOffsets[motorName] = position - Math.floor(maxRes / 2);
  }

  // Step 2: Record ranges with simplified interface for web
  alert(
    `πŸ“ STEP 2: Record Joint Ranges\n\nMove all joints through their full range of motion, then click OK when finished...`
  );

  const rangeMins: { [motor: string]: number } = {};
  const rangeMaxes: { [motor: string]: number } = {};

  // Initialize with current positions
  const initialPositions = await readMotorPositions(config);
  for (let i = 0; i < config.motorNames.length; i++) {
    const motorName = config.motorNames[i];
    const position = initialPositions[i];
    rangeMins[motorName] = position;
    rangeMaxes[motorName] = position;
  }

  // Record positions for a brief period
  const recordingDuration = 10000; // 10 seconds
  const startTime = Date.now();

  while (Date.now() - startTime < recordingDuration) {
    const positions = await readMotorPositions(config);

    for (let i = 0; i < config.motorNames.length; i++) {
      const motorName = config.motorNames[i];
      const position = positions[i];

      if (position < rangeMins[motorName]) {
        rangeMins[motorName] = position;
      }
      if (position > rangeMaxes[motorName]) {
        rangeMaxes[motorName] = position;
      }
    }

    await new Promise((resolve) => setTimeout(resolve, 100));
  }

  return {
    homing_offset: config.motorNames.map((name) => homingOffsets[name]),
    drive_mode: config.driveModes,
    start_pos: config.motorNames.map((name) => rangeMins[name]),
    end_pos: config.motorNames.map((name) => rangeMaxes[name]),
    calib_mode: config.calibModes,
    motor_names: config.motorNames,
  };
}

/**
 * Calibrate a device using an already connected port
 */
export async function calibrateWithPort(
  armType: "so100_follower" | "so100_leader",
  armId: string,
  connectedPort: SerialPort
): Promise<void> {
  try {
    // Create web serial port wrapper
    const port = new WebSerialPortWrapper(connectedPort);
    await port.initialize();

    // Get SO-100 calibration configuration
    const so100Config: WebSO100CalibrationConfig = {
      deviceType: armType,
      port,
      motorNames: [
        "shoulder_pan",
        "shoulder_lift",
        "elbow_flex",
        "wrist_flex",
        "wrist_roll",
        "gripper",
      ],
      driveModes: [0, 0, 0, 0, 0, 0],
      calibModes: [
        "position",
        "position",
        "position",
        "position",
        "position",
        "position",
      ],
      limits: {
        position_min: [0, 0, 0, 0, 0, 0],
        position_max: [4095, 4095, 4095, 4095, 4095, 4095],
        velocity_max: [100, 100, 100, 100, 100, 100],
        torque_max: [50, 50, 50, 50, 25, 25],
      },
    };

    // Perform calibration
    const calibrationResults = await performWebCalibration(so100Config);

    // Save to browser storage and download
    const calibrationData = {
      ...calibrationResults,
      device_type: armType,
      device_id: armId,
      calibrated_at: new Date().toISOString(),
      platform: "web",
      api: "Web Serial API",
    };

    const storageKey = `lerobot_calibration_${armType}_${armId}`;
    localStorage.setItem(storageKey, JSON.stringify(calibrationData));

    // Download calibration file
    downloadCalibrationFile(calibrationData, armId);

    // Close wrapper (but not the underlying port)
    await port.close();

    console.log(`Configuration saved to browser storage and downloaded.`);
  } catch (error) {
    throw new Error(
      `Web calibration failed: ${
        error instanceof Error ? error.message : error
      }`
    );
  }
}

/**
 * Calibrate a device in the browser using Web Serial API
 * Must be called from user interaction (button click)
 * This version requests a new port - use calibrateWithPort for already connected ports
 */
export async function calibrate(config: CalibrateConfig): Promise<void> {
  // Validate Web Serial API support
  if (!("serial" in navigator)) {
    throw new Error("Web Serial API not supported in this browser");
  }

  // Validate configuration
  if (Boolean(config.robot) === Boolean(config.teleop)) {
    throw new Error("Choose either a robot or a teleop.");
  }

  const deviceConfig = config.robot || config.teleop!;

  try {
    // Request a new port for this calibration
    const port = await navigator.serial.requestPort();
    await port.open({ baudRate: 1000000 });

    // Use the new port calibration function
    await calibrateWithPort(
      deviceConfig.type as "so100_follower" | "so100_leader",
      deviceConfig.id || deviceConfig.type,
      port
    );

    // Close the port we opened
    await port.close();
  } catch (error) {
    throw new Error(
      `Web calibration failed: ${
        error instanceof Error ? error.message : error
      }`
    );
  }
}

/**
 * Download calibration data as JSON file
 */
function downloadCalibrationFile(calibrationData: any, deviceId: string): void {
  const dataStr = JSON.stringify(calibrationData, null, 2);
  const dataBlob = new Blob([dataStr], { type: "application/json" });

  const url = URL.createObjectURL(dataBlob);
  const link = document.createElement("a");
  link.href = url;
  link.download = `${deviceId}_calibration.json`;

  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
}

/**
 * Check if Web Serial API is supported
 */
export function isWebSerialSupported(): boolean {
  return "serial" in navigator;
}

/**
 * Create a calibration button for web interface
 * Returns a button element that when clicked starts calibration
 */
export function createCalibrateButton(
  config: CalibrateConfig
): HTMLButtonElement {
  const button = document.createElement("button");
  button.textContent = "Calibrate Device";
  button.style.cssText = `
    padding: 10px 20px;
    background-color: #007bff;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 16px;
  `;

  button.addEventListener("click", async () => {
    button.disabled = true;
    button.textContent = "Calibrating...";

    try {
      await calibrate(config);
      button.textContent = "Calibration Complete!";
      button.style.backgroundColor = "#28a745";
    } catch (error) {
      button.textContent = "Calibration Failed";
      button.style.backgroundColor = "#dc3545";
      console.error("Calibration error:", error);
      alert(
        `Calibration failed: ${error instanceof Error ? error.message : error}`
      );
    } finally {
      setTimeout(() => {
        button.disabled = false;
        button.textContent = "Calibrate Device";
        button.style.backgroundColor = "#007bff";
      }, 3000);
    }
  });

  return button;
}