File size: 24,868 Bytes
1cc903b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 | """Dataset recording utility functions for teleoperation data collection."""
import argparse
import time
import traceback
from pathlib import Path
from typing import Dict, Optional, Tuple, Any, Union
import gymnasium as gym
import numpy as np
import torch
from isaacsim.simulation_app import SimulationApp
from isaaclab.envs import DirectRLEnv
from isaaclab_tasks.utils import parse_env_cfg
from lerobot.datasets.lerobot_dataset import LeRobotDataset
from lehome.devices import (
Se3Keyboard,
SO101Leader,
BiSO101Leader,
BiKeyboard,
)
from lehome.utils.env_utils import dynamic_reset_gripper_effort_limit_sim
from lehome.utils.record import (
get_next_experiment_path_with_gap,
append_episode_initial_pose,
)
from lehome.utils.logger import get_logger
from .common import stabilize_garment_after_reset
logger = get_logger(__name__)
def validate_task_and_device(args: argparse.Namespace) -> None:
"""Validate that task name matches the teleop device configuration.
Args:
args: Command-line arguments containing task and teleop_device.
Raises:
ValueError: If task is not specified.
AssertionError: If task and device configuration mismatch.
"""
if args.task is None:
raise ValueError("Please specify --task.")
if "Bi" in args.task:
assert (
args.teleop_device == "bi-so101leader"
or args.teleop_device == "bi-keyboard"
), "Only support bi-so101leader or bi-keyboard for bi-arm task"
else:
assert (
args.teleop_device == "so101leader" or args.teleop_device == "keyboard"
), "Only support so101leader or keyboard for single-arm task"
def create_teleop_interface(
env: DirectRLEnv, args: argparse.Namespace
) -> Union[Se3Keyboard, SO101Leader, BiSO101Leader, BiKeyboard]:
"""Create teleoperation interface based on device type.
Args:
env: Environment instance.
args: Command-line arguments containing teleop_device and related config.
Returns:
Teleoperation interface instance.
Raises:
ValueError: If teleop_device is invalid.
"""
if args.teleop_device == "keyboard":
return Se3Keyboard(env, sensitivity=0.25 * args.sensitivity)
if args.teleop_device == "so101leader":
return SO101Leader(env, port=args.port, recalibrate=args.recalibrate)
if args.teleop_device == "bi-so101leader":
return BiSO101Leader(
env,
left_port=args.left_arm_port,
right_port=args.right_arm_port,
recalibrate=args.recalibrate,
)
if args.teleop_device == "bi-keyboard":
return BiKeyboard(env, sensitivity=0.25 * args.sensitivity)
raise ValueError(
f"Invalid device interface '{args.teleop_device}'. "
f"Supported: 'keyboard', 'so101leader', 'bi-so101leader', 'bi-keyboard'."
)
def register_teleop_callbacks(
teleop_interface: Any, recording_enabled: bool = False
) -> Dict[str, bool]:
"""Register callback functions for teleoperation control keys.
Key bindings:
S: Start recording
N: Mark current episode as successful (only active during recording)
D: Discard current episode and re-record (only active during recording)
ESC: Abort entire recording process and clear buffer
Args:
teleop_interface: Teleoperation interface instance.
recording_enabled: Whether recording is enabled. If False, N/D keys are
disabled in idle phase.
Returns:
Dictionary of status flags for recording control.
"""
flags = {
"start": False, # S: Start recording
"success": False, # N: Success/early termination of current episode
"remove": False, # D: Discard current episode
"abort": False, # ESC: Abort entire recording process, clear buffer
}
def on_start():
flags["start"] = True
logger.info("[S] Recording started!")
def on_success():
if not recording_enabled or not flags["start"]:
# Ignore N key in idle phase (before recording starts)
logger.debug("[N] Ignored (recording not started yet)")
return
flags["success"] = True
logger.info("[N] Mark the current episode as successful.")
def on_remove():
if not recording_enabled or not flags["start"]:
# Ignore D key in idle phase (before recording starts)
logger.debug("[D] Ignored (recording not started yet)")
return
flags["remove"] = True
logger.info("[D] Discard the current episode and re-record.")
def on_abort():
flags["abort"] = True
logger.warning("[ESC] Abort recording, clearing the current episode buffer...")
teleop_interface.add_callback("S", on_start)
teleop_interface.add_callback("N", on_success)
teleop_interface.add_callback("D", on_remove)
teleop_interface.add_callback("ESCAPE", on_abort)
return flags
def create_dataset_if_needed(
args: argparse.Namespace,
) -> Tuple[Optional[LeRobotDataset], Optional[Path], Optional[Any], bool]:
"""Create LeRobotDataset if recording is enabled.
Args:
args: Command-line arguments containing recording configuration.
Returns:
Tuple of (dataset, json_path, solver, is_bi_arm):
- dataset: LeRobotDataset instance or None if not recording
- json_path: Path to object initial pose JSON file or None
- solver: RobotKinematics solver instance or None
- is_bi_arm: Boolean indicating if dual-arm configuration
Raises:
ValueError: If record_ee_pose is enabled but ee_urdf_path is not provided.
FileNotFoundError: If URDF file is not found.
"""
if not args.enable_record:
return None, None, None, False
action_names = [
"shoulder_pan",
"shoulder_lift",
"elbow_flex",
"wrist_flex",
"wrist_roll",
"gripper",
]
is_bi_arm = ("Bi" in (args.task or "")) or (
getattr(args, "teleop_device", "") or ""
).startswith("bi-")
if is_bi_arm:
left_names = [f"left_{n}" for n in action_names]
right_names = [f"right_{n}" for n in action_names]
joint_names = left_names + right_names
else:
joint_names = action_names
dim = len(joint_names)
features: Dict[str, Dict[str, Any]] = {
"observation.state": {
"dtype": "float32",
"shape": (dim,),
"names": joint_names,
},
"action": {
"dtype": "float32",
"shape": (dim,),
"names": joint_names,
},
}
if not getattr(args, "disable_depth", False):
features["observation.top_depth"] = {
"dtype": "uint16",
"shape": (480, 640),
"names": ["height", "width"],
"info": {
"unit": "millimeters",
"range_mm": [0, 65535],
"range_m": [0.0, 65.535],
"precision_mm": 1,
"conversion": "depth_meters = uint16_value / 1000.0"
}
}
if is_bi_arm:
image_keys = ["top_rgb", "left_rgb", "right_rgb"]
else:
image_keys = ["top_rgb", "wrist_rgb"]
for key in image_keys:
features[f"observation.images.{key}"] = {
"dtype": "video",
"shape": (480, 640, 3),
"names": ["height", "width", "channels"],
}
if getattr(args, "record_ee_pose", False):
if is_bi_arm:
ee_pose_dim = 16
ee_pose_names = [
"left_x",
"left_y",
"left_z",
"left_qx",
"left_qy",
"left_qz",
"left_qw",
"left_gripper",
"right_x",
"right_y",
"right_z",
"right_qx",
"right_qy",
"right_qz",
"right_qw",
"right_gripper",
]
else:
ee_pose_dim = 8
ee_pose_names = ["x", "y", "z", "qx", "qy", "qz", "qw", "gripper"]
features["observation.ee_pose"] = {
"dtype": "float32",
"shape": (ee_pose_dim,),
"names": ee_pose_names,
}
features["action.ee_pose"] = {
"dtype": "float32",
"shape": (ee_pose_dim,),
"names": ee_pose_names,
}
root_path = Path(getattr(args, "dataset_root", "Datasets/record"))
dataset = LeRobotDataset.create(
repo_id="abc",
fps=30,
root=get_next_experiment_path_with_gap(root_path),
use_videos=True,
image_writer_threads=8,
image_writer_processes=0,
features=features,
)
json_path = dataset.root / "meta" / "garment_info.json"
solver = None
if getattr(args, "record_ee_pose", False):
if not args.ee_urdf_path:
raise ValueError("--record_ee_pose requires --ee_urdf_path")
urdf_path = Path(args.ee_urdf_path)
if not urdf_path.exists():
raise FileNotFoundError(f"URDF not found: {urdf_path}")
from lehome.utils import RobotKinematics
if is_bi_arm:
solver_joint_names = [n.replace("left_", "") for n in joint_names[:5]]
else:
solver_joint_names = joint_names[:5]
solver = RobotKinematics(
str(urdf_path),
target_frame_name="gripper_frame_link",
joint_names=solver_joint_names,
)
arm_type = "dual-arm" if is_bi_arm else "single-arm"
logger.info(f"End-effector pose solver loaded ({arm_type})")
return dataset, json_path, solver, is_bi_arm
def run_idle_phase(
env: DirectRLEnv,
teleop_interface: Any,
args: argparse.Namespace,
count_render: int,
) -> Tuple[Optional[Dict[str, Any]], int]:
"""Run idle phase before recording starts.
Handles environment preparation, stabilization, and waits for user to press
S key to start recording.
Args:
env: Environment instance.
teleop_interface: Teleoperation interface.
args: Command-line arguments.
count_render: Current render count.
Returns:
Tuple of (object_initial_pose, updated_count_render).
"""
dynamic_reset_gripper_effort_limit_sim(env, args.teleop_device)
actions = teleop_interface.advance()
object_initial_pose = None
if count_render == 0:
logger.info("[Idle Phase] Initializing observations...")
env.initialize_obs()
count_render += 1
logger.info("[Idle Phase] Stabilizing garment after initialization...")
stabilize_garment_after_reset(env, args)
logger.info("[Idle Phase] Ready for recording")
if actions is None:
current_obs = env._get_observations()
if "observation.state" in current_obs:
current_state = current_obs["observation.state"]
if isinstance(current_state, np.ndarray):
maintain_action = (
torch.from_numpy(current_state).float().unsqueeze(0).to(env.device)
)
else:
maintain_action = torch.zeros(
1, len(current_state), dtype=torch.float32, device=env.device
)
else:
action_dim = 12 if "Bi" in args.task else 6
maintain_action = torch.zeros(
1, action_dim, dtype=torch.float32, device=env.device
)
env.step(maintain_action)
env.render()
else:
env.step(actions)
object_initial_pose = env.get_all_pose()
if object_initial_pose is None:
object_initial_pose = env.get_all_pose()
return object_initial_pose, count_render
def run_recording_phase(
env: DirectRLEnv,
teleop_interface: Any,
args: argparse.Namespace,
flags: Dict[str, bool],
dataset: LeRobotDataset,
json_path: Path,
initial_object_pose: Optional[Dict[str, Any]],
ee_solver: Optional[Any] = None,
is_bi_arm: bool = False,
) -> Dict[str, Any]:
"""Run recording phase after S key is pressed and recording is enabled.
Records episodes until num_episode is reached. Each episode can be marked as
successful (N key), discarded (D key), or aborted (ESC key).
Args:
env: Environment instance.
teleop_interface: Teleoperation interface.
args: Command-line arguments.
flags: Status flags dictionary.
dataset: LeRobotDataset instance.
json_path: Path to object initial pose JSON file.
initial_object_pose: Initial object pose dictionary.
ee_solver: Optional kinematic solver for end-effector pose computation.
is_bi_arm: Whether using dual-arm configuration.
Returns:
Final object initial pose dictionary.
"""
episode_index = 0
object_initial_pose = initial_object_pose
# Ensure we have a valid initial pose for the first episode
if object_initial_pose is None:
object_initial_pose = env.get_all_pose()
while episode_index < args.num_episode:
# Check if recording should be aborted
if flags["abort"]:
dataset.clear_episode_buffer()
dataset.finalize()
logger.warning(f"Recording aborted, completed {episode_index} episodes")
return object_initial_pose
flags["success"] = False
flags["remove"] = False
# Loop within a single episode
while not flags["success"]:
# Check if recording should be aborted
if flags["abort"]:
dataset.clear_episode_buffer()
dataset.finalize()
logger.warning(f"Recording aborted, completed {episode_index} episodes")
return object_initial_pose
try:
dynamic_reset_gripper_effort_limit_sim(env, args.teleop_device)
actions = teleop_interface.advance()
except Exception as e:
logger.error(f"[Recording] Error in teleop interface: {e}")
traceback.print_exc()
actions = None
if actions is None:
env.render()
else:
env.step(actions)
if args.log_success:
success = env._get_success()
observations = env._get_observations()
if (
getattr(args, "disable_depth", False)
and "observation.top_depth" in observations
):
observations.pop("observation.top_depth")
if getattr(args, "enable_pointcloud", False):
# Converting pointcloud online is time-consuming, please convert offline
# pointcloud = env._get_workspace_pointcloud(
# num_points=4096, use_fps=True
# )
print("Converting pointcloud online is time-consuming, please convert offline")
_, truncated = env._get_dones()
frame = {**observations, "task": args.task_description}
if (
ee_solver is not None
and "observation.state" in observations
and "action" in observations
):
from lehome.utils import compute_ee_pose_single_arm
obs_state = np.array(
observations["observation.state"], dtype=np.float32
)
action_state = np.array(observations["action"], dtype=np.float32)
if is_bi_arm:
obs_left = compute_ee_pose_single_arm(
ee_solver, obs_state[:6], args.ee_state_unit
)
obs_right = compute_ee_pose_single_arm(
ee_solver, obs_state[6:12], args.ee_state_unit
)
frame["observation.ee_pose"] = np.concatenate(
[obs_left, obs_right], axis=0
)
act_left = compute_ee_pose_single_arm(
ee_solver, action_state[:6], args.ee_state_unit
)
act_right = compute_ee_pose_single_arm(
ee_solver, action_state[6:12], args.ee_state_unit
)
frame["action.ee_pose"] = np.concatenate(
[act_left, act_right], axis=0
)
else:
frame["observation.ee_pose"] = compute_ee_pose_single_arm(
ee_solver, obs_state, args.ee_state_unit
)
frame["action.ee_pose"] = compute_ee_pose_single_arm(
ee_solver, action_state, args.ee_state_unit
)
dataset.add_frame(frame)
if truncated or flags["remove"]:
dataset.clear_episode_buffer()
logger.info(f"Re-recording episode {episode_index}")
try:
env.reset()
stabilize_garment_after_reset(env, args)
object_initial_pose = env.get_all_pose()
except Exception as e:
logger.error(
f"[Recording] Failed to reset environment during re-recording: {e}"
)
traceback.print_exc()
try:
object_initial_pose = env.get_all_pose()
except Exception:
object_initial_pose = None
flags["remove"] = False
continue
save_start_time = time.time()
logger.info(f"[Recording] Saving episode {episode_index}...")
try:
dataset.save_episode()
save_duration = time.time() - save_start_time
logger.info(
f"[Recording] Episode {episode_index} saved (took {save_duration:.1f}s)"
)
except Exception as e:
logger.error(f"[Recording] Failed to save episode {episode_index}: {e}")
traceback.print_exc()
garment_name = None
if hasattr(env, "cfg") and hasattr(env.cfg, "garment_name"):
garment_name = env.cfg.garment_name
scale = None
if hasattr(env, "object") and hasattr(env.object, "init_scale"):
try:
scale = env.object.init_scale
except Exception:
logger.warning("Failed to get scale from garment object")
try:
append_episode_initial_pose(
json_path,
episode_index,
object_initial_pose,
garment_name=garment_name,
scale=scale,
)
except Exception as e:
logger.error(
f"[Recording] Failed to save episode metadata for episode {episode_index}: {e}"
)
traceback.print_exc()
episode_index += 1
logger.info(
f"Episode {episode_index - 1} completed, progress: {episode_index}/{args.num_episode}"
)
try:
env.reset()
stabilize_garment_after_reset(env, args)
except Exception as e:
logger.error(f"[Recording] Failed to reset environment: {e}")
traceback.print_exc()
try:
object_initial_pose = env.get_all_pose()
except Exception as e:
logger.error(f"[Recording] Failed to get initial pose: {e}")
traceback.print_exc()
object_initial_pose = None
dataset.clear_episode_buffer()
dataset.finalize()
logger.info(f"All {args.num_episode} episodes recording completed!")
return object_initial_pose
def run_live_control_without_record(
env: DirectRLEnv,
teleop_interface: Any,
args: argparse.Namespace,
) -> None:
"""Run live teleoperation control without recording.
Handles the case when S key is pressed but recording is not enabled.
Performs simple teleoperation control without writing to dataset.
Args:
env: Environment instance.
teleop_interface: Teleoperation interface.
args: Command-line arguments.
"""
dynamic_reset_gripper_effort_limit_sim(env, args.teleop_device)
actions = teleop_interface.advance()
if actions is None:
current_obs = env._get_observations()
if "observation.state" in current_obs:
current_state = current_obs["observation.state"]
if isinstance(current_state, np.ndarray):
maintain_action = (
torch.from_numpy(current_state).float().unsqueeze(0).to(env.device)
)
else:
maintain_action = torch.zeros(
1, len(current_state), dtype=torch.float32, device=env.device
)
else:
action_dim = 12 if "Bi" in args.task else 6
maintain_action = torch.zeros(
1, action_dim, dtype=torch.float32, device=env.device
)
env.step(maintain_action)
env.render()
else:
env.step(actions)
if args.log_success:
_ = env._get_success()
def record_dataset(args: argparse.Namespace, simulation_app: SimulationApp) -> None:
"""Record dataset."""
# Get device configuration (default to "cpu" for compatibility)
device = getattr(args, "device", "cpu")
env_cfg = parse_env_cfg(
args.task,
device=device,
)
task_name = args.task
env_cfg.garment_name = args.garment_name
env_cfg.garment_version = args.garment_version
env_cfg.garment_cfg_base_path = args.garment_cfg_base_path
env_cfg.particle_cfg_path = args.particle_cfg_path
if args.use_random_seed:
env_cfg.use_random_seed = True
logger.info("Using random seed (no fixed seed)")
else:
env_cfg.use_random_seed = False
env_cfg.random_seed = args.seed
logger.info(f"Using fixed random seed: {args.seed}")
env: DirectRLEnv = gym.make(task_name, cfg=env_cfg).unwrapped
teleop_interface = create_teleop_interface(env, args)
flags = register_teleop_callbacks(
teleop_interface, recording_enabled=args.enable_record
)
teleop_interface.reset()
dataset, json_path, ee_solver, is_bi_arm = create_dataset_if_needed(args)
count_render = 0
printed_instructions = False
idle_frame_counter = 0
object_initial_pose: Optional[Dict[str, Any]] = None
try:
while simulation_app.is_running():
with torch.inference_mode():
if not flags["start"]:
pose, count_render = run_idle_phase(
env,
teleop_interface,
args,
count_render,
)
if pose is not None:
object_initial_pose = pose
if count_render > 0:
idle_frame_counter += 1
if idle_frame_counter == 100 and not printed_instructions:
logger.info("=" * 60)
logger.info("🎮 CONTROL INSTRUCTIONS 🎮")
logger.info("=" * 60)
logger.info(str(teleop_interface))
logger.info("=" * 60 + "\n\n")
printed_instructions = True
elif args.enable_record and dataset is not None:
object_initial_pose = run_recording_phase(
env,
teleop_interface,
args,
flags,
dataset,
json_path,
object_initial_pose,
ee_solver,
is_bi_arm,
)
break
else:
run_live_control_without_record(env, teleop_interface, args)
except KeyboardInterrupt:
logger.warning("\n[Ctrl+C] Interrupt signal detected")
# If Ctrl+C is pressed during recording, clear the current buffer
if args.enable_record and dataset is not None and flags["start"]:
logger.info("Clearing current episode buffer...")
dataset.clear_episode_buffer()
logger.info("Buffer cleared, dataset remains intact")
dataset.finalize()
logger.info("Dataset saved")
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
finally:
env.close()
|