Spaces:
Running
Running
File size: 6,227 Bytes
efe2c71 24f0634 130bae4 24f0634 ebd0121 130bae4 ebd0121 130bae4 efe2c71 2aa0afa 130bae4 2aa0afa efe2c71 24f0634 130bae4 efe2c71 24f0634 efe2c71 24f0634 efe2c71 24f0634 efe2c71 24f0634 efe2c71 130bae4 efe2c71 130bae4 efe2c71 130bae4 efe2c71 130bae4 efe2c71 130bae4 efe2c71 130bae4 24f0634 efe2c71 130bae4 efe2c71 130bae4 efe2c71 130bae4 efe2c71 130bae4 efe2c71 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 6fa48c4 130bae4 24f0634 6fa48c4 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 24f0634 130bae4 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 |
/**
* Web teleoperation functionality using Web Serial API
*/
import { createSO100Config } from "./robots/so100_config.js";
import type { RobotHardwareConfig } from "./types/robot-config.js";
import type { RobotConnection } from "./types/robot-connection.js";
import { WebSerialPortWrapper } from "./utils/serial-port-wrapper.js";
import {
readMotorPosition,
writeMotorPosition,
type MotorCommunicationPort,
} from "./utils/motor-communication.js";
import type {
MotorConfig,
TeleoperationState,
TeleoperationProcess,
TeleoperateConfig,
TeleoperatorConfig,
DirectTeleoperatorConfig,
} from "./types/teleoperation.js";
import {
KeyboardTeleoperator,
DirectTeleoperator,
type WebTeleoperator,
} from "./teleoperators/index.js";
// Re-export types for external use
export type {
MotorConfig,
TeleoperationState,
TeleoperationProcess,
TeleoperateConfig,
TeleoperatorConfig,
DirectTeleoperatorConfig,
} from "./types/teleoperation.js";
/**
* Create motor configurations from robot hardware config
* Pure function - converts robot specs to motor configs
*/
function createMotorConfigsFromRobotConfig(
robotConfig: RobotHardwareConfig
): MotorConfig[] {
return robotConfig.motorNames.map((name: string, i: number) => ({
id: robotConfig.motorIds[i],
name,
currentPosition: 2048,
minPosition: 1024,
maxPosition: 3072,
}));
}
/**
* Apply calibration data to motor configurations
* Pure function - takes calibration data as parameter
*/
export function applyCalibrationToMotorConfigs(
defaultConfigs: MotorConfig[],
calibrationData: { [motorName: string]: any }
): MotorConfig[] {
return defaultConfigs.map((defaultConfig) => {
const calibData = calibrationData[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,
};
}
return defaultConfig;
});
}
/**
* Create appropriate teleoperator based on configuration
*/
async function createTeleoperator(
config: TeleoperateConfig,
port: MotorCommunicationPort,
motorConfigs: MotorConfig[],
robotHardwareConfig: RobotHardwareConfig
): Promise<WebTeleoperator> {
switch (config.teleop.type) {
case "keyboard":
return new KeyboardTeleoperator(
config.teleop,
port,
motorConfigs,
robotHardwareConfig.keyboardControls,
config.onStateUpdate
);
case "direct":
return new DirectTeleoperator(
config.teleop,
port,
motorConfigs,
config.onStateUpdate
);
case "so100_leader":
throw new Error("Leader arm teleoperator not yet implemented");
case "gamepad":
throw new Error("Gamepad teleoperator not yet implemented");
default:
throw new Error(
`Unsupported teleoperator type: ${(config.teleop as any).type}`
);
}
}
/**
* Build TeleoperationState from teleoperator and motor configs
*/
function buildTeleoperationStateFromTeleoperator(
teleoperator: WebTeleoperator
): TeleoperationState {
const teleoperatorState = teleoperator.getState();
const isActive = (teleoperator as any).isActive;
return {
isActive: isActive || false,
motorConfigs: [...teleoperator.motorConfigs], // Get fresh motor configs from teleoperator
lastUpdate: Date.now(),
...teleoperatorState,
};
}
/**
* Main teleoperate function
*/
export async function teleoperate(
config: TeleoperateConfig
): Promise<TeleoperationProcess> {
const teleoperator = await createTeleoperatorProcess(config);
const motorConfigs = teleoperator.motorConfigs;
return {
start: () => {
teleoperator.start();
// CRITICAL: State update loop for UI synchronization
// This ensures sliders and UI reflect actual motor positions when moved via keyboard
if (config.onStateUpdate) {
const updateLoop = () => {
const state = buildTeleoperationStateFromTeleoperator(teleoperator);
if (state.isActive) {
config.onStateUpdate!(state);
setTimeout(updateLoop, 100); // 10fps state updates - keeps sliders in sync
}
};
updateLoop();
}
},
stop: () => teleoperator.stop(),
updateKeyState: (key: string, pressed: boolean) => {
// Delegate to teleoperator if it supports keyboard input
if (teleoperator instanceof KeyboardTeleoperator) {
teleoperator.updateKeyState(key, pressed);
}
},
getState: () => buildTeleoperationStateFromTeleoperator(teleoperator),
teleoperator,
disconnect: () => teleoperator.disconnect(),
};
}
/**
* Create teleoperator instance (shared logic)
*/
async function createTeleoperatorProcess(
config: TeleoperateConfig
): Promise<WebTeleoperator> {
// Validate required fields
if (!config.robot.robotType) {
throw new Error(
"Robot type is required for teleoperation. Please configure the robot first."
);
}
// Create web serial port wrapper
const port = new WebSerialPortWrapper(config.robot.port);
await port.initialize();
// Get robot-specific configuration
let robotHardwareConfig: RobotHardwareConfig;
if (config.robot.robotType.startsWith("so100")) {
robotHardwareConfig = createSO100Config(config.robot.robotType);
} else {
throw new Error(`Unsupported robot type: ${config.robot.robotType}`);
}
// Create motor configs from robot hardware specs
const defaultMotorConfigs =
createMotorConfigsFromRobotConfig(robotHardwareConfig);
// Apply calibration data if provided
const motorConfigs = config.calibrationData
? applyCalibrationToMotorConfigs(
defaultMotorConfigs,
config.calibrationData
)
: defaultMotorConfigs;
// Create teleoperator
const teleoperator = await createTeleoperator(
config,
port,
motorConfigs,
robotHardwareConfig
);
await teleoperator.initialize();
return teleoperator;
}
|