NERDDISCO commited on
Commit
19cd4c7
Β·
1 Parent(s): 6fa48c4

refactor(node): moved types around; removed unused comments and dead code

Browse files
src/lerobot/node/calibrate.ts CHANGED
@@ -1,15 +1,12 @@
1
  /**
2
  * Helper to recalibrate your device (robot or teleoperator).
3
  *
4
- * Direct port of Python lerobot calibrate.py
5
- *
6
  * Example:
7
  * ```
8
  * npx lerobot calibrate --robot.type=so100_follower --robot.port=COM4 --robot.id=my_follower_arm
9
  * ```
10
  */
11
 
12
- import type { CalibrateConfig } from "./robots/config.js";
13
  import { createSO100Follower } from "./robots/so100_follower.js";
14
  import { createSO100Leader } from "./teleoperators/so100_leader.js";
15
  import {
@@ -18,8 +15,9 @@ import {
18
  performInteractiveCalibration,
19
  setMotorLimits,
20
  verifyCalibration,
21
- type CalibrationResults,
22
  } from "./common/calibration.js";
 
 
23
  import { getSO100Config } from "./common/so100_config.js";
24
 
25
  /**
 
1
  /**
2
  * Helper to recalibrate your device (robot or teleoperator).
3
  *
 
 
4
  * Example:
5
  * ```
6
  * npx lerobot calibrate --robot.type=so100_follower --robot.port=COM4 --robot.id=my_follower_arm
7
  * ```
8
  */
9
 
 
10
  import { createSO100Follower } from "./robots/so100_follower.js";
11
  import { createSO100Leader } from "./teleoperators/so100_leader.js";
12
  import {
 
15
  performInteractiveCalibration,
16
  setMotorLimits,
17
  verifyCalibration,
 
18
  } from "./common/calibration.js";
19
+ import type { CalibrateConfig } from "./types/robot-config.js";
20
+ import type { CalibrationResults } from "./types/calibration.js";
21
  import { getSO100Config } from "./common/so100_config.js";
22
 
23
  /**
src/lerobot/node/common/calibration.ts CHANGED
@@ -53,49 +53,10 @@ function decodeSignMagnitude(
53
  * Despite the "SO100" name, this interface is now device-agnostic and configurable
54
  * for any robot using similar serial protocols (Feetech STS3215, etc.)
55
  */
56
- export interface SO100CalibrationConfig {
57
- deviceType: "so100_follower" | "so100_leader";
58
- port: SerialPort;
59
- motorNames: string[];
60
- motorIds: number[]; // Device-specific motor IDs (e.g., [1,2,3,4,5,6] for SO-100)
61
- driveModes: number[];
62
- calibModes: string[];
63
-
64
- // Protocol-specific configuration
65
- protocol: {
66
- resolution: number; // Motor resolution (e.g., 4096 for STS3215)
67
- homingOffsetAddress: number; // Register address for homing offset (e.g., 31 for STS3215)
68
- homingOffsetLength: number; // Length in bytes for homing offset register
69
- presentPositionAddress: number; // Register address for present position (e.g., 56 for STS3215)
70
- presentPositionLength: number; // Length in bytes for present position register
71
- minPositionLimitAddress: number; // Register address for min position limit (e.g., 9 for STS3215)
72
- minPositionLimitLength: number; // Length in bytes for min position limit register
73
- maxPositionLimitAddress: number; // Register address for max position limit (e.g., 11 for STS3215)
74
- maxPositionLimitLength: number; // Length in bytes for max position limit register
75
- signMagnitudeBit: number; // Sign bit index for homing offset encoding (e.g., 11 for STS3215)
76
- };
77
-
78
- limits: {
79
- position_min: number[];
80
- position_max: number[];
81
- velocity_max: number[];
82
- torque_max: number[];
83
- };
84
- }
85
-
86
- /**
87
- * Calibration results structure matching Python lerobot format
88
- * This should match the MotorCalibration dataclass structure in Python
89
- */
90
- export interface CalibrationResults {
91
- [motorName: string]: {
92
- id: number;
93
- drive_mode: number;
94
- homing_offset: number;
95
- range_min: number;
96
- range_max: number;
97
- };
98
- }
99
 
100
  /**
101
  * Initialize device communication
 
53
  * Despite the "SO100" name, this interface is now device-agnostic and configurable
54
  * for any robot using similar serial protocols (Feetech STS3215, etc.)
55
  */
56
+ import type {
57
+ SO100CalibrationConfig,
58
+ CalibrationResults,
59
+ } from "../types/calibration.js";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  /**
62
  * Initialize device communication
src/lerobot/node/common/so100_config.ts CHANGED
@@ -4,7 +4,7 @@
4
  * Mirrors Python lerobot device configuration approach
5
  */
6
 
7
- import type { SO100CalibrationConfig } from "./calibration.js";
8
  import { SerialPort } from "serialport";
9
 
10
  /**
 
4
  * Mirrors Python lerobot device configuration approach
5
  */
6
 
7
+ import type { SO100CalibrationConfig } from "../types/calibration.js";
8
  import { SerialPort } from "serialport";
9
 
10
  /**
src/lerobot/node/find_port.ts CHANGED
@@ -1,8 +1,6 @@
1
  /**
2
  * Helper to find the USB port associated with your MotorsBus.
3
  *
4
- * Direct port of Python lerobot find_port.py
5
- *
6
  * Example:
7
  * ```
8
  * npx lerobot find-port
 
1
  /**
2
  * Helper to find the USB port associated with your MotorsBus.
3
  *
 
 
4
  * Example:
5
  * ```
6
  * npx lerobot find-port
src/lerobot/node/robots/robot.ts CHANGED
@@ -8,8 +8,8 @@ import { SerialPort } from "serialport";
8
  import { mkdir, writeFile } from "fs/promises";
9
  import { existsSync, readFileSync, mkdirSync } from "fs";
10
  import { join } from "path";
11
- import type { RobotConfig } from "./config.js";
12
- import { getCalibrationDir, ROBOTS } from "../constants.js";
13
 
14
  export abstract class Robot {
15
  protected port: SerialPort | null = null;
 
8
  import { mkdir, writeFile } from "fs/promises";
9
  import { existsSync, readFileSync, mkdirSync } from "fs";
10
  import { join } from "path";
11
+ import type { RobotConfig } from "../types/robot-config.js";
12
+ import { getCalibrationDir, ROBOTS } from "../utils/constants.js";
13
 
14
  export abstract class Robot {
15
  protected port: SerialPort | null = null;
src/lerobot/node/robots/so100_follower.ts CHANGED
@@ -4,7 +4,7 @@
4
  */
5
 
6
  import { Robot } from "./robot.js";
7
- import type { RobotConfig } from "./config.js";
8
  import * as readline from "readline";
9
 
10
  export class SO100Follower extends Robot {
 
4
  */
5
 
6
  import { Robot } from "./robot.js";
7
+ import type { RobotConfig } from "../types/robot-config.js";
8
  import * as readline from "readline";
9
 
10
  export class SO100Follower extends Robot {
src/lerobot/node/teleoperate.ts CHANGED
@@ -1,33 +1,15 @@
1
  /**
2
  * Robot teleoperation using keyboard control
3
  *
4
- * Direct port of Python lerobot teleoperate.py (keyboard portion)
5
- *
6
  * Example:
7
  * ```
8
  * npx lerobot teleoperate --robot.type=so100_follower --robot.port=COM4 --teleop.type=keyboard
9
  * ```
10
  */
11
 
12
- import type { RobotConfig } from "./robots/config.js";
13
  import { createSO100Follower } from "./robots/so100_follower.js";
14
- import { KeyboardController } from "./keyboard_teleop.js";
15
-
16
- /**
17
- * Teleoperate configuration interface
18
- * Matches Python lerobot teleoperate argument structure
19
- */
20
- export interface TeleoperateConfig {
21
- robot: RobotConfig;
22
- teleop: TeleoperatorConfig;
23
- fps?: number; // Default: 60
24
- step_size?: number; // Default: 10 (motor position units)
25
- duration_s?: number | null; // Default: null (infinite)
26
- }
27
-
28
- export interface TeleoperatorConfig {
29
- type: "keyboard"; // Only keyboard for now, expandable later
30
- }
31
 
32
  /**
33
  * Main teleoperate function
@@ -43,7 +25,6 @@ export async function teleoperate(config: TeleoperateConfig): Promise<void> {
43
  throw new Error("Only keyboard teleoperation is currently supported");
44
  }
45
 
46
- const fps = config.fps || 60;
47
  const stepSize = config.step_size || 25;
48
  const duration = config.duration_s;
49
 
@@ -106,7 +87,7 @@ export async function teleoperate(config: TeleoperateConfig): Promise<void> {
106
  console.log("");
107
 
108
  // Start teleoperation control loop
109
- await teleoperationLoop(keyboardController, robot, fps, duration || null);
110
  } catch (error) {
111
  // Ensure we disconnect even if there's an error
112
  if (keyboardController) {
@@ -129,12 +110,10 @@ export async function teleoperate(config: TeleoperateConfig): Promise<void> {
129
 
130
  /**
131
  * Main teleoperation control loop
132
- * Provides real-time position feedback and performance metrics
133
  */
134
  async function teleoperationLoop(
135
  keyboardController: KeyboardController,
136
  robot: any,
137
- fps: number,
138
  duration: number | null
139
  ): Promise<void> {
140
  console.log("Initializing teleoperation...");
@@ -143,7 +122,6 @@ async function teleoperationLoop(
143
  await keyboardController.start();
144
 
145
  const startTime = performance.now();
146
- let loopCount = 0;
147
 
148
  // Set up graceful shutdown
149
  let running = true;
@@ -172,36 +150,6 @@ async function teleoperationLoop(
172
  }
173
  }
174
 
175
- /**
176
- * Display current robot status
177
- * Shows positions, ranges, and performance metrics
178
- */
179
- function displayStatus(
180
- positions: Record<string, number>,
181
- loopCount: number,
182
- avgLoopTime: number
183
- ): void {
184
- // Clear screen and show current status
185
- console.clear();
186
- console.log("=== KEYBOARD TELEOPERATION ===");
187
- console.log("");
188
-
189
- console.log("Current Positions:");
190
- for (const [motor, position] of Object.entries(positions)) {
191
- console.log(`${motor}: ${Math.round(position)}`);
192
- }
193
-
194
- console.log("");
195
- const fps = loopCount > 0 ? 1000 / avgLoopTime : 0;
196
- console.log(
197
- `Loop: ${avgLoopTime.toFixed(2)}ms (${fps.toFixed(
198
- 0
199
- )} Hz) | Status: Connected`
200
- );
201
- console.log("");
202
- console.log("Use arrow keys, WASD, Q/E, Space to control. ESC to stop.");
203
- }
204
-
205
  /**
206
  * Parse command line arguments in Python argparse style
207
  * Handles --robot.type=so100_follower --teleop.type=keyboard format
 
1
  /**
2
  * Robot teleoperation using keyboard control
3
  *
 
 
4
  * Example:
5
  * ```
6
  * npx lerobot teleoperate --robot.type=so100_follower --robot.port=COM4 --teleop.type=keyboard
7
  * ```
8
  */
9
 
 
10
  import { createSO100Follower } from "./robots/so100_follower.js";
11
+ import { KeyboardController } from "./utils/keyboard-teleop.js";
12
+ import type { TeleoperateConfig } from "./types/teleoperation.js";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  /**
15
  * Main teleoperate function
 
25
  throw new Error("Only keyboard teleoperation is currently supported");
26
  }
27
 
 
28
  const stepSize = config.step_size || 25;
29
  const duration = config.duration_s;
30
 
 
87
  console.log("");
88
 
89
  // Start teleoperation control loop
90
+ await teleoperationLoop(keyboardController, robot, duration || null);
91
  } catch (error) {
92
  // Ensure we disconnect even if there's an error
93
  if (keyboardController) {
 
110
 
111
  /**
112
  * Main teleoperation control loop
 
113
  */
114
  async function teleoperationLoop(
115
  keyboardController: KeyboardController,
116
  robot: any,
 
117
  duration: number | null
118
  ): Promise<void> {
119
  console.log("Initializing teleoperation...");
 
122
  await keyboardController.start();
123
 
124
  const startTime = performance.now();
 
125
 
126
  // Set up graceful shutdown
127
  let running = true;
 
150
  }
151
  }
152
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  /**
154
  * Parse command line arguments in Python argparse style
155
  * Handles --robot.type=so100_follower --teleop.type=keyboard format
src/lerobot/node/teleoperators/so100_leader.ts CHANGED
@@ -6,7 +6,7 @@
6
  */
7
 
8
  import { Teleoperator } from "./teleoperator.js";
9
- import type { TeleoperatorConfig } from "./config.js";
10
 
11
  export class SO100Leader extends Teleoperator {
12
  constructor(config: TeleoperatorConfig) {
 
6
  */
7
 
8
  import { Teleoperator } from "./teleoperator.js";
9
+ import type { TeleoperatorConfig } from "../types/teleoperator-config.js";
10
 
11
  export class SO100Leader extends Teleoperator {
12
  constructor(config: TeleoperatorConfig) {
src/lerobot/node/teleoperators/teleoperator.ts CHANGED
@@ -7,8 +7,8 @@
7
  import { SerialPort } from "serialport";
8
  import { mkdir, writeFile } from "fs/promises";
9
  import { join } from "path";
10
- import type { TeleoperatorConfig } from "./config.js";
11
- import { getCalibrationDir, TELEOPERATORS } from "../constants.js";
12
 
13
  export abstract class Teleoperator {
14
  protected port: SerialPort | null = null;
 
7
  import { SerialPort } from "serialport";
8
  import { mkdir, writeFile } from "fs/promises";
9
  import { join } from "path";
10
+ import type { TeleoperatorConfig } from "../types/teleoperator-config.js";
11
+ import { getCalibrationDir, TELEOPERATORS } from "../utils/constants.js";
12
 
13
  export abstract class Teleoperator {
14
  protected port: SerialPort | null = null;
src/lerobot/node/types/calibration.ts ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Calibration types for Node.js implementation
3
+ */
4
+
5
+ import type { SerialPort } from "serialport";
6
+
7
+ export interface SO100CalibrationConfig {
8
+ deviceType: "so100_follower" | "so100_leader";
9
+ port: SerialPort;
10
+ motorNames: string[];
11
+ motorIds: number[]; // Device-specific motor IDs (e.g., [1,2,3,4,5,6] for SO-100)
12
+ driveModes: number[];
13
+ calibModes: string[];
14
+
15
+ // Protocol-specific configuration
16
+ protocol: {
17
+ resolution: number; // Motor resolution (e.g., 4096 for STS3215)
18
+ homingOffsetAddress: number; // Register address for homing offset (e.g., 31 for STS3215)
19
+ homingOffsetLength: number; // Length in bytes for homing offset register
20
+ presentPositionAddress: number; // Register address for present position (e.g., 56 for STS3215)
21
+ presentPositionLength: number; // Length in bytes for present position register
22
+ minPositionLimitAddress: number; // Register address for min position limit (e.g., 9 for STS3215)
23
+ minPositionLimitLength: number; // Length in bytes for min position limit register
24
+ maxPositionLimitAddress: number; // Register address for max position limit (e.g., 11 for STS3215)
25
+ maxPositionLimitLength: number; // Length in bytes for max position limit register
26
+ signMagnitudeBit: number; // Sign bit index for homing offset encoding (e.g., 11 for STS3215)
27
+ };
28
+
29
+ limits: {
30
+ position_min: number[];
31
+ position_max: number[];
32
+ velocity_max: number[];
33
+ torque_max: number[];
34
+ };
35
+ }
36
+
37
+ export interface CalibrationResults {
38
+ [motorName: string]: {
39
+ id: number;
40
+ drive_mode: number;
41
+ homing_offset: number;
42
+ range_min: number;
43
+ range_max: number;
44
+ };
45
+ }
src/lerobot/node/{robots/config.ts β†’ types/robot-config.ts} RENAMED
@@ -1,10 +1,7 @@
1
  /**
2
- * Robot configuration types
3
- * Shared between Node.js and Web implementations
4
  */
5
 
6
- import type { TeleoperatorConfig } from "../teleoperators/config.js";
7
-
8
  export interface RobotConfig {
9
  type: "so100_follower";
10
  port: string;
@@ -20,3 +17,7 @@ export interface CalibrateConfig {
20
  robot?: RobotConfig;
21
  teleop?: TeleoperatorConfig;
22
  }
 
 
 
 
 
1
  /**
2
+ * Robot configuration types for Node.js implementation
 
3
  */
4
 
 
 
5
  export interface RobotConfig {
6
  type: "so100_follower";
7
  port: string;
 
17
  robot?: RobotConfig;
18
  teleop?: TeleoperatorConfig;
19
  }
20
+
21
+ // Re-export from teleoperator-config for convenience
22
+ import type { TeleoperatorConfig } from "./teleoperator-config.js";
23
+ export type { TeleoperatorConfig };
src/lerobot/node/types/teleoperation.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Teleoperation types for Node.js implementation
3
+ */
4
+
5
+ import type { RobotConfig } from "./robot-config.js";
6
+
7
+ export interface TeleoperateConfig {
8
+ robot: RobotConfig;
9
+ teleop: KeyboardTeleoperationConfig;
10
+ fps?: number; // Default: 60
11
+ step_size?: number; // Default: 10 (motor position units)
12
+ duration_s?: number | null; // Default: null (infinite)
13
+ }
14
+
15
+ export interface KeyboardTeleoperationConfig {
16
+ type: "keyboard"; // Only keyboard for now, expandable later
17
+ }
src/lerobot/node/{teleoperators/config.ts β†’ types/teleoperator-config.ts} RENAMED
@@ -1,6 +1,5 @@
1
  /**
2
- * Teleoperator configuration types
3
- * Shared between Node.js and Web implementations
4
  */
5
 
6
  export interface TeleoperatorConfig {
 
1
  /**
2
+ * Teleoperator configuration types for Node.js implementation
 
3
  */
4
 
5
  export interface TeleoperatorConfig {
src/lerobot/node/{constants.ts β†’ utils/constants.ts} RENAMED
File without changes
src/lerobot/node/{keyboard_teleop.ts β†’ utils/keyboard-teleop.ts} RENAMED
@@ -4,7 +4,7 @@
4
  */
5
 
6
  import * as readline from "readline";
7
- import { SO100Follower } from "./robots/so100_follower.js";
8
 
9
  /**
10
  * Keyboard controller for robot teleoperation
 
4
  */
5
 
6
  import * as readline from "readline";
7
+ import { SO100Follower } from "../robots/so100_follower.js";
8
 
9
  /**
10
  * Keyboard controller for robot teleoperation
src/lerobot/web/robots/robot.ts CHANGED
@@ -3,7 +3,7 @@
3
  * Uses Web Serial API for serial communication
4
  */
5
 
6
- import type { RobotConfig } from "../../node/robots/config.js";
7
 
8
  // Web Serial API type declarations (minimal for our needs)
9
  declare global {
 
3
  * Uses Web Serial API for serial communication
4
  */
5
 
6
+ import type { RobotConfig } from "../../node/types/robot-config.js";
7
 
8
  // Web Serial API type declarations (minimal for our needs)
9
  declare global {