def place(self, position, heightmap_rotation_angle, place_vertical_offset=0.04): logging.info('Executing: place at (%f, %f, %f)' % (position[0], position[1], position[2])) # Ensure gripper is closed gripper_fully_closed = self.close_gripper() if gripper_fully_closed: # There is no object present, so we cannot possibly place! return False # Compute tool orientation from heightmap rotation angle tool_rotation_angle = (heightmap_rotation_angle % np.pi) - np.pi / 2 # Avoid collision with floor position[2] = max(position[2] + place_vertical_offset, self.workspace_limits[2][0] + 0.02) # Move gripper to location above place target place_location_margin = 0.1 sim_ret, UR5_target_handle = vrep.simxGetObjectHandle( self.sim_client, 'UR5_target', vrep.simx_opmode_blocking) location_above_place_target = (position[0], position[1], position[2] + place_location_margin) self.move_to(location_above_place_target, None) sim_ret, gripper_orientation = vrep.simxGetObjectOrientation( self.sim_client, UR5_target_handle, -1, vrep.simx_opmode_blocking) if tool_rotation_angle - gripper_orientation[1] > 0: increment = 0.2 else: increment = -0.2 while abs(tool_rotation_angle - gripper_orientation[1]) >= 0.2: vrep.simxSetObjectOrientation( self.sim_client, UR5_target_handle, -1, (np.pi / 2, gripper_orientation[1] + increment, np.pi / 2), vrep.simx_opmode_blocking) time.sleep(0.01) sim_ret, gripper_orientation = vrep.simxGetObjectOrientation( self.sim_client, UR5_target_handle, -1, vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation( self.sim_client, UR5_target_handle, -1, (np.pi / 2, tool_rotation_angle, np.pi / 2), vrep.simx_opmode_blocking) # Approach place target self.move_to(position, None) # Ensure gripper is open self.open_gripper() # Move gripper to location above place target self.move_to(location_above_place_target, None) return True
def add_structure(cid): object_name = 'box_large' res, object_handle = vrep.simxGetObjectHandle(cid, object_name, vrep.simx_opmode_oneshot_wait) # object_pos = [random.uniform(-0.15, 0.15), random.uniform(-0.15, 0.15), 0.20] object_pos = [0, 0, 0.075] object_angle = [-1.5708, random.uniform(-1.5707, 1.5707), -1.5708] vrep.simxSetObjectOrientation(cid, object_handle, -1, object_angle, vrep.simx_opmode_oneshot) vrep.simxSetObjectPosition(cid, object_handle, -1, object_pos, vrep.simx_opmode_oneshot) time.sleep(1.0) return object_name, object_handle
def add_object(cid, object_name, object_pos): # object_name = random.choice(['apple', 'banana', 'sugar_box', 'cracker_box', 'mustard_bottle', 'lemon', 'orange', # 'tomato_soup_can']) res, object_handle = vrep.simxGetObjectHandle(cid, object_name, vrep.simx_opmode_oneshot_wait) # object_pos = [random.uniform(-0.15, 0.15), random.uniform(-0.15, 0.15), 0.20] object_angle = [random.uniform(-np.pi, np.pi), random.uniform(-np.pi, np.pi), random.uniform(-np.pi, np.pi)] # object_angle = [0, 0, 0] vrep.simxSetObjectOrientation(cid, object_handle, -1, object_angle, vrep.simx_opmode_oneshot) vrep.simxSetObjectPosition(cid, object_handle, -1, object_pos, vrep.simx_opmode_oneshot) time.sleep(1.0) return object_name, object_handle
def scenario_one(cid): name_list = [] locations = [[0.15, 0, 0.1], [-0.15, 0, 0.1], [0, 0.15, 0.15],[0, -0.15, 0.1]] handle_list = [] for i in range(0, 4): object_name = 'imported_part_' + str(i) res, object_handle = vrep.simxGetObjectHandle(cid, object_name, vrep.simx_opmode_oneshot_wait) # object_angle = [random.uniform(-np.pi, np.pi), random.uniform(-np.pi, np.pi), random.uniform(-np.pi, np.pi)] object_angle = [0, 0, 0] vrep.simxSetObjectOrientation(cid, object_handle, -1, object_angle, vrep.simx_opmode_oneshot) vrep.simxSetObjectPosition(cid, object_handle, -1, locations[i], vrep.simx_opmode_oneshot) time.sleep(1.0) name_list.append(object_name) handle_list.append(object_handle) return name_list, handle_list
def reposition_objects(self, workspace_limits): # Move gripper out of the way self.move_to([-0.1, 0, 0.3], None) for object_handle in self.object_handles: # Drop object at random x,y location and random orientation in robot workspace drop_x = (workspace_limits[0][1] - workspace_limits[0][0] - 0.2) * np.random.random_sample() + \ workspace_limits[0][0] + 0.1 drop_y = (workspace_limits[1][1] - workspace_limits[1][0] - 0.2) * np.random.random_sample() + \ workspace_limits[1][0] + 0.1 object_position = [drop_x, drop_y, 0.15] object_orientation = [ 2 * np.pi * np.random.random_sample(), 2 * np.pi * np.random.random_sample(), 2 * np.pi * np.random.random_sample() ] vrep.simxSetObjectPosition(self.sim_client, object_handle, -1, object_position, vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation(self.sim_client, object_handle, -1, object_orientation, vrep.simx_opmode_blocking) time.sleep(2)
def grasp_gnn_data(): wd = '/home/lou00015/data/gnn/' cid = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5) eid = 563 nb_grasp = 25 if cid != -1: pos = [0, 0, 0.15] while True: vrep.simxStartSimulation(cid, vrep.simx_opmode_blocking) panda = Robot(cid) obj_name, obj_hdl = add_object(cid, 'imported_part_0', pos) time.sleep(1.0) cloud = panda.get_pointcloud() centroid = np.average(cloud, axis=0) print('centroid: ', centroid) res, init_pos = vrep.simxGetObjectPosition(cid, obj_hdl, -1, vrep.simx_opmode_blocking) res, init_ori = vrep.simxGetObjectOrientation(cid, obj_hdl, -1, vrep.simx_opmode_blocking) if len(cloud) == 0: print('no cloud found') continue elif centroid[2] > 0.045: print('perception error') continue np.save(wd + 'cloud_' + str(eid) + '.npy', cloud) # save point cloud cloud = np.delete(cloud, np.where(cloud[:,2]<0.015), axis=0) pose_set, pt_set = grasp_pose_generation(45, cloud, nb_grasp) for i in range(0, nb_grasp): pose = pose_set[i] pt = pt_set[i] emptyBuff = bytearray() landing_mtx = [pose[0][0],pose[0][1],pose[0][2],pt[0], pose[1][0],pose[1][1], pose[1][2],pt[1], pose[2][0],pose[2][1],pose[2][2],pt[2]] np.save(wd + 'cloud_' + str(eid) + '_action_' + str(i) + '.npy', landing_mtx) # save action vrep.simxCallScriptFunction(cid, 'landing', vrep.sim_scripttype_childscript, 'setlanding', [], landing_mtx, [], emptyBuff, vrep.simx_opmode_blocking) ending_mtx = [pose[0][0], pose[0][1], pose[0][2], pt[0], pose[1][0], pose[1][1], pose[1][2], pt[1], pose[2][0], pose[2][1], pose[2][2], pt[2]+0.15] vrep.simxCallScriptFunction(cid, 'ending', vrep.sim_scripttype_childscript, 'setending', [], ending_mtx, [], emptyBuff, vrep.simx_opmode_blocking) time.sleep(1.0) print('executing experiment %d: ' % (eid*25+i)) print('at: ', pt) vrep.simxCallScriptFunction(cid, 'Sphere', vrep.sim_scripttype_childscript, 'grasp', [], [], [], emptyBuff, vrep.simx_opmode_blocking) while True: res, finish = vrep.simxGetIntegerSignal(cid, "finish", vrep.simx_opmode_oneshot_wait) if finish == 18: res, end_pos = vrep.simxGetObjectPosition(cid, obj_hdl, -1, vrep.simx_opmode_blocking) break if end_pos[2]>0.05: label = 1 else: label = 0 print(label) f = open(wd + 'label.txt', 'a+') f.write(str(label)) f.close() vrep.simxSetObjectPosition(cid, obj_hdl, -1, init_pos, vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation(cid, obj_hdl, -1, init_ori, vrep.simx_opmode_blocking) eid += 1 else: print('Failed to connect to simulation (V-REP remote API server). Exiting.') exit()
def push(self, position, heightmap_rotation_angle, push_vertical_offset=0.01, pushing_point_margin=0.1): logging.info('Executing: push at (%f, %f, %f)' % (position[0], position[1], position[2])) # Compute tool orientation from heightmap rotation angle tool_rotation_angle = (heightmap_rotation_angle % np.pi) - np.pi / 2 # Adjust pushing point to be on tip of finger position[2] = position[2] + push_vertical_offset # Compute pushing direction push_orientation = [1.0, 0.0] push_direction = np.asarray([ push_orientation[0] * np.cos(heightmap_rotation_angle) - push_orientation[1] * np.sin(heightmap_rotation_angle), push_orientation[0] * np.sin(heightmap_rotation_angle) + push_orientation[1] * np.cos(heightmap_rotation_angle) ]) # Move gripper to location above pushing point location_above_pushing_point = (position[0], position[1], position[2] + pushing_point_margin) # Compute gripper position and linear movement increments tool_position = location_above_pushing_point sim_ret, UR5_target_position = vrep.simxGetObjectPosition( self.sim_client, self.UR5_target_handle, -1, vrep.simx_opmode_blocking) move_direction = np.asarray([ tool_position[0] - UR5_target_position[0], tool_position[1] - UR5_target_position[1], tool_position[2] - UR5_target_position[2] ]) move_magnitude = np.linalg.norm(move_direction) move_step = 0.05 * move_direction / move_magnitude try: num_move_steps = int(np.floor(move_direction[0] / move_step[0])) except ValueError: return False # Compute gripper orientation and rotation increments sim_ret, gripper_orientation = vrep.simxGetObjectOrientation( self.sim_client, self.UR5_target_handle, -1, vrep.simx_opmode_blocking) rotation_step = 0.3 if ( tool_rotation_angle - gripper_orientation[1] > 0) else -0.3 num_rotation_steps = int( np.floor((tool_rotation_angle - gripper_orientation[1]) / rotation_step)) # Simultaneously move and rotate gripper for step_iter in range(max(num_move_steps, num_rotation_steps)): vrep.simxSetObjectPosition( self.sim_client, self.UR5_target_handle, -1, (UR5_target_position[0] + move_step[0] * min(step_iter, num_move_steps), UR5_target_position[1] + move_step[1] * min(step_iter, num_move_steps), UR5_target_position[2] + move_step[2] * min(step_iter, num_move_steps)), vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation( self.sim_client, self.UR5_target_handle, -1, (np.pi / 2, gripper_orientation[1] + rotation_step * min(step_iter, num_rotation_steps), np.pi / 2), vrep.simx_opmode_blocking) vrep.simxSetObjectPosition( self.sim_client, self.UR5_target_handle, -1, (tool_position[0], tool_position[1], tool_position[2]), vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation( self.sim_client, self.UR5_target_handle, -1, (np.pi / 2, tool_rotation_angle, np.pi / 2), vrep.simx_opmode_blocking) # Ensure gripper is closed self.close_gripper() # Approach pushing point self.move_to(position, None) # Compute target location (push to the right) push_length = 0.1 target_x = min( max(position[0] + push_direction[0] * push_length, self.workspace_limits[0][0]), self.workspace_limits[0][1]) target_y = min( max(position[1] + push_direction[1] * push_length, self.workspace_limits[1][0]), self.workspace_limits[1][1]) push_length = np.sqrt( np.power(target_x - position[0], 2) + np.power(target_y - position[1], 2)) # Move in pushing direction towards target location self.move_to([target_x, target_y, position[2]], None) # Move gripper to location above grasp target self.move_to([target_x, target_y, location_above_pushing_point[2]], None) push_success = True return push_success
def grasp( self, position, heightmap_rotation_angle, grasp_vertical_offset=-0.04, grasp_location_margin=0.15, ): logging.info('Executing: grasp at (%f, %f, %f)' % (position[0], position[1], position[2])) # Compute tool orientation from heightmap rotation angle tool_rotation_angle = (heightmap_rotation_angle % np.pi) - np.pi / 2 # Avoid collision with floor position = np.asarray(position).copy() position[2] = max(position[2] + grasp_vertical_offset, self.workspace_limits[2][0] + 0.02) # Move gripper to location above grasp target location_above_grasp_target = (position[0], position[1], position[2] + grasp_location_margin) # Compute gripper position and linear movement increments tool_position = location_above_grasp_target sim_ret, UR5_target_position = vrep.simxGetObjectPosition( self.sim_client, self.UR5_target_handle, -1, vrep.simx_opmode_blocking) move_direction = np.asarray([ tool_position[0] - UR5_target_position[0], tool_position[1] - UR5_target_position[1], tool_position[2] - UR5_target_position[2] ]) move_magnitude = np.linalg.norm(move_direction) move_step = 0.05 * move_direction / move_magnitude try: num_move_steps = int(np.floor(move_direction[0] / move_step[0])) except ValueError: return False # Compute gripper orientation and rotation increments sim_ret, gripper_orientation = vrep.simxGetObjectOrientation( self.sim_client, self.UR5_target_handle, -1, vrep.simx_opmode_blocking) rotation_step = 0.3 if ( tool_rotation_angle - gripper_orientation[1] > 0) else -0.3 num_rotation_steps = int( np.floor((tool_rotation_angle - gripper_orientation[1]) / rotation_step)) # Simultaneously move and rotate gripper for step_iter in range(max(num_move_steps, num_rotation_steps)): vrep.simxSetObjectPosition( self.sim_client, self.UR5_target_handle, -1, (UR5_target_position[0] + move_step[0] * min(step_iter, num_move_steps), UR5_target_position[1] + move_step[1] * min(step_iter, num_move_steps), UR5_target_position[2] + move_step[2] * min(step_iter, num_move_steps)), vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation( self.sim_client, self.UR5_target_handle, -1, (np.pi / 2, gripper_orientation[1] + rotation_step * min(step_iter, num_rotation_steps), np.pi / 2), vrep.simx_opmode_blocking) vrep.simxSetObjectPosition( self.sim_client, self.UR5_target_handle, -1, (tool_position[0], tool_position[1], tool_position[2]), vrep.simx_opmode_blocking) vrep.simxSetObjectOrientation( self.sim_client, self.UR5_target_handle, -1, (np.pi / 2, tool_rotation_angle, np.pi / 2), vrep.simx_opmode_blocking) # Ensure gripper is open self.open_gripper() # Approach grasp target self.move_to(position, None) # Close gripper to grasp target gripper_full_closed = self.close_gripper() # Move gripper to location above grasp target self.move_to(location_above_grasp_target, None) # Check if grasp is successful gripper_full_closed = self.close_gripper() grasp_success = not gripper_full_closed # Move the grasped object elsewhere if grasp_success: if self.place_enabled: self.go_home() else: self.num_obj_clear += 1 object_positions = np.asarray(self.get_obj_positions()) object_positions = object_positions[:, 2] grasped_object_ind = np.argmax(object_positions) grasped_object_handle = self.object_handles[grasped_object_ind] vrep.simxSetObjectPosition( self.sim_client, grasped_object_handle, -1, (-0.5, 0.5 + 0.05 * float(grasped_object_ind), 0.1), vrep.simx_opmode_blocking) return grasp_success