Spaces:
Running
Running
| /** | |
| * High-Performance Configuration for Robot Control | |
| * Optimized for minimum latency and maximum responsiveness | |
| * | |
| * These values are hardcoded constants optimized for local network operations. | |
| * Modify these values carefully - aggressive settings may cause instability. | |
| */ | |
| export const PERFORMANCE_CONFIG = { | |
| /** | |
| * WebSocket Connection Settings | |
| * Optimized for minimal connection overhead | |
| */ | |
| WEBSOCKET: { | |
| // Connection timeout - reduced from default 10s to 3s | |
| CONNECTION_TIMEOUT_MS: 3000, | |
| // Reconnection settings - faster recovery | |
| MAX_RECONNECT_ATTEMPTS: 10, | |
| INITIAL_RECONNECT_DELAY_MS: 250, // Start with 250ms instead of 1s | |
| MAX_RECONNECT_DELAY_MS: 2000, // Cap at 2s instead of exponential growth | |
| // Heartbeat frequency - more frequent to detect issues faster | |
| HEARTBEAT_INTERVAL_MS: 5000 // Every 5s instead of 30s | |
| }, | |
| /** | |
| * Robot Polling and Update Rates | |
| * Optimized for real-time responsiveness | |
| */ | |
| ROBOT_POLLING: { | |
| // USB Master polling - increased from 200ms to 50ms (20Hz) | |
| USB_MASTER_POLL_INTERVAL_MS: 50, | |
| // Robot sync interval - increased from 100ms to 33ms (~30Hz) | |
| ROBOT_SYNC_INTERVAL_MS: 33, | |
| // State update frequency for mock/slave drivers | |
| STATE_UPDATE_INTERVAL_MS: 33, // ~30Hz instead of 10Hz | |
| // Command sequence playback rate | |
| SEQUENCE_PLAYBACK_INTERVAL_MS: 16, // ~60Hz for smooth playback | |
| // Minimum position change threshold (servo units) | |
| POSITION_CHANGE_THRESHOLD: 2 // Reduced from 5 for more sensitivity | |
| }, | |
| /** | |
| * Data Processing Optimizations | |
| * Reduce computational overhead | |
| */ | |
| DATA_PROCESSING: { | |
| // Joint smoothing history size - reduced for lower latency | |
| SMOOTHING_HISTORY_SIZE: 2, // Reduced from 3 | |
| // Enable/disable smoothing by default (can cause latency) | |
| ENABLE_SMOOTHING: false // Disabled for minimum latency | |
| }, | |
| /** | |
| * Network and Serial Communication | |
| * Optimized for low-latency communication | |
| */ | |
| COMMUNICATION: { | |
| // Serial port settings for USB connections | |
| USB_BAUD_RATE: 1000000, // 1Mbps - maximum for most servos | |
| BATCH_COMMAND_DELAY_MS: 10 // Minimal delay between batch commands | |
| }, | |
| /** | |
| * Safety and Control Optimizations (inspired by lerobot) | |
| * Prevent dangerous movements and ensure smooth operation | |
| */ | |
| SAFETY: { | |
| // Maximum relative movement per step (degrees) | |
| MAX_RELATIVE_TARGET_DEG: 15, // Prevent sudden large movements | |
| // Maximum joint velocity (degrees/second) | |
| MAX_JOINT_VELOCITY_DEG_S: 120, // Limit movement speed | |
| // Enable position clamping for safety | |
| ENABLE_POSITION_CLAMPING: true, | |
| // Emergency stop on large position jumps | |
| EMERGENCY_STOP_THRESHOLD_DEG: 45 | |
| }, | |
| /** | |
| * Timing and Synchronization (inspired by lerobot) | |
| * Precise timing control for smooth operation | |
| */ | |
| TIMING: { | |
| // Use busy wait for precise timing (like lerobot) | |
| USE_BUSY_WAIT: true, | |
| // Enable high-precision timing | |
| USE_HIGH_PRECISION_TIMING: true, | |
| // Warmup time for device synchronization | |
| DEVICE_WARMUP_TIME_MS: 2000 | |
| }, | |
| /** | |
| * Logging and Debug Settings | |
| * Minimal logging for production performance | |
| */ | |
| LOGGING: { | |
| // Enable performance logging | |
| ENABLE_PERFORMANCE_LOGS: false, | |
| // Log level for WebSocket traffic | |
| WEBSOCKET_LOG_LEVEL: "error", // Only log errors | |
| // Enable timing measurements (like lerobot) | |
| ENABLE_TIMING_MEASUREMENTS: true, | |
| // Console log buffer size | |
| LOG_BUFFER_SIZE: 100, | |
| // Enable detailed performance metrics | |
| ENABLE_DETAILED_METRICS: false | |
| } | |
| } as const; | |
| /** | |
| * Performance Presets | |
| * Quick configurations for different scenarios | |
| */ | |
| export const PERFORMANCE_PRESETS = { | |
| /** | |
| * Ultra Low Latency - For same-network, high-performance scenarios | |
| * Maximum responsiveness, minimal buffering | |
| */ | |
| ULTRA_LOW_LATENCY: { | |
| ...PERFORMANCE_CONFIG, | |
| ROBOT_POLLING: { | |
| ...PERFORMANCE_CONFIG.ROBOT_POLLING, | |
| USB_MASTER_POLL_INTERVAL_MS: 20, // 50Hz | |
| ROBOT_SYNC_INTERVAL_MS: 16, // ~60Hz | |
| STATE_UPDATE_INTERVAL_MS: 16 // ~60Hz | |
| }, | |
| WEBSOCKET: { | |
| ...PERFORMANCE_CONFIG.WEBSOCKET, | |
| HEARTBEAT_INTERVAL_MS: 2000, // Every 2s | |
| CONNECTION_TIMEOUT_MS: 1500 // 1.5s timeout | |
| }, | |
| DATA_PROCESSING: { | |
| ...PERFORMANCE_CONFIG.DATA_PROCESSING, | |
| ENABLE_SMOOTHING: false, // No smoothing | |
| POSITION_CHANGE_THRESHOLD: 1 // Maximum sensitivity | |
| } | |
| }, | |
| /** | |
| * Balanced - Good performance with stability | |
| * Recommended for most use cases | |
| */ | |
| BALANCED: { | |
| ...PERFORMANCE_CONFIG, | |
| DATA_PROCESSING: { | |
| ...PERFORMANCE_CONFIG.DATA_PROCESSING, | |
| ENABLE_SMOOTHING: true, | |
| SMOOTHING_HISTORY_SIZE: 3 | |
| } | |
| }, | |
| /** | |
| * Stable - Conservative settings for unreliable networks | |
| * Prioritizes stability over latency | |
| */ | |
| STABLE: { | |
| ...PERFORMANCE_CONFIG, | |
| ROBOT_POLLING: { | |
| ...PERFORMANCE_CONFIG.ROBOT_POLLING, | |
| USB_MASTER_POLL_INTERVAL_MS: 100, // 10Hz | |
| ROBOT_SYNC_INTERVAL_MS: 100 // 10Hz | |
| }, | |
| WEBSOCKET: { | |
| ...PERFORMANCE_CONFIG.WEBSOCKET, | |
| HEARTBEAT_INTERVAL_MS: 10000, // Every 10s | |
| CONNECTION_TIMEOUT_MS: 5000 // 5s timeout | |
| } | |
| } | |
| } as const; | |
| /** | |
| * Active configuration - change this to switch presets | |
| * ULTRA_LOW_LATENCY: Maximum performance for local networks | |
| * BALANCED: Good compromise between speed and stability | |
| * STABLE: Conservative settings for unreliable connections | |
| */ | |
| export const ACTIVE_PERFORMANCE_CONFIG = PERFORMANCE_PRESETS.ULTRA_LOW_LATENCY; | |
| /** | |
| * Utility functions for configuration access | |
| */ | |
| export const getWebSocketConfig = () => ACTIVE_PERFORMANCE_CONFIG.WEBSOCKET; | |
| export const getRobotPollingConfig = () => ACTIVE_PERFORMANCE_CONFIG.ROBOT_POLLING; | |
| export const getDataProcessingConfig = () => ACTIVE_PERFORMANCE_CONFIG.DATA_PROCESSING; | |
| export const getCommunicationConfig = () => ACTIVE_PERFORMANCE_CONFIG.COMMUNICATION; | |
| export const getSafetyConfig = () => ACTIVE_PERFORMANCE_CONFIG.SAFETY; | |
| export const getTimingConfig = () => ACTIVE_PERFORMANCE_CONFIG.TIMING; | |
| export const getLoggingConfig = () => ACTIVE_PERFORMANCE_CONFIG.LOGGING; | |