mirror of
https://github.com/huggingface/lerobot.git
synced 2026-05-12 15:19:43 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c17a7be2b | |||
| c8c37bd339 | |||
| 0ccc08e347 | |||
| 7b207d44a0 | |||
| 0878c6880f | |||
| 11e6bd762a | |||
| ce3b9f627e | |||
| c66cd40176 |
@@ -30,7 +30,7 @@ pytest -sx tests/test_stuff.py::test_something
|
||||
```
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train --some.option=true
|
||||
lerobot-train --some.option=true
|
||||
```
|
||||
|
||||
## SECTION TO REMOVE BEFORE SUBMITTING YOUR PR
|
||||
|
||||
@@ -29,8 +29,8 @@ on:
|
||||
env:
|
||||
UV_VERSION: "0.8.0"
|
||||
PYTHON_VERSION: "3.10"
|
||||
DOCKER_IMAGE_NAME_CPU: huggingface/lerobot-gpu:latest
|
||||
DOCKER_IMAGE_NAME_GPU: huggingface/lerobot-cpu:latest
|
||||
DOCKER_IMAGE_NAME_CPU: huggingface/lerobot-cpu:latest
|
||||
DOCKER_IMAGE_NAME_GPU: huggingface/lerobot-gpu:latest
|
||||
|
||||
# Ensures that only the latest commit is built, canceling older runs.
|
||||
concurrency:
|
||||
|
||||
@@ -44,7 +44,7 @@ test-end-to-end:
|
||||
${MAKE} DEVICE=$(DEVICE) test-smolvla-ete-eval
|
||||
|
||||
test-act-ete-train:
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=act \
|
||||
--policy.dim_model=64 \
|
||||
--policy.n_action_steps=20 \
|
||||
@@ -68,12 +68,12 @@ test-act-ete-train:
|
||||
--output_dir=tests/outputs/act/
|
||||
|
||||
test-act-ete-train-resume:
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=tests/outputs/act/checkpoints/000002/pretrained_model/train_config.json \
|
||||
--resume=true
|
||||
|
||||
test-act-ete-eval:
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=tests/outputs/act/checkpoints/000004/pretrained_model \
|
||||
--policy.device=$(DEVICE) \
|
||||
--env.type=aloha \
|
||||
@@ -82,7 +82,7 @@ test-act-ete-eval:
|
||||
--eval.batch_size=1
|
||||
|
||||
test-diffusion-ete-train:
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=diffusion \
|
||||
--policy.down_dims='[64,128,256]' \
|
||||
--policy.diffusion_step_embed_dim=32 \
|
||||
@@ -106,7 +106,7 @@ test-diffusion-ete-train:
|
||||
--output_dir=tests/outputs/diffusion/
|
||||
|
||||
test-diffusion-ete-eval:
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=tests/outputs/diffusion/checkpoints/000002/pretrained_model \
|
||||
--policy.device=$(DEVICE) \
|
||||
--env.type=pusht \
|
||||
@@ -115,7 +115,7 @@ test-diffusion-ete-eval:
|
||||
--eval.batch_size=1
|
||||
|
||||
test-tdmpc-ete-train:
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=tdmpc \
|
||||
--policy.device=$(DEVICE) \
|
||||
--policy.push_to_hub=false \
|
||||
@@ -137,7 +137,7 @@ test-tdmpc-ete-train:
|
||||
--output_dir=tests/outputs/tdmpc/
|
||||
|
||||
test-tdmpc-ete-eval:
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=tests/outputs/tdmpc/checkpoints/000002/pretrained_model \
|
||||
--policy.device=$(DEVICE) \
|
||||
--env.type=xarm \
|
||||
@@ -148,7 +148,7 @@ test-tdmpc-ete-eval:
|
||||
|
||||
|
||||
test-smolvla-ete-train:
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=smolvla \
|
||||
--policy.n_action_steps=20 \
|
||||
--policy.chunk_size=20 \
|
||||
@@ -171,7 +171,7 @@ test-smolvla-ete-train:
|
||||
--output_dir=tests/outputs/smolvla/
|
||||
|
||||
test-smolvla-ete-eval:
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=tests/outputs/smolvla/checkpoints/000004/pretrained_model \
|
||||
--policy.device=$(DEVICE) \
|
||||
--env.type=aloha \
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<div align="center">
|
||||
|
||||
[](https://github.com/huggingface/lerobot/actions/workflows/nighty.yml?query=branch%3Amain)
|
||||
[](https://github.com/huggingface/lerobot/actions/workflows/nightly.yml?query=branch%3Amain)
|
||||
[](https://www.python.org/downloads/)
|
||||
[](https://github.com/huggingface/lerobot/blob/main/LICENSE)
|
||||
[](https://pypi.org/project/lerobot/)
|
||||
@@ -276,7 +276,7 @@ Check out [example 2](https://github.com/huggingface/lerobot/blob/main/examples/
|
||||
We also provide a more capable script to parallelize the evaluation over multiple environments during the same rollout. Here is an example with a pretrained model hosted on [lerobot/diffusion_pusht](https://huggingface.co/lerobot/diffusion_pusht):
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=lerobot/diffusion_pusht \
|
||||
--env.type=pusht \
|
||||
--eval.batch_size=10 \
|
||||
@@ -288,10 +288,10 @@ python -m lerobot.scripts.eval \
|
||||
Note: After training your own policy, you can re-evaluate the checkpoints with:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.eval --policy.path={OUTPUT_DIR}/checkpoints/last/pretrained_model
|
||||
lerobot-eval --policy.path={OUTPUT_DIR}/checkpoints/last/pretrained_model
|
||||
```
|
||||
|
||||
See `python -m lerobot.scripts.eval --help` for more instructions.
|
||||
See `lerobot-eval --help` for more instructions.
|
||||
|
||||
### Train your own policy
|
||||
|
||||
@@ -303,7 +303,7 @@ A link to the wandb logs for the run will also show up in yellow in your termina
|
||||
|
||||
\<img src="https://raw.githubusercontent.com/huggingface/lerobot/main/media/wandb.png" alt="WandB logs example"\>
|
||||
|
||||
Note: For efficiency, during training every checkpoint is evaluated on a low number of episodes. You may use `--eval.n_episodes=500` to evaluate on more episodes than the default. Or, after training, you may want to re-evaluate your best checkpoints on more episodes or change the evaluation settings. See `python -m lerobot.scripts.eval --help` for more instructions.
|
||||
Note: For efficiency, during training every checkpoint is evaluated on a low number of episodes. You may use `--eval.n_episodes=500` to evaluate on more episodes than the default. Or, after training, you may want to re-evaluate your best checkpoints on more episodes or change the evaluation settings. See `lerobot-eval --help` for more instructions.
|
||||
|
||||
#### Reproduce state-of-the-art (SOTA)
|
||||
|
||||
@@ -311,7 +311,7 @@ We provide some pretrained policies on our [hub page](https://huggingface.co/ler
|
||||
You can reproduce their training by loading the config from their run. Simply running:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train --config_path=lerobot/diffusion_pusht
|
||||
lerobot-train --config_path=lerobot/diffusion_pusht
|
||||
```
|
||||
|
||||
reproduces SOTA results for Diffusion Policy on the PushT task.
|
||||
|
||||
@@ -9,7 +9,7 @@ To instantiate a camera, you need a camera identifier. This identifier might cha
|
||||
To find the camera indices of the cameras plugged into your system, run the following script:
|
||||
|
||||
```bash
|
||||
python -m lerobot.find_cameras opencv # or realsense for Intel Realsense cameras
|
||||
lerobot-find-cameras opencv # or realsense for Intel Realsense cameras
|
||||
```
|
||||
|
||||
The output will look something like this if you have two cameras connected:
|
||||
|
||||
@@ -412,7 +412,7 @@ Example configuration for training the [reward classifier](https://huggingface.c
|
||||
To train the classifier, use the `train.py` script with your configuration:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train --config_path path/to/reward_classifier_train_config.json
|
||||
lerobot-train --config_path path/to/reward_classifier_train_config.json
|
||||
```
|
||||
|
||||
**Deploying and Testing the Model**
|
||||
@@ -458,7 +458,7 @@ The reward classifier will automatically provide rewards based on the visual inp
|
||||
3. **Train the classifier**:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train --config_path src/lerobot/configs/reward_classifier_train_config.json
|
||||
lerobot-train --config_path src/lerobot/configs/reward_classifier_train_config.json
|
||||
```
|
||||
|
||||
4. **Test the classifier**:
|
||||
|
||||
+19
-11
@@ -19,7 +19,7 @@ pip install -e ".[hopejr]"
|
||||
Before starting calibration and operation, you need to identify the USB ports for each HopeJR component. Run this script to find the USB ports for the arm, hand, glove, and exoskeleton:
|
||||
|
||||
```bash
|
||||
python -m lerobot.find_port
|
||||
lerobot-find-port
|
||||
```
|
||||
|
||||
This will display the available USB ports and their associated devices. Make note of the port paths (e.g., `/dev/tty.usbmodem58760433331`, `/dev/tty.usbmodem11301`) as you'll need to specify them in the `--robot.port` and `--teleop.port` parameters when recording data, replaying episodes, or running teleoperation scripts.
|
||||
@@ -31,7 +31,7 @@ Before performing teleoperation, HopeJR's limbs need to be calibrated. Calibrati
|
||||
### 1.1 Calibrate Robot Hand
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--robot.type=hope_jr_hand \
|
||||
--robot.port=/dev/tty.usbmodem58760432281 \
|
||||
--robot.id=blue \
|
||||
@@ -81,7 +81,7 @@ Once you have set the appropriate boundaries for all joints, click "Save" to sav
|
||||
### 1.2 Calibrate Teleoperator Glove
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=homunculus_glove \
|
||||
--teleop.port=/dev/tty.usbmodem11201 \
|
||||
--teleop.id=red \
|
||||
@@ -117,10 +117,18 @@ middle_dip | 1484 | 1500 | 1547
|
||||
|
||||
Once calibration is complete, the system will save the calibration to `/Users/your_username/.cache/huggingface/lerobot/calibration/teleoperators/homunculus_glove/red.json`
|
||||
|
||||
#### Visualizing Teleoperator Glove
|
||||
|
||||
After calibration, you can visualize the glove movements in real-time. Open the visualizer by navigating to the visualizer directory and opening the HTML file in your browser:
|
||||
|
||||
```bash
|
||||
open examples/hopejr/visualizer/index.html
|
||||
```
|
||||
|
||||
### 1.3 Calibrate Robot Arm
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--robot.type=hope_jr_arm \
|
||||
--robot.port=/dev/tty.usbserial-1110 \
|
||||
--robot.id=white
|
||||
@@ -146,7 +154,7 @@ Use the calibration interface to set the range boundaries for each joint. Move e
|
||||
### 1.4 Calibrate Teleoperator Exoskeleton
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=homunculus_arm \
|
||||
--teleop.port=/dev/tty.usbmodem11201 \
|
||||
--teleop.id=black
|
||||
@@ -178,7 +186,7 @@ Due to global variable conflicts in the Feetech middleware, teleoperation for ar
|
||||
### Hand
|
||||
|
||||
```bash
|
||||
python -m lerobot.teleoperate \
|
||||
lerobot-teleoperate \
|
||||
--robot.type=hope_jr_hand \
|
||||
--robot.port=/dev/tty.usbmodem58760432281 \
|
||||
--robot.id=blue \
|
||||
@@ -194,7 +202,7 @@ python -m lerobot.teleoperate \
|
||||
### Arm
|
||||
|
||||
```bash
|
||||
python -m lerobot.teleoperate \
|
||||
lerobot-teleoperate \
|
||||
--robot.type=hope_jr_arm \
|
||||
--robot.port=/dev/tty.usbserial-1110 \
|
||||
--robot.id=white \
|
||||
@@ -214,7 +222,7 @@ Record, Replay and Train with Hope-JR is still experimental.
|
||||
This step records the dataset, which can be seen as an example [here](https://huggingface.co/datasets/nepyope/hand_record_test_with_video_data/settings).
|
||||
|
||||
```bash
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=hope_jr_hand \
|
||||
--robot.port=/dev/tty.usbmodem58760432281 \
|
||||
--robot.id=right \
|
||||
@@ -236,7 +244,7 @@ python -m lerobot.record \
|
||||
### Replay
|
||||
|
||||
```bash
|
||||
python -m lerobot.replay \
|
||||
lerobot-replay \
|
||||
--robot.type=hope_jr_hand \
|
||||
--robot.port=/dev/tty.usbmodem58760432281 \
|
||||
--robot.id=right \
|
||||
@@ -248,7 +256,7 @@ python -m lerobot.replay \
|
||||
### Train
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--dataset.repo_id=nepyope/hand_record_test_with_video_data \
|
||||
--policy.type=act \
|
||||
--output_dir=outputs/train/hopejr_hand \
|
||||
@@ -263,7 +271,7 @@ python -m lerobot.scripts.train \
|
||||
This training run can be viewed as an example [here](https://wandb.ai/tino/lerobot/runs/rp0k8zvw?nw=nwusertino).
|
||||
|
||||
```bash
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=hope_jr_hand \
|
||||
--robot.port=/dev/tty.usbmodem58760432281 \
|
||||
--robot.id=right \
|
||||
|
||||
@@ -45,7 +45,7 @@ Note that the `id` associated with a robot is used to store the calibration file
|
||||
<hfoptions id="teleoperate_so101">
|
||||
<hfoption id="Command">
|
||||
```bash
|
||||
python -m lerobot.teleoperate \
|
||||
lerobot-teleoperate \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.id=my_awesome_follower_arm \
|
||||
@@ -101,7 +101,7 @@ With `rerun`, you can teleoperate again while simultaneously visualizing the cam
|
||||
<hfoptions id="teleoperate_koch_camera">
|
||||
<hfoption id="Command">
|
||||
```bash
|
||||
python -m lerobot.teleoperate \
|
||||
lerobot-teleoperate \
|
||||
--robot.type=koch_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.id=my_awesome_follower_arm \
|
||||
@@ -174,7 +174,7 @@ Now you can record a dataset. To record 5 episodes and upload your dataset to th
|
||||
<hfoptions id="record">
|
||||
<hfoption id="Command">
|
||||
```bash
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/tty.usbmodem585A0076841 \
|
||||
--robot.id=my_awesome_follower_arm \
|
||||
@@ -376,7 +376,7 @@ You can replay the first episode on your robot with either the command below or
|
||||
<hfoptions id="replay">
|
||||
<hfoption id="Command">
|
||||
```bash
|
||||
python -m lerobot.replay \
|
||||
lerobot-replay \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.id=my_awesome_follower_arm \
|
||||
@@ -428,10 +428,10 @@ Your robot should replicate movements similar to those you recorded. For example
|
||||
|
||||
## Train a policy
|
||||
|
||||
To train a policy to control your robot, use the [`python -m lerobot.scripts.train`](https://github.com/huggingface/lerobot/blob/main/src/lerobot/scripts/train.py) script. A few arguments are required. Here is an example command:
|
||||
To train a policy to control your robot, use the [`lerobot-train`](https://github.com/huggingface/lerobot/blob/main/src/lerobot/scripts/train.py) script. A few arguments are required. Here is an example command:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--dataset.repo_id=${HF_USER}/so101_test \
|
||||
--policy.type=act \
|
||||
--output_dir=outputs/train/act_so101_test \
|
||||
@@ -453,7 +453,7 @@ Training should take several hours. You will find checkpoints in `outputs/train/
|
||||
To resume training from a checkpoint, below is an example command to resume from `last` checkpoint of the `act_so101_test` policy:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=outputs/train/act_so101_test/checkpoints/last/pretrained_model/train_config.json \
|
||||
--resume=true
|
||||
```
|
||||
@@ -490,7 +490,7 @@ You can use the `record` script from [`lerobot/record.py`](https://github.com/hu
|
||||
<hfoptions id="eval">
|
||||
<hfoption id="Command">
|
||||
```bash
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=so100_follower \
|
||||
--robot.port=/dev/ttyACM1 \
|
||||
--robot.cameras="{ up: {type: opencv, index_or_path: /dev/video10, width: 640, height: 480, fps: 30}, side: {type: intelrealsense, serial_number_or_name: 233522074606, width: 640, height: 480, fps: 30}}" \
|
||||
|
||||
@@ -96,10 +96,10 @@ If you uploaded your dataset to the hub you can [visualize your dataset online](
|
||||
|
||||
## Train a policy
|
||||
|
||||
To train a policy to control your robot, use the [`python -m lerobot.scripts.train`](https://github.com/huggingface/lerobot/blob/main/src/lerobot/scripts/train.py) script. A few arguments are required. Here is an example command:
|
||||
To train a policy to control your robot, use the [`lerobot-train`](https://github.com/huggingface/lerobot/blob/main/src/lerobot/scripts/train.py) script. A few arguments are required. Here is an example command:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--dataset.repo_id=${HF_USER}/il_gym \
|
||||
--policy.type=act \
|
||||
--output_dir=outputs/train/il_sim_test \
|
||||
|
||||
@@ -31,7 +31,7 @@ pip install -e ".[dynamixel]"
|
||||
To find the port for each bus servo adapter, run this script:
|
||||
|
||||
```bash
|
||||
python -m lerobot.find_port
|
||||
lerobot-find-port
|
||||
```
|
||||
|
||||
<hfoptions id="example">
|
||||
@@ -98,7 +98,7 @@ For a visual reference on how to set the motor ids please refer to [this video](
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--robot.type=koch_follower \
|
||||
--robot.port=/dev/tty.usbmodem575E0031751 # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -174,7 +174,7 @@ Do the same steps for the leader arm but modify the command or script accordingl
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--teleop.type=koch_leader \
|
||||
--teleop.port=/dev/tty.usbmodem575E0031751 \ # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -211,7 +211,7 @@ Run the following command or API example to calibrate the follower arm:
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--robot.type=koch_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--robot.id=my_awesome_follower_arm # <- Give the robot a unique name
|
||||
@@ -249,7 +249,7 @@ Do the same steps to calibrate the leader arm, run the following command or API
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=koch_leader \
|
||||
--teleop.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--teleop.id=my_awesome_leader_arm # <- Give the robot a unique name
|
||||
|
||||
@@ -60,7 +60,7 @@ First, we will assemble the two SO100/SO101 arms. One to attach to the mobile ba
|
||||
To find the port for each bus servo adapter, run this script:
|
||||
|
||||
```bash
|
||||
python -m lerobot.find_port
|
||||
lerobot-find-port
|
||||
```
|
||||
|
||||
<hfoptions id="example">
|
||||
@@ -116,7 +116,7 @@ The instructions for configuring the motors can be found in the SO101 [docs](./s
|
||||
You can run this command to setup motors for LeKiwi. It will first setup the motors for arm (id 6..1) and then setup motors for wheels (9,8,7)
|
||||
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--robot.type=lekiwi \
|
||||
--robot.port=/dev/tty.usbmodem58760431551 # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -174,7 +174,7 @@ The calibration process is very important because it allows a neural network tra
|
||||
Make sure the arm is connected to the Raspberry Pi and run this script or API example (on the Raspberry Pi via SSH) to launch calibration of the follower arm:
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--robot.type=lekiwi \
|
||||
--robot.id=my_awesome_kiwi # <- Give the robot a unique name
|
||||
```
|
||||
@@ -193,7 +193,7 @@ Then, to calibrate the leader arm (which is attached to the laptop/pc). Run the
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=so100_leader \
|
||||
--teleop.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--teleop.id=my_awesome_leader_arm # <- Give the robot a unique name
|
||||
|
||||
@@ -54,7 +54,7 @@ If you don't have a gpu device, you can train using our notebook on [.
|
||||
|
||||
```bash
|
||||
cd lerobot && python -m lerobot.scripts.train \
|
||||
cd lerobot && lerobot-train \
|
||||
--policy.path=lerobot/smolvla_base \
|
||||
--dataset.repo_id=${HF_USER}/mydataset \
|
||||
--batch_size=64 \
|
||||
@@ -73,7 +73,7 @@ cd lerobot && python -m lerobot.scripts.train \
|
||||
Fine-tuning is an art. For a complete overview of the options for finetuning, run
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train --help
|
||||
lerobot-train --help
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
@@ -97,7 +97,7 @@ Similarly for when recording an episode, it is recommended that you are logged i
|
||||
Once you are logged in, you can run inference in your setup by doing:
|
||||
|
||||
```bash
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/ttyACM0 \ # <- Use your port
|
||||
--robot.id=my_blue_follower_arm \ # <- Use your robot id
|
||||
|
||||
@@ -26,7 +26,7 @@ Unlike the SO-101, the motor connectors are not easily accessible once the arm i
|
||||
To find the port for each bus servo adapter, run this script:
|
||||
|
||||
```bash
|
||||
python -m lerobot.find_port
|
||||
lerobot-find-port
|
||||
```
|
||||
|
||||
<hfoptions id="example">
|
||||
@@ -93,7 +93,7 @@ For a visual reference on how to set the motor ids please refer to [this video](
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--robot.type=so100_follower \
|
||||
--robot.port=/dev/tty.usbmodem585A0076841 # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -168,7 +168,7 @@ Do the same steps for the leader arm.
|
||||
<hfoptions id="setup_motors">
|
||||
<hfoption id="Command">
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--teleop.type=so100_leader \
|
||||
--teleop.port=/dev/tty.usbmodem575E0031751 # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -568,7 +568,7 @@ Run the following command or API example to calibrate the follower arm:
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--robot.type=so100_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--robot.id=my_awesome_follower_arm # <- Give the robot a unique name
|
||||
@@ -606,7 +606,7 @@ Do the same steps to calibrate the leader arm, run the following command or API
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=so100_leader \
|
||||
--teleop.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--teleop.id=my_awesome_leader_arm # <- Give the robot a unique name
|
||||
|
||||
@@ -162,7 +162,7 @@ It is advisable to install one 3-pin cable in the motor after placing them befor
|
||||
To find the port for each bus servo adapter, connect MotorBus to your computer via USB and power. Run the following script and disconnect the MotorBus when prompted:
|
||||
|
||||
```bash
|
||||
python -m lerobot.find_port
|
||||
lerobot-find-port
|
||||
```
|
||||
|
||||
<hfoptions id="example">
|
||||
@@ -240,7 +240,7 @@ Connect the usb cable from your computer and the power supply to the follower ar
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/tty.usbmodem585A0076841 # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -316,7 +316,7 @@ Do the same steps for the leader arm.
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--teleop.type=so101_leader \
|
||||
--teleop.port=/dev/tty.usbmodem575E0031751 # <- paste here the port found at previous step
|
||||
```
|
||||
@@ -353,7 +353,7 @@ Run the following command or API example to calibrate the follower arm:
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--robot.id=my_awesome_follower_arm # <- Give the robot a unique name
|
||||
@@ -402,7 +402,7 @@ Do the same steps to calibrate the leader arm, run the following command or API
|
||||
<hfoption id="Command">
|
||||
|
||||
```bash
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=so101_leader \
|
||||
--teleop.port=/dev/tty.usbmodem58760431551 \ # <- The port of your robot
|
||||
--teleop.id=my_awesome_leader_arm # <- Give the robot a unique name
|
||||
|
||||
@@ -62,7 +62,7 @@ By default, every field takes its default value specified in the dataclass. If a
|
||||
Let's say that we want to train [Diffusion Policy](../src/lerobot/policies/diffusion) on the [pusht](https://huggingface.co/datasets/lerobot/pusht) dataset, using the [gym_pusht](https://github.com/huggingface/gym-pusht) environment for evaluation. The command to do so would look like this:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--dataset.repo_id=lerobot/pusht \
|
||||
--policy.type=diffusion \
|
||||
--env.type=pusht
|
||||
@@ -77,7 +77,7 @@ Let's break this down:
|
||||
Let's see another example. Let's say you've been training [ACT](../src/lerobot/policies/act) on [lerobot/aloha_sim_insertion_human](https://huggingface.co/datasets/lerobot/aloha_sim_insertion_human) using the [gym-aloha](https://github.com/huggingface/gym-aloha) environment for evaluation with:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=act \
|
||||
--dataset.repo_id=lerobot/aloha_sim_insertion_human \
|
||||
--env.type=aloha \
|
||||
@@ -90,7 +90,7 @@ We now want to train a different policy for aloha on another task. We'll change
|
||||
Looking at the [`AlohaEnv`](../src/lerobot/envs/configs.py) config, the task is `"AlohaInsertion-v0"` by default, which corresponds to the task we trained on in the command above. The [gym-aloha](https://github.com/huggingface/gym-aloha?tab=readme-ov-file#description) environment also has the `AlohaTransferCube-v0` task which corresponds to this other task we want to train on. Putting this together, we can train this new policy on this different task using:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=act \
|
||||
--dataset.repo_id=lerobot/aloha_sim_transfer_cube_human \
|
||||
--env.type=aloha \
|
||||
@@ -127,7 +127,7 @@ Now, let's assume that we want to reproduce the run just above. That run has pro
|
||||
We can then simply load the config values from this file using:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=outputs/train/act_aloha_transfer/checkpoints/last/pretrained_model/ \
|
||||
--output_dir=outputs/train/act_aloha_transfer_2
|
||||
```
|
||||
@@ -137,7 +137,7 @@ python -m lerobot.scripts.train \
|
||||
Similarly to Hydra, we can still override some parameters in the CLI if we want to, e.g.:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=outputs/train/act_aloha_transfer/checkpoints/last/pretrained_model/ \
|
||||
--output_dir=outputs/train/act_aloha_transfer_2
|
||||
--policy.n_action_steps=80
|
||||
@@ -148,7 +148,7 @@ python -m lerobot.scripts.train \
|
||||
`--config_path` can also accept the repo_id of a repo on the hub that contains a `train_config.json` file, e.g. running:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train --config_path=lerobot/diffusion_pusht
|
||||
lerobot-train --config_path=lerobot/diffusion_pusht
|
||||
```
|
||||
|
||||
will start a training run with the same configuration used for training [lerobot/diffusion_pusht](https://huggingface.co/lerobot/diffusion_pusht)
|
||||
@@ -160,7 +160,7 @@ Being able to resume a training run is important in case it crashed or aborted f
|
||||
Let's reuse the command from the previous run and add a few more options:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=act \
|
||||
--dataset.repo_id=lerobot/aloha_sim_transfer_cube_human \
|
||||
--env.type=aloha \
|
||||
@@ -179,7 +179,7 @@ INFO 2025-01-24 16:10:56 ts/train.py:263 Checkpoint policy after step 100
|
||||
Now let's simulate a crash by killing the process (hit `ctrl`+`c`). We can then simply resume this run from the last checkpoint available with:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=outputs/train/run_resumption/checkpoints/last/pretrained_model/ \
|
||||
--resume=true
|
||||
```
|
||||
@@ -190,7 +190,7 @@ Another reason for which you might want to resume a run is simply to extend trai
|
||||
You could double the number of steps of the previous run with:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=outputs/train/run_resumption/checkpoints/last/pretrained_model/ \
|
||||
--resume=true \
|
||||
--steps=200000
|
||||
@@ -224,7 +224,7 @@ In addition to the features currently in Draccus, we've added a special `.path`
|
||||
For example, we could fine-tune a [policy pre-trained on the aloha transfer task](https://huggingface.co/lerobot/act_aloha_sim_transfer_cube_human) on the aloha insertion task. We can achieve this with:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.path=lerobot/act_aloha_sim_transfer_cube_human \
|
||||
--dataset.repo_id=lerobot/aloha_sim_insertion_human \
|
||||
--env.type=aloha \
|
||||
@@ -270,7 +270,7 @@ We'll summarize here the main use cases to remember from this tutorial.
|
||||
#### Train a policy from scratch – CLI
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=act \ # <- select 'act' policy
|
||||
--env.type=pusht \ # <- select 'pusht' environment
|
||||
--dataset.repo_id=lerobot/pusht # <- train on this dataset
|
||||
@@ -279,7 +279,7 @@ python -m lerobot.scripts.train \
|
||||
#### Train a policy from scratch - config file + CLI
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=path/to/pretrained_model \ # <- can also be a repo_id
|
||||
--policy.n_action_steps=80 # <- you may still override values
|
||||
```
|
||||
@@ -287,7 +287,7 @@ python -m lerobot.scripts.train \
|
||||
#### Resume/continue a training run
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--config_path=checkpoint/pretrained_model/ \
|
||||
--resume=true \
|
||||
--steps=200000 # <- you can change some training parameters
|
||||
@@ -296,7 +296,7 @@ python -m lerobot.scripts.train \
|
||||
#### Fine-tuning
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.path=lerobot/act_aloha_sim_transfer_cube_human \ # <- can also be a local path to a checkpoint
|
||||
--dataset.repo_id=lerobot/aloha_sim_insertion_human \
|
||||
--env.type=aloha \
|
||||
|
||||
@@ -18,7 +18,7 @@ Replays the actions of an episode from a dataset on a robot.
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.replay \
|
||||
lerobot-replay \
|
||||
--robot.type=so100_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.id=black \
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>3D Hand Joint Visualizer</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.controls {
|
||||
padding: 15px;
|
||||
background-color: #f5f5f5;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.status {
|
||||
padding: 10px;
|
||||
border-radius: 5px;
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
.connected {
|
||||
background-color: #d4edda;
|
||||
color: #155724;
|
||||
}
|
||||
|
||||
.disconnected {
|
||||
background-color: #f8d7da;
|
||||
color: #721c24;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 16px;
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
background-color: #cccccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#canvas-container {
|
||||
flex: 3;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
#sidebar {
|
||||
flex: 1;
|
||||
padding: 15px;
|
||||
background-color: #f8f9fa;
|
||||
overflow-y: auto;
|
||||
max-width: 300px;
|
||||
border-left: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.joint-info {
|
||||
margin-bottom: 10px;
|
||||
padding: 8px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.joint-name {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.joint-value {
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.bar-container {
|
||||
width: 100%;
|
||||
background-color: #e0e0e0;
|
||||
height: 10px;
|
||||
border-radius: 5px;
|
||||
overflow: hidden;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.bar {
|
||||
height: 100%;
|
||||
background-color: #4CAF50;
|
||||
width: 0%;
|
||||
transition: width 0.2s ease-in-out;
|
||||
}
|
||||
|
||||
.log-container {
|
||||
margin-top: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
padding: 10px;
|
||||
height: 150px;
|
||||
overflow-y: auto;
|
||||
font-family: monospace;
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
|
||||
.view-controls {
|
||||
position: absolute;
|
||||
bottom: 10px;
|
||||
left: 10px;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.view-button {
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 5px 10px;
|
||||
margin-right: 5px;
|
||||
border-radius: 3px;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="controls">
|
||||
<button id="connectButton">Connect to Device</button>
|
||||
<button id="disconnectButton" disabled>Disconnect</button>
|
||||
<select id="baudRate">
|
||||
<option value="9600">9600</option>
|
||||
<option value="19200">19200</option>
|
||||
<option value="38400">38400</option>
|
||||
<option value="57600">57600</option>
|
||||
<option value="115200" selected>115200</option>
|
||||
</select>
|
||||
<span id="statusIndicator" class="status disconnected">Status: Disconnected</span>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<div id="canvas-container">
|
||||
<!-- 3D canvas will be inserted here -->
|
||||
<div class="view-controls">
|
||||
<button class="view-button" id="frontView">Front</button>
|
||||
<button class="view-button" id="sideView">Side</button>
|
||||
<button class="view-button" id="topView">Top</button>
|
||||
<button class="view-button" id="resetView">Reset</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="sidebar">
|
||||
<h3>Joint Values</h3>
|
||||
<div id="jointsContainer">
|
||||
<!-- Joint info will be added here -->
|
||||
</div>
|
||||
|
||||
<div class="log-container" id="logContainer">
|
||||
<!-- Log messages will be added here -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Import Three.js -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.js"></script>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,669 @@
|
||||
// === Hand Visualizer with Pre-Connect Sliders + Per-Joint Angle Limits ===
|
||||
// Assumes your HTML already has elements with the following IDs:
|
||||
// connectButton, disconnectButton, baudRate, statusIndicator, jointsContainer, logContainer,
|
||||
// canvas-container, frontView, sideView, topView, resetView
|
||||
// Requires Three.js + OrbitControls loaded on the page.
|
||||
|
||||
// -------------------- Config --------------------
|
||||
const MAX_JOINTS = 16;
|
||||
const RAW_MIN = 0, RAW_MAX = 4096;
|
||||
const RAW_CENTER = (RAW_MIN + RAW_MAX) / 2;
|
||||
const DEG = Math.PI / 180;
|
||||
const UI_DEG_MIN = -90, UI_DEG_MAX = 90; // UI sliders for angle limits
|
||||
|
||||
// -------------------- State --------------------
|
||||
let port;
|
||||
let reader;
|
||||
let keepReading = false;
|
||||
let isConnected = false;
|
||||
const decoder = new TextDecoder();
|
||||
let inputBuffer = '';
|
||||
|
||||
let jointValues = new Array(MAX_JOINTS).fill(RAW_CENTER);
|
||||
|
||||
// Auto-calibration: track observed min/max per joint
|
||||
let observedMin = new Array(MAX_JOINTS).fill(Infinity);
|
||||
let observedMax = new Array(MAX_JOINTS).fill(-Infinity);
|
||||
let calibrationEnabled = true;
|
||||
|
||||
// Three.js
|
||||
let scene, camera, renderer, controls;
|
||||
let hand = { palm: null, fingers: [] };
|
||||
|
||||
// DOM
|
||||
const connectButton = document.getElementById('connectButton');
|
||||
const disconnectButton = document.getElementById('disconnectButton');
|
||||
const baudRateSelect = document.getElementById('baudRate');
|
||||
const statusIndicator = document.getElementById('statusIndicator');
|
||||
const jointsContainer = document.getElementById('jointsContainer');
|
||||
const logContainer = document.getElementById('logContainer');
|
||||
const canvasContainer = document.getElementById('canvas-container');
|
||||
const frontViewBtn = document.getElementById('frontView');
|
||||
const sideViewBtn = document.getElementById('sideView');
|
||||
const topViewBtn = document.getElementById('topView');
|
||||
const resetViewBtn = document.getElementById('resetView');
|
||||
|
||||
// Helpers
|
||||
const clamp = (x, a, b) => Math.max(a, Math.min(b, x));
|
||||
const invLerp = (a, b, x) => clamp((x - a) / (b - a), 0, 1);
|
||||
|
||||
// -------------------- Joint Map with per-joint angle limits --------------------
|
||||
const fingerJointMap = [
|
||||
// Thumb (4)
|
||||
{ finger:0, joint:0, type:'CMC_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:0, joint:1, type:'CMC_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:0, joint:2, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
{ finger:0, joint:3, type:'IP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
|
||||
// Index (3)
|
||||
{ finger:1, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:1, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:1, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
|
||||
// Middle (3)
|
||||
{ finger:2, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:2, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:2, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
|
||||
// Ring (3)
|
||||
{ finger:3, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:3, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:3, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false }, // +45° only
|
||||
|
||||
// Pinky (3)
|
||||
{ finger:4, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:4, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:4, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false } // +45° only
|
||||
];
|
||||
|
||||
// Assign angle limits (radians) per joint (default ±45°, exceptions: +45° only)
|
||||
for (const j of fingerJointMap) {
|
||||
const isThumb = j.finger === 0;
|
||||
const isPIP = j.type === 'PIP_FLEXION';
|
||||
let minA = -45 * DEG, maxA = +45 * DEG;
|
||||
if ((isThumb && (j.type === 'MCP_FLEXION' || j.type === 'IP_FLEXION')) || (!isThumb && isPIP)) {
|
||||
minA = 0;
|
||||
maxA = +45 * DEG;
|
||||
}
|
||||
j.angleMin = minA;
|
||||
j.angleMax = maxA;
|
||||
}
|
||||
|
||||
// -------------------- UI: Joint Panel --------------------
|
||||
const uiRefs = []; // per joint: { valueLabel, bar, barWrap, slider, invertChk, minDeg, maxDeg }
|
||||
|
||||
function initializeJointElements() {
|
||||
jointsContainer.innerHTML = '';
|
||||
uiRefs.length = 0;
|
||||
|
||||
for (let i = 0; i < MAX_JOINTS; i++) {
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'joint-info';
|
||||
|
||||
const fingerIndex = i < 4 ? 0 : Math.floor((i - 4) / 3) + 1;
|
||||
const jointInfo = fingerJointMap[i];
|
||||
const jointType = jointInfo?.type || 'Unknown';
|
||||
const fingerName = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky'][fingerIndex];
|
||||
|
||||
// Header
|
||||
const nameEl = document.createElement('div');
|
||||
nameEl.className = 'joint-name';
|
||||
nameEl.textContent = `${fingerName} – ${jointType}`;
|
||||
|
||||
// Value + bar
|
||||
const valueEl = document.createElement('div');
|
||||
valueEl.className = 'joint-value';
|
||||
valueEl.textContent = `Value: ${jointValues[i]}`;
|
||||
|
||||
const barWrap = document.createElement('div');
|
||||
barWrap.className = 'bar-container';
|
||||
const barEl = document.createElement('div');
|
||||
barEl.className = 'bar';
|
||||
barWrap.appendChild(barEl);
|
||||
|
||||
// Slider for pre-connect manual control
|
||||
const slider = document.createElement('input');
|
||||
slider.type = 'range';
|
||||
slider.min = String(RAW_MIN);
|
||||
slider.max = String(RAW_MAX);
|
||||
slider.value = String(jointValues[i]);
|
||||
slider.step = '1';
|
||||
slider.className = 'joint-slider';
|
||||
|
||||
slider.addEventListener('input', () => {
|
||||
if (isConnected) return; // ignore while connected
|
||||
let v = parseInt(slider.value, 10);
|
||||
if (jointInfo?.inverted) v = (jointInfo.min + jointInfo.max) - v;
|
||||
jointValues[i] = clamp(jointInfo ? v : 0, RAW_MIN, RAW_MAX);
|
||||
updateJointDisplay(i, jointValues[i]);
|
||||
updateHandModel();
|
||||
});
|
||||
|
||||
// Invert checkbox
|
||||
const invertLbl = document.createElement('label');
|
||||
invertLbl.className = 'invert-toggle';
|
||||
const invertChk = document.createElement('input');
|
||||
invertChk.type = 'checkbox';
|
||||
invertChk.checked = !!jointInfo?.inverted;
|
||||
invertChk.addEventListener('change', () => {
|
||||
if (jointInfo) jointInfo.inverted = invertChk.checked;
|
||||
addLogMessage(`${fingerName} ${jointType} inversion ${invertChk.checked ? 'enabled' : 'disabled'}`);
|
||||
});
|
||||
invertLbl.appendChild(invertChk);
|
||||
invertLbl.appendChild(document.createTextNode('Invert Values'));
|
||||
|
||||
// Angle limits (deg) controls
|
||||
const limitsRow = document.createElement('div');
|
||||
limitsRow.className = 'limits-row';
|
||||
|
||||
const minDeg = document.createElement('input');
|
||||
minDeg.type = 'number';
|
||||
minDeg.min = String(UI_DEG_MIN);
|
||||
minDeg.max = String(UI_DEG_MAX);
|
||||
minDeg.step = '1';
|
||||
minDeg.value = String(Math.round((jointInfo.angleMin || 0) / DEG));
|
||||
minDeg.className = 'limit-num';
|
||||
|
||||
const maxDeg = document.createElement('input');
|
||||
maxDeg.type = 'number';
|
||||
maxDeg.min = String(UI_DEG_MIN);
|
||||
maxDeg.max = String(UI_DEG_MAX);
|
||||
maxDeg.step = '1';
|
||||
maxDeg.value = String(Math.round((jointInfo.angleMax || 0) / DEG));
|
||||
maxDeg.className = 'limit-num';
|
||||
|
||||
const minLbl = document.createElement('span'); minLbl.textContent = 'min°';
|
||||
const maxLbl = document.createElement('span'); maxLbl.textContent = 'max°';
|
||||
minLbl.className = 'limit-label'; maxLbl.className = 'limit-label';
|
||||
|
||||
function syncLimits() {
|
||||
let mn = parseFloat(minDeg.value);
|
||||
let mx = parseFloat(maxDeg.value);
|
||||
if (isNaN(mn)) mn = -45;
|
||||
if (isNaN(mx)) mx = +45;
|
||||
if (mn > mx) [mn, mx] = [mx, mn];
|
||||
jointInfo.angleMin = clamp(mn, UI_DEG_MIN, UI_DEG_MAX) * DEG;
|
||||
jointInfo.angleMax = clamp(mx, UI_DEG_MIN, UI_DEG_MAX) * DEG;
|
||||
minDeg.value = String(Math.round(jointInfo.angleMin / DEG));
|
||||
maxDeg.value = String(Math.round(jointInfo.angleMax / DEG));
|
||||
updateHandModel();
|
||||
}
|
||||
minDeg.addEventListener('change', syncLimits);
|
||||
maxDeg.addEventListener('change', syncLimits);
|
||||
|
||||
limitsRow.appendChild(minLbl);
|
||||
limitsRow.appendChild(minDeg);
|
||||
limitsRow.appendChild(maxLbl);
|
||||
limitsRow.appendChild(maxDeg);
|
||||
|
||||
// Calibration controls
|
||||
const calibRow = document.createElement('div');
|
||||
calibRow.className = 'calib-row';
|
||||
|
||||
const resetCalibBtn = document.createElement('button');
|
||||
resetCalibBtn.textContent = 'Reset Calib';
|
||||
resetCalibBtn.className = 'calib-btn';
|
||||
resetCalibBtn.addEventListener('click', () => {
|
||||
observedMin[i] = Infinity;
|
||||
observedMax[i] = -Infinity;
|
||||
addLogMessage(`Reset calibration for ${fingerName} ${jointType}`);
|
||||
});
|
||||
|
||||
const calibStatus = document.createElement('span');
|
||||
calibStatus.className = 'calib-status';
|
||||
calibStatus.textContent = `Range: --`;
|
||||
|
||||
calibRow.appendChild(resetCalibBtn);
|
||||
calibRow.appendChild(calibStatus);
|
||||
|
||||
// Compose
|
||||
wrap.appendChild(nameEl);
|
||||
wrap.appendChild(valueEl);
|
||||
wrap.appendChild(barWrap);
|
||||
wrap.appendChild(slider);
|
||||
wrap.appendChild(invertLbl);
|
||||
wrap.appendChild(limitsRow);
|
||||
wrap.appendChild(calibRow);
|
||||
|
||||
jointsContainer.appendChild(wrap);
|
||||
|
||||
uiRefs[i] = { valueLabel: valueEl, bar: barEl, barWrap, slider, invertChk, minDeg, maxDeg, nameEl, calibStatus };
|
||||
}
|
||||
|
||||
setConnectedUI(false); // initial state: sliders active
|
||||
}
|
||||
|
||||
// Toggle UI between pre-connect SLIDERS vs post-connect BARS
|
||||
function setConnectedUI(connected) {
|
||||
isConnected = connected;
|
||||
for (let i = 0; i < uiRefs.length; i++) {
|
||||
const ui = uiRefs[i];
|
||||
if (!ui) continue;
|
||||
// Show bars when connected; sliders disabled/hidden
|
||||
ui.barWrap.style.display = connected ? '' : 'none';
|
||||
ui.slider.disabled = connected;
|
||||
ui.slider.style.display = connected ? 'none' : '';
|
||||
}
|
||||
|
||||
// Reset calibration when connecting
|
||||
if (connected) {
|
||||
observedMin.fill(Infinity);
|
||||
observedMax.fill(-Infinity);
|
||||
addLogMessage('Calibration reset - move joints through full range for best results');
|
||||
}
|
||||
}
|
||||
|
||||
// Update joint display (value text + bar color/width + slider position if needed)
|
||||
function updateJointDisplay(jointIndex, value) {
|
||||
const ui = uiRefs[jointIndex];
|
||||
const info = fingerJointMap[jointIndex];
|
||||
if (!ui || !info) return;
|
||||
|
||||
ui.valueLabel.textContent = `Value: ${value}`;
|
||||
|
||||
// bar
|
||||
const min = info.min, max = info.max;
|
||||
const pct = clamp((value - min) / (max - min), 0, 1) * 100;
|
||||
ui.bar.style.width = `${pct}%`;
|
||||
const hue = Math.floor(pct * 1.2); // 0..120
|
||||
ui.bar.style.backgroundColor = `hsl(${hue}, 80%, 50%)`;
|
||||
|
||||
// slider (only meaningful when not connected; keep in sync anyway)
|
||||
const rawForSlider = info.inverted ? (info.min + info.max) - value : value;
|
||||
if (!isConnected) ui.slider.value = String(clamp(Math.round(rawForSlider), RAW_MIN, RAW_MAX));
|
||||
}
|
||||
|
||||
// -------------------- Serial I/O --------------------
|
||||
async function readSerialData() {
|
||||
while (port?.readable && keepReading) {
|
||||
reader = port.readable.getReader();
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
if (value) processData(decoder.decode(value));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error reading:', err);
|
||||
addLogMessage(`Error: ${err.message}`);
|
||||
break;
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processData(chunk) {
|
||||
inputBuffer += chunk;
|
||||
let idx;
|
||||
while ((idx = inputBuffer.indexOf('\n')) !== -1) {
|
||||
const line = inputBuffer.slice(0, idx).trim();
|
||||
inputBuffer = inputBuffer.slice(idx + 1);
|
||||
|
||||
const vals = line.split(/\s+/).map(v => parseInt(v, 10));
|
||||
if (vals.length === MAX_JOINTS && vals.every(v => Number.isFinite(v))) {
|
||||
for (let i = 0; i < MAX_JOINTS; i++) {
|
||||
const info = fingerJointMap[i];
|
||||
if (!info) continue;
|
||||
|
||||
let rawValue = vals[i];
|
||||
|
||||
// Update calibration tracking
|
||||
if (calibrationEnabled) {
|
||||
observedMin[i] = Math.min(observedMin[i], rawValue);
|
||||
observedMax[i] = Math.max(observedMax[i], rawValue);
|
||||
|
||||
// Update calibration display
|
||||
const ui = uiRefs[i];
|
||||
if (ui && ui.calibStatus) {
|
||||
if (observedMin[i] !== Infinity && observedMax[i] !== -Infinity) {
|
||||
ui.calibStatus.textContent = `Range: ${observedMin[i]}-${observedMax[i]}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Remap observed range to target range
|
||||
if (observedMin[i] !== Infinity && observedMax[i] !== -Infinity && observedMax[i] > observedMin[i]) {
|
||||
const observedRange = observedMax[i] - observedMin[i];
|
||||
const targetRange = info.max - info.min;
|
||||
const normalizedValue = (rawValue - observedMin[i]) / observedRange;
|
||||
rawValue = info.min + (normalizedValue * targetRange);
|
||||
}
|
||||
}
|
||||
|
||||
let v = clamp(rawValue, info.min, info.max);
|
||||
if (info.inverted) v = (info.min + info.max) - v;
|
||||
jointValues[i] = v;
|
||||
updateJointDisplay(i, v);
|
||||
}
|
||||
updateHandModel();
|
||||
} else {
|
||||
addLogMessage(`Received: ${line}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function connectToDevice() {
|
||||
try {
|
||||
port = await navigator.serial.requestPort();
|
||||
const baudRate = parseInt(baudRateSelect.value, 10) || 115200;
|
||||
await port.open({ baudRate });
|
||||
|
||||
keepReading = true;
|
||||
setConnectedUI(true);
|
||||
|
||||
statusIndicator.textContent = 'Status: Connected';
|
||||
statusIndicator.className = 'status connected';
|
||||
connectButton.disabled = true;
|
||||
disconnectButton.disabled = false;
|
||||
baudRateSelect.disabled = true;
|
||||
|
||||
addLogMessage(`Connected at ${baudRate} baud`);
|
||||
readSerialData();
|
||||
} catch (e) {
|
||||
console.error('Connect error:', e);
|
||||
addLogMessage(`Connection error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function disconnectFromDevice() {
|
||||
try {
|
||||
keepReading = false;
|
||||
if (reader) {
|
||||
try { reader.cancel(); } catch {}
|
||||
}
|
||||
if (port) {
|
||||
await port.close();
|
||||
port = null;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Disconnect error:', e);
|
||||
addLogMessage(`Disconnection error: ${e.message}`);
|
||||
} finally {
|
||||
setConnectedUI(false);
|
||||
statusIndicator.textContent = 'Status: Disconnected';
|
||||
statusIndicator.className = 'status disconnected';
|
||||
connectButton.disabled = false;
|
||||
disconnectButton.disabled = true;
|
||||
baudRateSelect.disabled = false;
|
||||
addLogMessage('Disconnected');
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Three.js Scene --------------------
|
||||
function initThreeJS() {
|
||||
scene = new THREE.Scene();
|
||||
scene.background = new THREE.Color(0xf0f0f0);
|
||||
|
||||
camera = new THREE.PerspectiveCamera(
|
||||
75,
|
||||
canvasContainer.clientWidth / canvasContainer.clientHeight,
|
||||
0.1, 1000
|
||||
);
|
||||
camera.position.set(0, 15, 15);
|
||||
camera.lookAt(0, 0, 0);
|
||||
|
||||
renderer = new THREE.WebGLRenderer({ antialias: true });
|
||||
renderer.setSize(canvasContainer.clientWidth, canvasContainer.clientHeight);
|
||||
renderer.setPixelRatio(window.devicePixelRatio);
|
||||
canvasContainer.appendChild(renderer.domElement);
|
||||
|
||||
controls = new THREE.OrbitControls(camera, renderer.domElement);
|
||||
controls.enableDamping = true;
|
||||
controls.dampingFactor = 0.25;
|
||||
|
||||
const ambientLight = new THREE.AmbientLight(0x404040);
|
||||
scene.add(ambientLight);
|
||||
const dir1 = new THREE.DirectionalLight(0xffffff, 0.5);
|
||||
dir1.position.set(1, 1, 1);
|
||||
scene.add(dir1);
|
||||
const dir2 = new THREE.DirectionalLight(0xffffff, 0.3);
|
||||
dir2.position.set(-1, 1, -1);
|
||||
scene.add(dir2);
|
||||
|
||||
const gridHelper = new THREE.GridHelper(20, 20);
|
||||
scene.add(gridHelper);
|
||||
|
||||
createHandModel();
|
||||
window.addEventListener('resize', onWindowResize);
|
||||
animate();
|
||||
}
|
||||
|
||||
function createHandModel() {
|
||||
const palmMaterial = new THREE.MeshPhongMaterial({ color: 0xf5c396 });
|
||||
const fingerMaterial = new THREE.MeshPhongMaterial({ color: 0xf5c396 });
|
||||
const jointMaterial = new THREE.MeshPhongMaterial({ color: 0xe3a977 });
|
||||
|
||||
const palmGeometry = new THREE.BoxGeometry(7, 1, 8);
|
||||
hand.palm = new THREE.Mesh(palmGeometry, palmMaterial);
|
||||
hand.palm.position.set(0, 0, 0);
|
||||
hand.palm.rotation.x = Math.PI / 2; // hand vertical, palm facing forward
|
||||
scene.add(hand.palm);
|
||||
|
||||
const fingerWidth = 1, fingerHeight = 0.8;
|
||||
const fingerSegmentLengths = [3, 2, 1.5];
|
||||
const thumbSegmentLengths = [2, 2, 1.5];
|
||||
|
||||
const fingerBasePositions = [
|
||||
[ 3, 0, -2], // Thumb
|
||||
[ 1.5,-0.5,-4], // Index
|
||||
[ 0, -0.5,-4], // Middle
|
||||
[-1.5,-0.5,-4], // Ring
|
||||
[-3, -0.5,-4], // Pinky
|
||||
];
|
||||
const fingerBaseRot = [
|
||||
{ x:0, y:-Math.PI/3, z: Math.PI/3 }, // Thumb
|
||||
{ x:0, y:-Math.PI/48, z: 0 },
|
||||
{ x:0, y: Math.PI/48, z: 0 },
|
||||
{ x:0, y: Math.PI/32, z: 0 },
|
||||
{ x:0, y: Math.PI/24, z: 0 }
|
||||
];
|
||||
|
||||
for (let fIdx = 0; fIdx < 5; fIdx++) {
|
||||
const finger = { name:['Thumb','Index','Middle','Ring','Pinky'][fIdx], segments:[], joints:[] };
|
||||
const isThumb = fIdx === 0;
|
||||
const segLens = isThumb ? thumbSegmentLengths : fingerSegmentLengths;
|
||||
|
||||
finger.group = new THREE.Group();
|
||||
finger.group.position.set(...fingerBasePositions[fIdx]);
|
||||
finger.group.rotation.x = fingerBaseRot[fIdx].x;
|
||||
finger.group.rotation.y = fingerBaseRot[fIdx].y;
|
||||
finger.group.rotation.z = fingerBaseRot[fIdx].z;
|
||||
finger.group.userData.baseRot = {
|
||||
x:finger.group.rotation.x,
|
||||
y:finger.group.rotation.y,
|
||||
z:finger.group.rotation.z
|
||||
};
|
||||
hand.palm.add(finger.group);
|
||||
|
||||
let parent = finger.group;
|
||||
for (let s = 0; s < segLens.length; s++) {
|
||||
const segGroup = new THREE.Group();
|
||||
|
||||
const jGeom = new THREE.SphereGeometry(fingerWidth * 0.6, 8, 8);
|
||||
const joint = new THREE.Mesh(jGeom, jointMaterial);
|
||||
segGroup.add(joint);
|
||||
|
||||
const segGeom = new THREE.BoxGeometry(fingerWidth, fingerHeight, segLens[s]);
|
||||
const seg = new THREE.Mesh(segGeom, fingerMaterial);
|
||||
seg.position.z = -segLens[s] / 2;
|
||||
segGroup.add(seg);
|
||||
|
||||
parent.add(segGroup);
|
||||
|
||||
finger.segments.push(segGroup);
|
||||
finger.joints.push(joint);
|
||||
|
||||
if (s < segLens.length - 1) {
|
||||
const connector = new THREE.Group();
|
||||
connector.position.z = -segLens[s];
|
||||
segGroup.add(connector);
|
||||
parent = connector;
|
||||
}
|
||||
}
|
||||
|
||||
hand.fingers.push(finger);
|
||||
}
|
||||
|
||||
addFingerLabels();
|
||||
addHandLabel();
|
||||
}
|
||||
|
||||
function addFingerLabels() {
|
||||
const names = ['Thumb','Index','Middle','Ring','Pinky'];
|
||||
for (let i = 0; i < hand.fingers.length; i++) {
|
||||
const finger = hand.fingers[i];
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
canvas.width = 128; canvas.height = 32;
|
||||
ctx.fillStyle = '#ffffff'; ctx.fillRect(0,0,canvas.width,canvas.height);
|
||||
ctx.font = 'bold 16px Arial';
|
||||
ctx.fillStyle = '#000000';
|
||||
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
||||
ctx.fillText(names[i], canvas.width/2, canvas.height/2);
|
||||
|
||||
const texture = new THREE.CanvasTexture(canvas);
|
||||
const geom = new THREE.PlaneGeometry(2, 0.5);
|
||||
const mat = new THREE.MeshBasicMaterial({ map:texture, transparent:true, side:THREE.DoubleSide });
|
||||
const label = new THREE.Mesh(geom, mat);
|
||||
label.position.set(0, -1.5, -2);
|
||||
label.rotation.x = Math.PI / 2;
|
||||
finger.group.add(label);
|
||||
}
|
||||
}
|
||||
|
||||
function addHandLabel() {
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
canvas.width = 256; canvas.height = 64;
|
||||
ctx.fillStyle = '#ffffff'; ctx.fillRect(0,0,canvas.width,canvas.height);
|
||||
ctx.font = 'bold 24px Arial';
|
||||
ctx.fillStyle = '#000000';
|
||||
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
||||
ctx.fillText('RIGHT HAND (VERTICAL)', canvas.width/2, canvas.height/2);
|
||||
|
||||
const texture = new THREE.CanvasTexture(canvas);
|
||||
const geom = new THREE.PlaneGeometry(7, 1.75);
|
||||
const mat = new THREE.MeshBasicMaterial({ map:texture, transparent:true, side:THREE.DoubleSide });
|
||||
const label = new THREE.Mesh(geom, mat);
|
||||
label.position.set(0, -2, 0);
|
||||
label.rotation.x = Math.PI / 2;
|
||||
scene.add(label);
|
||||
}
|
||||
|
||||
function updateHandModel() {
|
||||
for (let i = 0; i < MAX_JOINTS; i++) {
|
||||
const info = fingerJointMap[i];
|
||||
if (!info) continue;
|
||||
const { finger, joint, type, min, max, angleMin, angleMax } = info;
|
||||
const raw = jointValues[i];
|
||||
const f = hand.fingers[finger];
|
||||
if (!f) continue;
|
||||
|
||||
const center = (min + max) / 2;
|
||||
let angle = 0;
|
||||
|
||||
if (type.includes('ABDUCTION')) {
|
||||
// symmetric around neutral
|
||||
const k = clamp((raw - center) / ((max - min) / 2), -1, 1);
|
||||
angle = angleMin + (k + 1) * 0.5 * (angleMax - angleMin);
|
||||
|
||||
const base = f.group.userData.baseRot || {x:0,y:0,z:0};
|
||||
if (finger === 0 && joint === 0) {
|
||||
// Thumb: abduction about Z (toward/away from palm)
|
||||
f.group.rotation.z = base.z + angle;
|
||||
} else {
|
||||
// Other fingers: side-to-side about Y
|
||||
f.group.rotation.y = base.y + angle;
|
||||
}
|
||||
} else if (type.includes('FLEXION')) {
|
||||
const isThumb = finger === 0;
|
||||
const isMCP = type === 'MCP_FLEXION';
|
||||
const isPIP = type === 'PIP_FLEXION';
|
||||
const positiveOnly = (isThumb && (type === 'MCP_FLEXION' || type === 'IP_FLEXION')) || (!isThumb && isPIP);
|
||||
|
||||
if (positiveOnly) {
|
||||
const t = raw <= center ? 0 : invLerp(center, max, raw); // 0..1
|
||||
angle = angleMin + t * (angleMax - angleMin); // 0..+limit
|
||||
} else {
|
||||
const k = clamp((raw - center) / ((max - min) / 2), -1, 1);
|
||||
angle = angleMin + (k + 1) * 0.5 * (angleMax - angleMin);
|
||||
}
|
||||
|
||||
if (isMCP) {
|
||||
// MCP flexion applies to the finger base group (same as abduction)
|
||||
const base = f.group.userData.baseRot || {x:0,y:0,z:0};
|
||||
f.group.rotation.x = base.x + angle;
|
||||
} else if (f.segments[joint]) {
|
||||
// PIP/DIP/IP flexion applies to individual segments
|
||||
f.segments[joint].rotation.x = angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Render Loop --------------------
|
||||
function onWindowResize() {
|
||||
camera.aspect = canvasContainer.clientWidth / canvasContainer.clientHeight;
|
||||
camera.updateProjectionMatrix();
|
||||
renderer.setSize(canvasContainer.clientWidth, canvasContainer.clientHeight);
|
||||
}
|
||||
|
||||
function animate() {
|
||||
requestAnimationFrame(animate);
|
||||
controls.update();
|
||||
renderer.render(scene, camera);
|
||||
}
|
||||
|
||||
// -------------------- Misc UI --------------------
|
||||
function addLogMessage(msg) {
|
||||
const el = document.createElement('div');
|
||||
el.textContent = msg;
|
||||
logContainer.appendChild(el);
|
||||
logContainer.scrollTop = logContainer.scrollHeight;
|
||||
while (logContainer.children.length > 100) {
|
||||
logContainer.removeChild(logContainer.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
// Camera view controls
|
||||
frontViewBtn?.addEventListener('click', () => { camera.position.set(0, 0, 20); camera.lookAt(0,0,0); controls.update(); });
|
||||
sideViewBtn?.addEventListener('click', () => { camera.position.set(20, 0, 0); camera.lookAt(0,0,0); controls.update(); });
|
||||
topViewBtn?.addEventListener('click', () => { camera.position.set(0, 20, 0); camera.lookAt(0,0,0); controls.update(); });
|
||||
resetViewBtn?.addEventListener('click', () => { camera.position.set(10,10,10); camera.lookAt(0,0,0); controls.update(); });
|
||||
|
||||
// Serial connect buttons
|
||||
connectButton?.addEventListener('click', connectToDevice);
|
||||
disconnectButton?.addEventListener('click', disconnectFromDevice);
|
||||
|
||||
// Web Serial support check
|
||||
if (!navigator.serial) {
|
||||
statusIndicator.textContent = 'Status: Web Serial API not supported in this browser';
|
||||
connectButton.disabled = true;
|
||||
addLogMessage('ERROR: Web Serial API is not supported in this browser. Try Chrome or Edge.');
|
||||
}
|
||||
|
||||
// -------------------- Boot --------------------
|
||||
initThreeJS();
|
||||
initializeJointElements();
|
||||
|
||||
// -------------------- Styles (inline) --------------------
|
||||
const styleElement = document.createElement('style');
|
||||
styleElement.textContent = `
|
||||
.joint-info { border-bottom: 1px solid #eee; padding: 8px 0; }
|
||||
.joint-name { font-weight: 600; margin-bottom: 4px; }
|
||||
.joint-value { font-size: 12px; color: #333; margin-bottom: 4px; }
|
||||
.bar-container { width: 100%; height: 8px; background: #ddd; border-radius: 4px; overflow: hidden; }
|
||||
.bar { height: 100%; width: 0%; background: #4caf50; }
|
||||
.joint-slider { width: 100%; margin: 6px 0; }
|
||||
.invert-toggle { display: inline-flex; align-items: center; gap: 6px; margin-top: 4px; font-size: 12px; color: #555; }
|
||||
.limits-row { display: flex; align-items: center; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
|
||||
.limit-label { font-size: 11px; color: #666; }
|
||||
.limit-num { width: 60px; }
|
||||
.calib-row { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
|
||||
.calib-btn { padding: 2px 6px; font-size: 11px; background: #f44336; color: white; border: none; border-radius: 3px; cursor: pointer; }
|
||||
.calib-btn:hover { background: #d32f2f; }
|
||||
.calib-status { font-size: 11px; color: #666; }
|
||||
.status.connected { color: #0a0; }
|
||||
.status.disconnected { color: #a00; }
|
||||
`;
|
||||
document.head.appendChild(styleElement);
|
||||
+1
-1
@@ -25,7 +25,7 @@ discord = "https://discord.gg/s3KuuzsPFb"
|
||||
|
||||
[project]
|
||||
name = "lerobot"
|
||||
version = "0.3.3"
|
||||
version = "0.3.4"
|
||||
description = "🤗 LeRobot: State-of-the-art Machine Learning for Real-World Robotics in Pytorch"
|
||||
readme = "README.md"
|
||||
license = { text = "Apache-2.0" }
|
||||
|
||||
@@ -18,7 +18,7 @@ Helper to recalibrate your device (robot or teleoperator).
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.calibrate \
|
||||
lerobot-calibrate \
|
||||
--teleop.type=so100_leader \
|
||||
--teleop.port=/dev/tty.usbmodem58760431551 \
|
||||
--teleop.id=blue
|
||||
|
||||
@@ -60,7 +60,7 @@ class OpenCVCamera(Camera):
|
||||
or port changes, especially on Linux. Use the provided utility script to find
|
||||
available camera indices or paths:
|
||||
```bash
|
||||
python -m lerobot.find_cameras opencv
|
||||
lerobot-find-cameras opencv
|
||||
```
|
||||
|
||||
The camera's default settings (FPS, resolution, color mode) are used unless
|
||||
@@ -165,8 +165,7 @@ class OpenCVCamera(Camera):
|
||||
self.videocapture.release()
|
||||
self.videocapture = None
|
||||
raise ConnectionError(
|
||||
f"Failed to open {self}."
|
||||
f"Run `python -m lerobot.find_cameras opencv` to find available cameras."
|
||||
f"Failed to open {self}.Run `lerobot-find-cameras opencv` to find available cameras."
|
||||
)
|
||||
|
||||
self._configure_capture_settings()
|
||||
|
||||
@@ -51,7 +51,7 @@ class RealSenseCamera(Camera):
|
||||
|
||||
Use the provided utility script to find available camera indices and default profiles:
|
||||
```bash
|
||||
python -m lerobot.find_cameras realsense
|
||||
lerobot-find-cameras realsense
|
||||
```
|
||||
|
||||
A `RealSenseCamera` instance requires a configuration object specifying the
|
||||
@@ -176,8 +176,7 @@ class RealSenseCamera(Camera):
|
||||
self.rs_profile = None
|
||||
self.rs_pipeline = None
|
||||
raise ConnectionError(
|
||||
f"Failed to open {self}."
|
||||
"Run `python -m lerobot.find_cameras realsense` to find available cameras."
|
||||
f"Failed to open {self}.Run `lerobot-find-cameras realsense` to find available cameras."
|
||||
) from e
|
||||
|
||||
self._configure_capture_settings()
|
||||
|
||||
@@ -20,7 +20,7 @@ Helper to find the camera devices available in your system.
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.find_cameras
|
||||
lerobot-find-cameras
|
||||
```
|
||||
"""
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ Helper to find the USB port associated with your MotorsBus.
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.find_port
|
||||
lerobot-find-port
|
||||
```
|
||||
"""
|
||||
|
||||
|
||||
@@ -222,7 +222,7 @@ class MotorsBus(abc.ABC):
|
||||
A MotorsBus subclass instance requires a port (e.g. `FeetechMotorsBus(port="/dev/tty.usbmodem575E0031751"`)).
|
||||
To find the port, you can run our utility script:
|
||||
```bash
|
||||
python -m lerobot.find_port.py
|
||||
lerobot-find-port.py
|
||||
>>> Finding all available ports for the MotorsBus.
|
||||
>>> ["/dev/tty.usbmodem575E0032081", "/dev/tty.usbmodem575E0031751"]
|
||||
>>> Remove the usb cable from your MotorsBus and press Enter when done.
|
||||
@@ -446,7 +446,7 @@ class MotorsBus(abc.ABC):
|
||||
except (FileNotFoundError, OSError, serial.SerialException) as e:
|
||||
raise ConnectionError(
|
||||
f"\nCould not connect on port '{self.port}'. Make sure you are using the correct port."
|
||||
"\nTry running `python -m lerobot.find_port`\n"
|
||||
"\nTry running `lerobot-find-port`\n"
|
||||
) from e
|
||||
|
||||
@abc.abstractmethod
|
||||
|
||||
@@ -30,7 +30,7 @@ pip install -e ".[pi0]"
|
||||
|
||||
Example of finetuning the pi0 pretrained model (`pi0_base` in `openpi`):
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.path=lerobot/pi0 \
|
||||
--dataset.repo_id=danaaubakirova/koch_test
|
||||
```
|
||||
@@ -38,7 +38,7 @@ python -m lerobot.scripts.train \
|
||||
Example of finetuning the pi0 neural network with PaliGemma and expert Gemma
|
||||
pretrained with VLM default parameters before pi0 finetuning:
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=pi0 \
|
||||
--dataset.repo_id=danaaubakirova/koch_test
|
||||
```
|
||||
|
||||
@@ -25,14 +25,14 @@ Disclaimer: It is not expected to perform as well as the original implementation
|
||||
|
||||
Example of finetuning the pi0+FAST pretrained model (`pi0_fast_base` in `openpi`):
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.path=lerobot/pi0fast_base \
|
||||
--dataset.repo_id=danaaubakirova/koch_test
|
||||
```
|
||||
|
||||
Example of training the pi0+FAST neural network with from scratch:
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=pi0fast \
|
||||
--dataset.repo_id=danaaubakirova/koch_test
|
||||
```
|
||||
|
||||
@@ -28,7 +28,7 @@ pip install -e ".[smolvla]"
|
||||
|
||||
Example of finetuning the smolvla pretrained model (`smolvla_base`):
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.path=lerobot/smolvla_base \
|
||||
--dataset.repo_id=danaaubakirova/svla_so100_task1_v3 \
|
||||
--batch_size=64 \
|
||||
@@ -38,7 +38,7 @@ python -m lerobot.scripts.train \
|
||||
Example of finetuning a smolVLA. SmolVLA is composed of a pretrained VLM,
|
||||
and an action expert.
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--policy.type=smolvla \
|
||||
--dataset.repo_id=danaaubakirova/svla_so100_task1_v3 \
|
||||
--batch_size=64 \
|
||||
|
||||
@@ -18,7 +18,7 @@ Records a dataset. Actions for the robot can be either generated by teleoperatio
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=so100_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.cameras="{laptop: {type: opencv, camera_index: 0, width: 640, height: 480}}" \
|
||||
@@ -36,7 +36,7 @@ python -m lerobot.record \
|
||||
|
||||
Example recording with bimanual so100:
|
||||
```shell
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=bi_so100_follower \
|
||||
--robot.left_arm_port=/dev/tty.usbmodem5A460851411 \
|
||||
--robot.right_arm_port=/dev/tty.usbmodem5A460812391 \
|
||||
|
||||
@@ -18,7 +18,7 @@ Replays the actions of an episode from a dataset on a robot.
|
||||
Examples:
|
||||
|
||||
```shell
|
||||
python -m lerobot.replay \
|
||||
lerobot-replay \
|
||||
--robot.type=so100_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.id=black \
|
||||
@@ -28,7 +28,7 @@ python -m lerobot.replay \
|
||||
|
||||
Example replay with bimanual so100:
|
||||
```shell
|
||||
python -m lerobot.replay \
|
||||
lerobot-replay \
|
||||
--robot.type=bi_so100_follower \
|
||||
--robot.left_arm_port=/dev/tty.usbmodem5A460851411 \
|
||||
--robot.right_arm_port=/dev/tty.usbmodem5A460812391 \
|
||||
|
||||
@@ -141,10 +141,10 @@ python lerobot/scripts/control_robot.py \
|
||||
|
||||
## Train a policy
|
||||
|
||||
To train a policy to control your robot, use the [`python -m lerobot.scripts.train`](../src/lerobot/scripts/train.py) script. A few arguments are required. Here is an example command:
|
||||
To train a policy to control your robot, use the [`lerobot-train`](../src/lerobot/scripts/train.py) script. A few arguments are required. Here is an example command:
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--dataset.repo_id=${HF_USER}/aloha_test \
|
||||
--policy.type=act \
|
||||
--output_dir=outputs/train/act_aloha_test \
|
||||
|
||||
@@ -21,7 +21,7 @@ You want to evaluate a model from the hub (eg: https://huggingface.co/lerobot/di
|
||||
for 10 episodes.
|
||||
|
||||
```
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=lerobot/diffusion_pusht \
|
||||
--env.type=pusht \
|
||||
--eval.batch_size=10 \
|
||||
@@ -32,7 +32,7 @@ python -m lerobot.scripts.eval \
|
||||
|
||||
OR, you want to evaluate a model checkpoint from the LeRobot training script for 10 episodes.
|
||||
```
|
||||
python -m lerobot.scripts.eval \
|
||||
lerobot-eval \
|
||||
--policy.path=outputs/train/diffusion_pusht/checkpoints/005000/pretrained_model \
|
||||
--env.type=pusht \
|
||||
--eval.batch_size=10 \
|
||||
|
||||
@@ -18,7 +18,7 @@ Helper to set motor ids and baudrate.
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.setup_motors \
|
||||
lerobot-setup-motors \
|
||||
--teleop.type=so100_leader \
|
||||
--teleop.port=/dev/tty.usbmodem575E0031751
|
||||
```
|
||||
|
||||
@@ -18,7 +18,7 @@ Simple script to control a robot from teleoperation.
|
||||
Example:
|
||||
|
||||
```shell
|
||||
python -m lerobot.teleoperate \
|
||||
lerobot-teleoperate \
|
||||
--robot.type=so101_follower \
|
||||
--robot.port=/dev/tty.usbmodem58760431541 \
|
||||
--robot.cameras="{ front: {type: opencv, index_or_path: 0, width: 1920, height: 1080, fps: 30}}" \
|
||||
@@ -32,7 +32,7 @@ python -m lerobot.teleoperate \
|
||||
Example teleoperation with bimanual so100:
|
||||
|
||||
```shell
|
||||
python -m lerobot.teleoperate \
|
||||
lerobot-teleoperate \
|
||||
--robot.type=bi_so100_follower \
|
||||
--robot.left_arm_port=/dev/tty.usbmodem5A460851411 \
|
||||
--robot.right_arm_port=/dev/tty.usbmodem5A460812391 \
|
||||
|
||||
@@ -44,7 +44,7 @@ Below is the short version on how to train and run inference/eval:
|
||||
### Train from scratch
|
||||
|
||||
```bash
|
||||
python -m lerobot.scripts.train \
|
||||
lerobot-train \
|
||||
--dataset.repo_id=${HF_USER}/<dataset> \
|
||||
--policy.type=act \
|
||||
--output_dir=outputs/train/<desired_policy_repo_id> \
|
||||
@@ -59,7 +59,7 @@ _Writes checkpoints to `outputs/train/<desired_policy_repo_id>/checkpoints/`._
|
||||
### Evaluate the policy/run inference
|
||||
|
||||
```bash
|
||||
python -m lerobot.record \
|
||||
lerobot-record \
|
||||
--robot.type=so100_follower \
|
||||
--dataset.repo_id=<hf_user>/eval_<dataset> \
|
||||
--policy.path=<hf_user>/<desired_policy_repo_id> \
|
||||
|
||||
@@ -17,10 +17,9 @@ import time
|
||||
|
||||
|
||||
def busy_wait(seconds):
|
||||
if platform.system() == "Darwin":
|
||||
# On Mac, `time.sleep` is not accurate and we need to use this while loop trick,
|
||||
if platform.system() == "Darwin" or platform.system() == "Windows":
|
||||
# On Mac and Windows, `time.sleep` is not accurate and we need to use this while loop trick,
|
||||
# but it consumes CPU cycles.
|
||||
# TODO(rcadene): find an alternative: from python 11, time.sleep is precise
|
||||
end_time = time.perf_counter() + seconds
|
||||
while time.perf_counter() < end_time:
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,182 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>3D Hand Joint Visualizer</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
.controls {
|
||||
padding: 15px;
|
||||
background-color: #f5f5f5;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.status {
|
||||
padding: 10px;
|
||||
border-radius: 5px;
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
.connected {
|
||||
background-color: #d4edda;
|
||||
color: #155724;
|
||||
}
|
||||
|
||||
.disconnected {
|
||||
background-color: #f8d7da;
|
||||
color: #721c24;
|
||||
}
|
||||
|
||||
button {
|
||||
padding: 8px 16px;
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
margin-right: 10px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background-color: #45a049;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
background-color: #cccccc;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#canvas-container {
|
||||
flex: 3;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
#sidebar {
|
||||
flex: 1;
|
||||
padding: 15px;
|
||||
background-color: #f8f9fa;
|
||||
overflow-y: auto;
|
||||
max-width: 300px;
|
||||
border-left: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.joint-info {
|
||||
margin-bottom: 10px;
|
||||
padding: 8px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.joint-name {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.joint-value {
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.bar-container {
|
||||
width: 100%;
|
||||
background-color: #e0e0e0;
|
||||
height: 10px;
|
||||
border-radius: 5px;
|
||||
overflow: hidden;
|
||||
margin-top: 5px;
|
||||
}
|
||||
|
||||
.bar {
|
||||
height: 100%;
|
||||
background-color: #4CAF50;
|
||||
width: 0%;
|
||||
transition: width 0.2s ease-in-out;
|
||||
}
|
||||
|
||||
.log-container {
|
||||
margin-top: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
padding: 10px;
|
||||
height: 150px;
|
||||
overflow-y: auto;
|
||||
font-family: monospace;
|
||||
background-color: #f8f9fa;
|
||||
}
|
||||
|
||||
.view-controls {
|
||||
position: absolute;
|
||||
bottom: 10px;
|
||||
left: 10px;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.view-button {
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 5px 10px;
|
||||
margin-right: 5px;
|
||||
border-radius: 3px;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="controls">
|
||||
<button id="connectButton">Connect to Device</button>
|
||||
<button id="disconnectButton" disabled>Disconnect</button>
|
||||
<select id="baudRate">
|
||||
<option value="9600">9600</option>
|
||||
<option value="19200">19200</option>
|
||||
<option value="38400">38400</option>
|
||||
<option value="57600">57600</option>
|
||||
<option value="115200" selected>115200</option>
|
||||
</select>
|
||||
<span id="statusIndicator" class="status disconnected">Status: Disconnected</span>
|
||||
</div>
|
||||
|
||||
<div class="container">
|
||||
<div id="canvas-container">
|
||||
<!-- 3D canvas will be inserted here -->
|
||||
<div class="view-controls">
|
||||
<button class="view-button" id="frontView">Front</button>
|
||||
<button class="view-button" id="sideView">Side</button>
|
||||
<button class="view-button" id="topView">Top</button>
|
||||
<button class="view-button" id="resetView">Reset</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="sidebar">
|
||||
<h3>Joint Values</h3>
|
||||
<div id="jointsContainer">
|
||||
<!-- Joint info will be added here -->
|
||||
</div>
|
||||
|
||||
<div class="log-container" id="logContainer">
|
||||
<!-- Log messages will be added here -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Import Three.js -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/three@0.132.2/examples/js/controls/OrbitControls.js"></script>
|
||||
|
||||
<script src="script.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,669 @@
|
||||
// === Hand Visualizer with Pre-Connect Sliders + Per-Joint Angle Limits ===
|
||||
// Assumes your HTML already has elements with the following IDs:
|
||||
// connectButton, disconnectButton, baudRate, statusIndicator, jointsContainer, logContainer,
|
||||
// canvas-container, frontView, sideView, topView, resetView
|
||||
// Requires Three.js + OrbitControls loaded on the page.
|
||||
|
||||
// -------------------- Config --------------------
|
||||
const MAX_JOINTS = 16;
|
||||
const RAW_MIN = 0, RAW_MAX = 4096;
|
||||
const RAW_CENTER = (RAW_MIN + RAW_MAX) / 2;
|
||||
const DEG = Math.PI / 180;
|
||||
const UI_DEG_MIN = -90, UI_DEG_MAX = 90; // UI sliders for angle limits
|
||||
|
||||
// -------------------- State --------------------
|
||||
let port;
|
||||
let reader;
|
||||
let keepReading = false;
|
||||
let isConnected = false;
|
||||
const decoder = new TextDecoder();
|
||||
let inputBuffer = '';
|
||||
|
||||
let jointValues = new Array(MAX_JOINTS).fill(RAW_CENTER);
|
||||
|
||||
// Auto-calibration: track observed min/max per joint
|
||||
let observedMin = new Array(MAX_JOINTS).fill(Infinity);
|
||||
let observedMax = new Array(MAX_JOINTS).fill(-Infinity);
|
||||
let calibrationEnabled = true;
|
||||
|
||||
// Three.js
|
||||
let scene, camera, renderer, controls;
|
||||
let hand = { palm: null, fingers: [] };
|
||||
|
||||
// DOM
|
||||
const connectButton = document.getElementById('connectButton');
|
||||
const disconnectButton = document.getElementById('disconnectButton');
|
||||
const baudRateSelect = document.getElementById('baudRate');
|
||||
const statusIndicator = document.getElementById('statusIndicator');
|
||||
const jointsContainer = document.getElementById('jointsContainer');
|
||||
const logContainer = document.getElementById('logContainer');
|
||||
const canvasContainer = document.getElementById('canvas-container');
|
||||
const frontViewBtn = document.getElementById('frontView');
|
||||
const sideViewBtn = document.getElementById('sideView');
|
||||
const topViewBtn = document.getElementById('topView');
|
||||
const resetViewBtn = document.getElementById('resetView');
|
||||
|
||||
// Helpers
|
||||
const clamp = (x, a, b) => Math.max(a, Math.min(b, x));
|
||||
const invLerp = (a, b, x) => clamp((x - a) / (b - a), 0, 1);
|
||||
|
||||
// -------------------- Joint Map with per-joint angle limits --------------------
|
||||
const fingerJointMap = [
|
||||
// Thumb (4)
|
||||
{ finger:0, joint:0, type:'CMC_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:0, joint:1, type:'CMC_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:0, joint:2, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
{ finger:0, joint:3, type:'IP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
|
||||
// Index (3)
|
||||
{ finger:1, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:1, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:1, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
|
||||
// Middle (3)
|
||||
{ finger:2, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:2, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:2, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:true }, // +45° only
|
||||
|
||||
// Ring (3)
|
||||
{ finger:3, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:true },
|
||||
{ finger:3, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:3, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false }, // +45° only
|
||||
|
||||
// Pinky (3)
|
||||
{ finger:4, joint:0, type:'MCP_ABDUCTION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:4, joint:1, type:'MCP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false },
|
||||
{ finger:4, joint:2, type:'PIP_FLEXION', min:RAW_MIN, max:RAW_MAX, inverted:false } // +45° only
|
||||
];
|
||||
|
||||
// Assign angle limits (radians) per joint (default ±45°, exceptions: +45° only)
|
||||
for (const j of fingerJointMap) {
|
||||
const isThumb = j.finger === 0;
|
||||
const isPIP = j.type === 'PIP_FLEXION';
|
||||
let minA = -45 * DEG, maxA = +45 * DEG;
|
||||
if ((isThumb && (j.type === 'MCP_FLEXION' || j.type === 'IP_FLEXION')) || (!isThumb && isPIP)) {
|
||||
minA = 0;
|
||||
maxA = +45 * DEG;
|
||||
}
|
||||
j.angleMin = minA;
|
||||
j.angleMax = maxA;
|
||||
}
|
||||
|
||||
// -------------------- UI: Joint Panel --------------------
|
||||
const uiRefs = []; // per joint: { valueLabel, bar, barWrap, slider, invertChk, minDeg, maxDeg }
|
||||
|
||||
function initializeJointElements() {
|
||||
jointsContainer.innerHTML = '';
|
||||
uiRefs.length = 0;
|
||||
|
||||
for (let i = 0; i < MAX_JOINTS; i++) {
|
||||
const wrap = document.createElement('div');
|
||||
wrap.className = 'joint-info';
|
||||
|
||||
const fingerIndex = i < 4 ? 0 : Math.floor((i - 4) / 3) + 1;
|
||||
const jointInfo = fingerJointMap[i];
|
||||
const jointType = jointInfo?.type || 'Unknown';
|
||||
const fingerName = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky'][fingerIndex];
|
||||
|
||||
// Header
|
||||
const nameEl = document.createElement('div');
|
||||
nameEl.className = 'joint-name';
|
||||
nameEl.textContent = `${fingerName} – ${jointType}`;
|
||||
|
||||
// Value + bar
|
||||
const valueEl = document.createElement('div');
|
||||
valueEl.className = 'joint-value';
|
||||
valueEl.textContent = `Value: ${jointValues[i]}`;
|
||||
|
||||
const barWrap = document.createElement('div');
|
||||
barWrap.className = 'bar-container';
|
||||
const barEl = document.createElement('div');
|
||||
barEl.className = 'bar';
|
||||
barWrap.appendChild(barEl);
|
||||
|
||||
// Slider for pre-connect manual control
|
||||
const slider = document.createElement('input');
|
||||
slider.type = 'range';
|
||||
slider.min = String(RAW_MIN);
|
||||
slider.max = String(RAW_MAX);
|
||||
slider.value = String(jointValues[i]);
|
||||
slider.step = '1';
|
||||
slider.className = 'joint-slider';
|
||||
|
||||
slider.addEventListener('input', () => {
|
||||
if (isConnected) return; // ignore while connected
|
||||
let v = parseInt(slider.value, 10);
|
||||
if (jointInfo?.inverted) v = (jointInfo.min + jointInfo.max) - v;
|
||||
jointValues[i] = clamp(jointInfo ? v : 0, RAW_MIN, RAW_MAX);
|
||||
updateJointDisplay(i, jointValues[i]);
|
||||
updateHandModel();
|
||||
});
|
||||
|
||||
// Invert checkbox
|
||||
const invertLbl = document.createElement('label');
|
||||
invertLbl.className = 'invert-toggle';
|
||||
const invertChk = document.createElement('input');
|
||||
invertChk.type = 'checkbox';
|
||||
invertChk.checked = !!jointInfo?.inverted;
|
||||
invertChk.addEventListener('change', () => {
|
||||
if (jointInfo) jointInfo.inverted = invertChk.checked;
|
||||
addLogMessage(`${fingerName} ${jointType} inversion ${invertChk.checked ? 'enabled' : 'disabled'}`);
|
||||
});
|
||||
invertLbl.appendChild(invertChk);
|
||||
invertLbl.appendChild(document.createTextNode('Invert Values'));
|
||||
|
||||
// Angle limits (deg) controls
|
||||
const limitsRow = document.createElement('div');
|
||||
limitsRow.className = 'limits-row';
|
||||
|
||||
const minDeg = document.createElement('input');
|
||||
minDeg.type = 'number';
|
||||
minDeg.min = String(UI_DEG_MIN);
|
||||
minDeg.max = String(UI_DEG_MAX);
|
||||
minDeg.step = '1';
|
||||
minDeg.value = String(Math.round((jointInfo.angleMin || 0) / DEG));
|
||||
minDeg.className = 'limit-num';
|
||||
|
||||
const maxDeg = document.createElement('input');
|
||||
maxDeg.type = 'number';
|
||||
maxDeg.min = String(UI_DEG_MIN);
|
||||
maxDeg.max = String(UI_DEG_MAX);
|
||||
maxDeg.step = '1';
|
||||
maxDeg.value = String(Math.round((jointInfo.angleMax || 0) / DEG));
|
||||
maxDeg.className = 'limit-num';
|
||||
|
||||
const minLbl = document.createElement('span'); minLbl.textContent = 'min°';
|
||||
const maxLbl = document.createElement('span'); maxLbl.textContent = 'max°';
|
||||
minLbl.className = 'limit-label'; maxLbl.className = 'limit-label';
|
||||
|
||||
function syncLimits() {
|
||||
let mn = parseFloat(minDeg.value);
|
||||
let mx = parseFloat(maxDeg.value);
|
||||
if (isNaN(mn)) mn = -45;
|
||||
if (isNaN(mx)) mx = +45;
|
||||
if (mn > mx) [mn, mx] = [mx, mn];
|
||||
jointInfo.angleMin = clamp(mn, UI_DEG_MIN, UI_DEG_MAX) * DEG;
|
||||
jointInfo.angleMax = clamp(mx, UI_DEG_MIN, UI_DEG_MAX) * DEG;
|
||||
minDeg.value = String(Math.round(jointInfo.angleMin / DEG));
|
||||
maxDeg.value = String(Math.round(jointInfo.angleMax / DEG));
|
||||
updateHandModel();
|
||||
}
|
||||
minDeg.addEventListener('change', syncLimits);
|
||||
maxDeg.addEventListener('change', syncLimits);
|
||||
|
||||
limitsRow.appendChild(minLbl);
|
||||
limitsRow.appendChild(minDeg);
|
||||
limitsRow.appendChild(maxLbl);
|
||||
limitsRow.appendChild(maxDeg);
|
||||
|
||||
// Calibration controls
|
||||
const calibRow = document.createElement('div');
|
||||
calibRow.className = 'calib-row';
|
||||
|
||||
const resetCalibBtn = document.createElement('button');
|
||||
resetCalibBtn.textContent = 'Reset Calib';
|
||||
resetCalibBtn.className = 'calib-btn';
|
||||
resetCalibBtn.addEventListener('click', () => {
|
||||
observedMin[i] = Infinity;
|
||||
observedMax[i] = -Infinity;
|
||||
addLogMessage(`Reset calibration for ${fingerName} ${jointType}`);
|
||||
});
|
||||
|
||||
const calibStatus = document.createElement('span');
|
||||
calibStatus.className = 'calib-status';
|
||||
calibStatus.textContent = `Range: --`;
|
||||
|
||||
calibRow.appendChild(resetCalibBtn);
|
||||
calibRow.appendChild(calibStatus);
|
||||
|
||||
// Compose
|
||||
wrap.appendChild(nameEl);
|
||||
wrap.appendChild(valueEl);
|
||||
wrap.appendChild(barWrap);
|
||||
wrap.appendChild(slider);
|
||||
wrap.appendChild(invertLbl);
|
||||
wrap.appendChild(limitsRow);
|
||||
wrap.appendChild(calibRow);
|
||||
|
||||
jointsContainer.appendChild(wrap);
|
||||
|
||||
uiRefs[i] = { valueLabel: valueEl, bar: barEl, barWrap, slider, invertChk, minDeg, maxDeg, nameEl, calibStatus };
|
||||
}
|
||||
|
||||
setConnectedUI(false); // initial state: sliders active
|
||||
}
|
||||
|
||||
// Toggle UI between pre-connect SLIDERS vs post-connect BARS
|
||||
function setConnectedUI(connected) {
|
||||
isConnected = connected;
|
||||
for (let i = 0; i < uiRefs.length; i++) {
|
||||
const ui = uiRefs[i];
|
||||
if (!ui) continue;
|
||||
// Show bars when connected; sliders disabled/hidden
|
||||
ui.barWrap.style.display = connected ? '' : 'none';
|
||||
ui.slider.disabled = connected;
|
||||
ui.slider.style.display = connected ? 'none' : '';
|
||||
}
|
||||
|
||||
// Reset calibration when connecting
|
||||
if (connected) {
|
||||
observedMin.fill(Infinity);
|
||||
observedMax.fill(-Infinity);
|
||||
addLogMessage('Calibration reset - move joints through full range for best results');
|
||||
}
|
||||
}
|
||||
|
||||
// Update joint display (value text + bar color/width + slider position if needed)
|
||||
function updateJointDisplay(jointIndex, value) {
|
||||
const ui = uiRefs[jointIndex];
|
||||
const info = fingerJointMap[jointIndex];
|
||||
if (!ui || !info) return;
|
||||
|
||||
ui.valueLabel.textContent = `Value: ${value}`;
|
||||
|
||||
// bar
|
||||
const min = info.min, max = info.max;
|
||||
const pct = clamp((value - min) / (max - min), 0, 1) * 100;
|
||||
ui.bar.style.width = `${pct}%`;
|
||||
const hue = Math.floor(pct * 1.2); // 0..120
|
||||
ui.bar.style.backgroundColor = `hsl(${hue}, 80%, 50%)`;
|
||||
|
||||
// slider (only meaningful when not connected; keep in sync anyway)
|
||||
const rawForSlider = info.inverted ? (info.min + info.max) - value : value;
|
||||
if (!isConnected) ui.slider.value = String(clamp(Math.round(rawForSlider), RAW_MIN, RAW_MAX));
|
||||
}
|
||||
|
||||
// -------------------- Serial I/O --------------------
|
||||
async function readSerialData() {
|
||||
while (port?.readable && keepReading) {
|
||||
reader = port.readable.getReader();
|
||||
try {
|
||||
while (true) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
if (value) processData(decoder.decode(value));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error reading:', err);
|
||||
addLogMessage(`Error: ${err.message}`);
|
||||
break;
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function processData(chunk) {
|
||||
inputBuffer += chunk;
|
||||
let idx;
|
||||
while ((idx = inputBuffer.indexOf('\n')) !== -1) {
|
||||
const line = inputBuffer.slice(0, idx).trim();
|
||||
inputBuffer = inputBuffer.slice(idx + 1);
|
||||
|
||||
const vals = line.split(/\s+/).map(v => parseInt(v, 10));
|
||||
if (vals.length === MAX_JOINTS && vals.every(v => Number.isFinite(v))) {
|
||||
for (let i = 0; i < MAX_JOINTS; i++) {
|
||||
const info = fingerJointMap[i];
|
||||
if (!info) continue;
|
||||
|
||||
let rawValue = vals[i];
|
||||
|
||||
// Update calibration tracking
|
||||
if (calibrationEnabled) {
|
||||
observedMin[i] = Math.min(observedMin[i], rawValue);
|
||||
observedMax[i] = Math.max(observedMax[i], rawValue);
|
||||
|
||||
// Update calibration display
|
||||
const ui = uiRefs[i];
|
||||
if (ui && ui.calibStatus) {
|
||||
if (observedMin[i] !== Infinity && observedMax[i] !== -Infinity) {
|
||||
ui.calibStatus.textContent = `Range: ${observedMin[i]}-${observedMax[i]}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Remap observed range to target range
|
||||
if (observedMin[i] !== Infinity && observedMax[i] !== -Infinity && observedMax[i] > observedMin[i]) {
|
||||
const observedRange = observedMax[i] - observedMin[i];
|
||||
const targetRange = info.max - info.min;
|
||||
const normalizedValue = (rawValue - observedMin[i]) / observedRange;
|
||||
rawValue = info.min + (normalizedValue * targetRange);
|
||||
}
|
||||
}
|
||||
|
||||
let v = clamp(rawValue, info.min, info.max);
|
||||
if (info.inverted) v = (info.min + info.max) - v;
|
||||
jointValues[i] = v;
|
||||
updateJointDisplay(i, v);
|
||||
}
|
||||
updateHandModel();
|
||||
} else {
|
||||
addLogMessage(`Received: ${line}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function connectToDevice() {
|
||||
try {
|
||||
port = await navigator.serial.requestPort();
|
||||
const baudRate = parseInt(baudRateSelect.value, 10) || 115200;
|
||||
await port.open({ baudRate });
|
||||
|
||||
keepReading = true;
|
||||
setConnectedUI(true);
|
||||
|
||||
statusIndicator.textContent = 'Status: Connected';
|
||||
statusIndicator.className = 'status connected';
|
||||
connectButton.disabled = true;
|
||||
disconnectButton.disabled = false;
|
||||
baudRateSelect.disabled = true;
|
||||
|
||||
addLogMessage(`Connected at ${baudRate} baud`);
|
||||
readSerialData();
|
||||
} catch (e) {
|
||||
console.error('Connect error:', e);
|
||||
addLogMessage(`Connection error: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function disconnectFromDevice() {
|
||||
try {
|
||||
keepReading = false;
|
||||
if (reader) {
|
||||
try { reader.cancel(); } catch {}
|
||||
}
|
||||
if (port) {
|
||||
await port.close();
|
||||
port = null;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Disconnect error:', e);
|
||||
addLogMessage(`Disconnection error: ${e.message}`);
|
||||
} finally {
|
||||
setConnectedUI(false);
|
||||
statusIndicator.textContent = 'Status: Disconnected';
|
||||
statusIndicator.className = 'status disconnected';
|
||||
connectButton.disabled = false;
|
||||
disconnectButton.disabled = true;
|
||||
baudRateSelect.disabled = false;
|
||||
addLogMessage('Disconnected');
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Three.js Scene --------------------
|
||||
function initThreeJS() {
|
||||
scene = new THREE.Scene();
|
||||
scene.background = new THREE.Color(0xf0f0f0);
|
||||
|
||||
camera = new THREE.PerspectiveCamera(
|
||||
75,
|
||||
canvasContainer.clientWidth / canvasContainer.clientHeight,
|
||||
0.1, 1000
|
||||
);
|
||||
camera.position.set(0, 15, 15);
|
||||
camera.lookAt(0, 0, 0);
|
||||
|
||||
renderer = new THREE.WebGLRenderer({ antialias: true });
|
||||
renderer.setSize(canvasContainer.clientWidth, canvasContainer.clientHeight);
|
||||
renderer.setPixelRatio(window.devicePixelRatio);
|
||||
canvasContainer.appendChild(renderer.domElement);
|
||||
|
||||
controls = new THREE.OrbitControls(camera, renderer.domElement);
|
||||
controls.enableDamping = true;
|
||||
controls.dampingFactor = 0.25;
|
||||
|
||||
const ambientLight = new THREE.AmbientLight(0x404040);
|
||||
scene.add(ambientLight);
|
||||
const dir1 = new THREE.DirectionalLight(0xffffff, 0.5);
|
||||
dir1.position.set(1, 1, 1);
|
||||
scene.add(dir1);
|
||||
const dir2 = new THREE.DirectionalLight(0xffffff, 0.3);
|
||||
dir2.position.set(-1, 1, -1);
|
||||
scene.add(dir2);
|
||||
|
||||
const gridHelper = new THREE.GridHelper(20, 20);
|
||||
scene.add(gridHelper);
|
||||
|
||||
createHandModel();
|
||||
window.addEventListener('resize', onWindowResize);
|
||||
animate();
|
||||
}
|
||||
|
||||
function createHandModel() {
|
||||
const palmMaterial = new THREE.MeshPhongMaterial({ color: 0xf5c396 });
|
||||
const fingerMaterial = new THREE.MeshPhongMaterial({ color: 0xf5c396 });
|
||||
const jointMaterial = new THREE.MeshPhongMaterial({ color: 0xe3a977 });
|
||||
|
||||
const palmGeometry = new THREE.BoxGeometry(7, 1, 8);
|
||||
hand.palm = new THREE.Mesh(palmGeometry, palmMaterial);
|
||||
hand.palm.position.set(0, 0, 0);
|
||||
hand.palm.rotation.x = Math.PI / 2; // hand vertical, palm facing forward
|
||||
scene.add(hand.palm);
|
||||
|
||||
const fingerWidth = 1, fingerHeight = 0.8;
|
||||
const fingerSegmentLengths = [3, 2, 1.5];
|
||||
const thumbSegmentLengths = [2, 2, 1.5];
|
||||
|
||||
const fingerBasePositions = [
|
||||
[ 3, 0, -2], // Thumb
|
||||
[ 1.5,-0.5,-4], // Index
|
||||
[ 0, -0.5,-4], // Middle
|
||||
[-1.5,-0.5,-4], // Ring
|
||||
[-3, -0.5,-4], // Pinky
|
||||
];
|
||||
const fingerBaseRot = [
|
||||
{ x:0, y:-Math.PI/3, z: Math.PI/3 }, // Thumb
|
||||
{ x:0, y:-Math.PI/48, z: 0 },
|
||||
{ x:0, y: Math.PI/48, z: 0 },
|
||||
{ x:0, y: Math.PI/32, z: 0 },
|
||||
{ x:0, y: Math.PI/24, z: 0 }
|
||||
];
|
||||
|
||||
for (let fIdx = 0; fIdx < 5; fIdx++) {
|
||||
const finger = { name:['Thumb','Index','Middle','Ring','Pinky'][fIdx], segments:[], joints:[] };
|
||||
const isThumb = fIdx === 0;
|
||||
const segLens = isThumb ? thumbSegmentLengths : fingerSegmentLengths;
|
||||
|
||||
finger.group = new THREE.Group();
|
||||
finger.group.position.set(...fingerBasePositions[fIdx]);
|
||||
finger.group.rotation.x = fingerBaseRot[fIdx].x;
|
||||
finger.group.rotation.y = fingerBaseRot[fIdx].y;
|
||||
finger.group.rotation.z = fingerBaseRot[fIdx].z;
|
||||
finger.group.userData.baseRot = {
|
||||
x:finger.group.rotation.x,
|
||||
y:finger.group.rotation.y,
|
||||
z:finger.group.rotation.z
|
||||
};
|
||||
hand.palm.add(finger.group);
|
||||
|
||||
let parent = finger.group;
|
||||
for (let s = 0; s < segLens.length; s++) {
|
||||
const segGroup = new THREE.Group();
|
||||
|
||||
const jGeom = new THREE.SphereGeometry(fingerWidth * 0.6, 8, 8);
|
||||
const joint = new THREE.Mesh(jGeom, jointMaterial);
|
||||
segGroup.add(joint);
|
||||
|
||||
const segGeom = new THREE.BoxGeometry(fingerWidth, fingerHeight, segLens[s]);
|
||||
const seg = new THREE.Mesh(segGeom, fingerMaterial);
|
||||
seg.position.z = -segLens[s] / 2;
|
||||
segGroup.add(seg);
|
||||
|
||||
parent.add(segGroup);
|
||||
|
||||
finger.segments.push(segGroup);
|
||||
finger.joints.push(joint);
|
||||
|
||||
if (s < segLens.length - 1) {
|
||||
const connector = new THREE.Group();
|
||||
connector.position.z = -segLens[s];
|
||||
segGroup.add(connector);
|
||||
parent = connector;
|
||||
}
|
||||
}
|
||||
|
||||
hand.fingers.push(finger);
|
||||
}
|
||||
|
||||
addFingerLabels();
|
||||
addHandLabel();
|
||||
}
|
||||
|
||||
function addFingerLabels() {
|
||||
const names = ['Thumb','Index','Middle','Ring','Pinky'];
|
||||
for (let i = 0; i < hand.fingers.length; i++) {
|
||||
const finger = hand.fingers[i];
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
canvas.width = 128; canvas.height = 32;
|
||||
ctx.fillStyle = '#ffffff'; ctx.fillRect(0,0,canvas.width,canvas.height);
|
||||
ctx.font = 'bold 16px Arial';
|
||||
ctx.fillStyle = '#000000';
|
||||
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
||||
ctx.fillText(names[i], canvas.width/2, canvas.height/2);
|
||||
|
||||
const texture = new THREE.CanvasTexture(canvas);
|
||||
const geom = new THREE.PlaneGeometry(2, 0.5);
|
||||
const mat = new THREE.MeshBasicMaterial({ map:texture, transparent:true, side:THREE.DoubleSide });
|
||||
const label = new THREE.Mesh(geom, mat);
|
||||
label.position.set(0, -1.5, -2);
|
||||
label.rotation.x = Math.PI / 2;
|
||||
finger.group.add(label);
|
||||
}
|
||||
}
|
||||
|
||||
function addHandLabel() {
|
||||
const canvas = document.createElement('canvas');
|
||||
const ctx = canvas.getContext('2d');
|
||||
canvas.width = 256; canvas.height = 64;
|
||||
ctx.fillStyle = '#ffffff'; ctx.fillRect(0,0,canvas.width,canvas.height);
|
||||
ctx.font = 'bold 24px Arial';
|
||||
ctx.fillStyle = '#000000';
|
||||
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
||||
ctx.fillText('RIGHT HAND (VERTICAL)', canvas.width/2, canvas.height/2);
|
||||
|
||||
const texture = new THREE.CanvasTexture(canvas);
|
||||
const geom = new THREE.PlaneGeometry(7, 1.75);
|
||||
const mat = new THREE.MeshBasicMaterial({ map:texture, transparent:true, side:THREE.DoubleSide });
|
||||
const label = new THREE.Mesh(geom, mat);
|
||||
label.position.set(0, -2, 0);
|
||||
label.rotation.x = Math.PI / 2;
|
||||
scene.add(label);
|
||||
}
|
||||
|
||||
function updateHandModel() {
|
||||
for (let i = 0; i < MAX_JOINTS; i++) {
|
||||
const info = fingerJointMap[i];
|
||||
if (!info) continue;
|
||||
const { finger, joint, type, min, max, angleMin, angleMax } = info;
|
||||
const raw = jointValues[i];
|
||||
const f = hand.fingers[finger];
|
||||
if (!f) continue;
|
||||
|
||||
const center = (min + max) / 2;
|
||||
let angle = 0;
|
||||
|
||||
if (type.includes('ABDUCTION')) {
|
||||
// symmetric around neutral
|
||||
const k = clamp((raw - center) / ((max - min) / 2), -1, 1);
|
||||
angle = angleMin + (k + 1) * 0.5 * (angleMax - angleMin);
|
||||
|
||||
const base = f.group.userData.baseRot || {x:0,y:0,z:0};
|
||||
if (finger === 0 && joint === 0) {
|
||||
// Thumb: abduction about Z (toward/away from palm)
|
||||
f.group.rotation.z = base.z + angle;
|
||||
} else {
|
||||
// Other fingers: side-to-side about Y
|
||||
f.group.rotation.y = base.y + angle;
|
||||
}
|
||||
} else if (type.includes('FLEXION')) {
|
||||
const isThumb = finger === 0;
|
||||
const isMCP = type === 'MCP_FLEXION';
|
||||
const isPIP = type === 'PIP_FLEXION';
|
||||
const positiveOnly = (isThumb && (type === 'MCP_FLEXION' || type === 'IP_FLEXION')) || (!isThumb && isPIP);
|
||||
|
||||
if (positiveOnly) {
|
||||
const t = raw <= center ? 0 : invLerp(center, max, raw); // 0..1
|
||||
angle = angleMin + t * (angleMax - angleMin); // 0..+limit
|
||||
} else {
|
||||
const k = clamp((raw - center) / ((max - min) / 2), -1, 1);
|
||||
angle = angleMin + (k + 1) * 0.5 * (angleMax - angleMin);
|
||||
}
|
||||
|
||||
if (isMCP) {
|
||||
// MCP flexion applies to the finger base group (same as abduction)
|
||||
const base = f.group.userData.baseRot || {x:0,y:0,z:0};
|
||||
f.group.rotation.x = base.x + angle;
|
||||
} else if (f.segments[joint]) {
|
||||
// PIP/DIP/IP flexion applies to individual segments
|
||||
f.segments[joint].rotation.x = angle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------- Render Loop --------------------
|
||||
function onWindowResize() {
|
||||
camera.aspect = canvasContainer.clientWidth / canvasContainer.clientHeight;
|
||||
camera.updateProjectionMatrix();
|
||||
renderer.setSize(canvasContainer.clientWidth, canvasContainer.clientHeight);
|
||||
}
|
||||
|
||||
function animate() {
|
||||
requestAnimationFrame(animate);
|
||||
controls.update();
|
||||
renderer.render(scene, camera);
|
||||
}
|
||||
|
||||
// -------------------- Misc UI --------------------
|
||||
function addLogMessage(msg) {
|
||||
const el = document.createElement('div');
|
||||
el.textContent = msg;
|
||||
logContainer.appendChild(el);
|
||||
logContainer.scrollTop = logContainer.scrollHeight;
|
||||
while (logContainer.children.length > 100) {
|
||||
logContainer.removeChild(logContainer.firstChild);
|
||||
}
|
||||
}
|
||||
|
||||
// Camera view controls
|
||||
frontViewBtn?.addEventListener('click', () => { camera.position.set(0, 0, 20); camera.lookAt(0,0,0); controls.update(); });
|
||||
sideViewBtn?.addEventListener('click', () => { camera.position.set(20, 0, 0); camera.lookAt(0,0,0); controls.update(); });
|
||||
topViewBtn?.addEventListener('click', () => { camera.position.set(0, 20, 0); camera.lookAt(0,0,0); controls.update(); });
|
||||
resetViewBtn?.addEventListener('click', () => { camera.position.set(10,10,10); camera.lookAt(0,0,0); controls.update(); });
|
||||
|
||||
// Serial connect buttons
|
||||
connectButton?.addEventListener('click', connectToDevice);
|
||||
disconnectButton?.addEventListener('click', disconnectFromDevice);
|
||||
|
||||
// Web Serial support check
|
||||
if (!navigator.serial) {
|
||||
statusIndicator.textContent = 'Status: Web Serial API not supported in this browser';
|
||||
connectButton.disabled = true;
|
||||
addLogMessage('ERROR: Web Serial API is not supported in this browser. Try Chrome or Edge.');
|
||||
}
|
||||
|
||||
// -------------------- Boot --------------------
|
||||
initThreeJS();
|
||||
initializeJointElements();
|
||||
|
||||
// -------------------- Styles (inline) --------------------
|
||||
const styleElement = document.createElement('style');
|
||||
styleElement.textContent = `
|
||||
.joint-info { border-bottom: 1px solid #eee; padding: 8px 0; }
|
||||
.joint-name { font-weight: 600; margin-bottom: 4px; }
|
||||
.joint-value { font-size: 12px; color: #333; margin-bottom: 4px; }
|
||||
.bar-container { width: 100%; height: 8px; background: #ddd; border-radius: 4px; overflow: hidden; }
|
||||
.bar { height: 100%; width: 0%; background: #4caf50; }
|
||||
.joint-slider { width: 100%; margin: 6px 0; }
|
||||
.invert-toggle { display: inline-flex; align-items: center; gap: 6px; margin-top: 4px; font-size: 12px; color: #555; }
|
||||
.limits-row { display: flex; align-items: center; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
|
||||
.limit-label { font-size: 11px; color: #666; }
|
||||
.limit-num { width: 60px; }
|
||||
.calib-row { display: flex; align-items: center; gap: 8px; margin-top: 4px; }
|
||||
.calib-btn { padding: 2px 6px; font-size: 11px; background: #f44336; color: white; border: none; border-radius: 3px; cursor: pointer; }
|
||||
.calib-btn:hover { background: #d32f2f; }
|
||||
.calib-status { font-size: 11px; color: #666; }
|
||||
.status.connected { color: #0a0; }
|
||||
.status.disconnected { color: #a00; }
|
||||
`;
|
||||
document.head.appendChild(styleElement);
|
||||
Reference in New Issue
Block a user