def fix_pose(self, name, pose=None, fraction=0.5): body = self.get_body(name) if pose is None: pose = get_pose(body) else: set_pose(body, pose) # TODO: can also drop in simulation x, y, z = point_from_pose(pose) roll, pitch, yaw = euler_from_quat(quat_from_pose(pose)) quat = quat_from_euler(Euler(yaw=yaw)) set_quat(body, quat) surface_name = self.get_supporting(name) if surface_name is None: return None, None if fraction == 0: new_pose = (Point(x, y, z), quat) return new_pose, surface_name surface_aabb = compute_surface_aabb(self, surface_name) new_z = (1 - fraction) * z + fraction * stable_z_on_aabb( body, surface_aabb) point = Point(x, y, new_z) set_point(body, point) print('{} error: roll={:.3f}, pitch={:.3f}, z-delta: {:.3f}'.format( name, roll, pitch, new_z - z)) new_pose = (point, quat) return new_pose, surface_name
def main(use_pr2_drake=True): connect(use_gui=True) add_data_path() plane = p.loadURDF("plane.urdf") table_path = "models/table_collision/table.urdf" table = load_pybullet(table_path, fixed_base=True) set_quat(table, quat_from_euler(Euler(yaw=PI / 2))) # table/table.urdf, table_square/table_square.urdf, cube.urdf, block.urdf, door.urdf obstacles = [plane, table] pr2_urdf = DRAKE_PR2_URDF if use_pr2_drake else PR2_URDF with HideOutput(): pr2 = load_model(pr2_urdf, fixed_base=True) # TODO: suppress warnings? dump_body(pr2) z = base_aligned_z(pr2) print(z) #z = stable_z_on_aabb(pr2, AABB(np.zeros(3), np.zeros(3))) print(z) set_point(pr2, Point(z=z)) print(get_aabb(pr2)) wait_if_gui() base_start = (-2, -2, 0) base_goal = (2, 2, 0) arm_start = SIDE_HOLDING_LEFT_ARM #arm_start = TOP_HOLDING_LEFT_ARM #arm_start = REST_LEFT_ARM arm_goal = TOP_HOLDING_LEFT_ARM #arm_goal = SIDE_HOLDING_LEFT_ARM left_joints = joints_from_names(pr2, PR2_GROUPS['left_arm']) right_joints = joints_from_names(pr2, PR2_GROUPS['right_arm']) torso_joints = joints_from_names(pr2, PR2_GROUPS['torso']) set_joint_positions(pr2, left_joints, arm_start) set_joint_positions(pr2, right_joints, rightarm_from_leftarm(REST_LEFT_ARM)) set_joint_positions(pr2, torso_joints, [0.2]) open_arm(pr2, 'left') # test_ikfast(pr2) add_line(base_start, base_goal, color=RED) print(base_start, base_goal) if use_pr2_drake: test_drake_base_motion(pr2, base_start, base_goal, obstacles=obstacles) else: test_base_motion(pr2, base_start, base_goal, obstacles=obstacles) test_arm_motion(pr2, left_joints, arm_goal) # test_arm_control(pr2, left_joints, arm_start) wait_if_gui('Finish?') disconnect()
def create_elements_bodies(node_points, elements, color=apply_alpha(RED, alpha=1), diameter=ELEMENT_DIAMETER, shrink=ELEMENT_SHRINK): # TODO: could scale the whole environment # TODO: create a version without shrinking for transit planning # URDF_USE_IMPLICIT_CYLINDER element_bodies = [] for (n1, n2) in elements: p1, p2 = node_points[n1], node_points[n2] height = max(np.linalg.norm(p2 - p1) - 2 * shrink, 0) #if height == 0: # Cannot keep this here # continue center = (p1 + p2) / 2 # extents = (p2 - p1) / 2 delta = p2 - p1 x, y, z = delta phi = np.math.atan2(y, x) theta = np.math.acos(z / np.linalg.norm(delta)) quat = quat_from_euler(Euler(pitch=theta, yaw=phi)) # p1 is z=-height/2, p2 is z=+height/2 # Much smaller than cylinder # Also faster, C_shape 177.398 vs 400 body = create_box(diameter, diameter, height, color=color, mass=STATIC_MASS) set_color(body, color) set_point(body, center) set_quat(body, quat) #dump_body(body, fixed=True) # Visually, smallest diameter is 2e-3 # The geometries and bounding boxes seem correct though # TODO: create_cylinder takes in a radius not diameter #body = create_cylinder(ELEMENT_DIAMETER, height, color=color, mass=STATIC_MASS) #print('Diameter={:.5f} | Height={:.5f}'.format(ELEMENT_DIAMETER/2., height)) #print(get_aabb_extent(get_aabb(body)).round(6).tolist()) #print(get_visual_data(body)) #print(get_collision_data(body)) #set_point(body, center) #set_quat(body, quat) #draw_aabb(get_aabb(body)) element_bodies.append(body) #wait_for_user() return element_bodies
def test_stir(visualize): # TODO: change the stirrer coordinate frame to be on its side arms = [LEFT_ARM] #spoon_name = 'spoon' # spoon.urdf is very messy and has a bad bounding box #spoon_quat = multiply_quats(quat_from_euler(Euler(pitch=-np.pi/2 - np.pi/16)), quat_from_euler(Euler(yaw=-np.pi/8))) #spoon_name, spoon_quat = 'stirrer', quat_from_euler(Euler(roll=-np.pi/2, yaw=np.pi/2)) spoon_name, spoon_quat = 'grey_spoon', quat_from_euler(Euler(yaw=-np.pi/2)) # grey_spoon | orange_spoon | green_spoon # *_spoon points in +y #bowl_name = 'bowl' bowl_name = 'whitebowl' items = [spoon_name, bowl_name] world, table_body = create_world(items, visualize=visualize) set_quat(world.get_body(spoon_name), spoon_quat) # get_reference_pose(spoon_name) initial_positions = { #spoon_name: [0.5, -0.2, 0], spoon_name: [0.3, 0.5, 0], bowl_name: [0.6, 0.1, 0], } with ClientSaver(world.perception.client): for name, point in initial_positions.items(): body = world.perception.sim_bodies[name] point[2] += stable_z(body, table_body) + Z_EPSILON world.perception.set_pose(name, point, get_quat(body)) #draw_aabb(get_aabb(body)) #wait_for_interrupt() #enable_gravity() #for i in range(10): # simulate_for_sim_duration(sim_duration=0.05, frequency=0.1) # print(get_quat(world.perception.sim_bodies[spoon_name])) # raw_input('Continue?') update_world(world, table_body) init_holding = hold_item(world, arms[0], spoon_name) assert init_holding is not None # TODO: add the concept of a recipe init = [ ('Contains', bowl_name, COFFEE), ('Contains', bowl_name, SUGAR), ] goal = [ #('Holding', spoon_name), ('Mixed', bowl_name), ] task = Task(init=init, init_holding=init_holding, goal=goal, arms=arms, reset_arms=False) return world, task
def estimate_spoon_capacity(world, spoon_name, beads, max_beads=100): beads = beads[:max_beads] if not beads: return 0 bead_radius = np.average(approximate_as_prism(beads[0])) / 2. spoon_body = world.get_body(spoon_name) spoon_mass = get_mass(spoon_body) set_mass(spoon_body, STATIC_MASS) set_point(spoon_body, (1, 0, 1)) set_quat(spoon_body, get_liquid_quat(spoon_name)) capacity_beads = fill_with_beads(world, spoon_name, beads, reset_contained=True, fix_outside=False, height_fraction=1.0, top_threshold=bead_radius) #wait_for_user() set_mass(spoon_body, spoon_mass) return len(capacity_beads)
def test_scoop(visualize): # TODO: start with the spoon in a hand arms = [LEFT_ARM] spoon_name = 'grey_spoon' # green_spoon | grey_spoon | orange_spoon spoon_quat = quat_from_euler(Euler(yaw=-np.pi/2)) # *_spoon points in +y bowl1_name = 'whitebowl' bowl2_name = 'bowl' items = [spoon_name, bowl1_name, bowl2_name] world, table_body = create_world(items, visualize=visualize) set_quat(world.get_body(spoon_name), spoon_quat) initial_positions = { spoon_name: [0.3, 0.5, 0], bowl1_name: [0.5, 0.1, 0], bowl2_name: [0.6, 0.5, 0], } with ClientSaver(world.perception.client): for name, point in initial_positions.items(): body = world.perception.sim_bodies[name] point[2] += stable_z(body, table_body) + Z_EPSILON world.perception.set_pose(name, point, get_quat(body)) update_world(world, table_body) init = [ ('Contains', bowl1_name, COFFEE), #('Contains', spoon_name, WATER), ] goal = [ #('Contains', spoon_name, WATER), ('Contains', bowl2_name, COFFEE), ] task = Task(init=init, goal=goal, arms=arms, reset_arms=True) # TODO: plan while not spilling the spoon return world, task