Robot / llm_panda /task_nullspace.py
Duc Do
Upload 254 files
6686473 verified
Raw History Blame Contribute Delete
5.58 kB
import numpy as np
def _skew(v):
return np.array([
[0.0, -v[2], v[1]],
[v[2], 0.0, -v[0]],
[-v[1], v[0], 0.0]
])
def _rot_z(theta):
c, s = np.cos(theta), np.sin(theta)
return np.array([
[c, -s, 0.0, 0.0],
[s, c, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[0.0, 0.0, 0.0, 1.0]
])
def _translate(x, y, z):
return np.array([
[1.0, 0.0, 0.0, x],
[0.0, 1.0, 0.0, y],
[0.0, 0.0, 1.0, z],
[0.0, 0.0, 0.0, 1.0]
])
class TaskManifold:
def get_transform(self, u: np.ndarray) -> np.ndarray:
raise NotImplementedError
def get_transform_jacobian(self, u: np.ndarray) -> np.ndarray:
raise NotImplementedError
def get_initial_guess(self) -> np.ndarray:
raise NotImplementedError
def get_bounds(self):
raise NotImplementedError
class CylinderTaskManifold(TaskManifold):
"""
Cylinder manifold for grasps.
u = [z, theta]
T_grasp = T_obj * T_z(z) * R_z(theta) * T_offset
"""
def __init__(self, T_obj: np.ndarray, height: float, T_offset: np.ndarray):
self.T_obj = T_obj
self.height = height
self.T_offset = T_offset
self.R_obj = T_obj[:3, :3]
self.p_obj = T_obj[:3, 3]
self.p_offset = T_offset[:3, 3]
def get_transform(self, u: np.ndarray) -> np.ndarray:
z, theta = u
T_task = _translate(0.0, 0.0, z) @ _rot_z(theta)
return self.T_obj @ T_task @ self.T_offset
def get_transform_jacobian(self, u: np.ndarray) -> np.ndarray:
"""
Returns a 6x2 spatial Jacobian [v; w] in the base frame.
"""
z, theta = u
# T_obj = [R_obj, p_obj; 0, 1]
# 1. Partial w.r.t z (translation along local Z)
# linear velocity: R_obj * [0, 0, 1]^T
v_z = self.R_obj[:, 2]
w_z = np.zeros(3)
J_z = np.concatenate([v_z, w_z])
# 2. Partial w.r.t theta (rotation around local Z)
# angular velocity: R_obj * [0, 0, 1]^T
w_theta = self.R_obj[:, 2]
# linear velocity at grasping point p_grasp:
# p_grasp = p_obj + R_obj * [0, 0, z]^T + R_obj * R_z(theta) * p_offset
# dp_grasp/dtheta = w_theta x (R_obj * R_z(theta) * p_offset)
R_z_theta = _rot_z(theta)[:3, :3]
r_offset = self.R_obj @ R_z_theta @ self.p_offset
v_theta = np.cross(w_theta, r_offset)
J_theta = np.concatenate([v_theta, w_theta])
return np.column_stack([J_z, J_theta])
def get_initial_guess(self) -> np.ndarray:
return np.array([0.0, 0.0])
def get_bounds(self):
# bounds for z and theta
return [(-self.height / 2.0, self.height / 2.0), (-np.pi, np.pi)]
class BoxTaskManifold(TaskManifold):
"""
Box manifold for grasps on a specific face.
u = [u_coord, v_coord, theta]
T_grasp = T_face * T_xy(u_coord, v_coord) * R_z(theta) * T_offset
"""
def __init__(self, T_face: np.ndarray, width: float, height: float, T_offset: np.ndarray):
self.T_face = T_face
self.width = width
self.height = height
self.T_offset = T_offset
self.R_face = T_face[:3, :3]
self.p_offset = T_offset[:3, 3]
def get_transform(self, u: np.ndarray) -> np.ndarray:
u_coord, v_coord, theta = u
T_task = _translate(u_coord, v_coord, 0.0) @ _rot_z(theta)
return self.T_face @ T_task @ self.T_offset
def get_transform_jacobian(self, u: np.ndarray) -> np.ndarray:
"""
Returns a 6x3 spatial Jacobian [v; w] in the base frame.
"""
u_coord, v_coord, theta = u
# 1. Partial w.r.t u_coord (translation along local X)
v_u = self.R_face[:, 0]
w_u = np.zeros(3)
J_u = np.concatenate([v_u, w_u])
# 2. Partial w.r.t v_coord (translation along local Y)
v_v = self.R_face[:, 1]
w_v = np.zeros(3)
J_v = np.concatenate([v_v, w_v])
# 3. Partial w.r.t theta (rotation around local Z)
w_theta = self.R_face[:, 2]
R_z_theta = _rot_z(theta)[:3, :3]
r_offset = self.R_face @ R_z_theta @ self.p_offset
v_theta = np.cross(w_theta, r_offset)
J_theta = np.concatenate([v_theta, w_theta])
return np.column_stack([J_u, J_v, J_theta])
def get_initial_guess(self) -> np.ndarray:
return np.array([0.0, 0.0, 0.0])
def get_bounds(self):
return [(-self.width / 2.0, self.width / 2.0), (-self.height / 2.0, self.height / 2.0), (-np.pi, np.pi)]
class GenericMeshTaskManifold(TaskManifold):
def __init__(self):
# The contract states that it must fail immediately if parameterized grasp surface patches are not pre-supplied.
raise ValueError("GenericMeshTaskManifold: Parameterized grasp surface patches not supplied. Implementation MUST stop.")
class TaskManifoldFactory:
@staticmethod
def create_manifold(shape_type: str, T_obj: np.ndarray, dimensions: dict, T_offset: np.ndarray) -> TaskManifold:
if shape_type == "cylinder":
return CylinderTaskManifold(T_obj, dimensions["height"], T_offset)
elif shape_type == "box":
# For a box, one must specify the specific T_face as T_obj.
return BoxTaskManifold(T_obj, dimensions["width"], dimensions["height"], T_offset)
elif shape_type == "mesh":
return GenericMeshTaskManifold()
else:
raise ValueError(f"Unknown shape type: {shape_type}")