Spaces:
Running
Running
File size: 8,664 Bytes
a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f a1cdf4d a8d792f |
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 |
"use client";
import type React from "react";
import { Book, Code2, Terminal, Copy, Check } from "lucide-react";
import { Button } from "@/components/ui/button";
import { useState } from "react";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
const CodeBlock = ({
children,
language = "typescript",
}: {
children: React.ReactNode;
language?: string;
}) => {
const [copied, setCopied] = useState(false);
const copyToClipboard = async (text: string) => {
try {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch (err) {
console.error("Failed to copy:", err);
}
};
const codeText =
typeof children === "string" ? children : children?.toString() || "";
return (
<div className="bg-muted/50 dark:bg-black/40 border border-border dark:border-white/10 rounded-md overflow-hidden my-4 relative">
<SyntaxHighlighter
language={language}
style={oneDark}
customStyle={{
margin: 0,
padding: "1rem",
fontSize: "0.875rem",
background: "transparent",
backgroundColor: "transparent",
}}
wrapLines={true}
wrapLongLines={true}
codeTagProps={{
style: {
background: "transparent",
backgroundColor: "transparent",
},
}}
>
{codeText}
</SyntaxHighlighter>
<Button
variant="outline"
size="sm"
onClick={() => copyToClipboard(codeText)}
className="absolute top-2 right-2 h-7 w-7 p-0 transition-all"
>
{copied ? <Check className="w-3 h-3" /> : <Copy className="w-3 h-3" />}
</Button>
</div>
);
};
export function DocsSection() {
return (
<div className="font-mono">
<div className="mb-6">
<h2 className="text-2xl font-bold text-primary tracking-wider mb-2 uppercase flex items-center gap-3">
<Book className="w-6 h-6" />
Docs
</h2>
<p className="text-sm text-muted-foreground">
Complete API reference for @lerobot/web robotics library.
</p>
</div>
<div className="bg-muted/40 dark:bg-black/30 border-l-4 border-cyan-500 dark:border-accent-cyan p-6 md:p-8 rounded-r-lg space-y-8">
{/* Getting Started */}
<div>
<h3 className="text-xl font-bold text-cyan-600 dark:text-accent-cyan tracking-wider uppercase flex items-center gap-2">
<Terminal className="w-5 h-5" />
Getting Started
</h3>
<p className="text-muted-foreground text-sm mt-2 mb-4">
Complete workflow from discovery to teleoperation.
</p>
<CodeBlock>
{`import { findPort, releaseMotors, calibrate, teleoperate } from "@lerobot/web";
// 1. find and connect to hardware like a robot arm
const findProcess = await findPort();
const robots = await findProcess.result;
const robot = robots[0];
// 2. release the motors and put them into the homing position
await releaseMotors(robot);
// 3. calibrate the motors by moving each motor through its full range of motion
const calibrationProcess = await calibrate({
robot,
onProgress: (message) => console.log(message),
onLiveUpdate: (data) => console.log("Live positions:", data),
});
// when done, stop calibration and get the min/max ranges for each motor
calibrationProcess.stop();
const calibrationData = await calibrationProcess.result;
// 4. start controlling the robot arm with your keyboard
const teleop = await teleoperate({
robot,
calibrationData,
teleop: { type: "keyboard" },
});
teleop.start();`}
</CodeBlock>
</div>
{/* Browser Requirements */}
<div>
<h3 className="text-xl font-bold text-cyan-600 dark:text-accent-cyan tracking-wider uppercase">
Browser Requirements
</h3>
<div className="mt-4 space-y-2 text-sm">
<div>
• <strong>Chromium 89+</strong> with WebSerial and WebUSB API
support
</div>
<div>
• <strong>HTTPS or localhost</strong>
</div>
<div>
• <strong>User gesture</strong> required for initial port
selection
</div>
</div>
</div>
{/* API Reference */}
<div>
<h3 className="text-xl font-bold text-cyan-600 dark:text-accent-cyan tracking-wider uppercase flex items-center gap-2">
<Code2 className="w-5 h-5" />
API Reference
</h3>
<div className="space-y-6 mt-4">
{/* findPort */}
<div>
<h4 className="font-bold text-primary">findPort(config?)</h4>
<p className="text-muted-foreground text-sm mt-1">
Discovers and connects to robotics hardware using WebSerial API.
</p>
<CodeBlock>
{`// Interactive Mode (Default) - Shows port dialog
const findProcess = await findPort();
const robots = await findProcess.result; // RobotConnection[]
// Auto-Connect Mode - Reconnects to known robots
const findProcess = await findPort({
robotConfigs: [
{ robotType: "so100_follower", robotId: "left_arm", serialNumber: "USB123" }
],
onMessage: (msg) => console.log(msg),
});
// Returns: FindPortProcess
{
result: Promise<RobotConnection[]>, // Array of robot connections
stop(): void // Cancel discovery process
}`}
</CodeBlock>
</div>
{/* calibrate */}
<div>
<h4 className="font-bold text-primary">calibrate(config)</h4>
<p className="text-muted-foreground text-sm mt-1">
Calibrates motor homing offsets and records range of motion.
</p>
<CodeBlock>
{`const calibrationProcess = await calibrate({
robot,
onProgress: (message) => {
console.log(message); // "⚙️ Setting motor homing offsets"
},
onLiveUpdate: (data) => {
// Real-time motor positions during range recording
Object.entries(data).forEach(([motor, info]) => {
console.log(\`\${motor}: \${info.current} (range: \${info.range})\`);
});
},
});
// Move robot through full range of motion...
calibrationProcess.stop(); // Stop range recording
const calibrationData = await calibrationProcess.result;
// Returns: CalibrationProcess
{
result: Promise<WebCalibrationResults>, // Python-compatible format
stop(): void // Stop calibration process
}`}
</CodeBlock>
</div>
{/* teleoperate */}
<div>
<h4 className="font-bold text-primary">teleoperate(config)</h4>
<p className="text-muted-foreground text-sm mt-1">
Enables real-time robot control with extensible input devices.
</p>
<CodeBlock>
{`// Keyboard Teleoperation
const keyboardTeleop = await teleoperate({
robot,
calibrationData: savedCalibrationData,
teleop: { type: "keyboard" },
onStateUpdate: (state) => {
console.log(\`Active: \${state.isActive}\`);
console.log(\`Motors:\`, state.motorConfigs);
},
});
// Direct Teleoperation
const directTeleop = await teleoperate({
robot,
calibrationData: savedCalibrationData,
teleop: { type: "direct" },
});
// Returns: TeleoperationProcess
{
start(): void, // Begin teleoperation
stop(): void, // Stop teleoperation and clear states
getState(): TeleoperationState, // Current state and motor positions
teleoperator: BaseWebTeleoperator, // Access teleoperator-specific methods
disconnect(): Promise<void> // Stop and disconnect
}`}
</CodeBlock>
</div>
{/* releaseMotors */}
<div>
<h4 className="font-bold text-primary">
releaseMotors(robot, motorIds?)
</h4>
<p className="text-muted-foreground text-sm mt-1">
Releases motor torque so robot can be moved freely by hand.
</p>
<CodeBlock>
{`// Release all motors for calibration
await releaseMotors(robot);
// Release specific motors only
await releaseMotors(robot, [1, 2, 3]);
// Parameters
robot: RobotConnection // Connected robot
motorIds?: number[] // Specific motor IDs (default: all motors for robot type)`}
</CodeBlock>
</div>
</div>
</div>
</div>
</div>
);
}
|