Spaces:
Running
Running
File size: 9,055 Bytes
130ae50 |
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 |
// Unified storage system for robot data
// Consolidates robot config, calibration data, and metadata under one key per device
export interface UnifiedRobotData {
device_info: {
serialNumber: string;
robotType: "so100_follower" | "so100_leader";
robotId: string;
usbMetadata?: any;
lastUpdated: string;
};
calibration?: {
// Motor calibration data (from lerobot_calibration_* keys)
shoulder_pan?: {
id: number;
drive_mode: number;
homing_offset: number;
range_min: number;
range_max: number;
};
shoulder_lift?: {
id: number;
drive_mode: number;
homing_offset: number;
range_min: number;
range_max: number;
};
elbow_flex?: {
id: number;
drive_mode: number;
homing_offset: number;
range_min: number;
range_max: number;
};
wrist_flex?: {
id: number;
drive_mode: number;
homing_offset: number;
range_min: number;
range_max: number;
};
wrist_roll?: {
id: number;
drive_mode: number;
homing_offset: number;
range_min: number;
range_max: number;
};
gripper?: {
id: number;
drive_mode: number;
homing_offset: number;
range_min: number;
range_max: number;
};
// Calibration metadata (from lerobot-calibration-* keys)
metadata: {
timestamp: string;
readCount: number;
platform: string;
api: string;
device_type: string;
device_id: string;
calibrated_at: string;
};
};
}
/**
* Get unified storage key for a robot by serial number
*/
export function getUnifiedKey(serialNumber: string): string {
return `lerobotjs-${serialNumber}`;
}
/**
* Migrate data from old storage keys to unified format
* Safely combines data from three sources:
* 1. lerobot-robot-{serialNumber} - robot config
* 2. lerobot-calibration-{serialNumber} - calibration metadata
* 3. lerobot_calibration_{robotType}_{robotId} - actual calibration data
*/
export function migrateToUnifiedStorage(
serialNumber: string
): UnifiedRobotData | null {
try {
const unifiedKey = getUnifiedKey(serialNumber);
// Check if already migrated
const existing = localStorage.getItem(unifiedKey);
if (existing) {
console.log(`β
Data already unified for ${serialNumber}`);
return JSON.parse(existing);
}
console.log(`π Migrating data for serial number: ${serialNumber}`);
// 1. Get robot configuration
const robotConfigKey = `lerobot-robot-${serialNumber}`;
const robotConfigRaw = localStorage.getItem(robotConfigKey);
if (!robotConfigRaw) {
return null;
}
const robotConfig = JSON.parse(robotConfigRaw);
console.log(`π Found robot config:`, robotConfig);
// 2. Get calibration metadata
const calibrationMetaKey = `lerobot-calibration-${serialNumber}`;
const calibrationMetaRaw = localStorage.getItem(calibrationMetaKey);
const calibrationMeta = calibrationMetaRaw
? JSON.parse(calibrationMetaRaw)
: null;
console.log(`π Found calibration metadata:`, calibrationMeta);
// 3. Get actual calibration data (using robotType and robotId from config)
const calibrationDataKey = `lerobot_calibration_${robotConfig.robotType}_${robotConfig.robotId}`;
const calibrationDataRaw = localStorage.getItem(calibrationDataKey);
const calibrationData = calibrationDataRaw
? JSON.parse(calibrationDataRaw)
: null;
console.log(`π§ Found calibration data:`, calibrationData);
// 4. Build unified structure
const unifiedData: UnifiedRobotData = {
device_info: {
serialNumber: robotConfig.serialNumber || serialNumber,
robotType: robotConfig.robotType,
robotId: robotConfig.robotId,
lastUpdated: robotConfig.lastUpdated || new Date().toISOString(),
},
};
// Add calibration if available
if (calibrationData && calibrationMeta) {
const motors: any = {};
// Copy motor data (excluding metadata fields)
Object.keys(calibrationData).forEach((key) => {
if (
![
"device_type",
"device_id",
"calibrated_at",
"platform",
"api",
].includes(key)
) {
motors[key] = calibrationData[key];
}
});
unifiedData.calibration = {
...motors,
metadata: {
timestamp: calibrationMeta.timestamp || calibrationData.calibrated_at,
readCount: calibrationMeta.readCount || 0,
platform: calibrationData.platform || "web",
api: calibrationData.api || "Web Serial API",
device_type: calibrationData.device_type || robotConfig.robotType,
device_id: calibrationData.device_id || robotConfig.robotId,
calibrated_at:
calibrationData.calibrated_at || calibrationMeta.timestamp,
},
};
}
// 5. Save unified data
localStorage.setItem(unifiedKey, JSON.stringify(unifiedData));
console.log(`β
Successfully unified data for ${serialNumber}`);
console.log(`π¦ Unified data:`, unifiedData);
// 6. Clean up old keys (optional - keep for now for safety)
// localStorage.removeItem(robotConfigKey);
// localStorage.removeItem(calibrationMetaKey);
// localStorage.removeItem(calibrationDataKey);
return unifiedData;
} catch (error) {
console.error(`β Failed to migrate data for ${serialNumber}:`, error);
return null;
}
}
/**
* Get unified robot data
*/
export function getUnifiedRobotData(
serialNumber: string
): UnifiedRobotData | null {
const unifiedKey = getUnifiedKey(serialNumber);
// Try to get existing unified data
const existing = localStorage.getItem(unifiedKey);
if (existing) {
try {
return JSON.parse(existing);
} catch (error) {
console.warn(`Failed to parse unified data for ${serialNumber}:`, error);
}
}
return null;
}
/**
* Save robot configuration to unified storage
*/
export function saveRobotConfig(
serialNumber: string,
robotType: "so100_follower" | "so100_leader",
robotId: string,
usbMetadata?: any
): void {
const unifiedKey = getUnifiedKey(serialNumber);
const existing =
getUnifiedRobotData(serialNumber) || ({} as UnifiedRobotData);
existing.device_info = {
serialNumber,
robotType,
robotId,
usbMetadata,
lastUpdated: new Date().toISOString(),
};
localStorage.setItem(unifiedKey, JSON.stringify(existing));
console.log(`πΎ Saved robot config for ${serialNumber}`);
}
/**
* Save calibration data to unified storage
*/
export function saveCalibrationData(
serialNumber: string,
calibrationData: any,
metadata: { timestamp: string; readCount: number }
): void {
const unifiedKey = getUnifiedKey(serialNumber);
const existing =
getUnifiedRobotData(serialNumber) || ({} as UnifiedRobotData);
// Ensure device_info exists
if (!existing.device_info) {
console.warn(
`No device info found for ${serialNumber}, cannot save calibration`
);
return;
}
// Extract motor data (exclude metadata fields)
const motors: any = {};
Object.keys(calibrationData).forEach((key) => {
if (
![
"device_type",
"device_id",
"calibrated_at",
"platform",
"api",
].includes(key)
) {
motors[key] = calibrationData[key];
}
});
existing.calibration = {
...motors,
metadata: {
timestamp: metadata.timestamp,
readCount: metadata.readCount,
platform: calibrationData.platform || "web",
api: calibrationData.api || "Web Serial API",
device_type:
calibrationData.device_type || existing.device_info.robotType,
device_id: calibrationData.device_id || existing.device_info.robotId,
calibrated_at: calibrationData.calibrated_at || metadata.timestamp,
},
};
localStorage.setItem(unifiedKey, JSON.stringify(existing));
console.log(`π§ Saved calibration data for ${serialNumber}`);
}
/**
* Check if robot is calibrated
*/
export function isRobotCalibrated(serialNumber: string): boolean {
const data = getUnifiedRobotData(serialNumber);
return !!data?.calibration?.metadata?.timestamp;
}
/**
* Get calibration status for dashboard
*/
export function getCalibrationStatus(
serialNumber: string
): { timestamp: string; readCount: number } | null {
const data = getUnifiedRobotData(serialNumber);
if (data?.calibration?.metadata) {
return {
timestamp: data.calibration.metadata.timestamp,
readCount: data.calibration.metadata.readCount,
};
}
return null;
}
/**
* Get robot configuration
*/
export function getRobotConfig(
serialNumber: string
): { robotType: string; robotId: string } | null {
const data = getUnifiedRobotData(serialNumber);
if (data?.device_info) {
return {
robotType: data.device_info.robotType,
robotId: data.device_info.robotId,
};
}
return null;
}
|