Skip to content

Commit 68e26b8

Browse files
committed
Cleanup RigidEntity inverse kinematic.
1 parent 2b9865a commit 68e26b8

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

genesis/engine/entities/rigid_entity/rigid_entity.py

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -895,9 +895,9 @@ def get_jacobian(self, link, local_point=None):
895895
links_state=sol.links_state,
896896
)
897897

898-
jacobian = self._jacobian.to_torch(gs.device).permute(2, 0, 1)
898+
jacobian = ti_to_torch(self._jacobian, transpose=True)
899899
if self._solver.n_envs == 0:
900-
jacobian = jacobian.squeeze(0)
900+
jacobian = jacobian[0]
901901

902902
return jacobian
903903

@@ -1144,9 +1144,7 @@ def inverse_kinematics(
11441144
if return_error:
11451145
qpos, error_pose = ret
11461146
return qpos, error_pose[..., 0, :]
1147-
1148-
else:
1149-
return ret
1147+
return ret
11501148

11511149
@gs.assert_built
11521150
def inverse_kinematics_multilink(
@@ -1348,14 +1346,20 @@ def inverse_kinematics_multilink(
13481346
self._solver._static_rigid_sim_config,
13491347
)
13501348

1351-
qpos = self._IK_qpos_best.to_torch(gs.device).transpose(1, 0)
1352-
qpos = qpos[0 if self._solver.n_envs == 0 else envs_idx]
1349+
qpos = ti_to_torch(self._IK_qpos_best, transpose=True)
1350+
if self._solver.n_envs == 0:
1351+
qpos = qpos[0].clone()
1352+
else:
1353+
qpos = qpos[envs_idx]
13531354

13541355
if return_error:
1355-
error_pose = (
1356-
self._IK_err_pose_best.to_torch(gs.device).reshape((self._IK_n_tgts, 6, -1))[:n_links].permute(2, 0, 1)
1357-
)
1358-
error_pose = error_pose[0 if self._solver.n_envs == 0 else envs_idx]
1356+
error_pose = ti_to_torch(self._IK_err_pose_best, transpose=True).reshape((-1, self._IK_n_tgts, 6))[
1357+
:, :n_links
1358+
]
1359+
if self._solver.n_envs == 0:
1360+
error_pose = error_pose[0].clone()
1361+
else:
1362+
error_pose = error_pose[envs_idx]
13591363
return qpos, error_pose
13601364
return qpos
13611365

@@ -1412,8 +1416,8 @@ def forward_kinematics(self, qpos, qs_idx_local=None, links_idx_local=None, envs
14121416
)
14131417

14141418
if self._solver.n_envs == 0:
1415-
links_pos = links_pos.squeeze(0)
1416-
links_quat = links_quat.squeeze(0)
1419+
links_pos = links_pos[0]
1420+
links_quat = links_quat[0]
14171421
return links_pos, links_quat
14181422

14191423
@ti.kernel
@@ -2044,7 +2048,7 @@ def get_verts(self):
20442048
tensor += tensor_free
20452049

20462050
if self._solver.n_envs == 0:
2047-
tensor = tensor.squeeze(0)
2051+
tensor = tensor[0]
20482052
return tensor
20492053

20502054
def _get_idx(self, idx_local, idx_local_max, idx_global_start=0, *, unsafe=False):
@@ -2678,7 +2682,7 @@ def get_links_net_contact_force(self, envs_idx=None, *, unsafe=False):
26782682
tensor = ti_to_torch(
26792683
self._solver.links_state.contact_force, envs_idx, slice(self.link_start, self.link_end), transpose=True
26802684
)
2681-
return tensor.squeeze(0) if self._solver.n_envs == 0 else tensor
2685+
return tensor[0] if self._solver.n_envs == 0 else tensor
26822686

26832687
def set_friction_ratio(self, friction_ratio, links_idx_local=None, envs_idx=None, *, unsafe=False):
26842688
"""
@@ -3351,34 +3355,24 @@ def kernel_rigid_entity_inverse_kinematics(
33513355

33523356
# Resample init q
33533357
if respect_joint_limit and i_sample < max_samples - 1:
3354-
for _i_l in range(n_links_by_dofs):
3355-
i_l = links_idx_by_dofs[_i_l]
3358+
for i_l_ in range(n_links_by_dofs):
3359+
i_l = links_idx_by_dofs[i_l_]
33563360
I_l = [i_l, i_b] if ti.static(static_rigid_sim_config.batch_links_info) else i_l
33573361

33583362
for i_j in range(links_info.joint_start[I_l], links_info.joint_end[I_l]):
33593363
I_j = [i_j, i_b] if ti.static(static_rigid_sim_config.batch_joints_info) else i_j
3364+
i_d = joints_info.dof_start[I_j]
3365+
I_d = [i_d, i_b] if ti.static(static_rigid_sim_config.batch_dofs_info) else i_d
33603366

3361-
I_dof_start = (
3362-
[joints_info.dof_start[I_j], i_b]
3363-
if ti.static(static_rigid_sim_config.batch_dofs_info)
3364-
else joints_info.dof_start[I_j]
3365-
)
3366-
q_start = joints_info.q_start[I_j]
3367-
dof_limit = dofs_info.limit[I_dof_start]
3368-
3369-
if joints_info.type[I_j] == gs.JOINT_TYPE.FREE:
3370-
pass
3371-
3372-
elif (
3367+
dof_limit = dofs_info.limit[I_d]
3368+
if (
33733369
joints_info.type[I_j] == gs.JOINT_TYPE.REVOLUTE
33743370
or joints_info.type[I_j] == gs.JOINT_TYPE.PRISMATIC
3375-
):
3376-
if ti.math.isinf(dof_limit[0]) or ti.math.isinf(dof_limit[1]):
3377-
pass
3378-
else:
3379-
rigid_global_info.qpos[q_start, i_b] = dof_limit[0] + ti.random() * (
3380-
dof_limit[1] - dof_limit[0]
3381-
)
3371+
) and not ti.math.isinf(dof_limit).any():
3372+
q_start = joints_info.q_start[I_j]
3373+
rigid_global_info.qpos[q_start, i_b] = dof_limit[0] + ti.random() * (
3374+
dof_limit[1] - dof_limit[0]
3375+
)
33823376
else:
33833377
pass # When respect_joint_limit=False, we can simply continue from the last solution
33843378

tests/test_integration.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_pick_and_place(mode, show_viewer):
4747
show_viewer=show_viewer,
4848
show_FPS=False,
4949
)
50-
plane = scene.add_entity(
50+
scene.add_entity(
5151
gs.morphs.Plane(),
5252
)
5353
cube = scene.add_entity(
@@ -57,10 +57,11 @@ def test_pick_and_place(mode, show_viewer):
5757
),
5858
surface=gs.surfaces.Plastic(color=(1, 0, 0)),
5959
)
60-
cube_2 = scene.add_entity(
60+
scene.add_entity(
6161
gs.morphs.Box(
6262
size=(0.05, 0.05, 0.05),
6363
pos=(0.4, 0.2, 0.025),
64+
fixed=True,
6465
),
6566
surface=gs.surfaces.Plastic(color=(0, 1, 0)),
6667
)
@@ -159,9 +160,9 @@ def test_pick_and_place(mode, show_viewer):
159160
# release
160161
franka.control_dofs_position(np.array([0.15, 0.15]), fingers_dof)
161162

162-
for i in range(550):
163+
for i in range(180):
163164
scene.step()
164-
if i > 550:
165+
if i > 150:
165166
qvel = cube.get_dofs_velocity()
166167
assert_allclose(qvel, 0, atol=0.02)
167168

0 commit comments

Comments
 (0)