-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy patharm_hand_xarm_leap.py
More file actions
170 lines (140 loc) · 5.59 KB
/
arm_hand_xarm_leap.py
File metadata and controls
170 lines (140 loc) · 5.59 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
from pathlib import Path
import mujoco
import mujoco.viewer
from loop_rate_limiters import RateLimiter
import mink
_HERE = Path(__file__).parent
_ARM_XML = _HERE / "ufactory_xarm7" / "scene.xml"
_HAND_XML = _HERE / "leap_hand" / "right_hand.xml"
fingers = ["tip_1", "tip_2", "tip_3", "th_tip"]
# fmt: off
HOME_QPOS = [
# xarm.
0, -.247, 0, .909, 0, 1.15644, 0,
# leap.
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
]
# fmt: on
def construct_model() -> mujoco.MjModel:
arm = mujoco.MjSpec.from_file(_ARM_XML.as_posix())
hand = mujoco.MjSpec.from_file(_HAND_XML.as_posix())
palm = hand.body("palm_lower")
palm.quat[:] = (0, 1, 0, 0)
palm.pos[:] = (0.065, -0.04, 0)
site = arm.site("attachment_site")
arm.attach(hand, prefix="leap_right/", site=site)
home_key = arm.key("home")
arm.delete(home_key)
arm.add_key(name="home", qpos=HOME_QPOS)
for finger in fingers:
body = arm.worldbody.add_body(name=f"{finger}_target", mocap=True)
body.add_geom(
type=mujoco.mjtGeom.mjGEOM_SPHERE,
size=(0.02,) * 3,
contype=0,
conaffinity=0,
rgba=(0.6, 0.3, 0.3, 0.5),
)
return arm.compile()
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"leap_right/{finger}",
frame_type="site",
root_name="leap_right/palm_lower",
root_type="body",
position_cost=1.0,
orientation_cost=0.0,
lm_damping=1e-3,
)
finger_tasks.append(task)
tasks = [end_effector_task, posture_task, *finger_tasks]
limits = [
mink.ConfigurationLimit(model=model),
]
# IK settings.
solver = "daqp"
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)
# Define a rectangular region centered about the origin of the initial
# end-effector pose. This can be used to clamp the end_effector_task target
# to ensure that the end-effector stays within this region.
# Roll and pitch limits are also defined to keep the hand pointed downward.
T_eef_init = configuration.get_transform_frame_to_world(
"attachment_site", "site"
)
translation_init = T_eef_init.translation()
x_limits = (translation_init[0] - 0.1, translation_init[0] + 0.1)
y_limits = (translation_init[1] - 0.1, translation_init[1] + 0.1)
z_limits = (translation_init[2] - 0.1, translation_init[2] + 0.1)
rpy_init = T_eef_init.rotation().as_rpy_radians()
roll_limits = (rpy_init.roll - 0.2, rpy_init.roll + 0.2)
pitch_limits = (rpy_init.pitch - 0.2, rpy_init.pitch + 0.2)
# 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"leap_right/{finger}", "site"
)
T_palm_prev = configuration.get_transform_frame_to_world(
"leap_right/palm_lower", "body"
)
rate = RateLimiter(frequency=200.0, warn=False)
while viewer.is_running():
# Clamp the target within the limits and then update the end-effector task.
T_wt = mink.SE3.from_mocap_name(model, data, "target")
T_wt_clamped = T_wt.clamp(
x_translation=x_limits,
y_translation=y_limits,
z_translation=z_limits,
roll_radians=roll_limits,
pitch_radians=pitch_limits,
)
end_effector_task.set_target(T_wt_clamped)
# Update finger tasks.
for finger, task in zip(fingers, finger_tasks):
T_pm = configuration.get_transform(
f"{finger}_target", "body", "leap_right/palm_lower", "body"
)
task.set_target(T_pm)
T_palm = configuration.get_transform_frame_to_world(
"leap_right/palm_lower", "body"
)
T = T_palm @ T_palm_prev.inverse()
T_palm_prev = T_palm.copy()
for finger in fingers:
mocap_id = model.body(f"{finger}_target").mocapid[0]
T_w_mocap = mink.SE3.from_mocap_id(data, mocap_id)
T_w_mocap_new = T @ T_w_mocap
data.mocap_pos[mocap_id] = T_w_mocap_new.translation()
data.mocap_quat[mocap_id] = T_w_mocap_new.rotation().wxyz
# Compute velocity and integrate into the next configuration.
vel = mink.solve_ik(
configuration, tasks, rate.dt, solver, damping=1e-3, limits=limits
)
configuration.integrate_inplace(vel, rate.dt)
mujoco.mj_camlight(model, data)
# Visualize at fixed FPS.
viewer.sync()
rate.sleep()