-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy path16_spot_demo.py
More file actions
292 lines (241 loc) · 10.2 KB
/
Copy path16_spot_demo.py
File metadata and controls
292 lines (241 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
Example script demonstrating a Boston Dynamics Spot robot with random leg actuation.
This example shows:
- Setting up the Spot robot in Isaac Lab
- Randomly actuating leg joints to create dynamic movements
- Streaming to Vision Pro
Usage:
python examples/18_spot_demo.py --num-envs 1
python examples/18_spot_demo.py --ip <vision_pro_ip> # Stream to Vision Pro
"""
import numpy as np
import torch
import argparse
from isaaclab.app import AppLauncher
# add argparse arguments
parser = argparse.ArgumentParser(
description="Boston Dynamics Spot robot with random leg actuation."
)
parser.add_argument("--num-envs", type=int, default=1, help="Number of environments to spawn.")
parser.add_argument("--ip", type=str, default=None, help="IP address of the Vision Pro device.")
parser.add_argument("--random-seed", type=int, default=None, help="Random seed for reproducibility.")
# 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
# Now import Isaac Lab modules
import isaaclab.sim as sim_utils
from isaaclab.actuators import ImplicitActuatorCfg
from isaaclab.assets import AssetBaseCfg
from isaaclab.assets.articulation import ArticulationCfg
from isaaclab.scene import InteractiveScene, InteractiveSceneCfg
from isaaclab.sim.spawners.materials import RigidBodyMaterialCfg
from isaaclab.utils.assets import ISAAC_NUCLEUS_DIR
# Set random seed for reproducibility
if args_cli.random_seed is not None:
np.random.seed(args_cli.random_seed)
torch.manual_seed(args_cli.random_seed)
# =============================================================================
# Spot Robot Configuration (simplified from IsaacLab's spot.py)
# Using ImplicitActuatorCfg for simplicity (the original uses DelayedPDActuatorCfg)
# =============================================================================
SPOT_CFG = ArticulationCfg(
spawn=sim_utils.UsdFileCfg(
usd_path=f"{ISAAC_NUCLEUS_DIR}/Robots/BostonDynamics/spot/spot.usd",
activate_contact_sensors=True,
rigid_props=sim_utils.RigidBodyPropertiesCfg(
disable_gravity=False,
retain_accelerations=False,
linear_damping=0.0,
angular_damping=0.0,
max_linear_velocity=1000.0,
max_angular_velocity=1000.0,
max_depenetration_velocity=1.0,
),
articulation_props=sim_utils.ArticulationRootPropertiesCfg(
enabled_self_collisions=True,
solver_position_iteration_count=8,
solver_velocity_iteration_count=4,
),
),
init_state=ArticulationCfg.InitialStateCfg(
pos=(0.0, 0.0, 0.6), # Start slightly above ground
joint_pos={
"[fh]l_hx": 0.1, # all left hip_x
"[fh]r_hx": -0.1, # all right hip_x
"f[rl]_hy": 0.9, # front hip_y
"h[rl]_hy": 1.1, # hind hip_y
".*_kn": -1.5, # all knees
},
joint_vel={".*": 0.0},
),
actuators={
"legs": ImplicitActuatorCfg(
joint_names_expr=[".*"],
effort_limit=45.0,
stiffness=60.0,
damping=1.5,
),
},
)
# =============================================================================
# Scene Configuration
# =============================================================================
class SpotDemoSceneCfg(InteractiveSceneCfg):
"""Scene configuration for Spot demonstration."""
# Ground plane with friction
ground = AssetBaseCfg(
prim_path="/World/defaultGroundPlane",
spawn=sim_utils.GroundPlaneCfg(
size=(20.0, 20.0),
physics_material=RigidBodyMaterialCfg(
static_friction=0.8,
dynamic_friction=0.6,
restitution=0.0,
),
),
)
# Dome light for visibility
dome_light = AssetBaseCfg(
prim_path="/World/Light",
spawn=sim_utils.DomeLightCfg(intensity=3000.0, color=(0.75, 0.75, 0.75)),
)
# Spot Robot
Spot_Robot = SPOT_CFG.replace(prim_path="{ENV_REGEX_NS}/Spot")
# =============================================================================
# Environment Class
# =============================================================================
class SpotDemoEnv:
"""Environment for Spot demonstration with random leg actuation."""
def __init__(self, args_cli):
self.args_cli = args_cli
# Setup simulation
gravity = np.array([0, 0, -9.81])
self.sim_cfg = sim_utils.SimulationCfg(
device=args_cli.device,
dt=0.005, # 5ms timestep
gravity=gravity,
)
self.sim = sim_utils.SimulationContext(self.sim_cfg)
self.sim.set_camera_view([3.0, 3.0, 2.0], [0.0, 0.0, 0.3])
# Create scene
self.scene_cfg = SpotDemoSceneCfg(
args_cli.num_envs, env_spacing=3.0, replicate_physics=True
)
self.scene = InteractiveScene(self.scene_cfg)
# Reset simulation
self.sim.reset()
# Get robot reference
self.robot = self.scene["Spot_Robot"]
# Get joint info
self.joint_pos_limits = self.robot.data.soft_joint_pos_limits[0]
self.num_joints = self.joint_pos_limits.shape[0]
self.default_joint_pos = self.robot.data.default_joint_pos.clone()
# Movement parameters
self.phase = 0.0
self.phase_speed = 0.1
self.movement_amplitude = 0.3
print(f"[Spot Demo] Robot has {self.num_joints} joints")
print(f"[Spot Demo] Joint names: {self.robot.data.joint_names[:12]}")
print(f"[Spot Demo] Number of environments: {args_cli.num_envs}")
def generate_walking_motion(self):
"""Generate pseudo-walking motion by oscillating leg joints."""
self.phase += self.phase_speed
# Get default positions
target_pos = self.default_joint_pos.clone()
# Create walking-like pattern (diagonal pairs move together)
# Front-left and hind-right move together, front-right and hind-left move together
for env_idx in range(self.args_cli.num_envs):
# Simple sinusoidal motion for hip_y and knee joints
phase_offset = env_idx * 0.5 # Offset for multiple envs
for joint_idx in range(self.num_joints):
joint_name = self.robot.data.joint_names[joint_idx]
# Hip Y joints (leg swing forward/back)
if "_hy" in joint_name:
if "fl_" in joint_name or "hr_" in joint_name:
# Diagonal pair 1
offset = self.movement_amplitude * np.sin(self.phase + phase_offset)
else:
# Diagonal pair 2
offset = self.movement_amplitude * np.sin(self.phase + phase_offset + np.pi)
target_pos[env_idx, joint_idx] += offset
# Knee joints (leg lift)
elif "_kn" in joint_name:
if "fl_" in joint_name or "hr_" in joint_name:
# Diagonal pair 1
offset = self.movement_amplitude * 0.5 * np.sin(self.phase + phase_offset)
else:
# Diagonal pair 2
offset = self.movement_amplitude * 0.5 * np.sin(self.phase + phase_offset + np.pi)
target_pos[env_idx, joint_idx] += offset
# Add small random noise for more natural motion
target_pos[env_idx, joint_idx] += (torch.rand(1, device=self.args_cli.device) - 0.5).item() * 0.02
# Clamp to joint limits
target_pos = torch.clamp(target_pos, self.joint_pos_limits[:, 0], self.joint_pos_limits[:, 1])
return target_pos
def step(self):
"""Perform one simulation step with walking motion."""
# Generate target positions
target_pos = self.generate_walking_motion()
# Set joint targets
self.robot.set_joint_position_target(target_pos)
# Step simulation
self.scene.write_data_to_sim()
self.sim.step()
self.scene.update(self.sim.get_physics_dt())
def get_robot_state(self):
"""Get current robot state for monitoring."""
root_pos = self.robot.data.root_pos_w
root_vel = self.robot.data.root_lin_vel_w
return {
"root_pos": root_pos,
"height": root_pos[:, 2].mean().item(),
"forward_vel": root_vel[:, 0].mean().item(),
}
# =============================================================================
# Main
# =============================================================================
if __name__ == "__main__":
print("\n" + "=" * 60)
print("Boston Dynamics Spot Robot Demo")
print("=" * 60 + "\n")
# Create environment
env = SpotDemoEnv(args_cli)
# Setup Vision Pro streaming if IP provided
streamer = None
if args_cli.ip is not None:
from avp_stream import VisionProStreamer
streamer = VisionProStreamer(ip=args_cli.ip)
streamer.configure_isaac(
scene=env.scene,
relative_to=[0, 0, 0, 0],
include_ground=False,
env_indices=[0],
)
streamer.start_webrtc()
print(f"[Spot Demo] Streaming to Vision Pro at {args_cli.ip}")
# Simulation loop
step_count = 0
print("\n[Spot Demo] Starting simulation. Press Ctrl+C to exit.\n")
try:
while True:
# Step simulation
env.step()
step_count += 1
# Get and print state periodically
if step_count % 200 == 0:
state = env.get_robot_state()
print(
f"Step {step_count:5d} | Height: {state['height']:6.3f}m | "
f"Phase: {env.phase:.2f}"
)
# Update streamer if active
if streamer is not None:
streamer.update_sim()
except KeyboardInterrupt:
print("\n[Spot Demo] Simulation ended by user.")
# Cleanup
simulation_app.close()