forked from kevinzakka/mink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharm_dual_panda.py
More file actions
404 lines (351 loc) · 11.7 KB
/
Copy patharm_dual_panda.py
File metadata and controls
404 lines (351 loc) · 11.7 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
from pathlib import Path
import mujoco
import mujoco.viewer
import numpy as np
from loop_rate_limiters import RateLimiter
import mink
_HERE = Path(__file__).parent
_MODEL_PATH = _HERE / "franka_emika_panda" / "dual_panda_scene.xml"
def initialize_model():
model = mujoco.MjModel.from_xml_path(_MODEL_PATH.as_posix())
data = mujoco.MjData(model)
mujoco.mj_forward(model, data) # Force update after stepping
configuration = mink.Configuration(model)
return model, data, configuration
def ik_task_constraints(model):
left_ee_task = mink.FrameTask(
frame_name="attachment_site_left",
frame_type="site",
position_cost=1.0,
orientation_cost=1.0,
lm_damping=1.0,
)
right_ee_task = mink.FrameTask(
frame_name="attachment_site_right",
frame_type="site",
position_cost=1.0,
orientation_cost=1.0,
lm_damping=1.0,
)
posture_task = mink.PostureTask(model=model, cost=1e-2)
tasks = [left_ee_task, right_ee_task, posture_task]
solver = "daqp"
pos_threshold, ori_threshold = 0.008, 0.008
max_iters = 20 # IK iterations per cycle
rate = RateLimiter(frequency=40.0, warn=False)
return (
left_ee_task,
right_ee_task,
posture_task,
solver,
tasks,
pos_threshold,
ori_threshold,
max_iters,
rate,
)
def define_waypoints(
data, site_ref_left, site_ref_right, target_quat_left, target_quat_right
):
"""Define example waypoints for both arms."""
# Positions
start_left_pos = data.site_xpos[site_ref_left].copy()
start_left_pos[0] -= 0.05
start_right_pos = data.site_xpos[site_ref_right].copy()
start_right_pos[0] += 0.05
middle_left_pos = start_left_pos.copy()
middle_left_pos[0] += 0.07
middle_right_pos = start_right_pos.copy()
middle_right_pos[0] -= 0.07
# Waypoints
left_waypoints = [
(start_left_pos, target_quat_left.copy()),
(middle_left_pos, target_quat_left.copy()),
]
right_waypoints = [
(start_right_pos, target_quat_right.copy()),
(middle_right_pos, target_quat_right.copy()),
]
# Lift positions
lifted_left_pos = middle_left_pos.copy()
lifted_left_pos[2] += 0.36
lifted_right_pos = middle_right_pos.copy()
lifted_right_pos[2] += 0.36
return left_waypoints, right_waypoints, lifted_left_pos, lifted_right_pos
def check_reached(
meas_left_pos,
tgt_left_pos,
meas_left_quat,
tgt_left_quat,
meas_right_pos,
tgt_right_pos,
meas_right_quat,
tgt_right_quat,
pos_threshold,
ori_threshold,
):
"""Returns True if both arms have reached position/orientation targets."""
err_pos_left = np.linalg.norm(meas_left_pos - tgt_left_pos)
err_ori_left = quaternion_error(meas_left_quat, tgt_left_quat)
err_pos_right = np.linalg.norm(meas_right_pos - tgt_right_pos)
err_ori_right = quaternion_error(meas_right_quat, tgt_right_quat)
return (
err_pos_left <= pos_threshold
and err_ori_left <= ori_threshold
and err_pos_right <= pos_threshold
and err_ori_right <= ori_threshold
)
def initialize_mocap_targets(model, data):
"""Initialize the mocap targets to match the initial end-effector sites."""
site_ref_left = model.site("site_left").id
site_ref_right = model.site("site_right").id
body_left_mocap_id = model.body("target_left").mocapid
body_right_mocap_id = model.body("target_right").mocapid
# Position
data.mocap_pos[body_left_mocap_id] = data.site_xpos[site_ref_left].copy()
data.mocap_pos[body_right_mocap_id] = data.site_xpos[site_ref_right].copy()
# Orientation
q_left = np.empty(4)
q_right = np.empty(4)
mujoco.mju_mat2Quat(q_left, data.site_xmat[site_ref_left])
mujoco.mju_mat2Quat(q_right, data.site_xmat[site_ref_right])
data.mocap_quat[body_left_mocap_id] = q_left
data.mocap_quat[body_right_mocap_id] = q_right
return q_left, q_right
def update_mocap_targets(model, data, left_pos, left_quat, right_pos, right_quat):
"""Set the position/orientation of both targets in the MjData."""
data.mocap_pos[model.body("target_left").mocapid] = left_pos
data.mocap_quat[model.body("target_left").mocapid] = left_quat
data.mocap_pos[model.body("target_right").mocapid] = right_pos
data.mocap_quat[model.body("target_right").mocapid] = right_quat
def quaternion_error(q_current, q_target):
"""Returns the minimum of (||q_cur - q_tar||, ||q_cur + q_tar||)."""
err1 = np.linalg.norm(q_current - q_target)
err2 = np.linalg.norm(q_current + q_target)
return min(err1, err2)
def ik_iteration(
configuration,
tasks,
dt,
solver,
data,
model,
site_left_id,
site_right_id,
left_target_pos,
left_target_quat,
right_target_pos,
right_target_quat,
pos_threshold,
ori_threshold,
gripper_val=255,
):
"""
Performs one IK iteration and checks if the targets are reached.
Returns True if both arms reached the target, otherwise False.
"""
# Solve IK and integrate
vel = mink.solve_ik(configuration, tasks, dt, solver, 5e-3)
configuration.integrate_inplace(vel, dt)
# Update controls
data.ctrl[0:7] = configuration.q[0:7]
data.ctrl[8:15] = configuration.q[9:16]
data.ctrl[7] = gripper_val
data.ctrl[15] = gripper_val
# Step the simulation
mujoco.mj_step(model, data)
# Measure
measured_left_pos = data.site_xpos[site_left_id]
measured_right_pos = data.site_xpos[site_right_id]
measured_left_quat = np.empty(4)
measured_right_quat = np.empty(4)
mujoco.mju_mat2Quat(measured_left_quat, data.site_xmat[site_left_id])
mujoco.mju_mat2Quat(measured_right_quat, data.site_xmat[site_right_id])
# Check if targets are reached
return check_reached(
measured_left_pos,
left_target_pos,
measured_left_quat,
left_target_quat,
measured_right_pos,
right_target_pos,
measured_right_quat,
right_target_quat,
pos_threshold,
ori_threshold,
)
def move_gripper(model, data, viewer, rate, steps=100, gripper_val=0):
"""
Closes or opens the gripper by setting ctrl to the specified value,
stepping the simulation, and syncing the viewer.
"""
for _ in range(steps):
data.ctrl[7], data.ctrl[15] = gripper_val, gripper_val
mujoco.mj_step(model, data) # Use model and data from parameters
viewer.sync()
rate.sleep()
def run_waypoints(
model,
data,
configuration,
viewer,
left_waypoints,
right_waypoints,
left_ee_task,
right_ee_task,
tasks,
solver,
site_left_id,
site_right_id,
pos_threshold,
ori_threshold,
max_iters,
rate,
):
"""Plans and executes each waypoint until reached."""
for wp_index, (left_wp, right_wp) in enumerate(
zip(left_waypoints, right_waypoints)
):
print(f"\nPlanning to waypoint {wp_index + 1}:")
# Unpack
current_left_pos, current_left_quat = left_wp
current_right_pos, current_right_quat = right_wp
reached = False
while not reached and viewer.is_running():
# Update mocap and tasks
update_mocap_targets(
model,
data,
current_left_pos,
current_left_quat,
current_right_pos,
current_right_quat,
)
T_wt_left = mink.SE3.from_mocap_name(model, data, "target_left")
T_wt_right = mink.SE3.from_mocap_name(model, data, "target_right")
left_ee_task.set_target(T_wt_left)
right_ee_task.set_target(T_wt_right)
# IK iterations
for _ in range(max_iters):
reached = ik_iteration(
configuration,
tasks,
rate.dt,
solver,
data,
model,
site_left_id,
site_right_id,
current_left_pos,
current_left_quat,
current_right_pos,
current_right_quat,
pos_threshold,
ori_threshold,
gripper_val=255,
)
viewer.sync()
rate.sleep()
if reached:
break
# If still not reached, do one more step for stability
if not reached:
mujoco.mj_step(model, data)
viewer.sync()
rate.sleep()
print(f"Waypoint {wp_index + 1} reached.\n")
def main():
model, data, configuration = initialize_model()
# Initialize tasks, solver, thresholds, etc.
(
left_ee_task,
right_ee_task,
posture_task,
solver,
tasks,
pos_threshold,
ori_threshold,
max_iters,
rate,
) = ik_task_constraints(model)
# Grab site IDs
site_left_id = model.site("attachment_site_left").id
site_right_id = model.site("attachment_site_right").id
site_ref_left = model.site("site_left").id
site_ref_right = model.site("site_right").id
# Initialize mocap targets
tgt_quat_left, tgt_quat_right = initialize_mocap_targets(model, data)
# Create example waypoints
left_waypoints, right_waypoints, lifted_left_pos, lifted_right_pos = (
define_waypoints(
data, site_ref_left, site_ref_right, tgt_quat_left, tgt_quat_right
)
)
# Launch viewer
with mujoco.viewer.launch_passive(
model=model, data=data, show_left_ui=False, show_right_ui=False
) as viewer:
mujoco.mjv_defaultFreeCamera(model, viewer.cam)
mujoco.mj_resetDataKeyframe(model, data, model.key("home1").id)
configuration.update(data.qpos)
posture_task.set_target_from_configuration(configuration)
mujoco.mj_forward(model, data)
# Execute waypoints
run_waypoints(
model,
data,
configuration,
viewer,
left_waypoints,
right_waypoints,
left_ee_task,
right_ee_task,
tasks,
solver,
site_left_id,
site_right_id,
pos_threshold,
ori_threshold,
max_iters,
rate,
)
# Close gripper
print("All waypoints reached. Closing gripper...")
move_gripper(model, data, viewer, rate, steps=100, gripper_val=0)
print("Gripper closed.")
# Lift object
data.mocap_pos[model.body("target_left").mocapid] = lifted_left_pos
data.mocap_pos[model.body("target_right").mocapid] = lifted_right_pos
T_wt_left = mink.SE3.from_mocap_name(model, data, "target_left")
T_wt_right = mink.SE3.from_mocap_name(model, data, "target_right")
left_ee_task.set_target(T_wt_left)
right_ee_task.set_target(T_wt_right)
# Final IK steps to lift
for _ in range(max_iters):
ik_iteration(
configuration,
tasks,
rate.dt,
solver,
data,
model,
site_left_id,
site_right_id,
lifted_left_pos,
tgt_quat_left,
lifted_right_pos,
tgt_quat_right,
pos_threshold,
ori_threshold,
gripper_val=0,
)
viewer.sync()
rate.sleep()
# Keep viewer open
while viewer.is_running():
data.ctrl[7], data.ctrl[15] = 0, 0
mujoco.mj_step(model, data)
viewer.sync()
rate.sleep()
if __name__ == "__main__":
main()