IsaacLab学习记录(三)

三、地形

1、基本地形结构

凸起/下凹的box:

def jump_plat_terrain(terrain, height, platform_size=1.): height = int(height / terrain.vertical_scale) platform_size = int(platform_size / terrain.horizontal_scale / 2) x1 = terrain.length // 2 - platform_size x2 = terrain.length // 2 + platform_size y1 = terrain.width // 2 - platform_size y2 = terrain.width // 2 + platform_size terrain.height_field_raw[x1:x2, y1:y2] = height//凸起 terrain.height_field_raw[x1:x2, y1:y2] = -height//下凹

定义:self.height_field_raw = np.zeros((self.tot_rows , self.tot_cols), dtype=np.int16)

示意:terrain.height_field_raw[x1:x2, y1:y2] = height 的意思就是在 x1 到 x2,y1 到 y2 区域创建高度为height的地形

阶梯:

def stairs_terrain(terrain, step_width, step_height): step_width = int(step_width / terrain.horizontal_scale) step_height = int(step_height / terrain.vertical_scale) num_steps = terrain.width // step_width height = step_height for i in range(num_steps): terrain.height_field_raw[i * step_width: (i + 1) * step_width, :] += height height += step_height return terrain

金字塔阶梯:

def pyramid_stairs_terrain(terrain, step_width, step_height, platform_size=1.): step_width = int(step_width / terrain.horizontal_scale) step_height = int(step_height / terrain.vertical_scale) platform_size = int(platform_size / terrain.horizontal_scale) height = 0 start_x = 0 stop_x = terrain.width start_y = 0 stop_y = terrain.length while (stop_x - start_x) > platform_size and (stop_y - start_y) > platform_size: start_x += step_width stop_x -= step_width start_y += step_width stop_y -= step_width height += step_height terrain.height_field_raw[start_x: stop_x, start_y: stop_y] = height return terrain

斜坡:

def big_slope(terrain, slope=1): x = np.arange(0, terrain.width) y = np.arange(0, terrain.length) xx, yy = np.meshgrid(x, y, sparse=True) xx = xx.reshape(terrain.width, 1) max_height = int(slope * (terrain.horizontal_scale / terrain.vertical_scale) * terrain.width / 10) terrain.height_field_raw[:, np.arange(terrain.length)] += (max_height * xx / terrain.width).astype(terrain.height_field_raw.dtype) return terrain

金字塔斜坡:

def pyramid_sloped_terrain(terrain, slope=1, platform_size=1.): x = np.arange(0, terrain.width) y = np.arange(0, terrain.length) center_x = int(terrain.width / 2) center_y = int(terrain.length / 2) xx, yy = np.meshgrid(x, y, sparse=True) xx = (center_x - np.abs(center_x-xx)) / center_x yy = (center_y - np.abs(center_y-yy)) / center_y xx = xx.reshape(terrain.width, 1) yy = yy.reshape(1, terrain.length) max_height = int(slope * (terrain.horizontal_scale / terrain.vertical_scale) * (terrain.width / 2)) terrain.height_field_raw += (max_height * xx * yy).astype(terrain.height_field_raw.dtype) platform_size = int(platform_size / terrain.horizontal_scale / 2) x1 = terrain.width // 2 - platform_size x2 = terrain.width // 2 + platform_size y1 = terrain.length // 2 - platform_size y2 = terrain.length // 2 + platform_size min_h = min(terrain.height_field_raw[x1, y1], 0) max_h = max(terrain.height_field_raw[x1, y1], 0) terrain.height_field_raw = np.clip(terrain.height_field_raw, min_h, max_h) return terrain

*具体含义可以让ai解释

2、创建流程

入口函数:create_sim 位于 legged_robot.py 中,在仿真初始化后、机器人环境生成前调用。

关键逻辑:判断 mesh_type,根据配置选择不同地形类型(plane, heightfield, trimesh)。

常用为 trimesh,这会调用 Terrain 类并执行 _create_trimesh 函数,完成地形物理参数设置。

Terrain 类中支持三种模式:课程 (curriculum)选定 (selected)随机(randomized)

调用方式:

terrain = self.make_terrain(choice, difficulty) self.add_terrain_to_map(terrain, i, j)

创建流程:

初始化 SubTerrain;根据 difficulty 设置参数;修改 height_field_raw 实现地形构造

def create_sim(self): self.up_axis_idx = 2 # 2 for z, 1 for y -> adapt gravity accordingly self.sim = self.gym.create_sim(self.sim_device_id, self.graphics_device_id, self.physics_engine, self.sim_params) mesh_type = self.cfg.terrain.mesh_type if mesh_type in ['heightfield', 'trimesh']: self.terrain = Terrain(self.cfg.terrain, self.num_envs) if mesh_type=='plane': self._create_ground_plane() elif mesh_type=='heightfield': self._create_heightfield() elif mesh_type=='trimesh': self._create_trimesh() elif mesh_type is not None: raise ValueError("Terrain mesh type not recognised. Allowed types are [None, plane, heightfield, trimesh]") self._create_envs()

3、自定义地形

1、IsaacLab/source/isaaclab/isaaclab/terrains/trimesh/mesh_terrains.py定义波浪地形函数:def wavy_terrain

2、mesh_terrains_cfg.py配置其参数:class MeshWavyTerrainCfg(SubTerrainBaseCfg)

3、init.py导入波浪地形:from .mesh_terrains_cfg import (MeshWavyTerrainCfg,

4、IsaacLab/source/isaaclab/isaaclab/terrains/config/YOUR中引入"wavy": terrain_gen.MeshWavyTerrainCfg

5、source/isaaclab_tasks/isaaclab_tasks/manager_based/locomotion/velocity/config/robot/your_env_cfg.py

重写MySceneCfg类为:terrain_generator=YOUR_TERRAINS_CFG

在同文件中重写++RobotYOUREnvCfg++(LocomotionVelocityRoughEnvCfg)类为:scene: MySceneCfg = MySceneCfg(num_envs=4096, env_spacing=2.5)#替换为你的地形配置

6、agents/rsl_rl_ppo_cfg中配置迭代次数等:class ++RobotYOURPPORunnerCfg++(RslRlOnPolicyRunnerCfg):

7、robot/init.py中注册新配置,如下:

gym.register( id="Isaac-Velocity-YOUR-robot-v0", entry_point="isaaclab.envs:ManagerBasedRLEnv", disable_env_checker=True, kwargs={ "env_cfg_entry_point": f"{__name__}.your_env_cfg:++RobotYOUREnvCfg++ ", "rsl_rl_cfg_entry_point": f"{agents.__name__}.rsl_rl_ppo_cfg:++RobotYOURPPORunnerCfg++", "skrl_cfg_entry_point": f"{agents.__name__}:skrl_rough_ppo_cfg.yaml", }, )

8、随后即可开始训练:./isaaclab.sh -p scripts/reinforcement_learning/rsl_rl/train.py --task=Isaac-Velocity-YOUR-robot-v0 --headless