Skip to content

Commit b896fa8

Browse files
feat: target displacement observation
1 parent 4e63b98 commit b896fa8

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

crisp_gym/envs/manipulator_env.py

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Example:
66
```python
77
from crisp_gym.envs import make_env
8+
:wa
89
910
env = make_env(
1011
env_type="manipulator_cartesian",
@@ -150,7 +151,6 @@ def __init__(
150151
}
151152
)
152153
self._previous_rotation_vector: NDArray | None = None
153-
self._previous_target_rotation_vector: NDArray | None = None
154154
self._uninitialized = True
155155

156156
def _should_check_proper_orientation_representation(self) -> bool:
@@ -590,6 +590,17 @@ def __init__(self, config: ManipulatorEnvConfig, namespace: str = ""):
590590
),
591591
},
592592
)
593+
if ObservationKeys.TARGET_DISPLACEMENT_OBS in self.config.observations_to_include_to_state:
594+
self.observation_space: gym.spaces.Dict = gym.spaces.Dict(
595+
{
596+
**self.observation_space.spaces,
597+
ObservationKeys.TARGET_DISPLACEMENT_OBS: gym.spaces.Box(
598+
low=-np.ones((target_dim + 1,), dtype=np.float32), # +1 for gripper
599+
high=np.ones((target_dim + 1,), dtype=np.float32),
600+
dtype=np.float32,
601+
),
602+
},
603+
)
593604

594605
# Create action space with appropriate rotation dimension
595606
rot_low = -np.ones((rot_dim,), dtype=np.float32) * np.pi
@@ -619,10 +630,19 @@ def __init__(self, config: ManipulatorEnvConfig, namespace: str = ""):
619630
dtype=np.float32,
620631
)
621632

633+
self._previous_target_rotation_vector: NDArray | None = None
634+
self._previous_target_displacement_rotation_vector: NDArray | None = None
635+
622636
@override
623637
def _get_obs(self) -> dict:
624638
obs = super()._get_obs()
625-
# Get target pose with configured orientation representation
639+
if (
640+
ObservationKeys.TARGET_DISPLACEMENT_OBS
641+
not in self.config.observations_to_include_to_state
642+
and ObservationKeys.TARGET_OBS not in self.config.observations_to_include_to_state
643+
):
644+
return obs
645+
626646
if ObservationKeys.TARGET_OBS in self.config.observations_to_include_to_state:
627647
target_pose_array = self.robot.target_pose.to_array(
628648
representation=self.config.orientation_representation
@@ -633,6 +653,25 @@ def _get_obs(self) -> dict:
633653
)
634654
self._previous_target_rotation_vector = target_pose_array[3:]
635655
obs[ObservationKeys.TARGET_OBS] = target_pose_array.astype(np.float32)
656+
657+
if ObservationKeys.TARGET_DISPLACEMENT_OBS in self.config.observations_to_include_to_state:
658+
delta_pose = self.robot.target_pose - self.robot.end_effector_pose
659+
delta_pose_array = delta_pose.to_array(
660+
representation=self.config.orientation_representation
661+
)
662+
if self._should_check_proper_orientation_representation():
663+
delta_pose_array = self._flip_rotation_vector_if_needed(
664+
self._previous_target_displacement_rotation_vector, delta_pose_array
665+
)
666+
self._previous_target_displacement_rotation_vector = delta_pose_array[3:]
667+
gripper_value = (
668+
self.gripper.target - self.gripper.value if self.gripper is not None else 0.0
669+
)
670+
obs[ObservationKeys.TARGET_DISPLACEMENT_OBS] = np.concatenate(
671+
[delta_pose_array, np.array([gripper_value], dtype=np.float32)],
672+
axis=0,
673+
).astype(np.float32)
674+
636675
return obs
637676

638677
@override

crisp_gym/envs/manipulator_env_config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class ObservationKeys:
2828
CARTESIAN_OBS = STATE_OBS + ".cartesian"
2929
TARGET_OBS = STATE_OBS + ".target"
3030
SENSOR_OBS = STATE_OBS + ".sensors"
31+
TARGET_DISPLACEMENT_OBS = STATE_OBS + ".target_displacement"
3132

3233
IMAGE_OBS = "observation.images"
3334

@@ -38,6 +39,7 @@ class ObservationKeys:
3839
ObservationKeys.CARTESIAN_OBS,
3940
ObservationKeys.TARGET_OBS,
4041
ObservationKeys.SENSOR_OBS,
42+
ObservationKeys.TARGET_DISPLACEMENT_OBS,
4143
}
4244

4345

0 commit comments

Comments
 (0)