Creating a Manager-Based Base Environment#
This tutorial builds a Cartpole environment with envs.ManagerBasedEnv
and envs.ManagerBasedEnvCfg. The configuration brings together the scene,
actions, observations, and reset events. Managers execute the configured terms.
The examples use OvPhysX and the Newton RTX visualizer without Isaac Sim. Install the ov
extra when running them; use --viz none for headless simulation without rendering.
For rewards and episode termination, continue with Creating a Manager-Based RL Environment. See Direct and Manager MDP for the difference between direct and manager-based environments.
The Code#
The tutorial corresponds to the create_cartpole_base_env script in the scripts/tutorials/03_envs
directory.
Code for create_cartpole_base_env.py
1# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
2# All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5
6"""
7This script demonstrates how to create a simple environment with a cartpole. It combines the concepts of
8scene, action, observation and event managers to create an environment.
9
10.. code-block:: bash
11
12 uv run --extra ov python scripts/tutorials/03_envs/create_cartpole_base_env.py --num_envs 32
13
14"""
15
16"""Parse the command-line arguments first."""
17
18
19import argparse
20
21from isaaclab.app import add_launcher_args, launch_simulation
22
23# add argparse arguments
24parser = argparse.ArgumentParser(description="Tutorial on creating a cartpole base environment.")
25parser.add_argument("--num_envs", type=int, default=16, help="Number of environments to spawn.")
26
27# append simulation launcher cli args
28add_launcher_args(parser)
29parser.set_defaults(visualizer=["newton_rtx"])
30# parse the arguments
31args_cli = parser.parse_args()
32
33"""Rest everything follows."""
34
35import math
36
37import torch
38from isaaclab_ov.physics import OvPhysxCfg
39
40import isaaclab.envs.mdp as mdp
41from isaaclab.envs import ManagerBasedEnvCfg
42from isaaclab.managers import EventTermCfg as EventTerm
43from isaaclab.managers import ObservationGroupCfg as ObsGroup
44from isaaclab.managers import ObservationTermCfg as ObsTerm
45from isaaclab.managers import SceneEntityCfg
46from isaaclab.utils import configclass, instantiate
47from isaaclab.visualizers import VisualizerCfg
48
49from isaaclab_tasks.core.cartpole.cartpole_manager_env_cfg import CartpoleSceneCfg
50
51
52@configclass
53class ActionsCfg:
54 """Action specifications for the environment."""
55
56 joint_efforts = mdp.JointEffortActionCfg(asset_name="robot", joint_names=["slider_to_cart"], scale=5.0)
57
58
59@configclass
60class ObservationsCfg:
61 """Observation specifications for the environment."""
62
63 @configclass
64 class PolicyCfg(ObsGroup):
65 """Observations for policy group."""
66
67 # observation terms (order preserved)
68 joint_pos_rel = ObsTerm(func=mdp.joint_pos_rel)
69 joint_vel_rel = ObsTerm(func=mdp.joint_vel_rel)
70
71 def __post_init__(self) -> None:
72 self.enable_corruption = False
73 self.concatenate_terms = True
74
75 # observation groups
76 policy: PolicyCfg = PolicyCfg()
77
78
79@configclass
80class EventCfg:
81 """Configuration for events."""
82
83 # on startup
84 add_pole_mass = EventTerm(
85 func=mdp.randomize_rigid_body_mass,
86 mode="startup",
87 params={
88 "asset_cfg": SceneEntityCfg("robot", body_names=["pole"]),
89 "mass_distribution_params": (0.1, 0.5),
90 "operation": "add",
91 },
92 )
93
94 # on reset
95 reset_cart_position = EventTerm(
96 func=mdp.reset_joints_by_offset,
97 mode="reset",
98 params={
99 "asset_cfg": SceneEntityCfg("robot", joint_names=["slider_to_cart"]),
100 "position_range": (-1.0, 1.0),
101 "velocity_range": (-0.1, 0.1),
102 },
103 )
104
105 reset_pole_position = EventTerm(
106 func=mdp.reset_joints_by_offset,
107 mode="reset",
108 params={
109 "asset_cfg": SceneEntityCfg("robot", joint_names=["cart_to_pole"]),
110 "position_range": (-0.125 * math.pi, 0.125 * math.pi),
111 "velocity_range": (-0.01 * math.pi, 0.01 * math.pi),
112 },
113 )
114
115
116@configclass
117class CartpoleEnvCfg(ManagerBasedEnvCfg):
118 """Configuration for the cartpole environment."""
119
120 # Scene settings
121 scene = CartpoleSceneCfg(num_envs=1024, env_spacing=2.5)
122 # Basic settings
123 observations = ObservationsCfg()
124 actions = ActionsCfg()
125 events = EventCfg()
126
127 def __post_init__(self):
128 """Post initialization."""
129 # viewer settings
130 self.sim.visualizer_cfgs = VisualizerCfg(eye=(4.5, 0.0, 6.0), lookat=(0.0, 0.0, 2.0))
131 # step settings
132 self.decimation = 4 # env step every 4 sim steps: 200Hz / 4 = 50Hz
133 # simulation settings
134 self.sim.physics = OvPhysxCfg()
135 self.sim.dt = 0.005 # sim step every 5ms: 200Hz
136
137
138def main():
139 """Main function."""
140 # parse the arguments
141 env_cfg = CartpoleEnvCfg()
142 env_cfg.scene.num_envs = args_cli.num_envs
143 env_cfg.sim.device = args_cli.device
144 # Launch the simulator runtime that the configuration needs
145 with launch_simulation(env_cfg, args_cli):
146 # setup base environment
147 env = instantiate(env_cfg)
148
149 # simulate physics
150 count = 0
151 while env.sim.is_running():
152 with torch.inference_mode():
153 # reset
154 if count % 300 == 0:
155 count = 0
156 env.reset()
157 print("-" * 80)
158 print("[INFO]: Resetting environment...")
159 # sample random actions
160 joint_efforts = torch.randn_like(env.action_manager.action)
161 # step the environment
162 obs, _ = env.step(joint_efforts)
163 # print current orientation of pole
164 print("[Env 0]: Pole joint: ", obs["policy"][0][1].item())
165 # update counter
166 count += 1
167
168 # close the environment
169 env.close()
170
171
172if __name__ == "__main__":
173 # run the main function
174 main()
The Code Explained#
The base class envs.ManagerBasedEnv wraps around many intricacies of the simulation interaction
and provides a simple interface for the user to run the simulation and interact with it. It
is composed of the following components:
scene.InteractiveScene- The scene that is used for the simulation.managers.ActionManager- The manager that handles actions.managers.ObservationManager- The manager that handles observations.managers.EventManager- The manager that schedules operations (such as domain randomization) at specified simulation events. For instance, at startup, on resets, or periodic intervals.
By configuring these components, the user can create different variations of the same environment
with minimal effort. In this tutorial, we will go through the different components of the
envs.ManagerBasedEnv class and how to configure them to create a new environment.
Designing the scene#
The first step in creating a new environment is to configure its scene. For the cartpole environment, we will be using the scene from the previous tutorial. Thus, we omit the scene configuration here. For more details on how to configure a scene, see Using the Interactive Scene.
Defining actions#
In the previous tutorial, we directly input the action to the cartpole using
robot.actuators.target_command.set_effort_index. In this tutorial, we will
use the managers.ActionManager to handle the actions.
The action manager can comprise of multiple managers.ActionTerm. Each action term
is responsible for applying control over a specific aspect of the environment. For instance,
for robotic arm, we can have two action terms – one for controlling the joints of the arm,
and the other for controlling the gripper. This composition allows the user to define
different control schemes for different aspects of the environment.
In the cartpole environment, we want to control the force applied to the cart to balance the pole. Thus, we will create an action term that controls the force applied to the cart.
@configclass
class ActionsCfg:
"""Action specifications for the environment."""
joint_efforts = mdp.JointEffortActionCfg(asset_name="robot", joint_names=["slider_to_cart"], scale=5.0)
Defining observations#
While the scene defines the state of the environment, the observations define the states
that are observable by the agent. These observations are used by the agent to make decisions
on what actions to take. In Isaac Lab, the observations are computed by the
managers.ObservationManager class.
Similar to the action manager, the observation manager can comprise of multiple observation terms. These are further grouped into observation groups which are used to define different observation spaces for the environment. For instance, for hierarchical control, we may want to define two observation groups – one for the low level controller and the other for the high level controller. It is assumed that all the observation terms in a group have the same dimensions.
For this tutorial, we will only define one observation group named "policy". While not completely
prescriptive, this group is a necessary requirement for various wrappers in Isaac Lab.
We define a group by inheriting from the managers.ObservationGroupCfg class. This class
collects different observation terms and help define common properties for the group, such
as enabling noise corruption or concatenating the observations into a single tensor.
The individual terms are defined by inheriting from the managers.ObservationTermCfg class.
This class takes in the managers.ObservationTermCfg.func that specifies the function or
callable class that computes the observation for that term. It includes other parameters for
defining the noise model, clipping, scaling, etc. However, we leave these parameters to their
default values for this tutorial.
@configclass
class ObservationsCfg:
"""Observation specifications for the environment."""
@configclass
class PolicyCfg(ObsGroup):
"""Observations for policy group."""
# observation terms (order preserved)
joint_pos_rel = ObsTerm(func=mdp.joint_pos_rel)
joint_vel_rel = ObsTerm(func=mdp.joint_vel_rel)
def __post_init__(self) -> None:
self.enable_corruption = False
self.concatenate_terms = True
# observation groups
policy: PolicyCfg = PolicyCfg()
For a policy that needs a flattened history with all terms from each time step together, set
history_length and history_order="time" on its managers.ObservationGroupCfg.
The resulting observation has shape (num_envs, history_length * combined_term_dim) and can be
reshaped to (num_envs, history_length, combined_term_dim). The default "term" order keeps
each term’s history together; use it for policies trained with the existing layout.
History is ordered from the oldest to the current step by default. For a policy whose deployment
runtime expects the current step first, set history_newest_first=True on the group or on
individual terms. Index 0 along the history dimension is then the current step, in both orders.
Defining events#
At this point, we have defined the scene, actions and observations for the cartpole environment. The general idea for all these components is to define the configuration classes and then pass them to the corresponding managers. The event manager is no different.
The managers.EventManager class is responsible for events corresponding to changes
in the simulation state. This includes resetting (or randomizing) the scene, randomizing physical
properties (such as mass, friction, etc.), and varying visual properties (such as colors, textures, etc.).
Each of these are specified through the managers.EventTermCfg class, which
takes in the managers.EventTermCfg.func that specifies the function or callable
class that performs the event.
Additionally, it expects the mode of the event. The mode specifies when the event term should be applied.
It is possible to specify your own mode. For this, you’ll need to adapt the ManagerBasedEnv class.
However, out of the box, Isaac Lab provides three commonly used modes:
"startup"- Event that takes place only once at environment startup."reset"- Event that occurs on environment termination and reset."interval"- Event that are executed at a given interval, i.e., periodically after a certain number of steps.
For this example, we define events that randomize the pole’s mass on startup. This is done only once since this operation is expensive and we don’t want to do it on every reset. We also create an event to randomize the initial joint state of the cartpole and the pole at every reset.
@configclass
class EventCfg:
"""Configuration for events."""
# on startup
add_pole_mass = EventTerm(
func=mdp.randomize_rigid_body_mass,
mode="startup",
params={
"asset_cfg": SceneEntityCfg("robot", body_names=["pole"]),
"mass_distribution_params": (0.1, 0.5),
"operation": "add",
},
)
# on reset
reset_cart_position = EventTerm(
func=mdp.reset_joints_by_offset,
mode="reset",
params={
"asset_cfg": SceneEntityCfg("robot", joint_names=["slider_to_cart"]),
"position_range": (-1.0, 1.0),
"velocity_range": (-0.1, 0.1),
},
)
reset_pole_position = EventTerm(
func=mdp.reset_joints_by_offset,
mode="reset",
params={
"asset_cfg": SceneEntityCfg("robot", joint_names=["cart_to_pole"]),
"position_range": (-0.125 * math.pi, 0.125 * math.pi),
"velocity_range": (-0.01 * math.pi, 0.01 * math.pi),
},
)
Tying it all together#
Having defined the scene and manager configurations, we can now define the environment configuration
through the envs.ManagerBasedEnvCfg class. This class takes in the scene, action, observation and
event configurations.
In addition to these, it also takes in the envs.ManagerBasedEnvCfg.sim which defines the simulation
parameters such as the timestep, gravity, etc. This is initialized to the default values, but can
be modified as needed. We recommend doing so by defining the __post_init__() method in the
envs.ManagerBasedEnvCfg class, which is called after the configuration is initialized.
@configclass
class CartpoleEnvCfg(ManagerBasedEnvCfg):
"""Configuration for the cartpole environment."""
# Scene settings
scene = CartpoleSceneCfg(num_envs=1024, env_spacing=2.5)
# Basic settings
observations = ObservationsCfg()
actions = ActionsCfg()
events = EventCfg()
def __post_init__(self):
"""Post initialization."""
# viewer settings
self.sim.visualizer_cfgs = VisualizerCfg(eye=(4.5, 0.0, 6.0), lookat=(0.0, 0.0, 2.0))
# step settings
self.decimation = 4 # env step every 4 sim steps: 200Hz / 4 = 50Hz
# simulation settings
self.sim.physics = OvPhysxCfg()
self.sim.dt = 0.005 # sim step every 5ms: 200Hz
Running the simulation#
Lastly, we revisit the simulation execution loop. This is now much simpler since we have
abstracted away most of the details into the environment configuration. We only need to
call the envs.ManagerBasedEnv.reset() method to reset the environment and envs.ManagerBasedEnv.step()
method to step the environment. Both these functions return the observation and an info dictionary
which may contain additional information provided by the environment. These can be used by an
agent for decision-making.
Before the environment is created, its configuration is passed to app.launch_simulation(), which
launches the simulator runtime that the configuration needs and closes it when the with block exits.
Inside this block, instantiate() constructs the environment from the class named by
the configuration’s class_type attribute. The class is resolved lazily, so the environment
class, which works on the USD stage, is only imported once the simulator runtime is running.
The envs.ManagerBasedEnv class does not have any notion of terminations since that concept is
specific for episodic tasks. Thus, the user is responsible for defining the termination condition
for the environment. In this tutorial, we reset the simulation at regular intervals.
def main():
"""Main function."""
# parse the arguments
env_cfg = CartpoleEnvCfg()
env_cfg.scene.num_envs = args_cli.num_envs
env_cfg.sim.device = args_cli.device
# Launch the simulator runtime that the configuration needs
with launch_simulation(env_cfg, args_cli):
# setup base environment
env = instantiate(env_cfg)
# simulate physics
count = 0
while env.sim.is_running():
with torch.inference_mode():
# reset
if count % 300 == 0:
count = 0
env.reset()
print("-" * 80)
print("[INFO]: Resetting environment...")
# sample random actions
joint_efforts = torch.randn_like(env.action_manager.action)
# step the environment
obs, _ = env.step(joint_efforts)
# print current orientation of pole
print("[Env 0]: Pole joint: ", obs["policy"][0][1].item())
# update counter
count += 1
# close the environment
env.close()
An important thing to note above is that the entire simulation loop is wrapped inside the
torch.inference_mode() context manager. This is because the environment uses PyTorch
operations under-the-hood and we want to ensure that the simulation is not slowed down by
the overhead of PyTorch’s autograd engine and gradients are not computed for the simulation
operations.
The Code Execution#
To run the base environment made in this tutorial, you can use the following command:
uv run --extra ov python scripts/tutorials/03_envs/create_cartpole_base_env.py --num_envs 32 --viz newton_rtx
This should open a stage with a ground plane, light source, and cartpoles. The simulation should be playing with random actions on the cartpole. The Newton RTX window displays the scene; its sidebar provides simulation and camera controls.
To stop the simulation, you can either close the window, or press Ctrl+C in the terminal where you
started the simulation.
In this tutorial, we learned about the different managers that help define a base environment. We
include more examples of defining the base environment in the scripts/tutorials/03_envs
directory. For completeness, they can be run using the following commands. The cube wraps its spawned
material in a VisualMaterialCfg and randomizes its color once at startup with
randomize_visual_material. Runtime material writes require initialization,
so this event uses startup rather than prestartup and does not require Kit Replicator:
# Floating cube environment with custom action term for PD control
uv run --extra ov python scripts/tutorials/03_envs/create_cube_base_env.py --num_envs 32 --viz newton_rtx
# Quadrupedal locomotion environment with a policy that interacts with the environment
uv run --extra ov python scripts/tutorials/03_envs/create_quadruped_base_env.py --num_envs 32 --viz newton_rtx
In the following tutorial, we will look at the envs.ManagerBasedRLEnv class and how to use it
to create a Markovian Decision Process (MDP).