|
| 1 | +# Copyright (c) 2022-2025, The Isaac Lab Project Developers. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +"""Script to an environment with random action agent.""" |
| 7 | + |
| 8 | +"""Launch Isaac Sim Simulator first.""" |
| 9 | + |
| 10 | +import argparse |
| 11 | + |
| 12 | +from isaaclab.app import AppLauncher |
| 13 | + |
| 14 | +# add argparse arguments |
| 15 | +parser = argparse.ArgumentParser(description="Random agent for Isaac Lab environments.") |
| 16 | +parser.add_argument( |
| 17 | + "--disable_fabric", action="store_true", default=False, help="Disable fabric and use USD I/O operations." |
| 18 | +) |
| 19 | +parser.add_argument("--num_envs", type=int, default=None, help="Number of environments to simulate.") |
| 20 | +parser.add_argument("--task", type=str, default=None, help="Name of the task.") |
| 21 | +# append AppLauncher cli args |
| 22 | +AppLauncher.add_app_launcher_args(parser) |
| 23 | +# parse the arguments |
| 24 | +args_cli = parser.parse_args() |
| 25 | + |
| 26 | +# launch omniverse app |
| 27 | +app_launcher = AppLauncher(args_cli) |
| 28 | +simulation_app = app_launcher.app |
| 29 | + |
| 30 | +"""Rest everything follows.""" |
| 31 | + |
| 32 | +import gymnasium as gym |
| 33 | +import torch |
| 34 | + |
| 35 | +import isaaclab_tasks # noqa: F401 |
| 36 | +from isaaclab_tasks.utils import parse_env_cfg |
| 37 | + |
| 38 | + |
| 39 | +def main(): |
| 40 | + """Random actions agent with Isaac Lab environment.""" |
| 41 | + # create environment configuration |
| 42 | + env_cfg = parse_env_cfg( |
| 43 | + args_cli.task, device=args_cli.device, num_envs=args_cli.num_envs, use_fabric=not args_cli.disable_fabric |
| 44 | + ) |
| 45 | + # create environment |
| 46 | + env = gym.make(args_cli.task, cfg=env_cfg) |
| 47 | + |
| 48 | + # print info (this is vectorized environment) |
| 49 | + print(f"[INFO]: Gym observation space: {env.observation_space}") |
| 50 | + print(f"[INFO]: Gym action space: {env.action_space}") |
| 51 | + # reset environment |
| 52 | + env.reset() |
| 53 | + # simulate environment |
| 54 | + while simulation_app.is_running(): |
| 55 | + # run everything in inference mode |
| 56 | + with torch.inference_mode(): |
| 57 | + # sample actions from -1 to 1 |
| 58 | + actions = 2 * torch.rand(env.action_space.shape, device=env.unwrapped.device) - 1 |
| 59 | + # apply actions |
| 60 | + env.step(actions) |
| 61 | + |
| 62 | + # close the simulator |
| 63 | + env.close() |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + # run the main function |
| 68 | + main() |
| 69 | + # close sim app |
| 70 | + simulation_app.close() |
0 commit comments