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
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
/**
 * Advanced Robot Optimization Utilities
 * Inspired by lerobot techniques for high-performance robot control
 */

import { getSafetyConfig, getTimingConfig, getLoggingConfig } from "$lib/configs/performanceConfig";
import type { RobotCommand } from "$lib/types/robotDriver";

/**
 * Performance timing utilities (inspired by lerobot's perf_counter usage)
 */
export class PerformanceTimer {
	private startTime: number;
	private logs: Map<string, number> = new Map();

	constructor() {
		this.startTime = performance.now();
	}

	/**
	 * Mark a timing checkpoint
	 */
	mark(label: string): void {
		const now = performance.now();
		this.logs.set(label, now - this.startTime);
		this.startTime = now;
	}

	/**
	 * Get timing for a specific label
	 */
	getTime(label: string): number | undefined {
		return this.logs.get(label);
	}

	/**
	 * Get all timing data
	 */
	getAllTimings(): Map<string, number> {
		return new Map(this.logs);
	}

	/**
	 * Log performance data in lerobot style
	 */
	logPerformance(prefix: string = "robot"): void {
		if (!getLoggingConfig().ENABLE_TIMING_MEASUREMENTS) return;

		const items: string[] = [];
		for (const [label, timeMs] of this.logs) {
			const hz = 1000 / timeMs;
			items.push(`${label}:${timeMs.toFixed(2)}ms (${hz.toFixed(1)}Hz)`);
		}

		if (items.length > 0) {
			console.log(`${prefix} ${items.join(" ")}`);
		}
	}

	/**
	 * Reset timing data
	 */
	reset(): void {
		this.logs.clear();
		this.startTime = performance.now();
	}
}

/**
 * Safety position clamping (inspired by lerobot's ensure_safe_goal_position)
 * Prevents sudden large movements that could damage the robot
 */
export function ensureSafeGoalPosition(
	goalPosition: number,
	currentPosition: number,
	maxRelativeTarget?: number
): number {
	const safetyConfig = getSafetyConfig();

	if (!safetyConfig.ENABLE_POSITION_CLAMPING) {
		return goalPosition;
	}

	const maxTarget = maxRelativeTarget || safetyConfig.MAX_RELATIVE_TARGET_DEG;
	const deltaPosition = goalPosition - currentPosition;

	// Check for emergency stop condition
	if (Math.abs(deltaPosition) > safetyConfig.EMERGENCY_STOP_THRESHOLD_DEG) {
		console.warn(
			`⚠️ Emergency stop: movement too large (${deltaPosition.toFixed(1)}° > ${safetyConfig.EMERGENCY_STOP_THRESHOLD_DEG}°)`
		);
		return currentPosition; // Don't move at all
	}

	// Clamp to maximum relative movement
	if (Math.abs(deltaPosition) > maxTarget) {
		const clampedPosition = currentPosition + Math.sign(deltaPosition) * maxTarget;
		console.log(
			`🛡️ Safety clamp: ${goalPosition.toFixed(1)}° → ${clampedPosition.toFixed(1)}° (max: ±${maxTarget}°)`
		);
		return clampedPosition;
	}

	return goalPosition;
}

/**
 * Velocity limiting (inspired by lerobot's joint velocity constraints)
 */
export function limitJointVelocity(
	currentPosition: number,
	goalPosition: number,
	deltaTimeMs: number,
	maxVelocityDegS?: number
): number {
	const safetyConfig = getSafetyConfig();
	const maxVel = maxVelocityDegS || safetyConfig.MAX_JOINT_VELOCITY_DEG_S;

	const deltaPosition = goalPosition - currentPosition;
	const deltaTimeS = deltaTimeMs / 1000;
	const requiredVelocity = Math.abs(deltaPosition) / deltaTimeS;

	if (requiredVelocity > maxVel) {
		const maxMovement = maxVel * deltaTimeS;
		const limitedPosition = currentPosition + Math.sign(deltaPosition) * maxMovement;

		console.log(
			`🐌 Velocity limit: ${requiredVelocity.toFixed(1)}°/s → ${maxVel}°/s (pos: ${limitedPosition.toFixed(1)}°)`
		);

		return limitedPosition;
	}

	return goalPosition;
}

/**
 * Busy wait for precise timing (inspired by lerobot's busy_wait function)
 * More accurate than setTimeout for high-frequency control loops
 */
export async function busyWait(durationMs: number): Promise<void> {
	const timingConfig = getTimingConfig();

	if (!timingConfig.USE_BUSY_WAIT || durationMs <= 0) {
		return;
	}

	const startTime = performance.now();
	const targetTime = startTime + durationMs;

	// Use a combination of setTimeout and busy waiting for efficiency
	if (durationMs > 5) {
		// For longer waits, use setTimeout for most of the duration
		await new Promise((resolve) => setTimeout(resolve, durationMs - 2));
	}

	// Busy wait for the remaining time for high precision
	while (performance.now() < targetTime) {
		// Busy loop - more accurate than setTimeout for short durations
	}
}

/**
 * Frame rate controller with precise timing
 */
export class FrameRateController {
	private lastFrameTime: number;
	private targetFrameTimeMs: number;
	private frameCount: number = 0;
	private performanceTimer: PerformanceTimer;

	constructor(targetFps: number) {
		this.targetFrameTimeMs = 1000 / targetFps;
		this.lastFrameTime = performance.now();
		this.performanceTimer = new PerformanceTimer();
	}

	/**
	 * Wait until the next frame should start
	 */
	async waitForNextFrame(): Promise<void> {
		const now = performance.now();
		const elapsed = now - this.lastFrameTime;
		const remaining = this.targetFrameTimeMs - elapsed;

		if (remaining > 0) {
			await busyWait(remaining);
		}

		this.lastFrameTime = performance.now();
		this.frameCount++;

		// Log performance periodically
		if (this.frameCount % 60 === 0) {
			const actualFrameTime = now - this.lastFrameTime + remaining;
			const actualFps = 1000 / actualFrameTime;
			this.performanceTimer.logPerformance(`frame_rate: ${actualFps.toFixed(1)}fps`);
		}
	}

	/**
	 * Get current frame timing info
	 */
	getFrameInfo(): { frameCount: number; actualFps: number; targetFps: number } {
		const now = performance.now();
		const actualFrameTime = now - this.lastFrameTime;
		const actualFps = 1000 / actualFrameTime;
		const targetFps = 1000 / this.targetFrameTimeMs;

		return {
			frameCount: this.frameCount,
			actualFps,
			targetFps
		};
	}
}

/**
 * Batch command processor for efficient joint updates
 */
export class BatchCommandProcessor {
	private commandQueue: RobotCommand[] = [];
	private batchSize: number;
	private processingInterval?: number;

	constructor(batchSize: number = 10, processingIntervalMs: number = 16) {
		this.batchSize = batchSize;
		this.startProcessing(processingIntervalMs);
	}

	/**
	 * Add a command to the batch queue
	 */
	queueCommand(command: RobotCommand): void {
		this.commandQueue.push(command);

		// Process immediately if batch is full
		if (this.commandQueue.length >= this.batchSize) {
			this.processBatch();
		}
	}

	/**
	 * Process a batch of commands
	 */
	private processBatch(): RobotCommand[] {
		if (this.commandQueue.length === 0) return [];

		const batch = this.commandQueue.splice(0, this.batchSize);

		// Merge commands for the same joints (latest wins)
		const mergedJoints = new Map<string, { name: string; value: number }>();

		for (const command of batch) {
			for (const joint of command.joints) {
				mergedJoints.set(joint.name, joint);
			}
		}

		const mergedCommand: RobotCommand = {
			timestamp: Date.now(),
			joints: Array.from(mergedJoints.values()),
			metadata: { source: "batch_processor", batchSize: batch.length }
		};

		return [mergedCommand];
	}

	/**
	 * Start automatic batch processing
	 */
	private startProcessing(intervalMs: number): void {
		this.processingInterval = setInterval(() => {
			if (this.commandQueue.length > 0) {
				this.processBatch();
			}
		}, intervalMs);
	}

	/**
	 * Stop batch processing
	 */
	stop(): void {
		if (this.processingInterval) {
			clearInterval(this.processingInterval);
			this.processingInterval = undefined;
		}
	}

	/**
	 * Get queue status
	 */
	getQueueStatus(): { queueLength: number; batchSize: number } {
		return {
			queueLength: this.commandQueue.length,
			batchSize: this.batchSize
		};
	}
}

/**
 * Connection health monitor
 */
export class ConnectionHealthMonitor {
	private lastSuccessTime: number;
	private errorCount: number = 0;
	private healthCheckInterval?: number;

	constructor(healthCheckIntervalMs: number = 1000) {
		this.lastSuccessTime = Date.now();
		this.startHealthCheck(healthCheckIntervalMs);
	}

	/**
	 * Report a successful operation
	 */
	reportSuccess(): void {
		this.lastSuccessTime = Date.now();
		this.errorCount = 0;
	}

	/**
	 * Report an error
	 */
	reportError(): void {
		this.errorCount++;
	}

	/**
	 * Check if connection is healthy
	 */
	isHealthy(maxErrorCount: number = 5, maxSilenceMs: number = 5000): boolean {
		const timeSinceLastSuccess = Date.now() - this.lastSuccessTime;
		return this.errorCount < maxErrorCount && timeSinceLastSuccess < maxSilenceMs;
	}

	/**
	 * Get health metrics
	 */
	getHealthMetrics(): { errorCount: number; timeSinceLastSuccessMs: number; isHealthy: boolean } {
		return {
			errorCount: this.errorCount,
			timeSinceLastSuccessMs: Date.now() - this.lastSuccessTime,
			isHealthy: this.isHealthy()
		};
	}

	/**
	 * Start health monitoring
	 */
	private startHealthCheck(intervalMs: number): void {
		this.healthCheckInterval = setInterval(() => {
			const metrics = this.getHealthMetrics();
			if (!metrics.isHealthy) {
				console.warn(
					`⚠️ Connection health warning: ${metrics.errorCount} errors, ${metrics.timeSinceLastSuccessMs}ms since last success`
				);
			}
		}, intervalMs);
	}

	/**
	 * Stop health monitoring
	 */
	stop(): void {
		if (this.healthCheckInterval) {
			clearInterval(this.healthCheckInterval);
			this.healthCheckInterval = undefined;
		}
	}
}

/**
 * Async operation timeout utility
 */
export function withTimeout<T>(
	promise: Promise<T>,
	timeoutMs: number,
	operation: string = "operation"
): Promise<T> {
	return Promise.race([
		promise,
		new Promise<never>((_, reject) =>
			setTimeout(() => reject(new Error(`${operation} timed out after ${timeoutMs}ms`)), timeoutMs)
		)
	]);
}