-
Notifications
You must be signed in to change notification settings - Fork 10
/
arm_hand_iiwa_allegro.py
155 lines (125 loc) · 4.75 KB
/
arm_hand_iiwa_allegro.py
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
from pathlib import Path
import mujoco
import mujoco.viewer
from dm_control import mjcf
from loop_rate_limiters import RateLimiter
import mink
_HERE = Path(__file__).parent
_ARM_XML = _HERE / "kuka_iiwa_14" / "scene.xml"
_HAND_XML = _HERE / "wonik_allegro" / "left_hand.xml"
fingers = ["rf_tip", "mf_tip", "ff_tip", "th_tip"]
# fmt: off
HOME_QPOS = [
# iiwa.
-0.0759329, 0.153982, 0.104381, -1.8971, 0.245996, 0.34972, -0.239115,
# allegro.
-0.0694123, 0.0551428, 0.986832, 0.671424,
-0.186261, -0.0866821, 1.01374, 0.728192,
-0.218949, -0.0318307, 1.25156, 0.840648,
1.0593, 0.638801, 0.391599, 0.57284,
]
# fmt: on
def construct_model():
arm_mjcf = mjcf.from_path(_ARM_XML.as_posix())
arm_mjcf.find("key", "home").remove()
hand_mjcf = mjcf.from_path(_HAND_XML.as_posix())
palm = hand_mjcf.worldbody.find("body", "palm")
palm.quat = (1, 0, 0, 0)
palm.pos = (0, 0, 0.095)
attach_site = arm_mjcf.worldbody.find("site", "attachment_site")
attach_site.attach(hand_mjcf)
arm_mjcf.keyframe.add("key", name="home", qpos=HOME_QPOS)
for finger in fingers:
body = arm_mjcf.worldbody.add("body", name=f"{finger}_target", mocap=True)
body.add(
"geom",
type="sphere",
size=".02",
contype="0",
conaffinity="0",
rgba=".6 .3 .3 .5",
)
return mujoco.MjModel.from_xml_string(
arm_mjcf.to_xml_string(), arm_mjcf.get_assets()
)
if __name__ == "__main__":
model = construct_model()
configuration = mink.Configuration(model)
end_effector_task = mink.FrameTask(
frame_name="attachment_site",
frame_type="site",
position_cost=1.0,
orientation_cost=1.0,
lm_damping=1.0,
)
posture_task = mink.PostureTask(model=model, cost=5e-2)
finger_tasks = []
for finger in fingers:
task = mink.RelativeFrameTask(
frame_name=f"allegro_left/{finger}",
frame_type="site",
root_name="allegro_left/palm",
root_type="body",
position_cost=1.0,
orientation_cost=0.0,
lm_damping=1.0,
)
finger_tasks.append(task)
tasks = [end_effector_task, posture_task, *finger_tasks]
limits = [
mink.ConfigurationLimit(model=model),
]
# IK settings.
solver = "quadprog"
model = configuration.model
data = configuration.data
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("home").id)
configuration.update(data.qpos)
posture_task.set_target_from_configuration(configuration)
# Initialize the mocap target at the end-effector site.
mink.move_mocap_to_frame(model, data, "target", "attachment_site", "site")
for finger in fingers:
mink.move_mocap_to_frame(
model, data, f"{finger}_target", f"allegro_left/{finger}", "site"
)
T_eef_prev = configuration.get_transform_frame_to_world(
"attachment_site", "site"
)
rate = RateLimiter(frequency=100.0, warn=False)
while viewer.is_running():
# Update kuka end-effector task.
T_wt = mink.SE3.from_mocap_name(model, data, "target")
end_effector_task.set_target(T_wt)
# Update finger tasks.
for finger, task in zip(fingers, finger_tasks):
T_pm = configuration.get_transform(
f"{finger}_target", "body", "allegro_left/palm", "body"
)
task.set_target(T_pm)
for finger in fingers:
T_eef = configuration.get_transform_frame_to_world(
"attachment_site", "site"
)
T = T_eef @ T_eef_prev.inverse()
T_w_mocap = mink.SE3.from_mocap_name(model, data, f"{finger}_target")
T_w_mocap_new = T @ T_w_mocap
data.mocap_pos[model.body(f"{finger}_target").mocapid[0]] = (
T_w_mocap_new.translation()
)
data.mocap_quat[model.body(f"{finger}_target").mocapid[0]] = (
T_w_mocap_new.rotation().wxyz
)
# Compute velocity and integrate into the next configuration.
vel = mink.solve_ik(
configuration, tasks, rate.dt, solver, 1e-3, limits=limits
)
configuration.integrate_inplace(vel, rate.dt)
mujoco.mj_camlight(model, data)
T_eef_prev = T_eef.copy()
# Visualize at fixed FPS.
viewer.sync()
rate.sleep()