File size: 5,903 Bytes
3aea7c6
 
 
18b0fa5
3aea7c6
 
 
 
18b0fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aea7c6
 
 
 
 
 
 
18b0fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3aea7c6
 
 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
 
 
 
 
 
 
 
18b0fa5
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
/**
 * 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;