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 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
 
 
 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
 
 
 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
18b0fa5
3aea7c6
 
 
 
 
 
18b0fa5
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
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
# ๐Ÿค– LeRobot Arena - Complete Robot Control System

A comprehensive WebSocket-based robot control platform featuring **master-slave architecture**, real-time 3D visualization, and seamless integration between physical robots and digital twins.

## ๐Ÿ›๏ธ Core Architecture

### Master-Slave Pattern

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    Commands     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    Execution    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    ๐ŸŽฎ MASTERS   โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚  ๐Ÿค– ROBOT CORE  โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บโ”‚   ๐Ÿ”ง SLAVES     โ”‚
โ”‚                 โ”‚                 โ”‚                 โ”‚                 โ”‚                 โ”‚
โ”‚ โ€ข Remote Server โ”‚    WebSocket    โ”‚ โ€ข Robot Manager โ”‚   USB/Mock/WS   โ”‚ โ€ข Physical Bot  โ”‚
โ”‚ โ€ข Demo Scripts  โ”‚    HTTP API     โ”‚ โ€ข State Sync    โ”‚   Direct I/O    โ”‚ โ€ข 3D Simulation โ”‚
โ”‚ โ€ข Manual UI     โ”‚    Sequences    โ”‚ โ€ข Safety Logic  โ”‚   Commands      โ”‚ โ€ข Mock Testing  โ”‚
โ”‚ โ€ข AI Agents     โ”‚                 โ”‚ โ€ข Multi-Robot   โ”‚                 โ”‚ โ€ข Remote Proxy  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

**Key Principles:**

- ๐ŸŽฏ **Single Source of Truth**: Only one master per robot at a time
- ๐Ÿ”’ **Safety First**: Manual control disabled when master active
- ๐ŸŒ **Multi-Modal**: Same commands work across all slave types
- ๐Ÿ”„ **Real-Time Sync**: Virtual and physical states synchronized
- ๐Ÿ“ก **Network Native**: Built for remote operation from day one

## ๐Ÿš€ Quick Start Guide

### 1. Server Setup

```bash
# Install dependencies
cd src-python
uv sync

# Start the WebSocket server
python start_server.py
# Server runs on http://localhost:8080
```

### 2. Frontend Integration

```bash
# In your Svelte app
npm run dev
# Visit http://localhost:5173
```

### 3. Create & Control Robot

```javascript
// Create robot in UI or via API
const robot = await robotManager.createRobot('my-robot', robotUrdfConfig);

// Option 1: Manual control (sliders in UI)
await robot.updateJointValue('Rotation', 45);

// Option 2: Demo sequences
await robotManager.connectDemoSequences('my-robot');

// Option 3: Remote server control
await robotManager.connectMaster('my-robot', {
  type: "remote-server",
  url: "ws://localhost:8080"
});

// Option 4: Direct API sequence trigger
curl -X POST http://localhost:8080/api/robots/my-robot/play-sequence/gentle-wave
```

## ๐ŸŽฎ Master Drivers (Command Sources)

### MockSequenceMaster

**Pre-programmed movement patterns for testing and demos**

```typescript
const config: MasterDriverConfig = {
	type: "mock-sequence",
	sequences: DEMO_SEQUENCES,
	autoStart: true,
	loopMode: true
};

await robotManager.connectMaster("robot-1", config);
```

**Available Sequences:**

- **๐ŸŒŠ Gentle Wave Pattern** (6s): Smooth greeting gesture with wrist movements
- **๐Ÿ” Small Scanning Pattern** (8s): Horizontal sweep for environment scanning
- **๐Ÿ’ช Tiny Flex Pattern** (8s): Articulation demonstration with elbow/jaw coordination

**Features:**

- โœ… Safe movement ranges (ยฑ25ยฐ max)
- โœ… Smooth interpolation between keyframes
- โœ… Loop mode for continuous operation
- โœ… Automatic master takeover

### RemoteServerMaster

**WebSocket connection to external control systems**

```typescript
const config: MasterDriverConfig = {
	type: "remote-server",
	url: "ws://localhost:8080",
	apiKey: "optional-auth-token"
};

await robotManager.connectMaster("robot-1", config);
```

**Features:**

- โœ… Real-time bidirectional communication
- โœ… Automatic reconnection with exponential backoff
- โœ… Heartbeat monitoring (30s intervals)
- โœ… Command acknowledgment
- โœ… Status and error reporting

**Message Format:**

```json
{
	"type": "command",
	"timestamp": "2024-01-01T12:00:00Z",
	"data": {
		"timestamp": 1704110400000,
		"joints": [{ "name": "Rotation", "value": 45, "speed": 100 }]
	}
}
```

## ๐Ÿ”ง Slave Drivers (Execution Targets)

### MockSlave

**Perfect simulation for development and testing**

```typescript
const config: SlaveDriverConfig = {
	type: "mock-slave",
	simulateLatency: 50, // Realistic response delay
	simulateErrors: false, // Random connection issues
	responseDelay: 20 // Command execution time
};

await robotManager.connectSlave("robot-1", config);
```

**Features:**

- โœ… Perfect command execution (real_value = virtual_value)
- โœ… Configurable network latency simulation
- โœ… Error injection for robustness testing
- โœ… Instant connection without hardware
- โœ… Real-time state feedback

### USBSlave

**Direct serial communication with physical robots**

```typescript
const config: SlaveDriverConfig = {
	type: "usb-slave",
	port: "/dev/ttyUSB0", // Auto-detect if undefined
	baudRate: 115200
};

await robotManager.connectSlave("robot-1", config);
```

**Features:**

- โœ… Feetech servo protocol support
- โœ… Position and speed control
- โœ… Real servo position feedback
- โœ… Calibration workflow for sync
- โœ… Error handling and recovery

**Calibration Process:**

1. Manually position robot to match digital twin
2. Click "Calibrate" to sync virtual/real coordinates
3. System calculates offset: `real_pos = raw_pos + offset`
4. All future commands automatically compensated

### WebSocketSlave

**Remote robot control via WebSocket relay**

```typescript
const config: SlaveDriverConfig = {
	type: "websocket-slave",
	url: "ws://robot-proxy:8080",
	robotId: "remote-arm-1"
};

await robotManager.connectSlave("robot-1", config);
```

**Use Cases:**

- ๐ŸŒ Control robots across internet
- ๐Ÿข Enterprise robot fleet management
- ๐Ÿ”’ Firewall-friendly robot access
- ๐Ÿ“ก Proxy through edge devices

## ๐Ÿ“ก WebSocket Protocol Specification

### Master โ†’ Server Communication

#### Send Joint Command

```json
{
	"type": "command",
	"timestamp": "2024-01-01T12:00:00Z",
	"data": {
		"timestamp": 1704110400000,
		"joints": [
			{ "name": "Rotation", "value": 45, "speed": 100 },
			{ "name": "Pitch", "value": -30, "speed": 150 }
		],
		"duration": 2000,
		"metadata": { "source": "manual_control" }
	}
}
```

#### Send Movement Sequence

```json
{
	"type": "sequence",
	"timestamp": "2024-01-01T12:00:00Z",
	"data": {
		"id": "custom-dance",
		"name": "Custom Dance Sequence",
		"commands": [
			{
				"timestamp": 0,
				"joints": [{ "name": "Rotation", "value": -30 }],
				"duration": 1000
			},
			{
				"timestamp": 1000,
				"joints": [{ "name": "Rotation", "value": 30 }],
				"duration": 1000
			}
		],
		"total_duration": 2000,
		"loop": false
	}
}
```

#### Heartbeat

```json
{
	"type": "heartbeat",
	"timestamp": "2024-01-01T12:00:00Z"
}
```

### Slave โ†’ Server Communication

#### Status Update

```json
{
	"type": "status_update",
	"timestamp": "2024-01-01T12:00:00Z",
	"data": {
		"isConnected": true,
		"lastConnected": "2024-01-01T11:58:00Z",
		"error": null
	}
}
```

#### Joint State Feedback

```json
{
	"type": "joint_states",
	"timestamp": "2024-01-01T12:00:00Z",
	"data": [
		{
			"name": "Rotation",
			"servo_id": 1,
			"type": "revolute",
			"virtual_value": 45.0,
			"real_value": 44.8,
			"limits": {
				"lower": -180,
				"upper": 180,
				"velocity": 200
			}
		}
	]
}
```

#### Error Reporting

```json
{
	"type": "error",
	"timestamp": "2024-01-01T12:00:00Z",
	"data": {
		"code": "SERVO_TIMEOUT",
		"message": "Servo 3 not responding",
		"joint": "Elbow",
		"severity": "warning"
	}
}
```

## ๐Ÿ› ๏ธ REST API Reference

### Robot Management

| Method | Endpoint                  | Description             | Example                                                                                                                               |
| ------ | ------------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| GET    | `/`                       | Server status & metrics | `curl http://localhost:8080/`                                                                                                         |
| GET    | `/api/robots`             | List all robots         | `curl http://localhost:8080/api/robots`                                                                                               |
| POST   | `/api/robots`             | Create new robot        | `curl -X POST -H "Content-Type: application/json" -d '{"robot_type":"so-arm100","name":"My Robot"}' http://localhost:8080/api/robots` |
| GET    | `/api/robots/{id}`        | Get robot details       | `curl http://localhost:8080/api/robots/robot-123`                                                                                     |
| GET    | `/api/robots/{id}/status` | Get connection status   | `curl http://localhost:8080/api/robots/robot-123/status`                                                                              |
| DELETE | `/api/robots/{id}`        | Delete robot            | `curl -X DELETE http://localhost:8080/api/robots/robot-123`                                                                           |

### Sequence Control

| Method | Endpoint                                  | Description         | Example                                                                             |
| ------ | ----------------------------------------- | ------------------- | ----------------------------------------------------------------------------------- |
| GET    | `/api/sequences`                          | List demo sequences | `curl http://localhost:8080/api/sequences`                                          |
| POST   | `/api/robots/{id}/play-sequence/{seq_id}` | Play sequence       | `curl -X POST http://localhost:8080/api/robots/robot-123/play-sequence/gentle-wave` |
| POST   | `/api/robots/{id}/stop-sequence`          | Stop sequences      | `curl -X POST http://localhost:8080/api/robots/robot-123/stop-sequence`             |

### WebSocket Endpoints

| Endpoint                | Purpose          | Client Type       | Example                                   |
| ----------------------- | ---------------- | ----------------- | ----------------------------------------- |
| `/ws/master/{robot_id}` | Send commands    | Control sources   | `ws://localhost:8080/ws/master/robot-123` |
| `/ws/slave/{robot_id}`  | Receive commands | Execution targets | `ws://localhost:8080/ws/slave/robot-123`  |

## ๐ŸŽฏ Usage Scenarios

### 1. ๐Ÿงช Development & Testing

```bash
# Create robot with mock slave for safe testing
robot = await robotManager.createRobot('test-bot', urdfConfig);
await robotManager.connectMockSlave('test-bot', 50);
await robotManager.connectDemoSequences('test-bot');
```

**Perfect for:**

- Algorithm development without hardware risk
- UI/UX testing with realistic feedback
- Automated testing pipelines
- Demo presentations

### 2. ๐Ÿฆพ Physical Robot Control

```bash
# Connect real hardware
robot = await robotManager.createRobot('real-bot', urdfConfig);
await robotManager.connectUSBSlave('real-bot', '/dev/ttyUSB0');

# Manual control via UI sliders
# OR automated via demo sequences
await robotManager.connectDemoSequences('real-bot');
```

**Calibration Workflow:**

1. Connect USB slave to robot
2. Manually position to match 3D model rest pose
3. Click "Calibrate" to sync coordinate systems
4. Robot now mirrors 3D model movements precisely

### 3. ๐ŸŒ Remote Operation

```bash
# Master controls slave over internet
# Master side:
await robotManager.connectMaster('local-avatar', {
  type: "remote-server",
  url: "wss://robot-farm.com:8080"
});

# Slave side (at robot location):
await robotManager.connectSlave('physical-robot', {
  type: "websocket-slave",
  url: "wss://robot-farm.com:8080",
  robotId: "local-avatar"
});
```

**Use Cases:**

- Telepresence robotics
- Remote maintenance and inspection
- Distributed manufacturing
- Educational robot sharing

### 4. ๐Ÿค– Multi-Robot Coordination

```bash
# One master controlling multiple robots
await robotManager.connectMaster('fleet-commander', masterConfig);

// Multiple slaves executing same commands
for (const robot of robotFleet) {
  await robotManager.connectSlave(robot.id, slaveConfig);
}
```

**Applications:**

- Synchronized dance performances
- Assembly line coordination
- Swarm robotics research
- Entertainment shows

### 5. ๐Ÿง  AI Agent Integration

```javascript
// AI agent as master driver
class AIAgentMaster implements MasterDriver {
  async generateNextCommand(): Promise<RobotCommand> {
    const sensorData = await this.readEnvironment();
    const decision = await this.aiModel.predict(sensorData);
    return this.convertToRobotCommand(decision);
  }
}

await robot.setMaster(new AIAgentMaster(config));
```

## ๐Ÿ”ง Integration Guide

### Frontend Integration (Svelte)

```typescript
import { robotManager } from "$lib/robot/RobotManager.svelte";
import { robotUrdfConfigMap } from "$lib/configs/robotUrdfConfig";

// Create robot
const robot = await robotManager.createRobot("my-robot", robotUrdfConfigMap["so-arm100"]);

// Add visualization
await robotManager.connectMockSlave("my-robot");

// Add control
await robotManager.connectDemoSequences("my-robot", true);

// Monitor state
robot.joints.forEach((joint) => {
	console.log(`${joint.name}: virtual=${joint.virtualValue}ยฐ real=${joint.realValue}ยฐ`);
});
```

### Backend Integration (Python)

```python
import asyncio
import websockets
import json

async def robot_controller():
    uri = "ws://localhost:8080/ws/master/my-robot"
    async with websockets.connect(uri) as websocket:
        # Send command
        command = {
            "type": "command",
            "timestamp": "2024-01-01T12:00:00Z",
            "data": {
                "timestamp": 1704110400000,
                "joints": [{"name": "Rotation", "value": 45}]
            }
        }
        await websocket.send(json.dumps(command))

        # Listen for responses
        async for message in websocket:
            data = json.loads(message)
            if data["type"] == "joint_states":
                print(f"Robot position: {data['data']}")

asyncio.run(robot_controller())
```

### Hardware Integration (Arduino/C++)

```cpp
#include <WiFi.h>
#include <WebSocketsClient.h>
#include <ArduinoJson.h>

WebSocketsClient webSocket;

void onWebSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
    if (type == WStype_TEXT) {
        DynamicJsonDocument doc(1024);
        deserializeJson(doc, payload);

        if (doc["type"] == "execute_command") {
            JsonArray joints = doc["data"]["joints"];
            for (JsonObject joint : joints) {
                String name = joint["name"];
                float value = joint["value"];
                moveServo(name, value);
            }
        }
    }
}

void setup() {
    webSocket.begin("192.168.1.100", 8080, "/ws/slave/arduino-bot");
    webSocket.onEvent(onWebSocketEvent);
}
```

## ๐Ÿ›ก๏ธ Security & Production

### Authentication

```typescript
// API key authentication (planned)
const master = new RemoteServerMaster(
	{
		type: "remote-server",
		url: "wss://secure-robot-farm.com:8080",
		apiKey: "your-secret-api-key"
	},
	robotId
);
```

### TLS/SSL

```bash
# Production deployment with SSL
uvicorn main:app --host 0.0.0.0 --port 443 --ssl-keyfile key.pem --ssl-certfile cert.pem
```

### Rate Limiting & Safety

```python
# Built-in protections
- Command rate limiting (100 commands/second max)
- Joint velocity limits from URDF
- Emergency stop on connection loss
- Position bounds checking
- Servo temperature monitoring
```

## ๐Ÿš€ Deployment Options

### Development

```bash
cd src-python && python start_server.py
```

### Docker

```dockerfile
FROM python:3.12-slim
COPY src-python/ /app/
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 8080
CMD ["python", "start_server.py"]
```

### Cloud (Railway/Heroku)

```bash
# Procfile
web: cd src-python && python start_server.py
```

### Raspberry Pi (Edge)

```bash
# systemd service for autostart
sudo systemctl enable lerobot-arena
sudo systemctl start lerobot-arena
```

## ๐Ÿงช Testing & Debugging

### Unit Tests

```bash
cd src-python
pytest tests/ -v
```

### Integration Tests

```javascript
// Frontend testing
import { expect, test } from "@playwright/test";

test("robot creation and control", async ({ page }) => {
	await page.goto("/");
	await page.click('[data-testid="create-robot"]');
	await page.click('[data-testid="connect-demo-sequences"]');
	await expect(page.locator('[data-testid="robot-status"]')).toContainText("Master + Slaves");
});
```

### Debug Mode

```bash
# Enable verbose logging
export LOG_LEVEL=DEBUG
python start_server.py

# Frontend debug
export VITE_DEBUG=true
npm run dev
```

### Health Monitoring

```bash
# Check server health
curl http://localhost:8080/

# Monitor WebSocket connections
curl http://localhost:8080/api/robots
```

## ๐Ÿ”ฎ Roadmap

### v2.0 - Enhanced Control

- [ ] **Script Player Master**: Execute Python/JS scripts
- [ ] **Simulation Slave**: Physics-based simulation
- [ ] **Force Control**: Torque and compliance modes
- [ ] **Vision Integration**: Camera feeds and computer vision

### v2.1 - Enterprise Features

- [ ] **Authentication**: JWT tokens and user management
- [ ] **Multi-tenancy**: Isolated robot fleets per organization
- [ ] **Monitoring**: Prometheus metrics and Grafana dashboards
- [ ] **Recording**: Command sequences and replay

### v2.2 - Advanced Robotics

- [ ] **Path Planning**: Trajectory optimization
- [ ] **Collision Detection**: Safety in shared workspaces
- [ ] **AI Integration**: Reinforcement learning environments
- [ ] **ROS Bridge**: Integration with ROS2 ecosystem

## ๐Ÿค Contributing

### Development Setup

```bash
# Frontend
npm install
npm run dev

# Backend
cd src-python
uv sync
python start_server.py

# Tests
npm run test
cd src-python && pytest
```

### Code Style

- **TypeScript**: ESLint + Prettier
- **Python**: Black + isort + mypy
- **Commits**: Conventional commits format

### Pull Request Process

1. Fork repository
2. Create feature branch
3. Add tests for new functionality
4. Ensure all tests pass
5. Update documentation
6. Submit PR with clear description

## ๐Ÿ“„ License

MIT License - Feel free to use in commercial and personal projects.

---

**Built with โค๏ธ for the robotics community**

_LeRobot Arena bridges the gap between digital twins and physical robots, making robotics accessible to developers, researchers, and enthusiasts worldwide._