Skip to content

Commit 14e2a30

Browse files
authored
Merge pull request #209 from RobotControlStack/feat/calibration-cache
feat: calibration cache for realsense
2 parents 04ebb4c + e0cea71 commit 14e2a30

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

extensions/rcs_realsense/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies = [
1010
"rcs==0.4.0",
1111
"pyrealsense2~=2.55.1",
1212
"apriltag==0.0.16",
13+
"diskcache",
1314
]
1415
readme = "README.md"
1516
maintainers = [

extensions/rcs_realsense/src/rcs_realsense/calibration.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import logging
22
import threading
33
import typing
4+
from pathlib import Path
45
from time import sleep
56

67
import apriltag
78
import cv2
9+
import diskcache as dc
810
import numpy as np
911
from rcs._core import common
1012
from rcs.camera.hw import CalibrationStrategy
@@ -19,7 +21,10 @@ class FR3BaseArucoCalibration(CalibrationStrategy):
1921

2022
def __init__(self, camera_name: str):
2123
# base frame to camera, world to base frame
22-
self._extrinsics: np.ndarray[tuple[typing.Literal[4], typing.Literal[4]], np.dtype[np.float64]] | None = None
24+
self._cache = dc.Cache(Path.home() / ".cache" / "rcs")
25+
self._extrinsics: np.ndarray[tuple[typing.Literal[4], typing.Literal[4]], np.dtype[np.float64]] | None = (
26+
self._cache.get(f"{camera_name}_extrinsics")
27+
) # None
2328
self.camera_name = camera_name
2429
self.tag_to_world = common.Pose(
2530
rpy_vector=np.array([np.pi, 0, -np.pi / 2]), translation=np.array([0.145, 0, 0])
@@ -53,6 +58,7 @@ def calibrate(
5358
cam_to_world = self.tag_to_world @ np.linalg.inv(tag_to_cam)
5459
world_to_cam = np.linalg.inv(cam_to_world)
5560
self._extrinsics = world_to_cam # type: ignore
61+
self._cache.set(f"{self.camera_name}_extrinsics", world_to_cam, expire=3600)
5662
return True
5763

5864
def get_extrinsics(self) -> np.ndarray[tuple[typing.Literal[4], typing.Literal[4]], np.dtype[np.float64]] | None:

python/rcs/envs/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,15 +693,15 @@ def __init__(self, env, gripper: common.Gripper, binary: bool = True, open_on_re
693693
self.action_space: gym.spaces.Dict
694694
self.action_space.spaces.update(get_space(GripperDictType).spaces)
695695
self.gripper_key = get_space_keys(GripperDictType)[0]
696-
self._gripper = gripper
696+
self.gripper = gripper
697697
self.binary = binary
698698
self._last_gripper_cmd = None
699699
self.open_on_reset = open_on_reset
700700

701701
def reset(self, **kwargs) -> tuple[dict[str, Any], dict[str, Any]]:
702702
if self.open_on_reset:
703703
# resetting opens the gripper
704-
self._gripper.reset()
704+
self.gripper.reset()
705705
self._last_gripper_cmd = None
706706
return super().reset(**kwargs)
707707

@@ -712,7 +712,7 @@ def observation(self, observation: dict[str, Any], info: dict[str, Any]) -> tupl
712712
self._last_gripper_cmd if self._last_gripper_cmd is not None else self.BINARY_GRIPPER_OPEN
713713
)
714714
else:
715-
observation[self.gripper_key] = self._gripper.get_normalized_width()
715+
observation[self.gripper_key] = self.gripper.get_normalized_width()
716716

717717
return observation, info
718718

@@ -725,9 +725,9 @@ def action(self, action: dict[str, Any]) -> dict[str, Any]:
725725
gripper_action = np.clip(gripper_action, 0.0, 1.0)
726726

727727
if self.binary:
728-
self._gripper.grasp() if gripper_action == self.BINARY_GRIPPER_CLOSED else self._gripper.open()
728+
self.gripper.grasp() if gripper_action == self.BINARY_GRIPPER_CLOSED else self.gripper.open()
729729
else:
730-
self._gripper.set_normalized_width(gripper_action)
730+
self.gripper.set_normalized_width(gripper_action)
731731
self._last_gripper_cmd = gripper_action
732732
del action[self.gripper_key]
733733
return action

python/rcs/envs/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ def get_tcp_offset(mjcf: str | Path) -> rcs.common.Pose:
9494
tcp_offset_translation = np.array(model.numeric("tcp_offset_translation").data)
9595
tcp_offset_rotation_matrix = np.array(model.numeric("tcp_offset_rotation_matrix").data)
9696
return rcs.common.Pose(
97-
translation=tcp_offset_translation, rotation=tcp_offset_rotation_matrix.reshape((3, 3))
98-
) # type: ignore
97+
translation=tcp_offset_translation, rotation=tcp_offset_rotation_matrix.reshape((3, 3)) # type: ignore
98+
)
9999
except KeyError:
100100
msg = "No tcp offset found in the model. Using the default tcp offset."
101101
logging.info(msg)

0 commit comments

Comments
 (0)