|
import itertools |
|
import math |
|
from typing import Dict, List |
|
|
|
from pyrep.objects.dummy import Dummy |
|
from pyrep.objects.joint import Joint |
|
from pyrep.objects.object import Object |
|
from pyrep.objects.shape import Shape |
|
from pyrep.objects.proximity_sensor import ProximitySensor |
|
from rlbench.backend.spawn_boundary import SpawnBoundary |
|
from rlbench.backend.task import Task |
|
from rlbench.backend.conditions import Condition, ConditionSet, DetectedCondition, NothingGrasped |
|
|
|
NUM_TARGETS = 4 |
|
|
|
class JointTriggerCondition(Condition): |
|
def __init__(self, joint: Joint, position: float): |
|
"""in radians if revoloute, or meters if prismatic""" |
|
self._joint = joint |
|
self._original_pos = joint.get_joint_position() |
|
self._pos = position |
|
self._done = False |
|
|
|
def condition_met(self): |
|
met = math.fabs( |
|
self._joint.get_joint_position() - self._original_pos) > self._pos |
|
if met: |
|
self._done = True |
|
return self._done, False |
|
|
|
class DetectedTriggerCondition(Condition): |
|
def __init__(self, obj: Object, detector: ProximitySensor, |
|
negated: bool = False): |
|
self._obj = obj |
|
self._detector = detector |
|
self._negated = negated |
|
self._done = False |
|
|
|
def condition_met(self): |
|
met = self._detector.is_detected(self._obj) |
|
if self._negated: |
|
met = not met |
|
if met: |
|
self._done = True |
|
return self._done, False |
|
|
|
|
|
|
|
class PutBlockBack(Task): |
|
def init_task(self) -> None: |
|
self._block = Shape("block") |
|
self._detectors = [ProximitySensor(f"success{i+1}") for i in range(NUM_TARGETS)] |
|
self._targets = [Shape(f"target{i+1}") for i in range(NUM_TARGETS)] |
|
|
|
self._button = Shape("push_buttons_target1") |
|
self._button_joint = Joint("target_button_joint1") |
|
self._center_detector = ProximitySensor("success0") |
|
|
|
self.spawn_boundary = SpawnBoundary([Shape("boundary")]) |
|
|
|
self.register_graspable_objects([self._block]) |
|
|
|
self.goal_conditions = [] |
|
|
|
def init_episode(self, index: int) -> List[str]: |
|
target_patch = self._targets[index] |
|
target_detector = self._detectors[index] |
|
|
|
self.spawn_boundary.clear() |
|
self.spawn_boundary.sample(self._button, min_distance=0.05) |
|
|
|
|
|
x, y, _ = target_patch.get_position() |
|
_, _, z = self._block.get_position() |
|
self._block.set_position([x, y, z]) |
|
|
|
waypoint1 = Dummy("waypoint1") |
|
_, _, z = waypoint1.get_position() |
|
waypoint1.set_position([x, y, z]) |
|
|
|
waypoint10 = Dummy("waypoint10") |
|
_, _, z = waypoint10.get_position() |
|
waypoint10.set_position([x, y, z]) |
|
|
|
waypoint11 = Dummy("waypoint11") |
|
_, _, z = waypoint11.get_position() |
|
waypoint11.set_position([x, y, z]) |
|
|
|
|
|
self.goal_conditions = [ |
|
|
|
DetectedTriggerCondition(self._block, target_detector, negated=True), |
|
|
|
DetectedTriggerCondition(self._block, self._center_detector), |
|
|
|
JointTriggerCondition(self._button_joint, 0.003), |
|
|
|
DetectedCondition(self._block, target_detector), |
|
|
|
NothingGrasped(self.robot.gripper), |
|
] |
|
condition_set = ConditionSet(self.goal_conditions, False) |
|
self.register_success_conditions([condition_set]) |
|
|
|
return ['move the block on the color patch to the center, then press the button, and finally move the block back to where it was placed'] |
|
|
|
def step(self) -> None: |
|
if len(self.goal_conditions) < 1: |
|
return |
|
|
|
met1, _ = self.goal_conditions[0].condition_met() |
|
|
|
|
|
met2, _ = self.goal_conditions[1].condition_met() |
|
|
|
|
|
met3, _ = self.goal_conditions[2].condition_met() |
|
|
|
|
|
met4, _ = self.goal_conditions[3].condition_met() |
|
|
|
|
|
met5, _ = self.goal_conditions[4].condition_met() |
|
|
|
|
|
def variation_count(self) -> int: |
|
return 4 |
|
|
|
def is_static_workspace(self) -> bool: |
|
return True |
|
|