|
1 | 1 | import logging |
| 2 | +import typing |
2 | 3 | from os import PathLike |
3 | 4 |
|
4 | 5 | import gymnasium as gym |
5 | 6 | import numpy as np |
6 | 7 | import rcs.hand.tilburg_hand |
| 8 | +from rcs._core.common import Kinematics, Pose |
7 | 9 | from rcs.camera.hw import HardwareCameraSet |
8 | 10 | from rcs.envs.base import ( |
9 | 11 | CameraSetWrapper, |
|
22 | 24 | from rcs_fr3.utils import default_fr3_hw_gripper_cfg, default_fr3_hw_robot_cfg |
23 | 25 |
|
24 | 26 | import rcs |
| 27 | +from frankik import FrankaKinematics |
25 | 28 |
|
26 | 29 | logger = logging.getLogger(__name__) |
27 | 30 | logger.setLevel(logging.INFO) |
28 | 31 |
|
29 | 32 |
|
| 33 | +class FrankIK(Kinematics): |
| 34 | + def __init__(self, allow_elbow_flips: bool = False): |
| 35 | + Kinematics.__init__(self) |
| 36 | + self.allow_elbow_flips = allow_elbow_flips |
| 37 | + self.kin = FrankaKinematics(robot_type="fr3") |
| 38 | + |
| 39 | + def forward(self, q0: np.ndarray[tuple[typing.Literal[7]], np.dtype[np.float64]], tcp_offset: Pose) -> Pose: |
| 40 | + print("forward called") |
| 41 | + return Pose(pose_matrix=self.kin.forward(q0, tcp_offset.pose_matrix())) |
| 42 | + |
| 43 | + def inverse( |
| 44 | + self, pose: Pose, q0: np.ndarray[tuple[typing.Literal[7]], np.dtype[np.float64]], tcp_offset: Pose |
| 45 | + ) -> np.ndarray[tuple[typing.Literal[7]], np.dtype[np.float64]] | None: |
| 46 | + tcp_offset = self.kin.FrankaHandTCPOffset |
| 47 | + return self.kin.inverse( |
| 48 | + pose.pose_matrix(), q0, tcp_offset.pose_matrix(), allow_elbow_flips=self.allow_elbow_flips |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +# FYI: this needs to be in global namespace to avoid auto garbage collection issues |
| 53 | +# pybind11 3.x would avoid this but with smart_holder but we cannot update due to the subfiles issue yet |
| 54 | +FastIK = FrankIK() |
| 55 | + |
| 56 | + |
30 | 57 | class RCSFR3EnvCreator(RCSHardwareEnvCreator): |
31 | 58 | def __call__( # type: ignore |
32 | 59 | self, |
@@ -62,6 +89,7 @@ def __call__( # type: ignore |
62 | 89 | robot_cfg.attachment_site, |
63 | 90 | urdf=robot_cfg.kinematic_model_path.endswith(".urdf"), |
64 | 91 | ) |
| 92 | + # ik = FastIK |
65 | 93 | # ik = rcs_robotics_library._core.rl.RoboticsLibraryIK(robot_cfg.kinematic_model_path) |
66 | 94 | robot = hw.Franka(ip, ik) |
67 | 95 | robot.set_config(robot_cfg) |
|
0 commit comments