DEV Community

Jambo
Jambo

Posted on • Edited on

LeRobot 機械手臂操作教程

本教程基於 Linux 環境編寫,假設用戶已完成環境配置、機械臂組裝與校準。教程中將 Leader 稱為主臂,Follower 稱為從臂。

訓練環境:Ubuntu 22.10,RTX 4060 TI (16G)

準備工作

由於 LeRobot 迭代速度快,建議切換到教程編寫時的版本:

git checkout d2645cb19fc521e5b117fe03d90a84f698d3d3f6
Enter fullscreen mode Exit fullscreen mode

1. 遙控操作

完成主從臂校準後,可用以下腳本控制主臂遙控從臂,顯示相機畫面與馬達資訊:

python -m lerobot.teleoperate \
 --robot.type=so101_follower \
 --robot.port=/dev/ttyACM1 \
 --robot.id=follower \
 --robot.cameras="{ front: {type: opencv, index_or_path: /dev/video2, width: 640, height: 480, fps: 30}}" \
 --teleop.type=so101_leader \
 --teleop.port=/dev/ttyACM0 \
 --teleop.id=leader \
 --display_data=true
Enter fullscreen mode Exit fullscreen mode

參數說明:

  • robot.idteleop.id:應與校準時的機械臂唯一 ID 一致,用於讀取校準時儲存的馬達資訊
  • robot.cameras:相機配置資訊,可執行 python -m lerobot.find_cameras opencv 查找可用相機。支援多機位配置,透過字典鍵區分與記錄不同相機

2. 資料收集

基本收集命令

python -m lerobot.record \
  --robot.type=so101_follower \
  --robot.port=/dev/ttyACM1 \
  --robot.id=follower \
  --robot.cameras="{ front: {type: opencv, index_or_path: /dev/video2, width: 640, height: 480, fps: 30}}" \
  --teleop.type=so101_leader \
  --teleop.port=/dev/ttyACM0 \
  --teleop.id=leader \
  --display_data=true \
  --dataset.num_episodes=20 \
  --dataset.push_to_hub=False \
  --dataset.repo_id=${HF_USER}/record-test \
  --dataset.single_task="Grab the black cube"
Enter fullscreen mode Exit fullscreen mode

資料儲存位置

  • 預設儲存路徑:~/.cache/huggingface/lerobot/{repo-id}
  • 自訂儲存:設定 HF_LEROBOT_HOME 環境變數,資料儲存於 HF_LEROBOT_HOME/repo_id

資料收集控制

收集過程可透過鍵盤控制:

  • 右箭頭 (→):錄製階段完成當前錄製並進入恢復階段;恢復階段進入下一輪錄製
  • 左箭頭 (←):重新錄製當前資料
  • ESC:完成當前錄製並退出

注意:腳本使用 pynput 監聽按鍵,SSH 或 Wayland 下可能無法運作。詳見:pynput 平台限制

恢復中斷的收集

若收集過程出錯,可在原命令加 --resume=true 參數恢復。

資料上傳

手動上傳資料至 HuggingFace:

huggingface-cli upload ${HF_USER}/record-test ~/.cache/huggingface/lerobot/{repo-id} --repo-type dataset
Enter fullscreen mode Exit fullscreen mode

注意:若未設定 --dataset.push_to_hub=False,收集完成時會自動上傳資料。

3. 模型訓練

基本訓練命令

python -m lerobot.scripts.train \
  --dataset.repo_id=${HF_USER}/record-test \
  --policy.type=act \
  --output_dir=outputs/train/act_so101_test \
  --job_name=act_so101_test \
  --policy.device=cuda \
  --wandb.enable=false \
  --policy.repo_id=${HF_USER}/my_policy \
  --policy.push_to_hub=false \
  --steps 50000 \
  --batch_size 32 \
  --save_freq 10000
Enter fullscreen mode Exit fullscreen mode

訓練腳本從 HF_LEROBOT_HOME/dataset.repo_id 路徑讀取資料進行訓練。

恢復訓練

從最新檢查點恢復訓練:

python -m lerobot.scripts.train \
  --config_path=outputs/train/act_so101_test/checkpoints/last/pretrained_model/train_config.json \
  --resume=true
Enter fullscreen mode Exit fullscreen mode

4. 模型推理與評估

使用訓練好的模型進行推理,同時記錄評估片段:

python -m lerobot.record \
  --robot.type=so101_follower \
  --robot.port=/dev/ttyACM1 \
  --robot.cameras="{ front: {type: opencv, index_or_path: /dev/video2, width: 640, height: 480, fps: 30}}" \
  --robot.id=follower \
  --display_data=true \
  --dataset.repo_id=${HF_USER}/eval_so101 \
  --dataset.single_task="Put lego brick into the transparent box" \
  --policy.path=outputs/train/act_so101_test/checkpoints/last/pretrained_model/
Enter fullscreen mode Exit fullscreen mode

Top comments (0)