|
| 1 | +import os |
| 2 | +from timeit import default_timer as timer |
| 3 | + |
| 4 | +import torch |
| 5 | + |
| 6 | +import pytorch_kinematics as pk |
| 7 | +import pytorch_seed |
| 8 | + |
| 9 | +import pybullet as p |
| 10 | +import pybullet_data |
| 11 | + |
| 12 | +visualize = False |
| 13 | + |
| 14 | + |
| 15 | +def _make_robot_translucent(robot_id, alpha=0.4): |
| 16 | + def make_transparent(link): |
| 17 | + link_id = link[1] |
| 18 | + rgba = list(link[7]) |
| 19 | + rgba[3] = alpha |
| 20 | + p.changeVisualShape(robot_id, link_id, rgbaColor=rgba) |
| 21 | + |
| 22 | + visual_data = p.getVisualShapeData(robot_id) |
| 23 | + for link in visual_data: |
| 24 | + make_transparent(link) |
| 25 | + |
| 26 | + |
| 27 | +def test_jacobian_follower(): |
| 28 | + pytorch_seed.seed(2) |
| 29 | + device = "cuda" if torch.cuda.is_available() else "cpu" |
| 30 | + # device = "cpu" |
| 31 | + urdf = "kuka_iiwa/model.urdf" |
| 32 | + search_path = pybullet_data.getDataPath() |
| 33 | + full_urdf = os.path.join(search_path, urdf) |
| 34 | + chain = pk.build_serial_chain_from_urdf(open(full_urdf).read(), "lbr_iiwa_link_7") |
| 35 | + chain = chain.to(device=device) |
| 36 | + |
| 37 | + # robot frame |
| 38 | + pos = torch.tensor([0.0, 0.0, 0.0], device=device) |
| 39 | + rot = torch.tensor([0.0, 0.0, 0.0], device=device) |
| 40 | + rob_tf = pk.Transform3d(pos=pos, rot=rot, device=device) |
| 41 | + |
| 42 | + # world frame goal |
| 43 | + M = 1000 |
| 44 | + # generate random goal joint angles (so these are all achievable) |
| 45 | + # use the joint limits to generate random joint angles |
| 46 | + lim = torch.tensor(chain.get_joint_limits(), device=device) |
| 47 | + goal_q = torch.rand(M, 7, device=device) * (lim[1] - lim[0]) + lim[0] |
| 48 | + |
| 49 | + # get ee pose (in robot frame) |
| 50 | + goal_in_rob_frame_tf = chain.forward_kinematics(goal_q) |
| 51 | + |
| 52 | + # transform to world frame for visualization |
| 53 | + goal_tf = rob_tf.compose(goal_in_rob_frame_tf) |
| 54 | + goal = goal_tf.get_matrix() |
| 55 | + goal_pos = goal[..., :3, 3] |
| 56 | + goal_rot = pk.matrix_to_euler_angles(goal[..., :3, :3], "XYZ") |
| 57 | + |
| 58 | + ik = pk.PseudoInverseIK(chain, max_iterations=30, num_retries=10, |
| 59 | + joint_limits=lim.T, |
| 60 | + early_stopping_any_converged=True, |
| 61 | + early_stopping_no_improvement="all", |
| 62 | + # line_search=pk.BacktrackingLineSearch(max_lr=0.2), |
| 63 | + debug=False, |
| 64 | + lr=0.2) |
| 65 | + |
| 66 | + # do IK |
| 67 | + timer_start = timer() |
| 68 | + sol = ik.solve(goal_in_rob_frame_tf) |
| 69 | + timer_end = timer() |
| 70 | + print("IK took %f seconds" % (timer_end - timer_start)) |
| 71 | + print("IK converged number: %d / %d" % (sol.converged.sum(), sol.converged.numel())) |
| 72 | + print("IK took %d iterations" % sol.iterations) |
| 73 | + print("IK solved %d / %d goals" % (sol.converged_any.sum(), M)) |
| 74 | + |
| 75 | + # visualize everything |
| 76 | + if visualize: |
| 77 | + p.connect(p.GUI) |
| 78 | + p.setRealTimeSimulation(False) |
| 79 | + p.configureDebugVisualizer(p.COV_ENABLE_GUI, 0) |
| 80 | + p.setAdditionalSearchPath(search_path) |
| 81 | + |
| 82 | + yaw = 90 |
| 83 | + pitch = -55 |
| 84 | + dist = 1. |
| 85 | + target = goal_pos[0].cpu().numpy() |
| 86 | + p.resetDebugVisualizerCamera(dist, yaw, pitch, target) |
| 87 | + |
| 88 | + p.loadURDF("plane.urdf", [0, 0, 0], useFixedBase=True) |
| 89 | + m = rob_tf.get_matrix() |
| 90 | + pos = m[0, :3, 3] |
| 91 | + rot = m[0, :3, :3] |
| 92 | + quat = pk.matrix_to_quaternion(rot) |
| 93 | + pos = pos.cpu().numpy() |
| 94 | + rot = pk.wxyz_to_xyzw(quat).cpu().numpy() |
| 95 | + armId = p.loadURDF(urdf, basePosition=pos, baseOrientation=rot, useFixedBase=True) |
| 96 | + |
| 97 | + _make_robot_translucent(armId, alpha=0.6) |
| 98 | + # p.resetBasePositionAndOrientation(armId, [0, 0, 0], [0, 0, 0, 1]) |
| 99 | + # draw goal |
| 100 | + # place a translucent sphere at the goal |
| 101 | + show_max_num_retries_per_goal = 10 |
| 102 | + for goal_num in range(M): |
| 103 | + # draw cone to indicate pose instead of sphere |
| 104 | + visId = p.createVisualShape(p.GEOM_MESH, fileName="meshes/cone.obj", meshScale=1.0, |
| 105 | + rgbaColor=[0., 1., 0., 0.5]) |
| 106 | + # visId = p.createVisualShape(p.GEOM_SPHERE, radius=0.05, rgbaColor=[0., 1., 0., 0.5]) |
| 107 | + r = goal_rot[goal_num] |
| 108 | + xyzw = pk.wxyz_to_xyzw(pk.matrix_to_quaternion(pk.euler_angles_to_matrix(r, "XYZ"))) |
| 109 | + goalId = p.createMultiBody(baseMass=0, baseVisualShapeIndex=visId, |
| 110 | + basePosition=goal_pos[goal_num].cpu().numpy(), |
| 111 | + baseOrientation=xyzw.cpu().numpy()) |
| 112 | + |
| 113 | + solutions = sol.solutions[goal_num] |
| 114 | + # sort based on if they converged |
| 115 | + converged = sol.converged[goal_num] |
| 116 | + idx = torch.argsort(converged.to(int), descending=True) |
| 117 | + solutions = solutions[idx] |
| 118 | + |
| 119 | + # print how many retries converged for this one |
| 120 | + print("Goal %d converged %d / %d" % (goal_num, converged.sum(), converged.numel())) |
| 121 | + |
| 122 | + for i, q in enumerate(solutions): |
| 123 | + if i > show_max_num_retries_per_goal: |
| 124 | + break |
| 125 | + for dof in range(q.shape[0]): |
| 126 | + p.resetJointState(armId, dof, q[dof]) |
| 127 | + input("Press enter to continue") |
| 128 | + |
| 129 | + p.removeBody(goalId) |
| 130 | + |
| 131 | + while True: |
| 132 | + p.stepSimulation() |
| 133 | + |
| 134 | + |
| 135 | +if __name__ == "__main__": |
| 136 | + test_jacobian_follower() |
0 commit comments