isaaclab是基于isaacsim的学习算法模块,具备两大特征:1. vectorization: 多环境并行以加速更新权重前的数据收集;2. 模块化设计:项目中各组件可以灵活替换。
关键组件
- Configurations:带@configclass的类定义,用于标识环境中需要被vectorize的部分
- Robots:配置实例,包括文件路径、物理属性、初始态、执行器等
- Apps and sims:使用AppLauncher作为入口点
新建项目
使用./isaaclab.sh --new,之后进行选项的选择。一般选择external(在isaaclab仓库外)、direct(不使用manager)。
总设计思路
传统RL中,环境负责根据actions更新“world”,但isaac中需要自己定义world,world由坐标系原点和距离单位定义。
Simulation和Application是基于world的,其中Application负责所有资源调用以及Simulation的启动和关闭;Simulation控制world的规则,包括时空、物理、运行频率等,把每个step分成各种sub-step,例如physics_step, render_step, XYZ_step等。
Simulation由USD Primitive构成,而Stage承载了所有prim之间的关系,例如/room/table/food。
Scene管理所有vectorization(即并行化)所需的prims,也称为simulation entities。并行训练时,只存在一个world,但存在多个复制的Scene。debug时要小心scene之间的影响。
Classes and Configs
以下为一个project_env_cfg.py案例,用以定义克隆时要关注的。
from isaaclab_assets.robots.cartpole import CARTPOLE_CFG
from isaaclab.assets import ArticulationCfg
from isaaclab.envs import DirectRLEnvCfg
from isaaclab.scene import InteractiveSceneCfg
from isaaclab.sim import SimulationCfg
from isaaclab.utils import configclass
@configclass
class IsaacLabTutorialEnvCfg(DirectRLEnvCfg):
# simulation,可选设置,因为有默认值
sim: SimulationCfg = SimulationCfg(dt=1 / 120, render_interval=2)
# robot(s),prim_path定义为在所有env_0,env_1等环境中的Robot
robot_cfg: ArticulationCfg = CARTPOLE_CFG.replace(prim_path="/World/envs/env_.*/Robot")
# scene,必须设置,无默认值
scene: InteractiveSceneCfg = InteractiveSceneCfg(num_envs=4096, env_spacing=4.0, replicate_physics=True)
Config中三个主要组成分别是simulation,robot和scene。其中robot由于通常有多个,所以一般定义一个regex的路径匹配机器人的位置。
以下为一个相匹配的环境设置project_env.py:
from .isaac_lab_tutorial_env_cfg import IsaacLabTutorialEnvCfg
class IsaacLabTutorialEnv(DirectRLEnv):
cfg: IsaacLabTutorialEnvCfg#初始化时接收其自己的设置
def __init__(self, cfg: IsaacLabTutorialEnvCfg, render_mode: str | None = None, **kwargs):
super().__init__(cfg, render_mode, **kwargs)#此时也调用_setup_scene
. . .
def _setup_scene(self):#构建scene、register robot、克隆
self.robot = Articulation(self.cfg.robot_cfg)#此时robot才得以存在
# add ground plane
spawn_ground_plane(prim_path="/World/ground", cfg=GroundPlaneCfg())
# add articulation to scene,所有Articulation都被保存在一个dict中
self.scene.articulations["robot"] = self.robot
# clone and replicate
self.scene.clone_environments(copy_from_source=False)
# add lights
light_cfg = sim_utils.DomeLightCfg(intensity=2000.0, color=(0.75, 0.75, 0.75))
light_cfg.func("/World/Light", light_cfg)
#设置命令
def _pre_physics_step(self, actions: torch.Tensor) -> None:
. . .
#应用动作
def _apply_action(self) -> None:
. . .
def _get_observations(self) -> dict:
. . .
def _get_rewards(self) -> torch.Tensor:
total_reward = compute_rewards(...)
return total_reward
#判断每个environment是否结束
def _get_dones(self) -> tuple[torch.Tensor, torch.Tensor]:
. . .
#对结束的environment重置
def _reset_idx(self, env_ids: Sequence[int] | None):
. . .
@torch.jit.script
def compute_rewards(...):
. . .
return total_reward
在该文件中包含了最主要的设置,[DirectRLEnv](https://isaac-sim.github.io/IsaacLab/main/source/api/lab/isaaclab.envs.html#isaaclab.envs.DirectRLEnv "isaaclab.envs.DirectRLEnv")接口定义了isaaclab、RL框架和环境的互动。 重要的_setup_scene`见注释。其他方法不涉及任何参数输入,因为environment只负责agent应用action,更新sim。