Spaces:
Running
Running
File size: 15,095 Bytes
efe2c71 5eb1bc0 efe2c71 5eb1bc0 efe2c71 5eb1bc0 efe2c71 5eb1bc0 efe2c71 5eb1bc0 efe2c71 5eb1bc0 efe2c71 |
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 |
import { useState, useEffect, useCallback, useRef } from "react";
import { useRobotConnection } from "./useRobotConnection";
import { getUnifiedRobotData } from "../lib/unified-storage";
import type { RobotConnection } from "../../lerobot/web/find_port.js";
export interface MotorConfig {
name: string;
minPosition: number;
maxPosition: number;
currentPosition: number;
homePosition: number;
}
export interface KeyState {
pressed: boolean;
lastPressed: number;
}
export interface UseTeleoperationOptions {
robot: RobotConnection;
enabled: boolean;
onError?: (error: string) => void;
}
export interface UseTeleoperationResult {
// Connection state from singleton
isConnected: boolean;
isActive: boolean;
// Motor state
motorConfigs: MotorConfig[];
// Keyboard state
keyStates: Record<string, KeyState>;
// Error state
error: string | null;
// Control methods
start: () => void;
stop: () => void;
goToHome: () => Promise<void>;
simulateKeyPress: (key: string) => void;
simulateKeyRelease: (key: string) => void;
moveMotorToPosition: (motorIndex: number, position: number) => Promise<void>;
}
const MOTOR_CONFIGS: MotorConfig[] = [
{
name: "shoulder_pan",
minPosition: 0,
maxPosition: 4095,
currentPosition: 2048,
homePosition: 2048,
},
{
name: "shoulder_lift",
minPosition: 1024,
maxPosition: 3072,
currentPosition: 2048,
homePosition: 2048,
},
{
name: "elbow_flex",
minPosition: 1024,
maxPosition: 3072,
currentPosition: 2048,
homePosition: 2048,
},
{
name: "wrist_flex",
minPosition: 1024,
maxPosition: 3072,
currentPosition: 2048,
homePosition: 2048,
},
{
name: "wrist_roll",
minPosition: 0,
maxPosition: 4095,
currentPosition: 2048,
homePosition: 2048,
},
{
name: "gripper",
minPosition: 1800,
maxPosition: 2400,
currentPosition: 2100,
homePosition: 2100,
},
];
// PROVEN VALUES from Node.js implementation (conventions.md)
const SMOOTH_CONTROL_CONFIG = {
STEP_SIZE: 25, // Proven optimal from conventions.md
CHANGE_THRESHOLD: 0.5, // Prevents micro-movements and unnecessary commands
MOTOR_DELAY: 1, // Minimal delay between motor commands (from conventions.md)
UPDATE_INTERVAL: 30, // 30ms = ~33Hz for responsive control (was 50ms = 20Hz)
} as const;
const KEYBOARD_CONTROLS = {
ArrowUp: { motorIndex: 1, direction: 1, description: "Shoulder lift up" },
ArrowDown: {
motorIndex: 1,
direction: -1,
description: "Shoulder lift down",
},
ArrowLeft: { motorIndex: 0, direction: -1, description: "Shoulder pan left" },
ArrowRight: {
motorIndex: 0,
direction: 1,
description: "Shoulder pan right",
},
w: { motorIndex: 2, direction: 1, description: "Elbow flex up" },
s: { motorIndex: 2, direction: -1, description: "Elbow flex down" },
a: { motorIndex: 3, direction: -1, description: "Wrist flex left" },
d: { motorIndex: 3, direction: 1, description: "Wrist flex right" },
q: { motorIndex: 4, direction: -1, description: "Wrist roll left" },
e: { motorIndex: 4, direction: 1, description: "Wrist roll right" },
o: { motorIndex: 5, direction: 1, description: "Gripper open" },
c: { motorIndex: 5, direction: -1, description: "Gripper close" },
Escape: { motorIndex: -1, direction: 0, description: "Emergency stop" },
};
export function useTeleoperation({
robot,
enabled,
onError,
}: UseTeleoperationOptions): UseTeleoperationResult {
const connection = useRobotConnection();
const [isActive, setIsActive] = useState(false);
const [motorConfigs, setMotorConfigs] =
useState<MotorConfig[]>(MOTOR_CONFIGS);
const [keyStates, setKeyStates] = useState<Record<string, KeyState>>({});
const [error, setError] = useState<string | null>(null);
const activeKeysRef = useRef<Set<string>>(new Set());
const motorPositionsRef = useRef<number[]>(
MOTOR_CONFIGS.map((m) => m.homePosition)
);
const movementIntervalRef = useRef<NodeJS.Timeout | null>(null);
// Load calibration data
useEffect(() => {
const loadCalibration = async () => {
try {
if (!robot.serialNumber) {
console.warn("No serial number available for calibration loading");
return;
}
const data = getUnifiedRobotData(robot.serialNumber);
if (data?.calibration) {
// Map motor names to calibration data
const motorNames = [
"shoulder_pan",
"shoulder_lift",
"elbow_flex",
"wrist_flex",
"wrist_roll",
"gripper",
];
const calibratedConfigs = MOTOR_CONFIGS.map((config, index) => {
const motorName = motorNames[index] as keyof NonNullable<
typeof data.calibration
>;
const calibratedMotor = data.calibration![motorName];
if (
calibratedMotor &&
typeof calibratedMotor === "object" &&
"homing_offset" in calibratedMotor &&
"range_min" in calibratedMotor &&
"range_max" in calibratedMotor
) {
// Use 2048 as default home position, adjusted by homing offset
const homePosition = 2048 + (calibratedMotor.homing_offset || 0);
return {
...config,
homePosition,
currentPosition: homePosition,
// IMPORTANT: Use actual calibrated limits instead of hardcoded ones
minPosition: calibratedMotor.range_min || config.minPosition,
maxPosition: calibratedMotor.range_max || config.maxPosition,
};
}
return config;
});
setMotorConfigs(calibratedConfigs);
// DON'T set motorPositionsRef here - it will be set when teleoperation starts
// motorPositionsRef.current = calibratedConfigs.map((m) => m.homePosition);
console.log("โ
Loaded calibration data for", robot.serialNumber);
}
} catch (error) {
console.warn("Failed to load calibration:", error);
}
};
loadCalibration();
}, [robot.serialNumber]);
// Keyboard event handlers
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
if (!isActive) return;
const key = event.key;
if (key in KEYBOARD_CONTROLS) {
event.preventDefault();
if (key === "Escape") {
setIsActive(false);
activeKeysRef.current.clear();
return;
}
if (!activeKeysRef.current.has(key)) {
activeKeysRef.current.add(key);
setKeyStates((prev) => ({
...prev,
[key]: { pressed: true, lastPressed: Date.now() },
}));
}
}
},
[isActive]
);
const handleKeyUp = useCallback(
(event: KeyboardEvent) => {
if (!isActive) return;
const key = event.key;
if (key in KEYBOARD_CONTROLS) {
event.preventDefault();
activeKeysRef.current.delete(key);
setKeyStates((prev) => ({
...prev,
[key]: { pressed: false, lastPressed: Date.now() },
}));
}
},
[isActive]
);
// Register keyboard events
useEffect(() => {
if (enabled && isActive) {
window.addEventListener("keydown", handleKeyDown);
window.addEventListener("keyup", handleKeyUp);
return () => {
window.removeEventListener("keydown", handleKeyDown);
window.removeEventListener("keyup", handleKeyUp);
};
}
}, [enabled, isActive, handleKeyDown, handleKeyUp]);
// CONTINUOUS MOVEMENT: For held keys with PROVEN smooth patterns from Node.js
useEffect(() => {
if (!isActive || !connection.isConnected) {
if (movementIntervalRef.current) {
clearInterval(movementIntervalRef.current);
movementIntervalRef.current = null;
}
return;
}
const processMovement = async () => {
if (activeKeysRef.current.size === 0) return;
const activeKeys = Array.from(activeKeysRef.current);
const changedMotors: Array<{ index: number; position: number }> = [];
// PROVEN PATTERN: Process all active keys and collect changes
for (const key of activeKeys) {
const control =
KEYBOARD_CONTROLS[key as keyof typeof KEYBOARD_CONTROLS];
if (control && control.motorIndex >= 0) {
const motorIndex = control.motorIndex;
const direction = control.direction;
const motor = motorConfigs[motorIndex];
if (motor) {
const currentPos = motorPositionsRef.current[motorIndex];
let newPos =
currentPos + direction * SMOOTH_CONTROL_CONFIG.STEP_SIZE;
// Clamp to motor limits
newPos = Math.max(
motor.minPosition,
Math.min(motor.maxPosition, newPos)
);
// PROVEN PATTERN: Only update if change is meaningful (0.5 unit threshold)
if (
Math.abs(newPos - currentPos) >
SMOOTH_CONTROL_CONFIG.CHANGE_THRESHOLD
) {
motorPositionsRef.current[motorIndex] = newPos;
changedMotors.push({ index: motorIndex, position: newPos });
}
}
}
}
// PROVEN PATTERN: Only send commands for motors that actually changed
if (changedMotors.length > 0) {
try {
for (const { index, position } of changedMotors) {
await connection.writeMotorPosition(index + 1, position);
// PROVEN PATTERN: Minimal delay between motor commands (1ms)
if (changedMotors.length > 1) {
await new Promise((resolve) =>
setTimeout(resolve, SMOOTH_CONTROL_CONFIG.MOTOR_DELAY)
);
}
}
// Update UI to reflect changes
setMotorConfigs((prev) =>
prev.map((config, index) => ({
...config,
currentPosition: motorPositionsRef.current[index],
}))
);
} catch (error) {
console.warn("Failed to update robot positions:", error);
}
}
};
// PROVEN TIMING: 30ms interval (~33Hz) for responsive continuous movement
movementIntervalRef.current = setInterval(
processMovement,
SMOOTH_CONTROL_CONFIG.UPDATE_INTERVAL
);
return () => {
if (movementIntervalRef.current) {
clearInterval(movementIntervalRef.current);
movementIntervalRef.current = null;
}
};
}, [
isActive,
connection.isConnected,
connection.writeMotorPosition,
motorConfigs,
]);
// Control methods
const start = useCallback(async () => {
if (!connection.isConnected) {
setError("Robot not connected");
onError?.("Robot not connected");
return;
}
try {
console.log(
"๐ฎ Starting teleoperation - reading current motor positions..."
);
// Read current positions of all motors using PROVEN utility
const motorIds = [1, 2, 3, 4, 5, 6];
const currentPositions = await connection.readAllMotorPositions(motorIds);
// Log all positions (trust the utility's fallback handling)
for (let i = 0; i < currentPositions.length; i++) {
const position = currentPositions[i];
console.log(`๐ Motor ${i + 1} current position: ${position}`);
}
// CRITICAL: Update positions BEFORE activating movement
motorPositionsRef.current = currentPositions;
// Update UI to show actual current positions
setMotorConfigs((prev) =>
prev.map((config, index) => ({
...config,
currentPosition: currentPositions[index],
}))
);
// IMPORTANT: Only activate AFTER positions are synchronized
setIsActive(true);
setError(null);
console.log(
"โ
Teleoperation started with synchronized positions:",
currentPositions
);
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: "Failed to start teleoperation";
setError(errorMessage);
onError?.(errorMessage);
console.error("โ Failed to start teleoperation:", error);
}
}, [
connection.isConnected,
connection.readAllMotorPositions,
motorConfigs,
onError,
]);
const stop = useCallback(() => {
setIsActive(false);
activeKeysRef.current.clear();
setKeyStates({});
console.log("๐ Teleoperation stopped");
}, []);
const goToHome = useCallback(async () => {
if (!connection.isConnected) {
setError("Robot not connected");
return;
}
try {
for (let i = 0; i < motorConfigs.length; i++) {
const motor = motorConfigs[i];
await connection.writeMotorPosition(i + 1, motor.homePosition);
motorPositionsRef.current[i] = motor.homePosition;
}
setMotorConfigs((prev) =>
prev.map((config) => ({
...config,
currentPosition: config.homePosition,
}))
);
console.log("๐ Moved to home position");
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Failed to go to home";
setError(errorMessage);
onError?.(errorMessage);
}
}, [
connection.isConnected,
connection.writeMotorPosition,
motorConfigs,
onError,
]);
const simulateKeyPress = useCallback(
(key: string) => {
if (!isActive) return;
activeKeysRef.current.add(key);
setKeyStates((prev) => ({
...prev,
[key]: { pressed: true, lastPressed: Date.now() },
}));
},
[isActive]
);
const simulateKeyRelease = useCallback(
(key: string) => {
if (!isActive) return;
activeKeysRef.current.delete(key);
setKeyStates((prev) => ({
...prev,
[key]: { pressed: false, lastPressed: Date.now() },
}));
},
[isActive]
);
const moveMotorToPosition = useCallback(
async (motorIndex: number, position: number) => {
if (!connection.isConnected) {
return;
}
try {
await connection.writeMotorPosition(motorIndex + 1, position);
// Update internal state
motorPositionsRef.current[motorIndex] = position;
setMotorConfigs((prev) =>
prev.map((config, index) => ({
...config,
currentPosition:
index === motorIndex ? position : config.currentPosition,
}))
);
} catch (error) {
console.warn(
`Failed to move motor ${motorIndex + 1} to position ${position}:`,
error
);
}
},
[connection.isConnected, connection.writeMotorPosition]
);
return {
isConnected: connection.isConnected,
isActive,
motorConfigs,
keyStates,
error,
start,
stop,
goToHome,
simulateKeyPress,
simulateKeyRelease,
moveMotorToPosition,
};
}
|