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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/**
 * Web teleoperation functionality using Web Serial API
 * Mirrors the Node.js implementation but adapted for browser environment
 */

import type { UnifiedRobotData } from "../../demo/lib/unified-storage.js";

/**
 * Motor position and limits for teleoperation
 */
export interface MotorConfig {
  id: number;
  name: string;
  currentPosition: number;
  minPosition: number;
  maxPosition: number;
  homePosition: number;
}

/**
 * Teleoperation state
 */
export interface TeleoperationState {
  isActive: boolean;
  motorConfigs: MotorConfig[];
  lastUpdate: number;
  keyStates: { [key: string]: { pressed: boolean; timestamp: number } };
}

/**
 * Keyboard control mapping (matches Node.js version)
 */
export const KEYBOARD_CONTROLS = {
  // Shoulder controls
  ArrowUp: { motor: "shoulder_lift", direction: 1, description: "Shoulder up" },
  ArrowDown: {
    motor: "shoulder_lift",
    direction: -1,
    description: "Shoulder down",
  },
  ArrowLeft: {
    motor: "shoulder_pan",
    direction: -1,
    description: "Shoulder left",
  },
  ArrowRight: {
    motor: "shoulder_pan",
    direction: 1,
    description: "Shoulder right",
  },

  // WASD controls
  w: { motor: "elbow_flex", direction: 1, description: "Elbow flex" },
  s: { motor: "elbow_flex", direction: -1, description: "Elbow extend" },
  a: { motor: "wrist_flex", direction: -1, description: "Wrist down" },
  d: { motor: "wrist_flex", direction: 1, description: "Wrist up" },

  // Wrist roll and gripper
  q: { motor: "wrist_roll", direction: -1, description: "Wrist roll left" },
  e: { motor: "wrist_roll", direction: 1, description: "Wrist roll right" },
  " ": { motor: "gripper", direction: 1, description: "Gripper toggle" },

  // Emergency stop
  Escape: {
    motor: "emergency_stop",
    direction: 0,
    description: "Emergency stop",
  },
} as const;

/**
 * Web Serial Port wrapper for teleoperation
 * Uses the same pattern as calibration - per-operation reader/writer access
 */
class WebTeleoperationPort {
  private port: SerialPort;

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

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

  async initialize(): Promise<void> {
    if (!this.port.readable || !this.port.writable) {
      throw new Error("Port is not open for teleoperation");
    }
    // Port is already open and ready - no need to grab persistent readers/writers
  }

  async writeMotorPosition(
    motorId: number,
    position: number
  ): Promise<boolean> {
    if (!this.port.writable) {
      throw new Error("Port not open for writing");
    }

    try {
      // STS3215 Write Goal_Position packet (matches Node.js exactly)
      const packet = new Uint8Array([
        0xff,
        0xff, // Header
        motorId, // Servo ID
        0x05, // Length
        0x03, // Instruction: WRITE_DATA
        42, // Goal_Position register address
        position & 0xff, // Position low byte
        (position >> 8) & 0xff, // Position high byte
        0x00, // Checksum placeholder
      ]);

      // Calculate checksum
      const checksum =
        ~(
          motorId +
          0x05 +
          0x03 +
          42 +
          (position & 0xff) +
          ((position >> 8) & 0xff)
        ) & 0xff;
      packet[8] = checksum;

      // Use per-operation writer like calibration does
      const writer = this.port.writable.getWriter();
      try {
        await writer.write(packet);
        return true;
      } finally {
        writer.releaseLock();
      }
    } catch (error) {
      console.warn(`Failed to write motor ${motorId} position:`, error);
      return false;
    }
  }

  async readMotorPosition(motorId: number): Promise<number | null> {
    if (!this.port.writable || !this.port.readable) {
      throw new Error("Port not open for reading/writing");
    }

    const writer = this.port.writable.getWriter();
    const reader = this.port.readable.getReader();

    try {
      // STS3215 Read Present_Position packet
      const packet = new Uint8Array([
        0xff,
        0xff, // Header
        motorId, // Servo ID
        0x04, // Length
        0x02, // Instruction: READ_DATA
        56, // Present_Position register address
        0x02, // Data length (2 bytes)
        0x00, // Checksum placeholder
      ]);

      const checksum = ~(motorId + 0x04 + 0x02 + 56 + 0x02) & 0xff;
      packet[7] = checksum;

      // Clear buffer first
      try {
        const { value, done } = await reader.read();
        if (done) return null;
      } catch (e) {
        // Buffer was empty, continue
      }

      await writer.write(packet);
      await new Promise((resolve) => setTimeout(resolve, 10));

      const { value: response, done } = await reader.read();
      if (done || !response || response.length < 7) {
        return null;
      }

      const id = response[2];
      const error = response[4];

      if (id === motorId && error === 0) {
        return response[5] | (response[6] << 8);
      }

      return null;
    } catch (error) {
      console.warn(`Failed to read motor ${motorId} position:`, error);
      return null;
    } finally {
      reader.releaseLock();
      writer.releaseLock();
    }
  }

  async disconnect(): Promise<void> {
    // Don't close the port itself - just cleanup wrapper
    // The port is managed by PortManager
  }
}

/**
 * Load calibration data from unified storage with fallback to defaults
 * Improved version that properly loads and applies calibration ranges
 */
export function loadCalibrationConfig(serialNumber: string): MotorConfig[] {
  // Default SO-100 configuration (matches Node.js defaults)
  const defaultConfigs: MotorConfig[] = [
    {
      id: 1,
      name: "shoulder_pan",
      currentPosition: 2048,
      minPosition: 1024,
      maxPosition: 3072,
      homePosition: 2048,
    },
    {
      id: 2,
      name: "shoulder_lift",
      currentPosition: 2048,
      minPosition: 1024,
      maxPosition: 3072,
      homePosition: 2048,
    },
    {
      id: 3,
      name: "elbow_flex",
      currentPosition: 2048,
      minPosition: 1024,
      maxPosition: 3072,
      homePosition: 2048,
    },
    {
      id: 4,
      name: "wrist_flex",
      currentPosition: 2048,
      minPosition: 1024,
      maxPosition: 3072,
      homePosition: 2048,
    },
    {
      id: 5,
      name: "wrist_roll",
      currentPosition: 2048,
      minPosition: 1024,
      maxPosition: 3072,
      homePosition: 2048,
    },
    {
      id: 6,
      name: "gripper",
      currentPosition: 2048,
      minPosition: 1024,
      maxPosition: 3072,
      homePosition: 2048,
    },
  ];

  try {
    // Load from unified storage
    const unifiedKey = `lerobotjs-${serialNumber}`;
    const unifiedDataRaw = localStorage.getItem(unifiedKey);

    if (!unifiedDataRaw) {
      console.log(
        `No calibration data found for ${serialNumber}, using defaults`
      );
      return defaultConfigs;
    }

    const unifiedData: UnifiedRobotData = JSON.parse(unifiedDataRaw);

    if (!unifiedData.calibration) {
      console.log(
        `No calibration in unified data for ${serialNumber}, using defaults`
      );
      return defaultConfigs;
    }

    // Map calibration data to motor configs
    const calibratedConfigs: MotorConfig[] = defaultConfigs.map(
      (defaultConfig) => {
        const calibData = (unifiedData.calibration as any)?.[
          defaultConfig.name
        ];

        if (
          calibData &&
          typeof calibData === "object" &&
          "id" in calibData &&
          "range_min" in calibData &&
          "range_max" in calibData
        ) {
          // Use calibrated values but keep current position as default
          return {
            ...defaultConfig,
            id: calibData.id,
            minPosition: calibData.range_min,
            maxPosition: calibData.range_max,
            homePosition: Math.floor(
              (calibData.range_min + calibData.range_max) / 2
            ),
          };
        }

        return defaultConfig;
      }
    );

    console.log(`✅ Loaded calibration data for ${serialNumber}`);
    return calibratedConfigs;
  } catch (error) {
    console.warn(`Failed to load calibration for ${serialNumber}:`, error);
    return defaultConfigs;
  }
}

/**
 * Web teleoperation controller
 */
export class WebTeleoperationController {
  private port: WebTeleoperationPort;
  private motorConfigs: MotorConfig[] = [];
  private isActive: boolean = false;
  private updateInterval: NodeJS.Timeout | null = null;
  private keyStates: {
    [key: string]: { pressed: boolean; timestamp: number };
  } = {};

  // Movement parameters (matches Node.js)
  private readonly STEP_SIZE = 8;
  private readonly UPDATE_RATE = 60; // 60 FPS
  private readonly KEY_TIMEOUT = 100; // ms

  constructor(port: SerialPort, serialNumber: string) {
    this.port = new WebTeleoperationPort(port);
    this.motorConfigs = loadCalibrationConfig(serialNumber);
  }

  async initialize(): Promise<void> {
    await this.port.initialize();

    // Read current positions
    for (const config of this.motorConfigs) {
      const position = await this.port.readMotorPosition(config.id);
      if (position !== null) {
        config.currentPosition = position;
      }
    }
  }

  getMotorConfigs(): MotorConfig[] {
    return [...this.motorConfigs];
  }

  getState(): TeleoperationState {
    return {
      isActive: this.isActive,
      motorConfigs: [...this.motorConfigs],
      lastUpdate: Date.now(),
      keyStates: { ...this.keyStates },
    };
  }

  updateKeyState(key: string, pressed: boolean): void {
    this.keyStates[key] = {
      pressed,
      timestamp: Date.now(),
    };
  }

  start(): void {
    if (this.isActive) return;

    this.isActive = true;
    this.updateInterval = setInterval(() => {
      this.updateMotorPositions();
    }, 1000 / this.UPDATE_RATE);

    console.log("🎮 Web teleoperation started");
  }

  stop(): void {
    if (!this.isActive) return;

    this.isActive = false;

    if (this.updateInterval) {
      clearInterval(this.updateInterval);
      this.updateInterval = null;
    }

    // Clear all key states
    this.keyStates = {};

    console.log("⏹️ Web teleoperation stopped");
  }

  async disconnect(): Promise<void> {
    this.stop();
    await this.port.disconnect();
  }

  private updateMotorPositions(): void {
    const now = Date.now();

    // Clear timed-out keys
    Object.keys(this.keyStates).forEach((key) => {
      if (now - this.keyStates[key].timestamp > this.KEY_TIMEOUT) {
        delete this.keyStates[key];
      }
    });

    // Process active keys
    const activeKeys = Object.keys(this.keyStates).filter(
      (key) =>
        this.keyStates[key].pressed &&
        now - this.keyStates[key].timestamp <= this.KEY_TIMEOUT
    );

    // Emergency stop check
    if (activeKeys.includes("Escape")) {
      this.stop();
      return;
    }

    // Calculate target positions based on active keys
    const targetPositions: { [motorName: string]: number } = {};

    for (const key of activeKeys) {
      const control = KEYBOARD_CONTROLS[key as keyof typeof KEYBOARD_CONTROLS];
      if (!control || control.motor === "emergency_stop") continue;

      const motorConfig = this.motorConfigs.find(
        (m) => m.name === control.motor
      );
      if (!motorConfig) continue;

      // Calculate new position
      const currentTarget =
        targetPositions[motorConfig.name] ?? motorConfig.currentPosition;
      const newPosition = currentTarget + control.direction * this.STEP_SIZE;

      // Apply limits
      targetPositions[motorConfig.name] = Math.max(
        motorConfig.minPosition,
        Math.min(motorConfig.maxPosition, newPosition)
      );
    }

    // Send motor commands
    Object.entries(targetPositions).forEach(([motorName, targetPosition]) => {
      const motorConfig = this.motorConfigs.find((m) => m.name === motorName);
      if (motorConfig && targetPosition !== motorConfig.currentPosition) {
        this.port
          .writeMotorPosition(motorConfig.id, Math.round(targetPosition))
          .then((success) => {
            if (success) {
              motorConfig.currentPosition = targetPosition;
            }
          });
      }
    });
  }

  // Programmatic control methods
  async moveMotor(motorName: string, targetPosition: number): Promise<boolean> {
    const motorConfig = this.motorConfigs.find((m) => m.name === motorName);
    if (!motorConfig) return false;

    const clampedPosition = Math.max(
      motorConfig.minPosition,
      Math.min(motorConfig.maxPosition, targetPosition)
    );

    const success = await this.port.writeMotorPosition(
      motorConfig.id,
      Math.round(clampedPosition)
    );
    if (success) {
      motorConfig.currentPosition = clampedPosition;
    }

    return success;
  }

  async setMotorPositions(positions: {
    [motorName: string]: number;
  }): Promise<boolean> {
    const results = await Promise.all(
      Object.entries(positions).map(([motorName, position]) =>
        this.moveMotor(motorName, position)
      )
    );

    return results.every((result) => result);
  }

  async goToHomePosition(): Promise<boolean> {
    const homePositions = this.motorConfigs.reduce((acc, config) => {
      acc[config.name] = config.homePosition;
      return acc;
    }, {} as { [motorName: string]: number });

    return this.setMotorPositions(homePositions);
  }
}

/**
 * Create teleoperation controller for connected robot
 */
export async function createWebTeleoperationController(
  port: SerialPort,
  serialNumber: string
): Promise<WebTeleoperationController> {
  const controller = new WebTeleoperationController(port, serialNumber);
  await controller.initialize();
  return controller;
}