| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from isaacsim import SimulationApp |
| from isaaclab.app import AppLauncher |
|
|
| import contextlib |
| import os |
| import torch |
| import tqdm |
| from typing import Optional |
|
|
| import tyro |
| from io_utils import VideoWriter |
|
|
| from config.args import Gr00tN1ClosedLoopArguments |
|
|
| args = tyro.cli(Gr00tN1ClosedLoopArguments) |
|
|
| if args.enable_pinocchio: |
| |
| |
| |
| import pinocchio |
|
|
| |
| app_launcher = AppLauncher( |
| headless=args.headless, enable_cameras=True, num_envs=args.num_envs, device=args.simulation_device |
| ) |
| simulation_app = app_launcher.app |
|
|
| import gymnasium as gym |
|
|
| from closed_loop_policy import create_sim_environment |
| from evaluators.gr00t_n1_evaluator import Gr00tN1Evaluator |
| from policies.gr00t_n1_policy import Gr00tN1Policy |
| from robot_joints import JointsAbsPosition |
|
|
| from isaaclab.envs import ManagerBasedRLEnvCfg |
|
|
| import isaaclab_eval_tasks.tasks |
|
|
|
|
| def run_closed_loop_policy( |
| args: Gr00tN1ClosedLoopArguments, |
| simulation_app: SimulationApp, |
| env_cfg: ManagerBasedRLEnvCfg, |
| policy: Gr00tN1Policy, |
| evaluator: Optional[Gr00tN1Evaluator] = None, |
| ): |
| |
| succeess_term = env_cfg.terminations.success |
| |
| env_cfg.terminations = {} |
|
|
| |
| env = gym.make(args.task, cfg=env_cfg).unwrapped |
| |
| env.seed(args.seed) |
|
|
| |
| video_writer = None |
| |
| if args.record_camera: |
| video_count = 0 |
| video_fpath = os.path.join( |
| args.record_video_output_path, f"{args.task_name}_{args.checkpoint_name}_{video_count}.mp4" |
| ) |
| video_writer = VideoWriter( |
| video_fpath, |
| |
| args.original_image_size[:2][::-1], |
| fps=20, |
| ) |
|
|
| with contextlib.suppress(KeyboardInterrupt) and torch.inference_mode(): |
| while simulation_app.is_running() and not simulation_app.is_exiting(): |
|
|
| |
| |
| if evaluator is not None and evaluator.num_rollouts >= args.max_num_rollouts: |
| break |
|
|
| |
| env.sim.reset() |
| env.reset(seed=args.seed) |
|
|
| robot = env.scene["robot"] |
| robot_state_sim = JointsAbsPosition( |
| robot.data.joint_pos, policy.gr1_state_joints_config, args.simulation_device |
| ) |
|
|
| ego_camera = env.scene["robot_pov_cam"] |
|
|
| if args.record_camera and video_writer is not None and os.path.exists(video_fpath): |
| |
| video_fpath = "_".join(video_fpath.split("_")[:-1]) + f"_{video_count}.mp4" |
| video_writer.change_file_path(video_fpath) |
|
|
| for _ in tqdm.tqdm(range(args.rollout_length)): |
| robot_state_sim.set_joints_pos(robot.data.joint_pos) |
|
|
| robot_action_sim = policy.get_new_goal(robot_state_sim, ego_camera, args.language_instruction) |
| rollout_action = robot_action_sim.get_joints_pos(args.simulation_device) |
|
|
| |
| assert rollout_action.shape[-1] == env.action_space.shape[1] |
|
|
| |
| for i in range(args.num_feedback_actions): |
| assert rollout_action[:, i, :].shape[0] == args.num_envs |
| env.step(rollout_action[:, i, :]) |
|
|
| if args.record_camera and video_writer is not None: |
| |
| video_writer.add_image(ego_camera.data.output["rgb"][0]) |
|
|
| if args.record_camera and video_writer is not None: |
| video_count += 1 |
|
|
| |
| if evaluator is not None: |
| evaluator.evaluate_step(env, succeess_term) |
| evaluator.summarize_demos() |
|
|
| |
| if evaluator is not None: |
| evaluator.maybe_write_eval_file() |
| if video_writer is not None: |
| video_writer.close() |
| env.close() |
|
|
|
|
| if __name__ == "__main__": |
| print("args", args) |
|
|
| |
| gr00t_n1_policy = Gr00tN1Policy(args) |
| env_cfg = create_sim_environment(args) |
| evaluator = Gr00tN1Evaluator(args.checkpoint_name, args.eval_file_path, args.seed) |
|
|
| |
| run_closed_loop_policy( |
| args=args, simulation_app=simulation_app, env_cfg=env_cfg, policy=gr00t_n1_policy, evaluator=evaluator |
| ) |
|
|
| |
| simulation_app.close() |
|
|