Multiple Robot Scenarios#
Learning Objectives#
This tutorial describes how to create and manage multiple robot scenarios in NVIDIA Isaac Sim. It explains how to use parameterization and Python classes to scale your simulations with multiple instances of robots performing similar tasks. After this tutorial, you will have more experience building scalable multi-robot simulations in NVIDIA Isaac Sim.
15-20 Minute Tutorial
Getting Started#
Prerequisites
Review Adding Multiple Robots prior to beginning this tutorial.
Begin with the source code open from the previous tutorial, Adding Multiple Robots.
Note
Pressing STOP, then PLAY in this workflow might not reset the world properly. Use the RESET button instead.
Organizing Robot Scenarios with Classes#
When working with multiple robots performing similar tasks, it’s helpful to encapsulate the robot setup and control logic into reusable classes. This approach allows you to easily create multiple instances with different parameters (like position offsets).
Create a RobotScenario class that manages a Jetbot pushing a cube to a Franka:
1import isaacsim.core.experimental.utils.app as app_utils
2import isaacsim.core.experimental.utils.stage as stage_utils
3import numpy as np
4from isaacsim.core.experimental.objects import DomeLight, GroundPlane
5from isaacsim.core.experimental.prims import Articulation, XformPrim
6from isaacsim.core.simulation_manager import SimulationEvent, SimulationManager
7from isaacsim.examples.base.base_sample_experimental import BaseSample
8from isaacsim.storage.native import get_assets_root_path
9
10app_utils.enable_extension("isaacsim.robot_motion.examples")
11
12from isaacsim.robot_motion.examples.manipulation import PickPlaceTask
13
14
15class RobotScenario:
16 """Encapsulates a Jetbot + Franka + Cube scenario with an offset."""
17
18 def __init__(self, name: str, offset: np.ndarray = np.array([0.0, 0.0, 0.0])):
19 self.name = name
20 self.offset = offset
21 self.state = 0
22 self.step_counter = 0
23 self.jetbot = None
24 self.pick_place = None
25 self.cube_goal = np.array([1.2, 0.0, 0.0]) + offset
26
27 def setup_scene(self):
28 """Create the robots and cube for this scenario."""
29 assets_root_path = get_assets_root_path()
30 base_path = f"/World/{self.name}"
31
32 # Add Jetbot
33 stage_utils.add_reference_to_stage(
34 usd_path=assets_root_path + "/Isaac/Robots_Multiphysics/NVIDIA/Jetbot/jetbot.usda",
35 path=f"{base_path}/Jetbot",
36 )
37 jetbot_xform = XformPrim(f"{base_path}/Jetbot")
38 jetbot_xform.reset_xform_op_properties()
39 jetbot_xform.set_world_poses(positions=(self.offset + np.array([0.0, 0.0, 0.035])).tolist())
40
41 # Add Franka and its shared motion-generation task
42 robot_offset = self.offset + np.array([0.8, -0.3, 0.0])
43 self.pick_place = PickPlaceTask(
44 robot_path=f"{base_path}/Franka",
45 cube_path=f"{base_path}/Cube",
46 offset=tuple(robot_offset),
47 cube_positions=[(-0.65, 0.3, 0.025)],
48 )
49 self.pick_place.setup_scene()
50
51 def initialize(self):
52 """Initialize articulation handles after scene load."""
53 base_path = f"/World/{self.name}"
54 self.jetbot = Articulation(f"{base_path}/Jetbot")
55 self.pick_place.initialize()
56 self.cube = self.pick_place.cubes[0]
57
58 def reset(self):
59 """Reset the scenario state."""
60 self.state = 0
61 self.step_counter = 0
62 self.pick_place.reset_robot()
63
64 def step(self, dt):
65 """Execute one step of the scenario logic."""
66 if self.state == 0:
67 # Jetbot pushes cube
68 cube_pos = self.cube.get_world_poses()[0].numpy()[0]
69 if np.linalg.norm(cube_pos[:2] - self.cube_goal[:2]) > 0.05:
70 self.jetbot.set_dof_velocity_targets([10.0, 10.0])
71 else:
72 self.jetbot.set_dof_velocity_targets([0.0, 0.0])
73 self.state = 1
74 self.step_counter = 0
75
76 elif self.state == 1:
77 # Jetbot backs up
78 self.jetbot.set_dof_velocity_targets([-8.0, -8.0])
79 self.step_counter += 1
80 if self.step_counter > 100:
81 self.jetbot.set_dof_velocity_targets([0.0, 0.0])
82 self.state = 2
83 self.step_counter = 0
84
85 elif self.state == 2:
86 # Execute the shared Franka pick-and-place task
87 self.pick_place.step(dt)
88
89
90class HelloWorld(BaseSample):
91 def __init__(self) -> None:
92 super().__init__()
93 self._physics_callback_id = None
94 self._scenarios = []
95
96 def setup_scene(self):
97 GroundPlane("/World/ground_plane")
98 dome_light = DomeLight("/World/DomeLight")
99 dome_light.set_intensities(1000)
100
101 # Create a single scenario
102 self._scenario = RobotScenario(name="scenario_0", offset=np.array([0.0, 0.0, 0.0]))
103 self._scenario.setup_scene()
104
105 async def setup_post_load(self):
106 self._scenario.initialize()
107
108 self._physics_callback_id = SimulationManager.register_callback(
109 self.physics_step, event=SimulationEvent.PHYSICS_POST_STEP
110 )
111
112 def physics_step(self, dt, context):
113 self._scenario.step(dt)
114
115 async def setup_post_reset(self):
116 self._scenario.reset()
117
118 def physics_cleanup(self):
119 if self._physics_callback_id is not None:
120 SimulationManager.deregister_callback(self._physics_callback_id)
121 self._physics_callback_id = None
122 self._scenario.pick_place.cleanup()
Scaling to Multiple Scenarios#
Adding the following operations:
Set number of scenarios:
1 self._num_scenarios = 3 # Number of parallel scenarios
Creating scenarios:
1 # Create multiple scenarios with Y-axis offsets
2 for i in range(self._num_scenarios):
3 offset = np.array([0.0, (i - 1) * 2.0, 0.0]) # Spread along Y-axis
4 scenario = RobotScenario(name=f"scenario_{i}", offset=offset, randomize=False)
5 scenario.setup_scene()
6 self._scenarios.append(scenario)
Initializing scenarios:
1 # Initialize all scenarios
2 for scenario in self._scenarios:
3 scenario.initialize()
Stepping all scenarios:
1 # Step all scenarios
2 for scenario in self._scenarios:
3 scenario.step(dt)
Resetting all scenarios:
1 # Reset all scenarios
2 for scenario in self._scenarios:
3 scenario.reset()
Clean up:
1 for scenario in self._scenarios:
2 scenario.pick_place.cleanup()
3 self._scenarios = []
Complete code:
1import isaacsim.core.experimental.utils.app as app_utils
2import isaacsim.core.experimental.utils.stage as stage_utils
3import numpy as np
4from isaacsim.core.experimental.objects import DomeLight, GroundPlane
5from isaacsim.core.experimental.prims import Articulation, XformPrim
6from isaacsim.core.simulation_manager import SimulationEvent, SimulationManager
7from isaacsim.examples.base.base_sample_experimental import BaseSample
8from isaacsim.storage.native import get_assets_root_path
9
10app_utils.enable_extension("isaacsim.robot_motion.examples")
11
12from isaacsim.robot_motion.examples.manipulation import PickPlaceTask
13
14
15class RobotScenario:
16 """Encapsulates a Jetbot + Franka + Cube scenario with an offset."""
17
18 def __init__(self, name: str, offset: np.ndarray = np.array([0.0, 0.0, 0.0]), randomize: bool = False):
19 self.name = name
20 self.offset = offset
21 self.state = 0
22 self.step_counter = 0
23 self.randomize = randomize
24 self.jetbot = None
25 self.pick_place = None
26 self.cube_goal = np.array([1.2, 0.0, 0.0]) + offset
27
28 # Randomize cube goal position if enabled
29 if self.randomize:
30 random_x = np.random.uniform(1.0, 1.6)
31 self.cube_goal = np.array([random_x, 0.0, 0.0]) + offset
32 else:
33 self.cube_goal = np.array([1.2, 0.0, 0.0]) + offset
34
35 def setup_scene(self):
36 """Create the robots and cube for this scenario."""
37 assets_root_path = get_assets_root_path()
38 base_path = f"/World/{self.name}"
39
40 # Add Jetbot
41 stage_utils.add_reference_to_stage(
42 usd_path=assets_root_path + "/Isaac/Robots_Multiphysics/NVIDIA/Jetbot/jetbot.usda",
43 path=f"{base_path}/Jetbot",
44 )
45 jetbot_xform = XformPrim(f"{base_path}/Jetbot")
46 jetbot_xform.reset_xform_op_properties()
47 jetbot_xform.set_world_poses(positions=(self.offset + np.array([0.0, 0.0, 0.035])).tolist())
48
49 # Add Franka and its shared motion-generation task
50 robot_offset = self.offset + np.array([0.8, -0.3, 0.0])
51 self.pick_place = PickPlaceTask(
52 robot_path=f"{base_path}/Franka",
53 cube_path=f"{base_path}/Cube",
54 offset=tuple(robot_offset),
55 cube_positions=[(-0.65, 0.3, 0.025)],
56 )
57 self.pick_place.setup_scene()
58
59 def initialize(self):
60 """Initialize articulation handles after scene load."""
61 base_path = f"/World/{self.name}"
62 self.jetbot = Articulation(f"{base_path}/Jetbot")
63 self.pick_place.initialize()
64 self.cube = self.pick_place.cubes[0]
65
66 def reset(self):
67 """Reset the scenario state."""
68 self.state = 0
69 self.step_counter = 0
70 self.pick_place.reset_robot()
71
72 def step(self, dt):
73 """Execute one step of the scenario logic."""
74 if self.state == 0:
75 # Jetbot pushes cube
76 cube_pos = self.cube.get_world_poses()[0].numpy()[0]
77 if np.linalg.norm(cube_pos[:2] - self.cube_goal[:2]) > 0.05:
78 self.jetbot.set_dof_velocity_targets([10.0, 10.0])
79 else:
80 self.jetbot.set_dof_velocity_targets([0.0, 0.0])
81 self.state = 1
82 self.step_counter = 0
83
84 elif self.state == 1:
85 # Jetbot backs up
86 self.jetbot.set_dof_velocity_targets([-8.0, -8.0])
87 self.step_counter += 1
88 if self.step_counter > 100:
89 self.jetbot.set_dof_velocity_targets([0.0, 0.0])
90 self.state = 2
91 self.step_counter = 0
92
93 elif self.state == 2:
94 # Execute the shared Franka pick-and-place task
95 self.pick_place.step(dt)
96
97
98class HelloWorld(BaseSample):
99 def __init__(self) -> None:
100 super().__init__()
101 self._physics_callback_id = None
102 self._scenarios = []
103 # -- Begin setting scenario number -- #
104 self._num_scenarios = 3 # Number of parallel scenarios
105 # -- End of setting scenario number -- #
106
107 def setup_scene(self):
108 GroundPlane("/World/ground_plane")
109 dome_light = DomeLight("/World/DomeLight")
110 dome_light.set_intensities(1000)
111
112 # -- Begin creating scenarios -- #
113 # Create multiple scenarios with Y-axis offsets
114 for i in range(self._num_scenarios):
115 offset = np.array([0.0, (i - 1) * 2.0, 0.0]) # Spread along Y-axis
116 scenario = RobotScenario(name=f"scenario_{i}", offset=offset, randomize=False)
117 scenario.setup_scene()
118 self._scenarios.append(scenario)
119 # -- End of creating scenarios -- #
120
121 async def setup_post_load(self):
122 # -- Begin initializing scenarios -- #
123 # Initialize all scenarios
124 for scenario in self._scenarios:
125 scenario.initialize()
126 # -- End of initializing scenarios -- #
127
128 self._physics_callback_id = SimulationManager.register_callback(
129 self.physics_step, event=SimulationEvent.PHYSICS_POST_STEP
130 )
131
132 def physics_step(self, dt, context):
133 # -- Begin stepping scenarios -- #
134 # Step all scenarios
135 for scenario in self._scenarios:
136 scenario.step(dt)
137 # -- End of stepping scenarios -- #
138
139 async def setup_post_reset(self):
140 # -- Begin resetting scenarios -- #
141 # Reset all scenarios
142 for scenario in self._scenarios:
143 scenario.reset()
144 # -- End of resetting scenarios -- #
145
146 def physics_cleanup(self):
147 if self._physics_callback_id is not None:
148 SimulationManager.deregister_callback(self._physics_callback_id)
149 self._physics_callback_id = None
150 # -- Begin remove all scenarios -- #
151 for scenario in self._scenarios:
152 scenario.pick_place.cleanup()
153 self._scenarios = []
154 # -- End of remove all scenarios -- #
Adding Randomization#
To make simulations more interesting, you can add randomization to the scenario parameters.
In the setup_scene method above, set randomize=True when creating each scenario:
1for i in range(self._num_scenarios):
2 offset = np.array([0.0, (i - 1) * 2.0, 0.0]) # Spread along Y-axis
3 scenario = RobotScenario(name=f"scenario_{i}", offset=offset, randomize=True)
4 scenario.setup_scene()
5 self._scenarios.append(scenario)
Best Practices for Scaling#
When creating large-scale multi-robot simulations:
Use unique paths: Each scenario should use unique USD prim paths to avoid conflicts. The
RobotScenarioclass uses the scenario name to create unique paths like/World/scenario_0/Jetbot.Manage state independently: Each scenario instance maintains its own state variables, allowing scenarios to progress independently.
Clean up properly: The
physics_cleanupmethod ensures callbacks are deregistered and scenario lists are cleared when the simulation is stopped.Consider performance: With many scenarios, consider reducing physics step frequency or using GPU-accelerated simulation for better performance.
Summary#
This tutorial covered the following topics:
Organizing robot scenarios into reusable Python classes
Using the
offsetparameter to position multiple scenarios in the worldScaling to multiple parallel scenarios with a simple loop
Adding randomization to scenario parameters
Best practices for managing multiple robot instances