Robot / llm_panda /evaluators.py
Duc Do
Upload 254 files
6686473 verified
Raw History Blame Contribute Delete
4.14 kB
import numpy as np
import logging
from typing import Tuple, Dict, Any
from .geofik import FrankaGeoFIK, IKResult, ReachabilityStatus
from .swivel_adapter import SwivelAdapter
from .task_nullspace import TaskManifold
logger = logging.getLogger(__name__)
class CachingContext:
"""
Evaluates and caches the kinematic chain to avoid redundant IK calls.
"""
def __init__(self, fik: FrankaGeoFIK, swivel: SwivelAdapter, task: TaskManifold):
self.fik = fik
self.swivel = swivel
self.task = task
self.last_x = None
self.last_q = None
self.last_J_task = None
self.last_J_ik = None
self.last_J_swivel = None
self.last_success = False
def update(self, x: np.ndarray) -> bool:
if self.last_x is not None and np.allclose(self.last_x, x, atol=1e-8):
return self.last_success
self.last_x = x.copy()
u = x[:-1]
theta = x[-1]
# 1. Task Manifold
T_target = self.task.get_transform(u)
self.last_J_task = self.task.get_transform_jacobian(u)
# 2. Swivel Adapter
q7, dq7_dtheta = self.swivel.get_q7_and_derivative(theta, T_target)
if q7 is None:
self.last_success = False
return False
# 3. GeoFIK Layer A
ik_result = self.fik.solve(T_target, q7)
if not ik_result.success:
self.last_success = False
return False
self.last_q = ik_result.q
# 4. Chain Rule Assembly
# J_pose is 6x7. v = J_pose * qdot
J_pose = ik_result.pose_jacobian
J_6 = J_pose[:, :6]
J_7 = J_pose[:, 6]
# Analytical inversion for redundant chain
# dq_0:5 = - inv(J_6) * J_7 * dq7
try:
J_6_inv = np.linalg.inv(J_6)
except np.linalg.LinAlgError:
self.last_success = False
return False
dq_dq7_6 = - J_6_inv @ J_7
dq_dq7 = np.append(dq_dq7_6, 1.0) # 7x1
self.last_J_swivel = dq_dq7 * dq7_dtheta
# J_ik = dq / du = (dq / dT) * (dT / du) = J_6_inv * J_task
dq_du_6 = J_6_inv @ self.last_J_task
dq_du_7 = np.zeros((1, self.last_J_task.shape[1]))
self.last_J_ik = np.vstack((dq_du_6, dq_du_7))
self.last_success = True
return True
class ObjectiveEvaluator:
"""
Evaluates optimization objectives.
Source: Architecture Design (Standard Robotics / Engineering)
"""
def __init__(self, ctx: CachingContext, q_nominal: np.ndarray):
self.ctx = ctx
self.q_nominal = q_nominal
def evaluate_displacement(self, x: np.ndarray) -> Tuple[float, np.ndarray]:
"""
f(x) = 0.5 * ||q - q_nominal||^2
df/dx = (q - q_nominal)^T * (dq/dx)
"""
if not self.ctx.update(x):
return 1e6, np.zeros_like(x)
diff = self.ctx.last_q - self.q_nominal
cost = 0.5 * np.sum(diff**2)
# dq_dx = [J_ik, J_swivel]
dq_dx = np.column_stack((self.ctx.last_J_ik, self.ctx.last_J_swivel))
grad = diff.T @ dq_dx
return cost, grad
class ConstraintEvaluator:
"""
Evaluates hard constraints (e.g., joint limits).
"""
def __init__(self, ctx: CachingContext, q_low: np.ndarray, q_up: np.ndarray):
self.ctx = ctx
self.q_low = q_low
self.q_up = q_up
def evaluate_limits(self, x: np.ndarray) -> Tuple[np.ndarray, np.ndarray]:
"""
c(x) >= 0 -> q - q_low >= 0, q_up - q >= 0
Returns: constraints (14,), Jacobian (14, len(x))
"""
if not self.ctx.update(x):
return -np.ones(14), np.zeros((14, len(x)))
c_low = self.ctx.last_q - self.q_low
c_up = self.q_up - self.ctx.last_q
constraints = np.concatenate((c_low, c_up))
dq_dx = np.column_stack((self.ctx.last_J_ik, self.ctx.last_J_swivel))
grad_low = dq_dx
grad_up = -dq_dx
grad = np.vstack((grad_low, grad_up))
return constraints, grad