Spaces:
Running
Running
File size: 16,814 Bytes
02eac4b |
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 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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 |
<script lang="ts">
import { onMount } from 'svelte';
import { robotics } from 'lerobot-arena-client';
import type { robotics as roboticsTypes } from 'lerobot-arena-client';
// Get data from load function
let { data } = $props();
let workspaceId = data.workspaceId;
let roomIdFromUrl = data.roomId;
// State
let consumer: robotics.RoboticsConsumer;
let connected = $state<boolean>(false);
let connecting = $state<boolean>(false);
let roomId = $state<string>(roomIdFromUrl || '');
let participantId = $state<string>('');
let error = $state<string>('');
// Current robot state
let currentJoints = $state<Record<string, number>>({});
let lastUpdate = $state<Date | null>(null);
// Real-time monitoring
let updateCount = $state<number>(0);
let stateSyncCount = $state<number>(0);
let errorCount = $state<number>(0);
// History tracking
let jointHistory = $state<
Array<{ timestamp: string; joints: roboticsTypes.JointData[]; source?: string }>
>([]);
let stateHistory = $state<Array<{ timestamp: string; state: Record<string, number> }>>([]);
let errorHistory = $state<Array<{ timestamp: string; message: string }>>([]);
// Visualization data for joint trend (last 20 updates)
let jointTrends = $state<Record<string, number[]>>({});
// Debug info
let debugInfo = $state<{
connectionAttempts: number;
messagesReceived: number;
lastMessageType: string;
wsConnected: boolean;
currentRoom: string;
workspaceId: string;
}>({
connectionAttempts: 0,
messagesReceived: 0,
lastMessageType: '',
wsConnected: false,
currentRoom: '',
workspaceId: workspaceId
});
function updateUrlWithRoom(roomId: string) {
const url = new URL(window.location.href);
url.searchParams.set('room', roomId);
window.history.replaceState({}, '', url.toString());
}
async function connectConsumer() {
if (!roomId.trim() || !participantId.trim()) {
error = 'Please enter both Room ID and Participant ID';
return;
}
debugInfo.connectionAttempts++;
try {
connecting = true;
error = '';
consumer = new robotics.RoboticsConsumer('http://localhost:8000');
// Set up event handlers
consumer.onConnected(() => {
connected = true;
connecting = false;
debugInfo.wsConnected = true;
debugInfo.currentRoom = roomId;
loadInitialState();
updateUrlWithRoom(roomId);
});
consumer.onDisconnected(() => {
connected = false;
debugInfo.wsConnected = false;
});
consumer.onError((errorMsg) => {
error = errorMsg;
errorCount++;
debugInfo.messagesReceived++;
debugInfo.lastMessageType = 'ERROR';
errorHistory = [
{
timestamp: new Date().toLocaleTimeString(),
message: errorMsg
},
...errorHistory
].slice(0, 10); // Keep last 10 errors only
});
// Joint update callback
consumer.onJointUpdate((joints) => {
updateCount++;
lastUpdate = new Date();
debugInfo.messagesReceived++;
debugInfo.lastMessageType = 'JOINT_UPDATE';
// Update current state
joints.forEach((joint) => {
currentJoints[joint.name] = joint.value;
// Update trends
if (!jointTrends[joint.name]) {
jointTrends[joint.name] = [];
}
jointTrends[joint.name] = [...jointTrends[joint.name], joint.value].slice(-10); // Keep last 10 only
});
// Add to history
jointHistory = [
{
timestamp: new Date().toLocaleTimeString(),
joints,
source: 'producer'
},
...jointHistory
].slice(0, 20); // Keep last 20 updates only
});
// State sync callback
consumer.onStateSync((state) => {
stateSyncCount++;
lastUpdate = new Date();
debugInfo.messagesReceived++;
debugInfo.lastMessageType = 'STATE_SYNC';
// Update current state
currentJoints = { ...state };
// Update trends for all joints
Object.entries(state).forEach(([name, value]) => {
if (!jointTrends[name]) {
jointTrends[name] = [];
}
jointTrends[name] = [...jointTrends[name], value].slice(-10); // Keep last 10 only
});
// Add to history
stateHistory = [
{
timestamp: new Date().toLocaleTimeString(),
state
},
...stateHistory
].slice(0, 10); // Keep last 10 state syncs only
});
const success = await consumer.connect(workspaceId, roomId, participantId);
if (!success) {
error = 'Failed to connect. Room might not exist.';
connecting = false;
}
} catch (err) {
error = `Connection failed: ${err}`;
connecting = false;
}
}
async function loadInitialState() {
if (!consumer || !connected) return;
try {
const state = await consumer.getStateSyncAsync();
if (Object.keys(state).length > 0) {
currentJoints = state;
// Initialize trends
Object.entries(state).forEach(([name, value]) => {
jointTrends[name] = [value];
});
}
} catch (err) {
console.error('Failed to load initial state:', err);
}
}
async function exitSession() {
if (consumer && connected) {
await consumer.disconnect();
}
connected = false;
debugInfo.wsConnected = false;
debugInfo.currentRoom = '';
}
function clearHistory() {
jointHistory = [];
stateHistory = [];
errorHistory = [];
updateCount = 0;
stateSyncCount = 0;
errorCount = 0;
debugInfo.messagesReceived = 0;
}
// Update roomId when URL parameter changes
$effect(() => {
if (roomIdFromUrl) {
roomId = roomIdFromUrl;
}
});
// Update debugInfo when workspaceId changes
$effect(() => {
debugInfo.workspaceId = workspaceId;
});
onMount(() => {
participantId = `consumer_${Date.now()}`;
return () => {
exitSession();
};
});
</script>
<svelte:head>
<title>Robotics Consumer{roomId ? ` - Room ${roomId}` : ''} - Workspace {workspaceId} - LeRobot Arena</title>
</svelte:head>
<div class="mx-auto max-w-6xl space-y-6">
<!-- Header -->
<div class="flex items-center justify-between">
<div>
<h1 class="font-mono text-2xl font-bold text-gray-900">🤖 Robotics Consumer</h1>
<p class="mt-1 font-mono text-sm text-gray-600">
Workspace: <span class="font-bold text-blue-600">{workspaceId}</span>
{#if roomId}
| Room: <span class="font-bold text-blue-600">{roomId}</span>
{:else}
| Monitor robot arm state in real-time
{/if}
</p>
</div>
<div class="flex items-center space-x-4">
<div class="flex items-center space-x-2">
{#if connected}
<div class="h-3 w-3 rounded-full bg-green-500"></div>
<span class="font-mono text-sm font-medium text-green-700">Connected</span>
{:else if connecting}
<div class="h-3 w-3 animate-pulse rounded-full bg-yellow-500"></div>
<span class="font-mono text-sm font-medium text-yellow-700">Connecting...</span>
{:else}
<div class="h-3 w-3 rounded-full bg-red-500"></div>
<span class="font-mono text-sm font-medium text-red-700">Disconnected</span>
{/if}
</div>
<a
href="/{workspaceId}/robotics"
class="rounded border bg-gray-100 px-3 py-2 font-mono text-sm hover:bg-gray-200"
>
← Back to Robotics
</a>
</div>
</div>
<!-- Debug Info -->
<div class="rounded border bg-gray-900 p-4 font-mono text-sm text-green-400">
<div class="mb-2 font-bold">ROBOTICS CONSUMER DEBUG - WORKSPACE {debugInfo.workspaceId}{roomId ? ` - ROOM ${roomId}` : ''}</div>
<div class="grid grid-cols-3 gap-4 md:grid-cols-5">
<div>Attempts: {debugInfo.connectionAttempts}</div>
<div>Messages: {debugInfo.messagesReceived}</div>
<div>Last: {debugInfo.lastMessageType || 'None'}</div>
<div>WS: {debugInfo.wsConnected ? 'ON' : 'OFF'}</div>
<div>Room: {debugInfo.currentRoom || 'None'}</div>
</div>
<div class="mt-2 grid grid-cols-3 gap-4">
<div>Updates: {updateCount}</div>
<div>State Syncs: {stateSyncCount}</div>
<div>Errors: {errorCount}</div>
</div>
<div class="mt-2">Last Update: {lastUpdate ? lastUpdate.toLocaleTimeString() : 'Never'}</div>
{#if error}
<div class="mt-2 text-red-400">Error: {error}</div>
{/if}
</div>
{#if !connected}
<!-- Connection Section -->
<div class="rounded border p-6">
<h2 class="mb-4 font-mono text-lg font-semibold">Connect to Robotics Room</h2>
<div class="grid grid-cols-1 gap-6 md:grid-cols-2">
<div>
<label for="roomId" class="mb-1 block font-mono text-sm font-medium text-gray-700">
Room ID
</label>
<input
id="roomId"
type="text"
bind:value={roomId}
placeholder="Enter room ID to monitor"
class="w-full rounded border border-gray-300 px-3 py-2 font-mono focus:border-blue-500 focus:ring-blue-500"
/>
</div>
<div>
<label for="participantId" class="mb-1 block font-mono text-sm font-medium text-gray-700">
Participant ID
</label>
<input
id="participantId"
type="text"
bind:value={participantId}
placeholder="Your participant ID"
class="w-full rounded border border-gray-300 px-3 py-2 font-mono focus:border-blue-500 focus:ring-blue-500"
/>
</div>
</div>
<div class="mt-4">
<button
onclick={connectConsumer}
disabled={connecting || !roomId.trim() || !participantId.trim()}
class={[
'rounded border px-4 py-2 font-mono',
connecting || !roomId.trim() || !participantId.trim()
? 'bg-gray-200 text-gray-500'
: 'bg-blue-600 text-white hover:bg-blue-700'
]}
>
{connecting ? 'Connecting...' : 'Join as Observer'}
</button>
</div>
{#if error}
<div class="mt-4 rounded border border-red-200 bg-red-50 p-4">
<p class="font-mono text-sm text-red-700">{error}</p>
</div>
{/if}
</div>
{:else}
<!-- Monitoring Interface -->
<div class="space-y-6">
<!-- Status Overview -->
<div class="grid grid-cols-2 gap-4 md:grid-cols-4">
<div class="rounded border p-4 text-center">
<div class="font-mono text-2xl font-bold text-blue-600">{updateCount}</div>
<div class="font-mono text-sm text-gray-500">Joint Updates</div>
</div>
<div class="rounded border p-4 text-center">
<div class="font-mono text-2xl font-bold text-purple-600">{stateSyncCount}</div>
<div class="font-mono text-sm text-gray-500">State Syncs</div>
</div>
<div class="rounded border p-4 text-center">
<div class="font-mono text-2xl font-bold text-red-600">{errorCount}</div>
<div class="font-mono text-sm text-gray-500">Errors</div>
</div>
<div class="rounded border p-4 text-center">
<div class="font-mono text-2xl font-bold text-green-600">
{lastUpdate ? lastUpdate.toLocaleTimeString() : 'N/A'}
</div>
<div class="font-mono text-sm text-gray-500">Last Update</div>
</div>
</div>
<!-- Robot State Display -->
<div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
<!-- Robot Visualization -->
<div class="rounded border p-4 lg:col-span-2">
<h2 class="mb-3 font-mono text-lg font-semibold">Robot Arm Visualization</h2>
<div class="aspect-video rounded border bg-gray-50 p-4">
<div class="flex h-full items-center justify-center">
<div class="text-center">
<div class="mb-4 text-6xl">🦾</div>
<p class="font-mono text-gray-600">3D Robot Visualization</p>
<p class="font-mono text-xs text-gray-500">Live joint positions from producer</p>
</div>
</div>
</div>
<!-- Current Joint Values -->
{#if Object.keys(currentJoints).length > 0}
<div class="mt-4 grid grid-cols-2 gap-4 md:grid-cols-3">
{#each Object.entries(currentJoints) as [name, value]}
<div class="text-center">
<div class="font-mono text-sm text-gray-500 capitalize">
{name.replace(/([A-Z])/g, ' $1').replace(/^./, (str) => str.toUpperCase())}
</div>
<div class="font-mono text-lg font-bold">{value.toFixed(1)}°</div>
</div>
{/each}
</div>
{/if}
</div>
<!-- Session Info & Controls -->
<div class="space-y-6">
<!-- Session Info -->
<div class="rounded border p-4">
<h2 class="mb-3 font-mono text-lg font-semibold">Session Info</h2>
<div class="grid grid-cols-1 gap-2 font-mono text-sm">
<div><span class="text-gray-500">Workspace:</span> {workspaceId}</div>
<div><span class="text-gray-500">Room:</span> {roomId}</div>
<div><span class="text-gray-500">ID:</span> {participantId}</div>
<div><span class="text-gray-500">Role:</span> Consumer</div>
<div><span class="text-gray-500">Active Joints:</span> {Object.keys(currentJoints).length}</div>
</div>
<div class="mt-4 flex space-x-3">
<button
onclick={clearHistory}
class="rounded border bg-gray-100 px-3 py-2 font-mono text-sm hover:bg-gray-200"
>
Clear History
</button>
<button
onclick={exitSession}
class="rounded border bg-gray-100 px-3 py-2 font-mono text-sm hover:bg-gray-200"
>
🚪 Exit Session
</button>
</div>
</div>
<!-- Joint Trends -->
{#if Object.keys(jointTrends).length > 0}
<div class="rounded border p-4">
<h2 class="mb-3 font-mono text-lg font-semibold">Joint Trends</h2>
<div class="max-h-48 space-y-2 overflow-y-auto">
{#each Object.entries(jointTrends) as [name, values]}
<div class="rounded bg-gray-50 p-2">
<div class="flex items-center justify-between">
<span class="font-mono text-xs text-gray-600 capitalize">
{name.replace(/([A-Z])/g, ' $1').replace(/^./, (str) => str.toUpperCase())}
</span>
<span class="font-mono text-xs font-bold">
{values[values.length - 1]?.toFixed(1) || '0.0'}°
</span>
</div>
<div class="mt-1 flex h-4 items-end space-x-1">
{#each values as value, i}
<div
class="w-2 bg-blue-300"
style="height: {Math.abs(value) / 180 * 100}%"
title="{value.toFixed(1)}°"
></div>
{/each}
</div>
</div>
{/each}
</div>
</div>
{/if}
</div>
</div>
<!-- Real-time Updates -->
<div class="grid grid-cols-1 gap-6 lg:grid-cols-2">
<!-- Joint Updates History -->
<div class="rounded border p-4">
<h2 class="mb-3 font-mono text-lg font-semibold">Recent Joint Updates</h2>
<div class="max-h-64 space-y-2 overflow-y-auto">
{#each jointHistory.slice(0, 10) as update}
<div class="rounded border-l-4 border-blue-200 bg-gray-50 p-2">
<div class="flex items-center justify-between">
<span class="font-mono text-xs text-gray-500">{update.timestamp}</span>
<span class="font-mono text-xs text-blue-600">JOINT_UPDATE</span>
</div>
<div class="mt-1 text-sm">
<span class="font-mono text-gray-700">
{update.joints.length} joint(s):
{update.joints.map(j => `${j.name}=${j.value.toFixed(1)}°`).join(', ')}
</span>
</div>
</div>
{:else}
<p class="py-4 text-center font-mono text-sm text-gray-500">No joint updates received yet</p>
{/each}
</div>
</div>
<!-- State Sync History -->
<div class="rounded border p-4">
<h2 class="mb-3 font-mono text-lg font-semibold">State Sync History</h2>
<div class="max-h-64 space-y-2 overflow-y-auto">
{#each stateHistory as state}
<div class="rounded border-l-4 border-purple-200 bg-gray-50 p-2">
<div class="flex items-center justify-between">
<span class="font-mono text-xs text-gray-500">{state.timestamp}</span>
<span class="font-mono text-xs text-purple-600">STATE_SYNC</span>
</div>
<div class="mt-1 text-sm">
<div class="font-mono text-gray-700">
{Object.keys(state.state).length} joints synchronized
</div>
</div>
</div>
{:else}
<p class="py-4 text-center font-mono text-sm text-gray-500">
No state syncs received yet
</p>
{/each}
</div>
</div>
</div>
<!-- Error History -->
{#if errorHistory.length > 0}
<div class="rounded border p-4">
<h2 class="mb-3 font-mono text-lg font-semibold">Recent Errors</h2>
<div class="max-h-32 space-y-2 overflow-y-auto">
{#each errorHistory as error}
<div class="rounded border-l-4 border-red-200 bg-red-50 p-2">
<div class="flex items-center justify-between">
<span class="font-mono text-xs text-gray-500">{error.timestamp}</span>
<span class="font-mono text-xs text-red-600">ERROR</span>
</div>
<div class="mt-1 font-mono text-sm text-red-700">
{error.message}
</div>
</div>
{/each}
</div>
</div>
{/if}
</div>
{/if}
</div> |