| |
| |
| |
| |
|
|
| """ |
| This script demonstrates how to create a simple environment with a cartpole. It combines the concepts of |
| scene, action, observation and event managers to create an environment. |
| |
| .. code-block:: bash |
| |
| ./isaaclab.sh -p scripts/tutorials/03_envs/create_cartpole_base_env.py --num_envs 32 |
| |
| """ |
|
|
| """Launch Isaac Sim Simulator first.""" |
|
|
|
|
| import argparse |
|
|
| from isaaclab.app import AppLauncher |
|
|
| |
| parser = argparse.ArgumentParser(description="Tutorial on creating a cartpole base environment.") |
| parser.add_argument("--num_envs", type=int, default=16, help="Number of environments to spawn.") |
|
|
| |
| AppLauncher.add_app_launcher_args(parser) |
| |
| args_cli = parser.parse_args() |
|
|
| |
| app_launcher = AppLauncher(args_cli) |
| simulation_app = app_launcher.app |
|
|
| """Rest everything follows.""" |
|
|
| import math |
|
|
| import torch |
|
|
| import isaaclab.envs.mdp as mdp |
| from isaaclab.envs import ManagerBasedEnv, ManagerBasedEnvCfg |
| from isaaclab.managers import EventTermCfg as EventTerm |
| from isaaclab.managers import ObservationGroupCfg as ObsGroup |
| from isaaclab.managers import ObservationTermCfg as ObsTerm |
| from isaaclab.managers import SceneEntityCfg |
| from isaaclab.utils import configclass |
|
|
| from isaaclab_tasks.manager_based.classic.cartpole.cartpole_env_cfg import CartpoleSceneCfg |
|
|
|
|
| @configclass |
| class ActionsCfg: |
| """Action specifications for the environment.""" |
|
|
| joint_efforts = mdp.JointEffortActionCfg(asset_name="robot", joint_names=["slider_to_cart"], scale=5.0) |
|
|
|
|
| @configclass |
| class ObservationsCfg: |
| """Observation specifications for the environment.""" |
|
|
| @configclass |
| class PolicyCfg(ObsGroup): |
| """Observations for policy group.""" |
|
|
| |
| joint_pos_rel = ObsTerm(func=mdp.joint_pos_rel) |
| joint_vel_rel = ObsTerm(func=mdp.joint_vel_rel) |
|
|
| def __post_init__(self) -> None: |
| self.enable_corruption = False |
| self.concatenate_terms = True |
|
|
| |
| policy: PolicyCfg = PolicyCfg() |
|
|
|
|
| @configclass |
| class EventCfg: |
| """Configuration for events.""" |
|
|
| |
| add_pole_mass = EventTerm( |
| func=mdp.randomize_rigid_body_mass, |
| mode="startup", |
| params={ |
| "asset_cfg": SceneEntityCfg("robot", body_names=["pole"]), |
| "mass_distribution_params": (0.1, 0.5), |
| "operation": "add", |
| }, |
| ) |
|
|
| |
| reset_cart_position = EventTerm( |
| func=mdp.reset_joints_by_offset, |
| mode="reset", |
| params={ |
| "asset_cfg": SceneEntityCfg("robot", joint_names=["slider_to_cart"]), |
| "position_range": (-1.0, 1.0), |
| "velocity_range": (-0.1, 0.1), |
| }, |
| ) |
|
|
| reset_pole_position = EventTerm( |
| func=mdp.reset_joints_by_offset, |
| mode="reset", |
| params={ |
| "asset_cfg": SceneEntityCfg("robot", joint_names=["cart_to_pole"]), |
| "position_range": (-0.125 * math.pi, 0.125 * math.pi), |
| "velocity_range": (-0.01 * math.pi, 0.01 * math.pi), |
| }, |
| ) |
|
|
|
|
| @configclass |
| class CartpoleEnvCfg(ManagerBasedEnvCfg): |
| """Configuration for the cartpole environment.""" |
|
|
| |
| scene = CartpoleSceneCfg(num_envs=1024, env_spacing=2.5) |
| |
| observations = ObservationsCfg() |
| actions = ActionsCfg() |
| events = EventCfg() |
|
|
| def __post_init__(self): |
| """Post initialization.""" |
| |
| self.viewer.eye = [4.5, 0.0, 6.0] |
| self.viewer.lookat = [0.0, 0.0, 2.0] |
| |
| self.decimation = 4 |
| |
| self.sim.dt = 0.005 |
|
|
|
|
| def main(): |
| """Main function.""" |
| |
| env_cfg = CartpoleEnvCfg() |
| env_cfg.scene.num_envs = args_cli.num_envs |
| env_cfg.sim.device = args_cli.device |
| |
| env = ManagerBasedEnv(cfg=env_cfg) |
|
|
| |
| count = 0 |
| while simulation_app.is_running(): |
| with torch.inference_mode(): |
| |
| if count % 300 == 0: |
| count = 0 |
| env.reset() |
| print("-" * 80) |
| print("[INFO]: Resetting environment...") |
| |
| joint_efforts = torch.randn_like(env.action_manager.action) |
| |
| obs, _ = env.step(joint_efforts) |
| |
| print("[Env 0]: Pole joint: ", obs["policy"][0][1].item()) |
| |
| count += 1 |
|
|
| |
| env.close() |
|
|
|
|
| if __name__ == "__main__": |
| |
| main() |
| |
| simulation_app.close() |
|
|