Spaces:
Running
Running
File size: 3,231 Bytes
bdc1ac8 |
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 |
/**
* Port Discovery Demo
*
* Demonstrates how to find and connect to robot hardware programmatically
*/
import { findPort, connectPort } from "@lerobot/node";
import type { RobotConnection, DiscoveredPort } from "@lerobot/node";
async function demoFindPort() {
console.log("π Port Discovery Demo");
console.log("======================\n");
try {
// Demo 1: Basic port discovery
console.log("π Demo 1: Basic robot discovery");
console.log("Looking for connected robots...\n");
const findProcess = await findPort({
onMessage: (message) => console.log(` π‘ ${message}`),
});
const discoveredPorts = await findProcess.result;
if (discoveredPorts.length === 0) {
console.log("β No robots found.");
console.log("\nπ§ Make sure your robot is:");
console.log(" - Connected via USB");
console.log(" - Powered on");
console.log(" - Using a working USB cable");
return;
}
console.log(`\nβ
Found ${discoveredPorts.length} robot port(s):`);
discoveredPorts.forEach((port, index) => {
console.log(` ${index + 1}. ${port.robotType} on ${port.path}`);
console.log(` Port: ${port.path}`);
console.log(` Type: ${port.robotType}`);
});
// Demo 2: Connect to discovered robot
console.log("\nπ Demo 2: Connecting to discovered robot");
const robot = await connectPort(
discoveredPorts[0].path,
"so100_follower",
"demo_robot"
);
console.log(
`β
Connected to robot: ${robot.robotType} (ID: ${robot.robotId})`
);
console.log(` Port: ${robot.port.path}`);
console.log(` Connected: ${robot.isConnected ? "β
" : "β"}`);
console.log(` Baudrate: ${robot.port.baudRate}`);
// Demo 3: Connection details
console.log("\nπ Demo 3: Connection details");
console.log("Robot connection properties:");
console.log(` Name: ${robot.name}`);
console.log(` Type: ${robot.robotType}`);
console.log(` ID: ${robot.robotId}`);
console.log(` Port: ${robot.port.path}`);
console.log(` Serial: ${robot.serialNumber}`);
console.log(` Connected: ${robot.isConnected ? "β
" : "β"}`);
// Demo 4: Silent discovery (no progress messages)
console.log("\nπ€« Demo 4: Silent discovery");
console.log("Finding robots without progress messages...");
const silentProcess = await findPort(); // No onMessage callback
const silentRobots = await silentProcess.result;
console.log(`Found ${silentRobots.length} robot(s) silently`);
console.log("\nπ Port discovery demo completed!");
console.log("\nβΉοΈ Note: For interactive port discovery, use the CLI:");
console.log(" npx lerobot find-port");
} catch (error) {
console.error("\nβ Port discovery failed:", error.message);
console.log("\nπ§ Troubleshooting:");
console.log("- Check USB connections");
console.log("- Verify robot is powered on");
console.log("- Try different USB ports/cables");
console.log("- On Linux: Check serial port permissions");
console.log("- For interactive port discovery, use: npx lerobot find-port");
process.exit(1);
}
}
demoFindPort();
|