# HopeJR

## 필수 조건

- [하드웨어 설정](https://github.com/TheRobotStudio/HOPEJr)

## LeRobot 설치

LeRobot를 설치하려면 [설치 지침](https://github.com/huggingface/lerobot#installation)을 따르세요.

HopeJR 의존성이 포함된 LeRobot 설치:

```bash
pip install -e ".[hopejr]"
```

## 장치 구성

교정 및 작동을 시작하기 전에 각 HopeJR 구성 요소에 해당하는 USB 포트를 확인해야 합니다. 팔, 손, 장갑, 외골격의 USB 포트를 찾기 위해 다음 스크립트를 실행하세요:

```bash
lerobot-find-port
```

이 명령은 사용 가능한 USB 포트와 해당 포트에 연결된 장치를 표시합니다. 데이터 기록, 에피소드 재생, 텔레오퍼레이션 스크립트 실행 시 `--robot.port` 및 `--teleop.port` 매개변수에 지정해야 하므로 포트 경로(예: `/dev/tty.usbmodem58760433331`, `/dev/tty.usbmodem11301`)를 기록해 두세요.

## 1단계: 보정

텔레오퍼레이션을 수행하기 전에 HopeJR의 팔다리를 보정해야 합니다. 보정 파일은 `~/.cache/huggingface/lerobot/calibration`에 저장됩니다.

### 1.1 로봇 손 보정

```bash
lerobot-calibrate \
    --robot.type=hope_jr_hand \
    --robot.port=/dev/tty.usbmodem58760432281 \
    --robot.id=blue \
    --robot.side=right
```

…

**썸네일**:

- **CMC**: 엄지를 손에 연결하는 기본 관절
- **MCP**: 손가락 마디
- **PIP**: 첫 번째 손가락 관절
- **DIP**: 손가락 끝 마디

**검지, 중지, 약지, 소지**:

- **방사근**: 손가락의 뿌리를 엄지 쪽으로 이동시킵니다
- **Ulnar flexor**: 손가락의 뿌리를 소지 방향으로 이동시킵니다
- **PIP/DIP**: 손가락의 원위 지골과 근위 지골을 굽힘

이 중 각각은 GUI를 통해 개별적으로 보정해야 합니다. 대칭적인 움직임을 얻기 위해, ulnar와 radial 굴곡근은 동일한 크기의 범위(다만 오프셋은 다름)를 가져야 합니다.

  <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/lerobot/calibration_gui_1.png" alt="손 캘리브레이션 GUI에서 경계 설정" title="손 캘리브레이션 GUI에서 경계 설정" width="100%"
  >

위에서 보여준 대로 보정(calibration) 인터페이스를 사용하여 각 조인트의 범위 경계를 설정합니다.

  <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/lerobot/calibration_gui_2.png" alt="교정 값 저장" title="교정 값 저장" width="100%"
  >

모든 관절에 적절한 경계를 설정한 후, 저장을 클릭하여 보정에 사용된 값을 모터에 저장합니다.

### 1.2 텔레오퍼레이터 장갑 보정

```bash
lerobot-calibrate \
    --teleop.type=homunculus_glove \
    --teleop.port=/dev/tty.usbmodem11201 \
    --teleop.id=red \
    --teleop.side=right
```

엄지를 시작으로 각 손가락을 전체 가동 범위로 움직입니다.

```
Move thumb through its entire range of motion.
Recording positions. Press ENTER to stop...

-------------------------------------------
NAME      |    MIN |    POS |    MAX
thumb_cmc |   1790 |   1831 |   1853
thumb_mcp |   1497 |   1514 |   1528
thumb_pip |   1466 |   1496 |   1515
thumb_dip |   1463 |   1484 |   1514
```

각 손가락에 대해 계속 진행합니다:

```
Move middle through its entire range of motion.
Recording positions. Press ENTER to stop...

-------------------------------------------
NAME                 |    MIN |    POS |    MAX
middle_mcp_abduction |   1598 |   1718 |   1820
middle_mcp_flexion   |   1512 |   1658 |   2136
middle_dip           |   1484 |   1500 |   1547
```

교정이 완료되면 시스템은 교정 정보를 `/Users/your_username/.cache/huggingface/lerobot/calibration/teleoperators/homunculus_glove/red.json`에 저장합니다.

### 1.3 로봇 팔 보정

```bash
lerobot-calibrate \
    --robot.type=hope_jr_arm \
    --robot.port=/dev/tty.usbserial-1110 \
    --robot.id=white
```

이렇게 하면 각 모터의 범위 한계를 설정할 수 있는 보정 GUI가 열립니다. 팔의 동작은 다음과 같이 구성됩니다:

- **Shoulder**: 피치, 요, 롤
- **Elbow**: 굽힘
- **Wrist**: 피치, 요, 롤

  <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/lerobot/calibration_gui_2.png" alt="팔 보정 GUI에서 경계 설정" title="팔 보정 GUI에서 경계 설정" width="100%"
  >

교정 인터페이스를 사용하여 각 관절의 범위 경계를 설정합니다. 각 관절을 전체 운동 범위로 이동하고, 그에 따라 최소값과 최대값을 조정합니다. 모든 관절에 적절한 경계를 설정한 후, 교정을 저장합니다.

### 1.4 텔레오퍼레이터 엑소스켈레톤 보정

```bash
lerobot-calibrate \
    --teleop.type=homunculus_arm \
    --teleop.port=/dev/tty.usbmodem11201 \
    --teleop.id=black
```

외골격을 사용하면 로봇 팔을 제어할 수 있습니다. 보정(calibration) 중에 모든 관절을 전체 가동 범위로 움직이도록 안내를 받습니다:

```
Move all joints through their entire range of motion.
Recording positions. Press ENTER to stop...

-------------------------------------------
-------------------------------------------
NAME            |    MIN |    POS |    MAX
shoulder_pitch  |    586 |    736 |    895
shoulder_yaw    |   1257 |   1374 |   1390
shoulder_roll   |    449 |   1034 |   2564
elbow_flex      |   3023 |   3117 |   3134
wrist_roll      |   3073 |   3096 |   3147
wrist_yaw       |   2143 |   2171 |   2185
wrist_pitch     |   1975 |   1993 |   2074
Calibration saved to /Users/your_username/.cache/huggingface/lerobot/calibration/teleoperators/homunculus_arm/black.json
```

## 2단계: 텔레오퍼레이션

Feetech 미들웨어의 전역 변수 충돌로 인해, 팔과 손의 텔레오퍼레이션은 별도의 셸 세션에서 실행해야 합니다:

### 손

```bash
lerobot-teleoperate \
    --robot.type=hope_jr_hand \
    --robot.port=/dev/tty.usbmodem58760432281 \
    --robot.id=blue \
    --robot.side=right \
    --teleop.type=homunculus_glove \
    --teleop.port=/dev/tty.usbmodem11201 \
    --teleop.id=red \
    --teleop.side=right \
    --display_data=true \
    --fps=30
```

### Arm

```bash
lerobot-teleoperate \
    --robot.type=hope_jr_arm \
    --robot.port=/dev/tty.usbserial-1110 \
    --robot.id=white \
    --teleop.type=homunculus_arm \
    --teleop.port=/dev/tty.usbmodem11201 \
    --teleop.id=black \
    --display_data=true \
    --fps=30
```

## 3단계: 기록, 재생, 학습

Hope-JR를 사용한 레코딩, 리플레이 및 학습은 아직 실험 단계입니다.

### 기록

이 단계에서는 데이터셋을 기록하며, 예시는 [여기](https://huggingface.co/datasets/nepyope/hand_record_test_with_video_data)에서 확인할 수 있습니다.

```bash
lerobot-record \
    --robot.type=hope_jr_hand \
    --robot.port=/dev/tty.usbmodem58760432281 \
    --robot.id=right \
    --robot.side=right \
    --robot.cameras='{"main": {"type": "opencv", "index_or_path": 0, "width": 640, "height": 480, "fps": 30}}' \
    --teleop.type=homunculus_glove \
    --teleop.port=/dev/tty.usbmodem1201 \
    --teleop.id=right \
    --teleop.side=right \
    --dataset.repo_id=<USER>/hand_record_test_with_video_data \
    --dataset.single_task="Hand recording test with video data" \
    --dataset.num_episodes=1 \
    --dataset.episode_time_s=5 \
    --dataset.push_to_hub=true \
    --dataset.private=true \
    --dataset.streaming_encoding=true \
    --dataset.encoder_threads=2 \
    --display_data=true
# Optional: --dataset.rgb_encoder.vcodec=auto
```

### 리플레이

```bash
lerobot-replay \
    --robot.type=hope_jr_hand \
    --robot.port=/dev/tty.usbmodem58760432281 \
    --robot.id=right \
    --robot.side=right \
    --dataset.repo_id=<USER>/hand_record_test_with_camera \
    --dataset.episode=0
```

### 학습

```bash
lerobot-train \
  --dataset.repo_id=<USER>/hand_record_test_with_video_data \
  --policy.type=act \
  --output_dir=outputs/train/hopejr_hand \
  --job_name=hopejr \
  --policy.device=mps \
  --trackio.enable=true \
  --policy.repo_id=<USER>/hand_test_policy
```

### 평가

```bash
lerobot-record \
  --robot.type=hope_jr_hand \
  --robot.port=/dev/tty.usbmodem58760432281 \
  --robot.id=right \
  --robot.side=right \
  --robot.cameras='{"main": {"type": "opencv", "index_or_path": 0, "width": 640, "height": 480, "fps": 30}}' \
  --display_data=false \
  --dataset.repo_id=<USER>/eval_hopejr \
  --dataset.single_task="Evaluate hopejr hand policy" \
  --dataset.num_episodes=10 \
  --dataset.streaming_encoding=true \
  --dataset.encoder_threads=2 \
  --policy.path=outputs/train/hopejr_hand/checkpoints/last/pretrained_model
# Optional: --dataset.rgb_encoder.vcodec=auto
```

