File size: 6,585 Bytes
1e71a55 | 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 | # Created by skywoodsz on 2026/02/07.
import argparse
import os
import time
import json
from isaaclab.app import AppLauncher
from demo.solution import AlgSolution
solution = AlgSolution()
# -----------------------------------------------------------------------------
# CLI
# -----------------------------------------------------------------------------
parser = argparse.ArgumentParser(description="Play Atec Tasks (ENV only, no RL).")
parser.add_argument("--video", action="store_true", default=False, help="Record videos during play.")
parser.add_argument("--video_length", type=int, default=200, help="Length of the recorded video (in steps).")
parser.add_argument(
"--disable_fabric", action="store_true", default=False, help="Disable fabric and use USD I/O operations."
)
parser.add_argument("--num_envs", type=int, default=1, help="Number of environments to simulate.")
parser.add_argument("--task", type=str, default=None, help="Name of the task.")
parser.add_argument("--real-time", action="store_true", default=False, help="Run in real-time, if possible.")
parser.add_argument(
"--debug",
action="store_true",
default=False,
help="Enable debug prints for per-step reward/time metrics.",
)
# Isaac Sim / Kit args
AppLauncher.add_app_launcher_args(parser)
args_cli = parser.parse_args()
# If recording video, need cameras enabled in IsaacLab/Kit
if args_cli.video:
args_cli.enable_cameras = True
# -----------------------------------------------------------------------------
# Launch Isaac Sim / Kit
# -----------------------------------------------------------------------------
app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app
# -----------------------------------------------------------------------------
# Imports AFTER simulation_app is created (IsaacLab pattern)
# -----------------------------------------------------------------------------
import gymnasium as gym # noqa: E402
import torch # noqa: E402
from isaaclab.envs import DirectMARLEnv, multi_agent_to_single_agent # noqa: E402
from isaaclab.utils.dict import print_dict # noqa: E402
import atec_rl_lab.tasks # noqa: F401, E402 (register your tasks)
from isaaclab_tasks.utils import parse_env_cfg
from rl_utils import camera_follow
def play() -> tuple[float, float]:
if args_cli.task is None:
raise ValueError("Please provide --task, e.g. --task ATEC-TaskA-G1")
is_task_e = isinstance(args_cli.task, str) and args_cli.task.startswith("ATEC-TaskE")
# -------------------------------------------------------------------------
# Create env (plain Gym env)
# -------------------------------------------------------------------------
env_cfg = parse_env_cfg(
args_cli.task,
device=args_cli.device,
num_envs=args_cli.num_envs,
use_fabric=not args_cli.disable_fabric
)
env = gym.make(args_cli.task, cfg=env_cfg, render_mode="rgb_array" if args_cli.video else None)
# Convert MARL -> single agent if needed (kept from your original script)
if isinstance(env.unwrapped, DirectMARLEnv):
env = multi_agent_to_single_agent(env)
# -------------------------------------------------------------------------
# Optional: video wrapper
# -------------------------------------------------------------------------
if args_cli.video:
# Put videos in ./logs/videos/play by default (edit as you like)
video_kwargs = {
"video_folder": os.path.abspath(os.path.join("logs", "videos", args_cli.task, "play")),
"step_trigger": lambda step: step == 0,
"video_length": args_cli.video_length,
"disable_logger": True,
}
print("[INFO] Recording videos during play.")
print_dict(video_kwargs, nesting=4)
env = gym.wrappers.RecordVideo(env, **video_kwargs)
# -------------------------------------------------------------------------
# Reset
# -------------------------------------------------------------------------
obs, _ = env.reset()
dt = env.unwrapped.step_dt if hasattr(env.unwrapped, "step_dt") else None
timestep = 0
# -------------------------------------------------------------------------
# Play loop
# -------------------------------------------------------------------------
total_episode_reward = 0.0
total_elapsed_time = 0.0
while simulation_app.is_running():
with torch.inference_mode():
start_time = time.time()
# ===== Your controller goes here =====
resp = solution.predicts(obs, total_episode_reward)
giveup = resp["giveup"]
if giveup:
break
actions = resp["action"]
actions = torch.tensor(actions, dtype=torch.float32, device='cuda').view(1, -1)
obs, reward, terminated, truncated, info = env.step(actions)
if not is_task_e:
camera_follow(env)
sim_dt = info["Step_dt"]
if isinstance(reward, torch.Tensor):
total_episode_reward += reward.mean().item() / sim_dt
else:
total_episode_reward += float(reward) / sim_dt
if isinstance(info, dict) and "Elapsed_Time" in info:
elapsed = info["Elapsed_Time"] # simulation time from env as primary source
total_elapsed_time = elapsed.item() if hasattr(elapsed, "item") else float(elapsed)
elif dt is not None:
total_elapsed_time += dt # wall clock time as fallback
if args_cli.debug:
print(f"total_episode_reward:{total_episode_reward: .2f}")
print(f"total_elapsed_time:{total_elapsed_time: .2f}")
done = (terminated.item() or truncated.item())
if done:
break
timestep += 1
# If recording one video, exit after video_length steps
if args_cli.video and timestep >= args_cli.video_length:
break
# Real-time pacing
if args_cli.real_time and dt is not None:
sleep_time = dt - (time.time() - start_time)
if sleep_time > 0:
time.sleep(sleep_time)
env.close()
return total_episode_reward, total_elapsed_time
if __name__ == "__main__":
score, elapsed_time = play()
print(f"score: {score:.2f}, elapsed_time: {elapsed_time:.2f} seconds")
# Finally, close the simulation app
print("Closing simulation app...")
simulation_app.close()
|