Phase 1 Prompt and the Python code
Browse files- example_code_1.py +120 -0
example_code_1.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tray_sim import SIMULATOR_MODEL, run_tray_simulation, classify_stability
|
| 4 |
+
|
| 5 |
+
def main():
|
| 6 |
+
sim = SIMULATOR_MODEL()
|
| 7 |
+
sim.create_pusher('3.0 3.0 0.05')
|
| 8 |
+
|
| 9 |
+
# Tray parameters
|
| 10 |
+
physical_parameters_for_object_id_tray = {
|
| 11 |
+
'sliding_friction': 0.1,
|
| 12 |
+
'armature': 0.1,
|
| 13 |
+
'stiffness': 0.0,
|
| 14 |
+
'mass': 0.5,
|
| 15 |
+
'damping': 20
|
| 16 |
+
}
|
| 17 |
+
sim.create_tray(object_physics=physical_parameters_for_object_id_tray)
|
| 18 |
+
|
| 19 |
+
# Object parameters
|
| 20 |
+
physical_parameters_for_object_id_1 = { # Bottle
|
| 21 |
+
'sliding_friction': 0.22,
|
| 22 |
+
'armature': 0.41,
|
| 23 |
+
'stiffness': 0.53,
|
| 24 |
+
'mass': 20.0,
|
| 25 |
+
'damping': 7.0
|
| 26 |
+
}
|
| 27 |
+
physical_parameters_for_object_id_2 = { # Martini glass
|
| 28 |
+
'sliding_friction': 0.52,
|
| 29 |
+
'armature': 0.63,
|
| 30 |
+
'stiffness': 1.22,
|
| 31 |
+
'mass': 10.0,
|
| 32 |
+
'damping': 8.0
|
| 33 |
+
}
|
| 34 |
+
physical_parameters_for_object_id_3 = { # Wine glass
|
| 35 |
+
'sliding_friction': 0.73,
|
| 36 |
+
'armature': 1.04,
|
| 37 |
+
'stiffness': 0.94,
|
| 38 |
+
'mass': 4.0,
|
| 39 |
+
'damping': 7.9
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
# Create objects
|
| 43 |
+
sim.create_object(
|
| 44 |
+
object_id=1,
|
| 45 |
+
object_name='bottle',
|
| 46 |
+
object_location=('row_1', 'column_3'),
|
| 47 |
+
object_color='orange',
|
| 48 |
+
object_physics=physical_parameters_for_object_id_1
|
| 49 |
+
)
|
| 50 |
+
sim.create_object(
|
| 51 |
+
object_id=2,
|
| 52 |
+
object_name='martini_glass',
|
| 53 |
+
object_location=('row_1', 'column_2'),
|
| 54 |
+
object_color='orange',
|
| 55 |
+
object_physics=physical_parameters_for_object_id_2
|
| 56 |
+
)
|
| 57 |
+
sim.create_object(
|
| 58 |
+
object_id=3,
|
| 59 |
+
object_name='wine_glass',
|
| 60 |
+
object_location=('row_1', 'column_1'),
|
| 61 |
+
object_color='orange',
|
| 62 |
+
object_physics=physical_parameters_for_object_id_3
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# Run simulation and collect trajectories
|
| 66 |
+
model = sim.model
|
| 67 |
+
data = sim.data
|
| 68 |
+
trajectories = {
|
| 69 |
+
'bottle': [],
|
| 70 |
+
'martini_glass': [],
|
| 71 |
+
'wine_glass': []
|
| 72 |
+
}
|
| 73 |
+
sample_steps = [0, 25, 50, 75, 100, 125, 150, 175, 199] # 9 points as in PAGE13
|
| 74 |
+
|
| 75 |
+
for t in range(200):
|
| 76 |
+
if t == 50: # PUSH_START_STEP
|
| 77 |
+
pusher_idx = 3 * 7 # 3 objects * 7 DOF
|
| 78 |
+
pusher_vel_idx = 3 * 6 # 3 objects * 6 velocities
|
| 79 |
+
direction = np.array([0.0, 0.0, 0.05]) - np.array([3.0, 3.0, 0.05])
|
| 80 |
+
direction[2] = 0.0
|
| 81 |
+
direction /= np.linalg.norm(direction)
|
| 82 |
+
push_velocity = -4.8 * direction / np.sqrt(2) # Normalize to match (-4.8, -4.8)
|
| 83 |
+
data.qvel[pusher_vel_idx:pusher_vel_idx+3] = push_velocity
|
| 84 |
+
|
| 85 |
+
mujoco.mj_step(model, data)
|
| 86 |
+
|
| 87 |
+
if t in sample_steps:
|
| 88 |
+
for i, obj_name in enumerate(['bottle', 'martini_glass', 'wine_glass'], 1):
|
| 89 |
+
pos = data.qpos[(i-1)*7:(i-1)*7+3]
|
| 90 |
+
# Adjust z to match document's center of gravity
|
| 91 |
+
if obj_name == 'bottle':
|
| 92 |
+
pos[2] += 1.1
|
| 93 |
+
else:
|
| 94 |
+
pos[2] += 0.5
|
| 95 |
+
trajectories[obj_name].append([round(p, 1) for p in pos])
|
| 96 |
+
|
| 97 |
+
sim.create_scene()
|
| 98 |
+
sim_out = sim.run_simulation() # Ensure scene is finalized
|
| 99 |
+
del sim
|
| 100 |
+
|
| 101 |
+
# Format and save trajectories
|
| 102 |
+
with open('object_traj_example_1.txt', 'w') as f:
|
| 103 |
+
f.write("bottle_motion_trajectory (x, y, z) = [\n")
|
| 104 |
+
for pos in trajectories['bottle']:
|
| 105 |
+
f.write(f"({pos[0]}, {pos[1]}, {pos[2]}),\n")
|
| 106 |
+
f.write("]\n")
|
| 107 |
+
f.write("martini_glass_motion_trajectory (x, y, z) = [\n")
|
| 108 |
+
for pos in trajectories['martini_glass']:
|
| 109 |
+
f.write(f"({pos[0]}, {pos[1]}, {pos[2]}),\n")
|
| 110 |
+
f.write("]\n")
|
| 111 |
+
f.write("wine_glass_motion_trajectory (x, y, z) = [\n")
|
| 112 |
+
for pos in trajectories['wine_glass']:
|
| 113 |
+
f.write(f"({pos[0]}, {pos[1]}, {pos[2]}),\n")
|
| 114 |
+
f.write("]\n")
|
| 115 |
+
|
| 116 |
+
return trajectories
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
trajectories = main()
|
| 120 |
+
print("Trajectories saved to object_traj_example_1.txt")
|