|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
Simple script to control a robot from teleoperation.
|
|
|
|
Example:
|
|
|
|
```shell
|
|
python -m 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}}" \
|
|
--robot.id=black \
|
|
--teleop.type=so101_leader \
|
|
--teleop.port=/dev/tty.usbmodem58760431551 \
|
|
--teleop.id=blue \
|
|
--display_data=true
|
|
```
|
|
"""
|
|
|
|
import logging
|
|
import time
|
|
from dataclasses import asdict, dataclass
|
|
from pprint import pformat
|
|
|
|
import draccus
|
|
import rerun as rr
|
|
|
|
from lerobot.cameras.opencv.configuration_opencv import OpenCVCameraConfig
|
|
from lerobot.cameras.realsense.configuration_realsense import RealSenseCameraConfig
|
|
from lerobot.robots import (
|
|
Robot,
|
|
RobotConfig,
|
|
hope_jr,
|
|
koch_follower,
|
|
make_robot_from_config,
|
|
so100_follower,
|
|
so101_follower,
|
|
)
|
|
from lerobot.teleoperators import (
|
|
Teleoperator,
|
|
TeleoperatorConfig,
|
|
gamepad,
|
|
homunculus,
|
|
koch_leader,
|
|
make_teleoperator_from_config,
|
|
so100_leader,
|
|
so101_leader,
|
|
)
|
|
from lerobot.utils.robot_utils import busy_wait
|
|
from lerobot.utils.utils import init_logging, move_cursor_up
|
|
from lerobot.utils.visualization_utils import _init_rerun, log_rerun_data
|
|
|
|
|
|
@dataclass
|
|
class TeleoperateConfig:
|
|
|
|
teleop: TeleoperatorConfig
|
|
robot: RobotConfig
|
|
|
|
fps: int = 60
|
|
teleop_time_s: float | None = None
|
|
|
|
display_data: bool = False
|
|
|
|
|
|
def teleop_loop(
|
|
teleop: Teleoperator, robot: Robot, fps: int, display_data: bool = False, duration: float | None = None
|
|
):
|
|
display_len = max(len(key) for key in robot.action_features)
|
|
start = time.perf_counter()
|
|
while True:
|
|
loop_start = time.perf_counter()
|
|
action = teleop.get_action()
|
|
if display_data:
|
|
observation = robot.get_observation()
|
|
log_rerun_data(observation, action)
|
|
|
|
robot.send_action(action)
|
|
dt_s = time.perf_counter() - loop_start
|
|
busy_wait(1 / fps - dt_s)
|
|
|
|
loop_s = time.perf_counter() - loop_start
|
|
|
|
print("\n" + "-" * (display_len + 10))
|
|
print(f"{'NAME':<{display_len}} | {'NORM':>7}")
|
|
for motor, value in action.items():
|
|
print(f"{motor:<{display_len}} | {value:>7.2f}")
|
|
print(f"\ntime: {loop_s * 1e3:.2f}ms ({1 / loop_s:.0f} Hz)")
|
|
|
|
if duration is not None and time.perf_counter() - start >= duration:
|
|
return
|
|
|
|
move_cursor_up(len(action) + 5)
|
|
|
|
|
|
@draccus.wrap()
|
|
def teleoperate(cfg: TeleoperateConfig):
|
|
init_logging()
|
|
logging.info(pformat(asdict(cfg)))
|
|
if cfg.display_data:
|
|
_init_rerun(session_name="teleoperation")
|
|
|
|
teleop = make_teleoperator_from_config(cfg.teleop)
|
|
robot = make_robot_from_config(cfg.robot)
|
|
|
|
teleop.connect()
|
|
robot.connect()
|
|
|
|
try:
|
|
teleop_loop(teleop, robot, cfg.fps, display_data=cfg.display_data, duration=cfg.teleop_time_s)
|
|
except KeyboardInterrupt:
|
|
pass
|
|
finally:
|
|
if cfg.display_data:
|
|
rr.rerun_shutdown()
|
|
teleop.disconnect()
|
|
robot.disconnect()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
teleoperate()
|
|
|