"""Revised StackPyramid expert: the native solution with five failure causes removed. Same primitives, poses and planner settings as mani_skill.examples.motionplanning.panda.solutions.stack_pyramid, except: 1. Push condition. The native expert pushes A only when A is more than 7 cm from B, but success needs A within 6.16 cm (xy) of B, so scenes starting 6.16-7 cm apart fail without a push. Here A is pushed when it is more than PUSH_IF_M from B. 2. Push goal. The native expert drags A to 0.8 x B's world position, which is 0.2 |B| from B whatever the side A comes from (up to 4.5 cm, and A ends 0.3-0.5 cm behind the fingers), so A sometimes stops beyond 6.16 cm. Here the TCP drags A to PUSH_GAP_M from B on A's own side. 3. Press depth. The native expert presses on A towards a TCP at A's centre height, about 3 cm below where the closed fingers rest on A's top; the press loads A onto the table, the arm lags behind its push path (joint paths) or stops short of it (keypose rule), and A ends 1.5-2.5 cm short of the goal. Here the descent onto A and the push aim the TCP at PRESS_Z_M, about 1 cm below the resting height. 4. Lift-off. The native expert leaves the push by moving straight towards the pose above C, from fingers pressed on A's top; executed at the keypose rule's speed this drags A off B. Here the TCP first rises straight up from where it stopped to LIFT_Z_M. 5. Grasp orientation for C. The native expert takes the face pair closest to the TCP's current closing axis and only rotates when the planner cannot reach it, so the open fingers can land on A or B next to C. Here the face pair whose finger footprints keep the larger clearance from A and B comes first; the native order of +-angles for reachability is kept. """ import numpy as np import sapien from mani_skill.examples.motionplanning.base_motionplanner.utils import compute_grasp_info_by_obb, get_actor_obb from mani_skill.examples.motionplanning.panda.motionplanner import PandaArmMotionPlanningSolver from transforms3d.euler import euler2quat PUSH_IF_M = 0.055 PRESS_Z_M = 0.04 PUSH_GAP_M = 0.045 LIFT_Z_M = 0.10 FINGER_INNER_M, FINGER_OUTER_M, FINGER_HALF_WIDTH_M = 0.04, 0.052, 0.01 """Open-finger footprint along the closing axis from the TCP, and half its width across.""" def square_distance(point, centre, yaw, half=0.02): """Signed xy distance from a point to a cube's square footprint (negative inside).""" c, s = np.cos(-yaw), np.sin(-yaw) d = np.array([[c, -s], [s, c]]) @ (np.asarray(point) - np.asarray(centre)) q = np.abs(d) - half return float(np.linalg.norm(np.maximum(q, 0)) + min(max(q[0], q[1]), 0)) def clearance(centre, closing, neighbours): """Smallest distance between the two open-finger footprints around `centre` and the neighbour cubes.""" closing = np.asarray(closing[:2]) / np.linalg.norm(closing[:2]) across = np.array([-closing[1], closing[0]]) points = [centre + sign * along * closing + width * across for sign in (1, -1) for along in np.linspace(FINGER_INNER_M, FINGER_OUTER_M, 4) for width in np.linspace(-FINGER_HALF_WIDTH_M, FINGER_HALF_WIDTH_M, 5)] return min(square_distance(p, c, yaw) for p in points for c, yaw in neighbours) def yaw_of(actor): w, x, y, z = actor.pose.q[0].cpu().numpy() return float(np.arctan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z))) def solve(env, seed=None, debug=False, vis=False): env.reset(seed=seed) assert env.unwrapped.control_mode in ["pd_joint_pos", "pd_joint_pos_vel"], env.unwrapped.control_mode planner = PandaArmMotionPlanningSolver( env, debug=debug, vis=vis, base_pose=env.unwrapped.agent.robot.pose, visualize_target_grasp_pose=vis, print_env_info=False, ) FINGER_LENGTH = 0.025 env = env.unwrapped approaching = np.array([0, 0, -1]) a, b = env.cubeA.pose.sp.p, env.cubeB.pose.sp.p if np.linalg.norm((a - b)[:2]) > PUSH_IF_M: target_closing = env.agent.tcp.pose.to_transformation_matrix()[0, :3, 1].cpu().numpy() closing = compute_grasp_info_by_obb(get_actor_obb(env.cubeA), approaching=approaching, target_closing=target_closing, depth=FINGER_LENGTH)["closing"] planner.close_gripper() grasp_pose = env.agent.build_grasp_pose(approaching, closing, np.r_[a[:2], PRESS_Z_M]) planner.move_to_pose_with_screw(grasp_pose * sapien.Pose([0, 0, -0.05])) planner.move_to_pose_with_screw(grasp_pose) planner.close_gripper() side = (a - b)[:2] / np.linalg.norm((a - b)[:2]) goal = np.r_[b[:2] + PUSH_GAP_M * side, grasp_pose.p[2]] planner.move_to_pose_with_screw(sapien.Pose(goal, grasp_pose.q)) tcp = env.agent.tcp.pose.sp.p planner.move_to_pose_with_screw(sapien.Pose(np.r_[tcp[:2], LIFT_Z_M], grasp_pose.q)) pushed = True else: pushed = False target_closing = env.agent.tcp.pose.to_transformation_matrix()[0, :3, 1].cpu().numpy() info = compute_grasp_info_by_obb(get_actor_obb(env.cubeC), approaching=approaching, target_closing=target_closing, depth=FINGER_LENGTH) closing, center = info["closing"], info["center"] base = env.agent.build_grasp_pose(approaching, closing, center) neighbours = [(actor.pose.sp.p[:2], yaw_of(actor)) for actor in (env.cubeA, env.cubeB)] order = sorted((0.0, np.pi / 2), key=lambda angle: -clearance( center[:2], (base * sapien.Pose(q=euler2quat(0, 0, angle))).to_transformation_matrix()[:3, 1], neighbours)) grasp_pose = base for angle in [x for first in order for x in (first, -first)]: candidate = base * sapien.Pose(q=euler2quat(0, 0, angle)) if planner.move_to_pose_with_screw(candidate, dry_run=True) == -1: continue grasp_pose = candidate break else: print("Fail to find a valid grasp pose") planner.move_to_pose_with_screw(grasp_pose * sapien.Pose([0, 0, -0.05])) if pushed: planner.open_gripper() planner.move_to_pose_with_screw(grasp_pose) planner.close_gripper() lift_pose = sapien.Pose([0, 0, 0.1]) * grasp_pose planner.move_to_pose_with_screw(lift_pose) goal_pose_A = env.cubeA.pose * sapien.Pose([0, 0, env.cube_half_size[2] * 2]) goal_pose_B = env.cubeB.pose * sapien.Pose([0, 0, env.cube_half_size[2] * 2]) goal_pose_p = (goal_pose_A.p + goal_pose_B.p) / 2 offset = (goal_pose_p - env.cubeC.pose.p).cpu().numpy()[0] planner.move_to_pose_with_screw(sapien.Pose(lift_pose.p + offset, lift_pose.q)) res = planner.open_gripper() planner.close() return res