chrisjcc commited on
Commit
0a7688b
·
verified ·
1 Parent(s): a7a023c

Apply a Force to the Tray

Browse files
Files changed (1) hide show
  1. tray_sim.py +45 -1
tray_sim.py CHANGED
@@ -8,13 +8,46 @@ MODEL_PATH = "assets/tray.xml"
8
  N_OBJECTS = 5 # number of dynamic blocks to randomize
9
  PUSH_START_STEP = 50
10
  SIM_STEPS = 200
11
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def run_tray_simulation(seed=0, num_objects=N_OBJECTS, azimuth=45, elevation=-25, distance=0.6):
14
  np.random.seed(seed)
15
  model = mujoco.MjModel.from_xml_path(MODEL_PATH)
16
  data = mujoco.MjData(model)
 
17
 
 
 
 
18
  # Locate pusher DOF index (last one in qpos if you have 10 objects)
19
  # Compute DOF offsets
20
  pusher_idx = num_objects * 7
@@ -70,6 +103,17 @@ def run_tray_simulation(seed=0, num_objects=N_OBJECTS, azimuth=45, elevation=-25
70
  #data.qvel[pusher_vel_idx:pusher_vel_idx+3] = [-0.05, 0.05, 0.0] # toward center
71
  data.qvel[pusher_vel_idx:pusher_vel_idx+3] = push_velocity # toward center
72
 
 
 
 
 
 
 
 
 
 
 
 
73
  mujoco.mj_step(model, data)
74
  renderer.update_scene(data, camera=cam, scene_option=scene_option)
75
  img = renderer.render()
 
8
  N_OBJECTS = 5 # number of dynamic blocks to randomize
9
  PUSH_START_STEP = 50
10
  SIM_STEPS = 200
11
+ IMPACT_STEP = 60 # is a good starting point, just after pusher activates.
12
+
13
+ def classify_stability(data, model):
14
+ """
15
+ Classify object stability based on position data.
16
+
17
+ For each object in the scene:
18
+ - Check the object's position using data.qpos[z].
19
+ - Determine whether the object is upright, within the tray bounds, or has fallen/rolled over.
20
+ - Define thresholds for "stable" as:
21
+ * z-position > 0.01
22
+ * x and y within tray bounds of ±0.3
23
+
24
+ Args:
25
+ data: MuJoCo data object containing simulation state.
26
+ model: MuJoCo model (unused in this function but typically passed alongside data).
27
+
28
+ Returns:
29
+ List of booleans indicating whether each object is stable.
30
+ """
31
+
32
+ stable_objects = []
33
+ tray_bounds = 0.3
34
+ for i in range(N_OBJECTS):
35
+ pos = data.qpos[i*7 : i*7 + 3]
36
+ if abs(pos[0]) < tray_bounds and abs(pos[1]) < tray_bounds and pos[2] > 0.01:
37
+ stable_objects.append(True)
38
+ else:
39
+ stable_objects.append(False)
40
+ return stable_objects
41
 
42
  def run_tray_simulation(seed=0, num_objects=N_OBJECTS, azimuth=45, elevation=-25, distance=0.6):
43
  np.random.seed(seed)
44
  model = mujoco.MjModel.from_xml_path(MODEL_PATH)
45
  data = mujoco.MjData(model)
46
+ tray_id = mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_BODY, "tray")
47
 
48
+ # Apply a transient force to the tray
49
+ tray_force_applied = False # One-time trigger
50
+
51
  # Locate pusher DOF index (last one in qpos if you have 10 objects)
52
  # Compute DOF offsets
53
  pusher_idx = num_objects * 7
 
103
  #data.qvel[pusher_vel_idx:pusher_vel_idx+3] = [-0.05, 0.05, 0.0] # toward center
104
  data.qvel[pusher_vel_idx:pusher_vel_idx+3] = push_velocity # toward center
105
 
106
+ if t == IMPACT_STEP and not tray_force_applied:
107
+ mujoco.mj_applyFT(
108
+ model, data,
109
+ tray_id,
110
+ force=[5.0, 0.0, 0.0], # Impulse to the right; adjust magnitude
111
+ torque=[0.0, 0.0, 0.0],
112
+ point=[0.0, 0.0, 0.0], # Apply at body origin
113
+ frame=0 # World frame
114
+ )
115
+ tray_force_applied = True
116
+
117
  mujoco.mj_step(model, data)
118
  renderer.update_scene(data, camera=cam, scene_option=scene_option)
119
  img = renderer.render()