| | |
| | |
| | |
| | |
| |
|
| | """ |
| | This script demonstrates the environment for a quadruped robot with height-scan sensor. |
| | |
| | In this example, we use a locomotion policy to control the robot. The robot is commanded to |
| | move forward at a constant velocity. The height-scan sensor is used to detect the height of |
| | the terrain. |
| | |
| | .. code-block:: bash |
| | |
| | # Run the script |
| | ./isaaclab.sh -p scripts/tutorials/03_envs/create_quadruped_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 quadruped base environment.") |
| | parser.add_argument("--num_envs", type=int, default=64, 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 torch |
| |
|
| | import isaaclab.envs.mdp as mdp |
| | import isaaclab.sim as sim_utils |
| | from isaaclab.assets import ArticulationCfg, AssetBaseCfg |
| | 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.scene import InteractiveSceneCfg |
| | from isaaclab.sensors import RayCasterCfg, patterns |
| | from isaaclab.terrains import TerrainImporterCfg |
| | from isaaclab.utils import configclass |
| | from isaaclab.utils.assets import ISAACLAB_NUCLEUS_DIR, check_file_path, read_file |
| | from isaaclab.utils.noise import AdditiveUniformNoiseCfg as Unoise |
| |
|
| | |
| | |
| | |
| | from isaaclab.terrains.config.rough import ROUGH_TERRAINS_CFG |
| | from isaaclab_assets.robots.anymal import ANYMAL_C_CFG |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | def constant_commands(env: ManagerBasedEnv) -> torch.Tensor: |
| | """The generated command from the command generator.""" |
| | return torch.tensor([[1, 0, 0]], device=env.device).repeat(env.num_envs, 1) |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | @configclass |
| | class MySceneCfg(InteractiveSceneCfg): |
| | """Example scene configuration.""" |
| |
|
| | |
| | terrain = TerrainImporterCfg( |
| | prim_path="/World/ground", |
| | terrain_type="generator", |
| | terrain_generator=ROUGH_TERRAINS_CFG, |
| | max_init_terrain_level=5, |
| | collision_group=-1, |
| | physics_material=sim_utils.RigidBodyMaterialCfg( |
| | friction_combine_mode="multiply", |
| | restitution_combine_mode="multiply", |
| | static_friction=1.0, |
| | dynamic_friction=1.0, |
| | ), |
| | debug_vis=False, |
| | ) |
| |
|
| | |
| | robot: ArticulationCfg = ANYMAL_C_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot") |
| |
|
| | |
| | height_scanner = RayCasterCfg( |
| | prim_path="{ENV_REGEX_NS}/Robot/base", |
| | offset=RayCasterCfg.OffsetCfg(pos=(0.0, 0.0, 20.0)), |
| | ray_alignment="yaw", |
| | pattern_cfg=patterns.GridPatternCfg(resolution=0.1, size=[1.6, 1.0]), |
| | debug_vis=True, |
| | mesh_prim_paths=["/World/ground"], |
| | ) |
| |
|
| | |
| | light = AssetBaseCfg( |
| | prim_path="/World/light", |
| | spawn=sim_utils.DistantLightCfg(color=(0.75, 0.75, 0.75), intensity=3000.0), |
| | ) |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | @configclass |
| | class ActionsCfg: |
| | """Action specifications for the MDP.""" |
| |
|
| | joint_pos = mdp.JointPositionActionCfg(asset_name="robot", joint_names=[".*"], scale=0.5, use_default_offset=True) |
| |
|
| |
|
| | @configclass |
| | class ObservationsCfg: |
| | """Observation specifications for the MDP.""" |
| |
|
| | @configclass |
| | class PolicyCfg(ObsGroup): |
| | """Observations for policy group.""" |
| |
|
| | |
| | base_lin_vel = ObsTerm(func=mdp.base_lin_vel, noise=Unoise(n_min=-0.1, n_max=0.1)) |
| | base_ang_vel = ObsTerm(func=mdp.base_ang_vel, noise=Unoise(n_min=-0.2, n_max=0.2)) |
| | projected_gravity = ObsTerm( |
| | func=mdp.projected_gravity, |
| | noise=Unoise(n_min=-0.05, n_max=0.05), |
| | ) |
| | velocity_commands = ObsTerm(func=constant_commands) |
| | joint_pos = ObsTerm(func=mdp.joint_pos_rel, noise=Unoise(n_min=-0.01, n_max=0.01)) |
| | joint_vel = ObsTerm(func=mdp.joint_vel_rel, noise=Unoise(n_min=-1.5, n_max=1.5)) |
| | actions = ObsTerm(func=mdp.last_action) |
| | height_scan = ObsTerm( |
| | func=mdp.height_scan, |
| | params={"sensor_cfg": SceneEntityCfg("height_scanner")}, |
| | noise=Unoise(n_min=-0.1, n_max=0.1), |
| | clip=(-1.0, 1.0), |
| | ) |
| |
|
| | def __post_init__(self): |
| | self.enable_corruption = True |
| | self.concatenate_terms = True |
| |
|
| | |
| | policy: PolicyCfg = PolicyCfg() |
| |
|
| |
|
| | @configclass |
| | class EventCfg: |
| | """Configuration for events.""" |
| |
|
| | reset_scene = EventTerm(func=mdp.reset_scene_to_default, mode="reset") |
| |
|
| |
|
| | |
| | |
| | |
| |
|
| |
|
| | @configclass |
| | class QuadrupedEnvCfg(ManagerBasedEnvCfg): |
| | """Configuration for the locomotion velocity-tracking environment.""" |
| |
|
| | |
| | scene: MySceneCfg = MySceneCfg(num_envs=args_cli.num_envs, env_spacing=2.5) |
| | |
| | observations: ObservationsCfg = ObservationsCfg() |
| | actions: ActionsCfg = ActionsCfg() |
| | events: EventCfg = EventCfg() |
| |
|
| | def __post_init__(self): |
| | """Post initialization.""" |
| | |
| | self.decimation = 4 |
| | |
| | self.sim.dt = 0.005 |
| | self.sim.physics_material = self.scene.terrain.physics_material |
| | self.sim.device = args_cli.device |
| | |
| | |
| | if self.scene.height_scanner is not None: |
| | self.scene.height_scanner.update_period = self.decimation * self.sim.dt |
| |
|
| |
|
| | def main(): |
| | """Main function.""" |
| | |
| | env_cfg = QuadrupedEnvCfg() |
| | env = ManagerBasedEnv(cfg=env_cfg) |
| |
|
| | |
| | policy_path = ISAACLAB_NUCLEUS_DIR + "/Policies/ANYmal-C/HeightScan/policy.pt" |
| | |
| | if not check_file_path(policy_path): |
| | raise FileNotFoundError(f"Policy file '{policy_path}' does not exist.") |
| | file_bytes = read_file(policy_path) |
| | |
| | policy = torch.jit.load(file_bytes).to(env.device).eval() |
| |
|
| | |
| | count = 0 |
| | obs, _ = env.reset() |
| | while simulation_app.is_running(): |
| | with torch.inference_mode(): |
| | |
| | if count % 1000 == 0: |
| | obs, _ = env.reset() |
| | count = 0 |
| | print("-" * 80) |
| | print("[INFO]: Resetting environment...") |
| | |
| | action = policy(obs["policy"]) |
| | |
| | obs, _ = env.step(action) |
| | |
| | count += 1 |
| |
|
| | |
| | env.close() |
| |
|
| |
|
| | if __name__ == "__main__": |
| | |
| | main() |
| | |
| | simulation_app.close() |
| |
|