Spaces:
Running
Running
File size: 4,317 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 |
#!/usr/bin/env node
/**
* Basic Producer Example - LeRobot Arena
*
* This example demonstrates:
* - Creating a room
* - Connecting as a producer
* - Sending joint updates and state sync
* - Basic error handling
*/
import { RoboticsProducer } from '../dist/robotics/index.js';
async function main() {
console.log('π€ LeRobot Arena Basic Producer Example π€');
// Create producer client
const producer = new RoboticsProducer('http://localhost:8000');
// Set up event callbacks
producer.onConnected(() => {
console.log('β
Producer connected!');
});
producer.onDisconnected(() => {
console.log('π Producer disconnected!');
});
producer.onError((error) => {
console.error('β Producer error:', error);
});
try {
// Create a room
console.log('\nπ¦ Creating room...');
const roomId = await producer.createRoom();
console.log(`β
Room created: ${roomId}`);
// Connect as producer
console.log('\nπ Connecting as producer...');
const success = await producer.connect(roomId, 'demo-producer');
if (!success) {
console.error('β Failed to connect as producer!');
return;
}
console.log(`β
Connected to room ${roomId} as producer`);
// Show connection info
const info = producer.getConnectionInfo();
console.log('\nπ Connection Info:');
console.log(` Room ID: ${info.room_id}`);
console.log(` Role: ${info.role}`);
console.log(` Participant ID: ${info.participant_id}`);
// Send initial state sync
console.log('\nπ€ Sending initial state...');
await producer.sendStateSync({
base: 0.0,
shoulder: 0.0,
elbow: 0.0,
wrist: 0.0,
gripper: 0.0
});
console.log('β
Initial state sent');
// Simulate robot movement
console.log('\nπ€ Simulating robot movement...');
const movements = [
{ name: 'shoulder', value: 45.0, description: 'Raise shoulder' },
{ name: 'elbow', value: -30.0, description: 'Bend elbow' },
{ name: 'wrist', value: 15.0, description: 'Turn wrist' },
{ name: 'gripper', value: 0.5, description: 'Close gripper' },
];
for (let i = 0; i < movements.length; i++) {
const movement = movements[i];
console.log(` ${i + 1}. ${movement.description}: ${movement.value}Β°`);
await producer.sendJointUpdate([{
name: movement.name,
value: movement.value
}]);
// Wait between movements
await new Promise(resolve => setTimeout(resolve, 1000));
}
// Send combined update
console.log('\nπ€ Sending combined update...');
await producer.sendJointUpdate([
{ name: 'base', value: 90.0 },
{ name: 'shoulder', value: 60.0 },
{ name: 'elbow', value: -45.0 }
]);
console.log('β
Combined update sent');
// Send heartbeat
console.log('\nπ Sending heartbeat...');
await producer.sendHeartbeat();
console.log('β
Heartbeat sent');
// Demonstrate emergency stop
console.log('\nπ¨ Testing emergency stop...');
await producer.sendEmergencyStop('Demo emergency stop - testing safety');
console.log('β
Emergency stop sent');
console.log('\nβ
Basic producer example completed!');
console.log(`\nπ‘ Room ID: ${roomId}`);
console.log(' You can use this room ID with the consumer example');
// Keep running for a bit to allow consumers to connect
console.log('\nβ³ Keeping producer alive for 30 seconds...');
console.log(' (Consumers can connect during this time)');
await new Promise(resolve => setTimeout(resolve, 30000));
} catch (error) {
console.error('β Error:', error.message);
} finally {
// Always disconnect and cleanup
if (producer.isConnected()) {
console.log('\nπ§Ή Cleaning up...');
await producer.disconnect();
// Optionally delete the room
const roomId = producer.currentRoomId;
if (roomId) {
await producer.deleteRoom(roomId);
console.log(`ποΈ Deleted room ${roomId}`);
}
}
console.log('π Goodbye!');
}
}
// Handle Ctrl+C gracefully
process.on('SIGINT', () => {
console.log('\n\nπ Received SIGINT, shutting down gracefully...');
process.exit(0);
});
main().catch(console.error); |