Unverified Commit eb75c536 authored by Mayank Mittal's avatar Mayank Mittal Committed by GitHub

Adapts tutorial for interactive scene (#282)

# Description

This MR goes over the scene tutorial and makes it consistent with the
articulation tutorial.

## Type of change

- This change requires a documentation update

## Checklist

- [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with
`./orbit.sh --format`
- [x] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] I have updated the changelog and the corresponding version in the
extension's `config/extension.toml` file
- [x] I have added my name to the `CONTRIBUTORS.md` or my name already
exists there
parent 79fec2ff
This diff is collapsed.
...@@ -76,7 +76,7 @@ implementing a :class:`InteractiveSceneCfg`. This will then be used to construct ...@@ -76,7 +76,7 @@ implementing a :class:`InteractiveSceneCfg`. This will then be used to construct
a :class:`InteractiveScene` which handles spawning of the objects in the scene. a :class:`InteractiveScene` which handles spawning of the objects in the scene.
In this tutorial, we will be using the configuration from ``cartpole_scene.py``. In this tutorial, we will be using the configuration from ``cartpole_scene.py``.
See :ref:`interactive-scene` for a tutorial on how to create it. See :ref:`tutorial-interactive-scene` for a tutorial on how to create it.
The scene used here consists of a ground plane, the cartpole and some lights. The scene used here consists of a ground plane, the cartpole and some lights.
......
...@@ -78,7 +78,7 @@ The first step in creating a new environment is to design the scene in which the ...@@ -78,7 +78,7 @@ The first step in creating a new environment is to design the scene in which the
The scene used in this tutorial is the same one used in the tutorial :ref:`creating-base-env`, so we won't The scene used in this tutorial is the same one used in the tutorial :ref:`creating-base-env`, so we won't
go over it in detail again here. go over it in detail again here.
Also see :ref:`interactive-scene` for even more details on scene creation. Also see :ref:`tutorial-interactive-scene` for even more details on scene creation.
Designing the Action and Observation Spaces Designing the Action and Observation Spaces
------------------------------------------- -------------------------------------------
......
...@@ -11,7 +11,6 @@ import gymnasium as gym ...@@ -11,7 +11,6 @@ import gymnasium as gym
from . import agents from . import agents
from .cartpole_env_cfg import CartpoleEnvCfg from .cartpole_env_cfg import CartpoleEnvCfg
from .cartpole_scene import CartpoleSceneCfg
## ##
# Register Gym environments. # Register Gym environments.
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
import math import math
import omni.isaac.orbit.sim as sim_utils
from omni.isaac.orbit.assets import ArticulationCfg, AssetBaseCfg
from omni.isaac.orbit.envs import RLTaskEnvCfg from omni.isaac.orbit.envs import RLTaskEnvCfg
from omni.isaac.orbit.managers import ObservationGroupCfg as ObsGroup from omni.isaac.orbit.managers import ObservationGroupCfg as ObsGroup
from omni.isaac.orbit.managers import ObservationTermCfg as ObsTerm from omni.isaac.orbit.managers import ObservationTermCfg as ObsTerm
...@@ -12,11 +14,46 @@ from omni.isaac.orbit.managers import RandomizationTermCfg as RandTerm ...@@ -12,11 +14,46 @@ from omni.isaac.orbit.managers import RandomizationTermCfg as RandTerm
from omni.isaac.orbit.managers import RewardTermCfg as RewTerm from omni.isaac.orbit.managers import RewardTermCfg as RewTerm
from omni.isaac.orbit.managers import SceneEntityCfg from omni.isaac.orbit.managers import SceneEntityCfg
from omni.isaac.orbit.managers import TerminationTermCfg as DoneTerm from omni.isaac.orbit.managers import TerminationTermCfg as DoneTerm
from omni.isaac.orbit.scene import InteractiveSceneCfg
from omni.isaac.orbit.utils import configclass from omni.isaac.orbit.utils import configclass
import omni.isaac.orbit_tasks.classic.cartpole.mdp as mdp import omni.isaac.orbit_tasks.classic.cartpole.mdp as mdp
from .cartpole_scene import CartpoleSceneCfg ##
# Pre-defined configs
##
from omni.isaac.orbit.assets.config.cartpole import CARTPOLE_CFG # isort:skip
##
# Scene definition
##
@configclass
class CartpoleSceneCfg(InteractiveSceneCfg):
"""Configuration for a cart-pole scene."""
# ground plane
ground = AssetBaseCfg(
prim_path="/World/ground",
spawn=sim_utils.GroundPlaneCfg(size=(100.0, 100.0)),
)
# cartpole
robot: ArticulationCfg = CARTPOLE_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot")
# lights
dome_light = AssetBaseCfg(
prim_path="/World/DomeLight",
spawn=sim_utils.DomeLightCfg(color=(0.9, 0.9, 0.9), intensity=500.0),
)
distant_light = AssetBaseCfg(
prim_path="/World/DistantLight",
spawn=sim_utils.DistantLightCfg(color=(0.9, 0.9, 0.9), intensity=2500.0),
init_state=AssetBaseCfg.InitialStateCfg(rot=(0.738, 0.477, 0.477, 0.0)),
)
## ##
# MDP settings # MDP settings
......
# Copyright (c) 2022-2023, The ORBIT Project Developers.
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
from __future__ import annotations
import omni.isaac.orbit.sim as sim_utils
from omni.isaac.orbit.assets import ArticulationCfg, AssetBaseCfg
from omni.isaac.orbit.assets.config.cartpole import CARTPOLE_CFG
from omni.isaac.orbit.scene import InteractiveSceneCfg
from omni.isaac.orbit.utils import configclass
@configclass
class CartpoleSceneCfg(InteractiveSceneCfg):
"""Configuration for a cartpole scene."""
# ground plane
ground = AssetBaseCfg(
prim_path="/World/ground",
spawn=sim_utils.GroundPlaneCfg(size=(100.0, 100.0)),
)
# cartpole
robot: ArticulationCfg = CARTPOLE_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot")
# lights
dome_light = AssetBaseCfg(
prim_path="/World/DomeLight",
spawn=sim_utils.DomeLightCfg(color=(0.9, 0.9, 0.9), intensity=500.0),
)
distant_light = AssetBaseCfg(
prim_path="/World/DistantLight",
spawn=sim_utils.DistantLightCfg(color=(0.9, 0.9, 0.9), intensity=2500.0),
init_state=AssetBaseCfg.InitialStateCfg(rot=(0.738, 0.477, 0.477, 0.0)),
)
...@@ -3,9 +3,13 @@ ...@@ -3,9 +3,13 @@
# #
# SPDX-License-Identifier: BSD-3-Clause # SPDX-License-Identifier: BSD-3-Clause
""" """This script demonstrates how to use the interactive scene interface to quickly setup a scene with multiple prims.
This script demonstrates how to use the scene interface to quickly setup a scene with multiple
articulated robots and sensors. .. code-block:: bash
# Usage
./orbit.sh -p source/standalone/tutorials/03_scene/scene_creation.py --num_envs 32
""" """
from __future__ import annotations from __future__ import annotations
...@@ -20,7 +24,6 @@ from omni.isaac.orbit.app import AppLauncher ...@@ -20,7 +24,6 @@ from omni.isaac.orbit.app import AppLauncher
# add argparse arguments # add argparse arguments
parser = argparse.ArgumentParser(description="This script demonstrates how to use the scene interface.") parser = argparse.ArgumentParser(description="This script demonstrates how to use the scene interface.")
parser.add_argument("--num_envs", type=int, default=2, help="Number of environments to spawn.") parser.add_argument("--num_envs", type=int, default=2, help="Number of environments to spawn.")
# append AppLauncher cli args # append AppLauncher cli args
AppLauncher.add_app_launcher_args(parser) AppLauncher.add_app_launcher_args(parser)
# parse the arguments # parse the arguments
...@@ -31,72 +34,95 @@ app_launcher = AppLauncher(args_cli) ...@@ -31,72 +34,95 @@ app_launcher = AppLauncher(args_cli)
simulation_app = app_launcher.app simulation_app = app_launcher.app
"""Rest everything follows.""" """Rest everything follows."""
import math
import torch
import traceback import traceback
import carb import carb
import omni.isaac.orbit.sim as sim_utils import omni.isaac.orbit.sim as sim_utils
from omni.isaac.orbit.scene import InteractiveScene from omni.isaac.orbit.assets import ArticulationCfg, AssetBaseCfg
from omni.isaac.orbit.assets.config import CARTPOLE_CFG
from omni.isaac.orbit.scene import InteractiveScene, InteractiveSceneCfg
from omni.isaac.orbit.sim import SimulationContext from omni.isaac.orbit.sim import SimulationContext
from omni.isaac.orbit.utils import configclass
from omni.isaac.orbit_tasks.classic.cartpole.cartpole_scene import CartpoleSceneCfg
@configclass
class CartpoleSceneCfg(InteractiveSceneCfg):
"""Configuration for a cart-pole scene."""
# Main # ground plane
def main(): ground = AssetBaseCfg(prim_path="/World/defaultGroundPlane", spawn=sim_utils.GroundPlaneCfg())
# Load kit helper
sim = SimulationContext(sim_utils.SimulationCfg(device="cpu", use_gpu_pipeline=False))
# Set main camera
sim.set_camera_view([2.5, 2.5, 4.5], [0.0, 0.0, 2.0])
# Spawn things into stage # lights
scene = InteractiveScene(CartpoleSceneCfg(num_envs=args_cli.num_envs, env_spacing=5.0)) dome_light = AssetBaseCfg(
prim_path="/World/Light", spawn=sim_utils.DomeLightCfg(intensity=3000.0, color=(0.75, 0.75, 0.75))
)
# Play the simulator # articulation
sim.reset() cartpole: ArticulationCfg = CARTPOLE_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot")
# Now we are ready!
print("[INFO]: Setup complete...")
def run_simulator(sim: sim_utils.SimulationContext, scene: InteractiveScene):
"""Runs the simulation loop."""
# Extract scene entities
# note: we only do this here for readability.
robot = scene["cartpole"]
# Define simulation stepping # Define simulation stepping
sim_dt = sim.get_physics_dt() sim_dt = sim.get_physics_dt()
count = 0 count = 0
# Extract cartpole from InteractiveScene
cartpole = scene.articulations["robot"]
# Simulation loop # Simulation loop
while simulation_app.is_running(): while simulation_app.is_running():
# reset # Reset
if count % 1000 == 0: if count % 500 == 0:
# reset counter # reset counter
count = 0 count = 0
# reset the scene entities
# Get default joint positions and velocities and set them as targets # root state
joint_pos, joint_vel = cartpole.data.default_joint_pos, cartpole.data.default_joint_vel # we offset the root state by the origin since the states are written in simulation world frame
# if this is not done, then the robots will be spawned at the (0, 0, 0) of the simulation world
joint_ids, _ = cartpole.find_joints("cart_to_pole") root_state = robot.data.default_root_state.clone()
root_state[:, :3] += scene.env_origins
# Set joint position to be pi/8 so the pole will move robot.write_root_state_to_sim(root_state)
joint_pos[:, joint_ids[0]] = math.pi / 8.0 # set joint positions with some noise
joint_pos, joint_vel = robot.data.default_joint_pos.clone(), robot.data.default_joint_vel.clone()
cartpole.set_joint_position_target(joint_pos) joint_pos += torch.rand_like(joint_pos) * 0.1
cartpole.set_joint_velocity_target(joint_vel) robot.write_joint_state_to_sim(joint_pos, joint_vel)
# clear internal buffers
scene.write_data_to_sim() scene.reset()
print("[INFO]: Resetting robot state...") print("[INFO]: Resetting robot state...")
# Apply random action
# perform step # -- generate random joint efforts
efforts = torch.randn_like(robot.data.joint_pos) * 5.0
# -- apply action to the robot
robot.set_joint_effort_target(efforts)
# -- write data to sim
scene.write_data_to_sim()
# Perform step
sim.step() sim.step()
# Increment counter
count += 1 count += 1
# Update buffers
# update buffers
scene.update(sim_dt) scene.update(sim_dt)
# End simulation loop
def main():
"""Main function."""
# Load kit helper
sim_cfg = sim_utils.SimulationCfg(device="cpu", use_gpu_pipeline=False)
sim = SimulationContext(sim_cfg)
# Set main camera
sim.set_camera_view([2.5, 0.0, 4.0], [0.0, 0.0, 2.0])
# Design scene
scene_cfg = CartpoleSceneCfg(num_envs=args_cli.num_envs, env_spacing=2.0)
scene = InteractiveScene(scene_cfg)
# Play the simulator
sim.reset()
# Now we are ready!
print("[INFO]: Setup complete...")
# Run the simulator
run_simulator(sim, scene)
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment