如是我闻: 在使用指南01中
- 演示如何使用独立的Python脚本启动和控制Isaac Sim模拟器。
- 介绍Orbit框架中两个最常用的类app.AppLauncher和sim.SimulationContext。
- 实践在Oribit中设置一个空场景
代码
本指南对应于orbit/source/standalone/tutorials/00_sim目录中的create_empty.py脚本。
# Copyright (c) 2022-2024, The ORBIT Project Developers.
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause"""This script demonstrates how to create a simple stage in Isaac Sim... code-block:: bash# Usage./orbit.sh -p source/standalone/tutorials/00_sim/create_empty.py"""from __future__ import annotations"""Launch Isaac Sim Simulator first."""
import argparsefrom omni.isaac.orbit.app import AppLauncher# create argparser
parser = argparse.ArgumentParser(description="Tutorial on creating an empty stage.")
# append AppLauncher cli args
AppLauncher.add_app_launcher_args(parser)
# parse the arguments
args_cli = parser.parse_args()
# launch omniverse app
app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app"""Rest everything follows."""import tracebackimport carbfrom omni.isaac.orbit.sim import SimulationCfg, SimulationContextdef main():"""Main function."""# Initialize the simulation contextsim_cfg = SimulationCfg(dt=0.01, substeps=1)sim = SimulationContext(sim_cfg)# Set main camerasim.set_camera_view([2.5, 2.5, 2.5], [0.0, 0.0, 0.0])# Play the simulatorsim.reset()# Now we are ready!print("[INFO]: Setup complete...")# Simulate physicswhile simulation_app.is_running():# perform stepsim.step()if __name__ == "__main__":try:# run the main executionmain()except Exception as err:carb.log_error(err)carb.log_error(traceback.format_exc())raisefinally:# close sim appsimulation_app.close()
代码解析
启动模拟器
在使用Orbit进行独立Python脚本开发时,第一步是启动模拟器应用。这一步是必要的,因为Isaac Sim的各种依赖模块只有在模拟器应用运行后才可用。
要实现这一步,我们可以使用app.AppLauncher
类进行导入。这个实用程序类是基于,用于启动模拟器的omni.isaac.kit.SimulationApp
类进行封装的。app.AppLauncher
提供了使用命令行参数和环境变量配置模拟器的功能。
在本指南中,我们主要来看如何向用户自定义的argparse.ArgumentParser
里,来添加命令行选项。通过将解析器实例传递给app.AppLauncher.add_app_launcher_args()
方法,可以向解析器添加不同的参数。这些参数包括以无显模式启动应用程序、配置不同的实时流选项以及启用离屏渲染。
import argparsefrom omni.isaac.orbit.app import AppLauncher# create argparser
parser = argparse.ArgumentParser(description="Tutorial on creating an empty stage.")
# append AppLauncher cli args
AppLauncher.add_app_launcher_args(parser)
# parse the arguments
args_cli = parser.parse_args()
# launch omniverse app
app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app
导入python模块
当模拟应用启动后,我们可以导入来自Isaac Sim及其他库的多种Python模块。本例中,我们引入了两个重要模块:
-
carb
:这是Omniverse提供的一个库,它包含了多种微服务和诊断工具,用于增强应用的功能和性能。 -
omni.isaac.orbit.sim
:这是Orbit框架中的一个专门子包,专注于执行所有与核心模拟器操作相关的任务。
import carbfrom omni.isaac.orbit.sim import SimulationCfg, SimulationContext
设置模拟环境
通过独立脚本启动模拟器,我们可以控制模拟器的播放、暂停和逐步执行。这些操作均通过模拟环境(simulation context) 来实现。模拟环境负责管理时间线上的各种事件,并为模拟设置物理场景。
在Orbit平台中,sim.SimulationContext
类是基于Isaac Sim中的omni.isaac.core.simulation_context.SimulationContext
类进行扩展的,它通过Python的数据类对象来配置模拟环境,并处理模拟步进过程中的复杂情况。
在本指南中,我们将物理和渲染的时间间隔设定为0.01秒。这件事通过向sim.SimulationCfg
传递物理和渲染的时间间隔的参数完成,进而用于创建一个模拟环境的实例。
# Initialize the simulation contextsim_cfg = SimulationCfg(dt=0.01, substeps=1)sim = SimulationContext(sim_cfg)# Set main camerasim.set_camera_view([2.5, 2.5, 2.5], [0.0, 0.0, 0.0])
在建立了模拟环境之后,我们至今为止只完成了对模拟场景中物理作用(physics acting)的配置。这涉及到选择模拟使用的设备、设定重力向量以及调整其他一些高级求解参数。接下来,为了让模拟运行起来,我们还需要完成两个核心步骤:
- 构建模拟场景 (Designing the simulation scene):向场景中添加传感器、机器人及其他模拟物体。
- 执行模拟循环 (Running the simulation loop):操作模拟器进行逐步执行,并从模拟器中设置及获取数据。
本指南将首先着眼于第二步,我们将从一个空白场景开始,优先考虑如何控制模拟过程。在随后的教程中,我们会深入讨论第一步,并学习如何通过模拟处理与模拟器进行互动。
运行模拟
在设置好模拟场景之后,首先要做的就是调用sim.SimulationContext.reset()
方法。这个方法会播放时间线并在模拟器中的初始化物理处理。在开始模拟步进过程之前,sim.SimulationContext.reset()
必须首先被调用,否则模拟处理将无法正确初始化。
sim.SimulationContext.reset()
区别于sim.SimulationContext.play()
方法,后者只播放时间线并不初始化物理处理
在播放模拟时间线之后,我们建立了一个简单的模拟循环,在模拟应用程序运行时不断地步进模拟器。sim.SimulationContext.step()
方法接收一个参数render
,该参数决定是否在步进中更新与渲染相关的事件。默认情况下,这个标志被设置为True。
while simulation_app.is_running():# perform stepsim.step()
退出模拟
最后,通过调用omni.isaac.kit.SimulationApp.close()
方法,停止模拟应用程序并关闭其窗口。为了在发生错误时能够有品地关闭应用程序,我们在try-catch语句中执行这一操作。
# close sim appsimulation_app.close()
运行代码
现在我们已经大致浏览了代码的各个部分,让我们来执行代码来看看最终的结果
./orbit.sh -p source/standalone/tutorials/00_sim/create_empty.py
运行后,模拟应该正常运行,场景应该正确渲染。要停止模拟,可以直接关闭窗口,或者在终端中按Ctrl+C
。
向上述脚本传递--help
参数将显示app.AppLauncher
类之前添加的不同命令行参数。要以无显模式运行脚本,可以执行以下操作:
./orbit.sh -p source/standalone/tutorials/00_sim/create_empty.py --headless
现在我们对如何使用独立的python脚本运行模拟,有一个基本的了解了,在下一篇指南中我们将一起探索如何向场景中添加物品(assets)。
愿本文渡一切机器人模拟器苦
非常的有品
以上