|
import os |
|
import time |
|
from turtle import pos |
|
|
|
import numpy as np |
|
import pybullet as p |
|
|
|
from surrol.const import ASSET_DIR_PATH |
|
from surrol.tasks.psm_env import PsmEnv, goal_distance |
|
from surrol.utils.pybullet_utils import get_link_pose, wrap_angle |
|
|
|
|
|
class PegTransfer(PsmEnv): |
|
POSE_BOARD = ((0.55, 0, 0.6861), (0, 0, 0)) |
|
WORKSPACE_LIMITS = ((0.50, 0.60), (-0.05, 0.05), (0.686, 0.745)) |
|
SCALING = 5. |
|
|
|
|
|
|
|
def _env_setup(self): |
|
super(PegTransfer, self)._env_setup() |
|
self.has_object = True |
|
|
|
|
|
workspace_limits = self.workspace_limits1 |
|
pos = (workspace_limits[0][0], |
|
workspace_limits[1][1], |
|
workspace_limits[2][1]) |
|
orn = (0.5, 0.5, -0.5, -0.5) |
|
joint_positions = self.psm1.inverse_kinematics((pos, orn), self.psm1.EEF_LINK_INDEX) |
|
self.psm1.reset_joint(joint_positions) |
|
self.block_gripper = False |
|
|
|
|
|
obj_id = p.loadURDF(os.path.join(ASSET_DIR_PATH, 'peg_board/peg_board.urdf'), |
|
np.array(self.POSE_BOARD[0]) * self.SCALING, |
|
p.getQuaternionFromEuler(self.POSE_BOARD[1]), |
|
globalScaling=self.SCALING) |
|
self.obj_ids['fixed'].append(obj_id) |
|
self._pegs = np.arange(12) |
|
np.random.shuffle(self._pegs[:6]) |
|
np.random.shuffle(self._pegs[6: 12]) |
|
|
|
|
|
|
|
num_blocks = 4 |
|
|
|
for i in self._pegs[6: 6 + num_blocks]: |
|
pos, orn = get_link_pose(self.obj_ids['fixed'][1], i) |
|
yaw = (np.random.rand() - 0.5) * np.deg2rad(60) |
|
obj_id = p.loadURDF(os.path.join(ASSET_DIR_PATH, 'block/block.urdf'), |
|
np.array(pos) + np.array([0, 0, 0.03]), |
|
p.getQuaternionFromEuler((0, 0, yaw)), |
|
useFixedBase=False, |
|
globalScaling=self.SCALING) |
|
self.obj_ids['rigid'].append(obj_id) |
|
self._blocks = np.array(self.obj_ids['rigid'][-num_blocks:]) |
|
np.random.shuffle(self._blocks) |
|
|
|
for obj_id in self._blocks[:1]: |
|
|
|
p.changeVisualShape(obj_id, -1, rgbaColor=(255 / 255, 69 / 255, 58 / 255, 1)) |
|
self.obj_id, self.obj_link1 = self._blocks[0], 1 |
|
|
|
|
|
|
|
def _is_success(self, achieved_goal, desired_goal): |
|
""" Indicates whether or not the achieved goal successfully achieved the desired goal. |
|
""" |
|
|
|
return np.logical_and( |
|
goal_distance(achieved_goal[..., :2], desired_goal[..., :2]) < 5e-3 * self.SCALING, |
|
np.abs(achieved_goal[..., -1] - desired_goal[..., -1]) < 4e-3 * self.SCALING |
|
).astype(np.float32) |
|
|
|
def _sample_goal(self) -> np.ndarray: |
|
""" Samples a new goal and returns it. |
|
""" |
|
goal = np.array(get_link_pose(self.obj_ids['fixed'][1], self._pegs[0])[0]) |
|
return goal.copy() |
|
|
|
def _sample_goal_callback(self): |
|
""" Define waypoints |
|
""" |
|
super()._sample_goal_callback() |
|
self._waypoints = [None, None, None, None, None, None, None] |
|
pos_obj, orn_obj = get_link_pose(self.obj_id, self.obj_link1) |
|
orn = p.getEulerFromQuaternion(orn_obj) |
|
orn_eef = get_link_pose(self.psm1.body, self.psm1.EEF_LINK_INDEX)[1] |
|
orn_eef = p.getEulerFromQuaternion(orn_eef) |
|
yaw = orn[2] if abs(wrap_angle(orn[2] - orn_eef[2])) < abs(wrap_angle(orn[2] + np.pi - orn_eef[2])) \ |
|
else wrap_angle(orn[2] + np.pi) |
|
|
|
self._waypoints[0] = np.array([pos_obj[0], pos_obj[1], |
|
pos_obj[2] + 0.045 * self.SCALING, yaw, 0.5]) |
|
self._waypoints[1] = np.array([pos_obj[0], pos_obj[1], |
|
pos_obj[2] + (0.003 + 0.0102) * self.SCALING, yaw, 0.5]) |
|
self._waypoints[2] = np.array([pos_obj[0], pos_obj[1], |
|
pos_obj[2] + (0.003 + 0.0102) * self.SCALING, yaw, -0.5]) |
|
self._waypoints[3] = np.array([pos_obj[0], pos_obj[1], |
|
pos_obj[2] + 0.045 * self.SCALING, yaw, -0.5]) |
|
|
|
|
|
pos_peg = get_link_pose(self.obj_ids['fixed'][1], |
|
self._pegs[self.obj_id - np.min(self._blocks) + 6])[0] |
|
pos_place = [self.goal[0] + pos_obj[0] - pos_peg[0], |
|
self.goal[1] + pos_obj[1] - pos_peg[1], self._waypoints[0][2]] |
|
self._waypoints[4] = np.array([pos_place[0], pos_place[1], pos_place[2], yaw, -0.5]) |
|
self._waypoints[5] = np.array([pos_place[0], pos_place[1], self._waypoints[2][2], yaw, -0.5]) |
|
|
|
self._waypoints[6] = np.array([pos_place[0], pos_place[1], self._waypoints[2][2], yaw, 0.5]) |
|
self.waypoints = self._waypoints.copy() |
|
|
|
def _meet_contact_constraint_requirement(self): |
|
|
|
pose = get_link_pose(self.obj_id, -1) |
|
return pose[0][2] > self.goal[2] + 0.01 * self.SCALING |
|
|
|
def get_oracle_action(self, obs) -> np.ndarray: |
|
""" |
|
Define a human expert strategy |
|
""" |
|
|
|
action = np.zeros(5) |
|
for i, waypoint in enumerate(self._waypoints): |
|
if waypoint is None: |
|
continue |
|
|
|
|
|
|
|
delta_pos = (waypoint[:3] - obs['observation'][:3]) / 0.01 / self.SCALING |
|
delta_yaw = (waypoint[3] - obs['observation'][5]).clip(-0.4, 0.4) |
|
if np.abs(delta_pos).max() > 1: |
|
delta_pos /= np.abs(delta_pos).max() |
|
scale_factor = 0.7 |
|
delta_pos *= scale_factor |
|
action = np.array([delta_pos[0], delta_pos[1], delta_pos[2], delta_yaw, waypoint[4]]) |
|
if np.linalg.norm(delta_pos) * 0.01 / scale_factor < 2e-3 and np.abs(delta_yaw) < np.deg2rad(2.): |
|
self._waypoints[i] = None |
|
break |
|
|
|
return action |
|
|
|
def subgoal_grasp(self): |
|
scale_factor = 0.7 |
|
|
|
robot_state = self._get_robot_state(idx=0) |
|
pos_robot = robot_state[:3] |
|
yaw_robot = robot_state[5] |
|
orn_eef = get_link_pose(self.psm1.body, self.psm1.EEF_LINK_INDEX)[1] |
|
orn_eef = p.getEulerFromQuaternion(orn_eef) |
|
yaw_angle = robot_state[-1] |
|
|
|
pos_tip = np.array(get_link_pose(self.psm1.body, self.psm1.TIP_LINK_INDEX)[0]) |
|
|
|
|
|
pos_obj, orn_obj = get_link_pose(self.obj_id, self.obj_link1) |
|
orn = p.getEulerFromQuaternion(orn_obj) |
|
yaw_grasp = orn[2] if abs(wrap_angle(orn[2] - orn_eef[2])) < abs(wrap_angle(orn[2] + np.pi - orn_eef[2])) \ |
|
else wrap_angle(orn[2] + np.pi) |
|
|
|
pos_grasp = np.array([pos_obj[0], pos_obj[1], |
|
pos_obj[2]]) |
|
|
|
|
|
is_grasp = yaw_angle < 0.2 |
|
is_success = self._is_success(pos_grasp, pos_tip) |
|
return is_success.astype(np.float32) |
|
|
|
def is_success(self, achieved_goal, desired_goal): |
|
""" Indicates whether or not the achieved goal successfully achieved the desired goal. |
|
""" |
|
return self._is_success(achieved_goal, desired_goal) |
|
|
|
@property |
|
def pegs(self): |
|
return self._pegs |
|
|
|
|
|
if __name__ == "__main__": |
|
env = PegTransfer(render_mode='human') |
|
|
|
env.test() |
|
env.close() |
|
time.sleep(2) |
|
|