Spaces:
Running
Running
File size: 18,952 Bytes
18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 18b0fa5 3aea7c6 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 |
# LeRobot Arena - Robot Control Architecture v2.0
> **Master-Slave Pattern for Scalable Robot Control**
> A revolutionary architecture that separates command generation (Masters) from execution (Slaves), enabling sophisticated robot control scenarios from simple manual operation to complex multi-robot coordination.
## ๐๏ธ Architecture Overview
The architecture follows a **Master-Slave Pattern** with complete separation of concerns:
```
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Web Frontend โ โ RobotManager โ โ Masters โ
โ โโโโโบโ โโโโโบโ โ
โ โข 3D Visualization โ โข Robot Creation โ โ โข USB Master โ
โ โข Manual Controlโ โ โข Master/Slave โ โ โข Remote Server โ
โ โข Monitoring โ โ Orchestration โ โ โข Mock Sequence โ
โ (disabled when โ โ โข State Sync โ โ (1 per robot) โ
โ master active) โ โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโ โ
โผ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ Robot Class โโโโโบโ Slaves โ
โ โ โ โ
โ โข Joint States โ โ โข USB Robot โ
โ โข URDF Model โ โ โข Remote Robot โ
โ โข Command Queue โ โ โข WebSocket โ
โ โข Calibration โ โ (N per robot) โ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโ
โ Python Backend โ
โ โ
โ โข WebSocket API โ
โ โข Connection Mgr โ
โ โข Robot Manager โ
โโโโโโโโโโโโโโโโโโโโ
```
### Control Flow States
```
โโโโโโโโโโโโโโโโโโโ Master Connected โโโโโโโโโโโโโโโโโโโ
โ Manual Mode โ โโโโโโโโโโโโโโโโโโโโโโโบ โ Master Mode โ
โ โ โ โ
โ โ
Panel Active โ โ โ Panel Locked โ
โ โ
Direct Controlโ โ โ
Master Commandsโ
โ โ No Master โ โ โ
All Slaves Execโ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
Master Disconnected
```
## ๐ฏ Core Concepts
### Masters (Command Sources)
**Purpose**: Generate and provide robot control commands
| Type | Description | Connection | Use Case |
|------|-------------|------------|----------|
| **USB Master** | Physical robot as command source | USB/Serial | Teleoperation, motion teaching |
| **Remote Server** | WebSocket/HTTP command reception | Network | External control systems |
| **Mock Sequence** | Predefined movement patterns | Internal | Testing, demonstrations |
**Key Rules**:
- ๐ **Exclusive Control**: Only 1 master per robot
- ๐ซ **Panel Lock**: Manual control disabled when master active
- ๐ **Seamless Switch**: Masters can be swapped dynamically
### Slaves (Execution Targets)
**Purpose**: Execute commands on physical or virtual robots
| Type | Description | Connection | Use Case |
|------|-------------|------------|----------|
| **USB Slave** | Physical robot control | USB/Serial | Hardware execution |
| **Remote Server Slave** | Network robot control | WebSocket | Distributed robots |
| **WebSocket Slave** | Real-time WebSocket execution | WebSocket | Cloud robots |
**Key Rules**:
- ๐ข **Multiple Allowed**: N slaves per robot
- ๐ฏ **Parallel Execution**: All slaves execute same commands
- ๐ **Independent Operation**: Slaves can fail independently
### Architecture Comparison
| Aspect | v1.0 (Single Driver) | v2.0 (Master-Slave) |
|--------|---------------------|---------------------|
| **Connection Model** | 1 Driver โ 1 Robot | 1 Master + N Slaves โ 1 Robot |
| **Command Source** | Always UI Panel | Master OR UI Panel |
| **Execution Targets** | Single Connection | Multiple Parallel |
| **Control Hierarchy** | Flat | Hierarchical |
| **Scalability** | Limited | Unlimited |
## ๐ Project Structure
### Frontend Architecture (TypeScript + Svelte)
```
src/lib/robot/
โโโ Robot.svelte.ts # Individual robot master-slave coordination
โโโ RobotManager.svelte.ts # Global robot orchestration
โโโ drivers/
โโโ USBMaster.ts # Physical robot as command source
โโโ RemoteServerMaster.ts # WebSocket command reception
โโโ USBSlave.ts # Physical robot execution
โโโ RemoteServerSlave.ts # Network robot execution
โโโ WebSocketSlave.ts # Real-time WebSocket execution
src/lib/types/
โโโ robotDriver.ts # Master/Slave interfaces
โโโ robot.ts # Robot state management
```
### Backend Architecture (Python + FastAPI)
```
src-python/src/
โโโ main.py # FastAPI server + WebSocket endpoints
โโโ robot_manager.py # Server-side robot lifecycle
โโโ connection_manager.py # WebSocket connection handling
โโโ models.py # Pydantic data models
```
## ๐ฎ Usage Examples
### Basic Robot Setup
```typescript
import { robotManager } from "$lib/robot/RobotManager.svelte";
// Create robot from URDF
const robot = await robotManager.createRobot("demo-arm", {
urdfPath: "/robots/so-arm100/robot.urdf",
jointNameIdMap: { "Rotation": 1, "Pitch": 2, "Elbow": 3 },
restPosition: { "Rotation": 0, "Pitch": 0, "Elbow": 0 }
});
// Add execution targets (slaves)
await robotManager.connectUSBSlave("demo-arm"); // Real hardware
await robotManager.connectRemoteServerSlave("demo-arm"); // Network robot
// Connect command source (master) - panel becomes locked
await robotManager.connectUSBMaster("demo-arm");
// Result: USB master controls both USB and Remote slaves
```
### Master Switching Workflow
```typescript
const robot = robotManager.getRobot("my-robot");
// Start with manual control
console.log(robot.manualControlEnabled); // โ
true
// Switch to USB master (robot becomes command source)
await robotManager.connectUSBMaster("my-robot");
console.log(robot.manualControlEnabled); // โ false (panel locked)
// Switch to remote control
await robotManager.disconnectMaster("my-robot");
await robotManager.connectMaster("my-robot", {
type: "remote-server",
url: "ws://robot-controller:8080/ws"
});
// Restore manual control
await robotManager.disconnectMaster("my-robot");
console.log(robot.manualControlEnabled); // โ
true (panel restored)
```
## ๐ Driver Implementations
### USB Master Driver
**Physical robot as command source for teleoperation**
```typescript
// USBMaster.ts - Core implementation
export class USBMaster implements MasterDriver {
readonly type = "master" as const;
private feetechDriver: FeetechSerialDriver;
private pollIntervalId?: number;
async connect(): Promise<void> {
// Initialize feetech.js serial connection
this.feetechDriver = new FeetechSerialDriver({
port: this.config.port || await this.detectPort(),
baudRate: this.config.baudRate || 115200
});
await this.feetechDriver.connect();
this.startPolling();
}
private startPolling(): void {
this.pollIntervalId = setInterval(async () => {
try {
// Read current joint positions from hardware
const jointStates = await this.readAllJoints();
// Convert to robot commands
const commands = this.convertToCommands(jointStates);
// Emit commands to slaves
this.notifyCommand(commands);
} catch (error) {
console.error('USB Master polling error:', error);
}
}, this.config.pollInterval || 100);
}
private async readAllJoints(): Promise<DriverJointState[]> {
const states: DriverJointState[] = [];
for (const [jointName, servoId] of Object.entries(this.jointMap)) {
const position = await this.feetechDriver.readPosition(servoId);
states.push({
name: jointName,
servoId,
type: "revolute",
virtualValue: position,
realValue: position
});
}
return states;
}
}
```
**Usage Pattern:**
- Connect USB robot as master
- Physical robot becomes the command source
- Move robot manually โ slaves follow the movement
- Ideal for: Teleoperation, motion teaching, demonstration recording
### USB Slave Driver
**Physical robot as execution target**
```typescript
// USBSlave.ts - Core implementation
export class USBSlave implements SlaveDriver {
readonly type = "slave" as const;
private feetechDriver: FeetechSerialDriver;
private calibrationOffsets: Map<string, number> = new Map();
async executeCommand(command: RobotCommand): Promise<void> {
for (const joint of command.joints) {
const servoId = this.getServoId(joint.name);
if (!servoId) continue;
// Apply calibration offset
const offset = this.calibrationOffsets.get(joint.name) || 0;
const adjustedValue = joint.value + offset;
// Send to hardware via feetech.js
await this.feetechDriver.writePosition(servoId, adjustedValue, {
speed: joint.speed || 100,
acceleration: 50
});
}
}
async readJointStates(): Promise<DriverJointState[]> {
const states: DriverJointState[] = [];
for (const joint of this.jointStates) {
const position = await this.feetechDriver.readPosition(joint.servoId);
const offset = this.calibrationOffsets.get(joint.name) || 0;
states.push({
...joint,
realValue: position - offset // Remove offset for accurate state
});
}
return states;
}
async calibrate(): Promise<void> {
console.log('Calibrating USB robot...');
for (const joint of this.jointStates) {
// Read current hardware position
const currentPos = await this.feetechDriver.readPosition(joint.servoId);
// Calculate offset: desired_rest - actual_position
const offset = joint.restPosition - currentPos;
this.calibrationOffsets.set(joint.name, offset);
console.log(`Joint ${joint.name}: offset=${offset.toFixed(1)}ยฐ`);
}
}
}
```
**Features:**
- Direct hardware control via feetech.js
- Real position feedback
- Calibration offset support
- Smooth motion interpolation
### Remote Server Master
**Network command reception via WebSocket**
```typescript
// RemoteServerMaster.ts - Core implementation
export class RemoteServerMaster implements MasterDriver {
readonly type = "master" as const;
private websocket?: WebSocket;
private reconnectAttempts = 0;
async connect(): Promise<void> {
const wsUrl = `${this.config.url}/ws/master/${this.robotId}`;
this.websocket = new WebSocket(wsUrl);
this.websocket.onopen = () => {
console.log(`Remote master connected: ${wsUrl}`);
this.reconnectAttempts = 0;
this.updateStatus({ isConnected: true });
};
this.websocket.onmessage = (event) => {
try {
const message = JSON.parse(event.data);
this.handleServerMessage(message);
} catch (error) {
console.error('Failed to parse server message:', error);
}
};
this.websocket.onclose = () => {
this.updateStatus({ isConnected: false });
this.attemptReconnect();
};
}
private handleServerMessage(message: any): void {
switch (message.type) {
case 'command':
// Convert server message to robot command
const command: RobotCommand = {
timestamp: Date.now(),
joints: message.data.joints.map((j: any) => ({
name: j.name,
value: j.value,
speed: j.speed
}))
};
this.notifyCommand([command]);
break;
case 'sequence':
// Handle command sequence
const sequence: CommandSequence = message.data;
this.notifySequence(sequence);
break;
}
}
async sendSlaveStatus(slaveStates: DriverJointState[]): Promise<void> {
if (!this.websocket) return;
const statusMessage = {
type: 'slave_status',
timestamp: new Date().toISOString(),
robot_id: this.robotId,
data: {
joints: slaveStates.map(state => ({
name: state.name,
virtual_value: state.virtualValue,
real_value: state.realValue
}))
}
};
this.websocket.send(JSON.stringify(statusMessage));
}
}
```
**Protocol:**
```json
// Command from server to robot
{
"type": "command",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"joints": [
{ "name": "Rotation", "value": 45, "speed": 100 },
{ "name": "Elbow", "value": -30, "speed": 80 }
]
}
}
// Status from robot to server
{
"type": "slave_status",
"timestamp": "2024-01-15T10:30:01Z",
"robot_id": "robot-1",
"data": {
"joints": [
{ "name": "Rotation", "virtual_value": 45, "real_value": 44.8 },
{ "name": "Elbow", "virtual_value": -30, "real_value": -29.9 }
]
}
}
```
### Remote Server Slave
**Network robot execution via WebSocket**
```typescript
// RemoteServerSlave.ts - Core implementation
export class RemoteServerSlave implements SlaveDriver {
readonly type = "slave" as const;
private websocket?: WebSocket;
async executeCommand(command: RobotCommand): Promise<void> {
if (!this.websocket) throw new Error('Not connected');
const message = {
type: 'command',
timestamp: new Date().toISOString(),
robot_id: this.config.robotId,
data: {
joints: command.joints.map(j => ({
name: j.name,
value: j.value,
speed: j.speed
}))
}
};
this.websocket.send(JSON.stringify(message));
// Wait for acknowledgment
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Command timeout')), 5000);
const messageHandler = (event: MessageEvent) => {
const response = JSON.parse(event.data);
if (response.type === 'command_ack') {
clearTimeout(timeout);
this.websocket?.removeEventListener('message', messageHandler);
resolve();
}
};
this.websocket.addEventListener('message', messageHandler);
});
}
async readJointStates(): Promise<DriverJointState[]> {
if (!this.websocket) throw new Error('Not connected');
const message = {
type: 'status_request',
timestamp: new Date().toISOString(),
robot_id: this.config.robotId
};
this.websocket.send(JSON.stringify(message));
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => reject(new Error('Status timeout')), 3000);
const messageHandler = (event: MessageEvent) => {
const response = JSON.parse(event.data);
if (response.type === 'joint_states') {
clearTimeout(timeout);
this.websocket?.removeEventListener('message', messageHandler);
const states = response.data.joints.map((j: any) => ({
name: j.name,
servoId: j.servo_id,
type: j.type,
virtualValue: j.virtual_value,
realValue: j.real_value
}));
resolve(states);
}
};
this.websocket.addEventListener('message', messageHandler);
});
}
}
```
## ๐ Command Flow Architecture
### Command Structure
```typescript
interface RobotCommand {
timestamp: number;
joints: {
name: string;
value: number; // degrees for revolute, speed for continuous
speed?: number; // optional movement speed
}[];
duration?: number; // optional execution time
metadata?: Record<string, unknown>;
}
```
### Control Flow
1. **Master Generation**: Masters generate commands from various sources
2. **Robot Routing**: Robot class routes commands to all connected slaves
3. **Parallel Execution**: All slaves execute commands simultaneously
4. **State Feedback**: Slaves report back real joint positions
5. **Synchronization**: Robot maintains synchronized state across all slaves
### State Management
```typescript
// Robot.svelte.ts - Core state management
export interface ManagedJointState {
name: string;
urdfJoint: IUrdfJoint;
servoId?: number;
// State values
virtualValue: number; // What the UI shows
realValue?: number; // What hardware reports
commandedValue: number; // Last commanded value
// Calibration
calibrationOffset: number; // Hardware compensation
restPosition: number; // Safe default position
// Synchronization
lastVirtualUpdate: Date;
lastRealUpdate?: Date;
lastCommandUpdate?: Date;
}
```
## ๐ Benefits Summary
| Benefit | Description | Impact |
|---------|-------------|---------|
| **๐ Clear Control Hierarchy** | Masters provide commands exclusively, slaves execute in parallel | No command conflicts, predictable behavior |
| **๐ Flexible Command Sources** | Easy switching between manual, automated, and remote control | Supports development, testing, and production |
| **๐ก Multiple Execution Targets** | Same commands executed on multiple robots simultaneously | Real hardware + simulation testing |
| **๐๏ธ Automatic Panel Management** | UI automatically adapts to master presence | Intuitive user experience |
| **๐ Development Workflow** | Clear separation enables independent development | Faster iteration cycles |
---
**This architecture provides unprecedented flexibility for robot control, from simple manual operation to sophisticated multi-robot coordination, all with a clean, extensible, and production-ready design.**
|