nbv_rec_control/utils/control_util.py

56 lines
1.7 KiB
Python
Raw Normal View History

2024-10-06 21:30:20 +08:00
import numpy as np
from frankapy import FrankaArm
2024-10-06 21:52:05 +08:00
from autolab_core import RigidTransform
2024-10-06 21:30:20 +08:00
class ControlUtil:
__fa = FrankaArm(robot_num=2)
2024-10-06 21:52:05 +08:00
BASE_TO_DISPLAYTBLE:np.ndarray = np.asarray([
2024-10-06 21:30:20 +08:00
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
2024-10-06 21:52:05 +08:00
CAMERA_TO_GRIPPER:np.ndarray = np.asarray([
2024-10-06 21:30:20 +08:00
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
])
@staticmethod
def get_franka_arm() -> FrankaArm:
return ControlUtil.__fa
@staticmethod
def get_pose() -> np.ndarray:
2024-10-06 21:52:05 +08:00
gripper_to_base = ControlUtil.__fa.get_pose().matrix
cam_to_world = ControlUtil.BASE_TO_DISPLAYTBLE @ gripper_to_base @ ControlUtil.CAMERA_TO_GRIPPER
return cam_to_world
@staticmethod
def set_pose(cam_to_world: np.ndarray) -> None:
gripper_to_base = np.linalg.inv(ControlUtil.BASE_TO_DISPLAYTBLE) @ cam_to_world @ np.linalg.inv(ControlUtil.CAMERA_TO_GRIPPER)
gripper_to_base = RigidTransform(rotation=gripper_to_base[:3, :3], translation=gripper_to_base[:3, 3], from_frame="franka_tool", to_frame="world")
ControlUtil.__fa.goto_pose(gripper_to_base, use_impedance=False, ignore_errors=False)
2024-10-06 21:30:20 +08:00
@staticmethod
def reset() -> None:
ControlUtil.__fa.reset_joints()
2024-10-06 21:52:05 +08:00
2024-10-06 21:30:20 +08:00
# ----------- Debug Test -------------
if __name__ == "__main__":
2024-10-06 21:52:05 +08:00
test_pose = np.asarray([
[1, 0, 0, 0.4],
[0, -1, 0, 0],
[0, 0, -1, 0.6],
[0, 0, 0, 1]
])
ControlUtil.set_pose(test_pose)
2024-10-06 21:30:20 +08:00
print(ControlUtil.get_pose())
ControlUtil.reset()
print(ControlUtil.get_pose())