如是我聞: 在使用指南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)。
愿本文渡一切機器人模擬器苦
非常的有品
以上