Download llm_panda/grasp_optimizer.py from C2-151/Robot: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/spaces/C2-151/Robot/resolve/main/llm_panda/grasp_optimizer.py
- Command line
-
hf download hf://spaces/C2-151/Robot/llm_panda/grasp_optimizer.py
-
curl -L -o grasp_optimizer.py https://huggingface.co/spaces/C2-151/Robot/resolve/main/llm_panda/grasp_optimizer.py
3.58 kB
| import numpy as np | |
| from scipy.optimize import minimize | |
| import logging | |
| from typing import Tuple, Dict, Any, List, Optional | |
| from dataclasses import dataclass | |
| from .geofik import FrankaGeoFIK, IKResult | |
| from .swivel_adapter import SwivelAdapter | |
| from .task_nullspace import TaskManifold | |
| from .evaluators import CachingContext, ObjectiveEvaluator, ConstraintEvaluator | |
| logger = logging.getLogger(__name__) | |
| class GraspOptimizationResult: | |
| success: bool | |
| q: Optional[np.ndarray] | |
| message: str | |
| iterations: int | |
| final_cost: float | |
| class GraspOptimizer: | |
| """ | |
| Phase 4: Optimization Engine. | |
| Uses SciPy SLSQP to find optimal redundant parameters (u, theta) | |
| subject to reachability and collision constraints. | |
| """ | |
| def __init__(self, fik: FrankaGeoFIK, swivel: SwivelAdapter): | |
| self.fik = fik | |
| self.swivel = swivel | |
| def optimize(self, task: TaskManifold, q_nominal: np.ndarray, maxiter: int = 100) -> GraspOptimizationResult: | |
| """ | |
| Solves the Task Nullspace Optimization problem. | |
| x = [u..., theta] | |
| """ | |
| ctx = CachingContext(self.fik, self.swivel, task) | |
| obj_eval = ObjectiveEvaluator(ctx, q_nominal) | |
| con_eval = ConstraintEvaluator(ctx, self.fik.geometry.q_low, self.fik.geometry.q_up) | |
| def objective(x): | |
| cost, grad = obj_eval.evaluate_displacement(x) | |
| return cost | |
| def jacobian(x): | |
| cost, grad = obj_eval.evaluate_displacement(x) | |
| return grad | |
| def constraint_func(x): | |
| c, grad = con_eval.evaluate_limits(x) | |
| return c | |
| def constraint_jac(x): | |
| c, grad = con_eval.evaluate_limits(x) | |
| return grad | |
| constraints = [{'type': 'ineq', 'fun': constraint_func, 'jac': constraint_jac}] | |
| # Initial guess | |
| u_guess = task.get_initial_guess() | |
| theta_guess = 0.0 # Default swivel | |
| x0 = np.append(u_guess, theta_guess) | |
| # Bounds for u and theta | |
| u_bounds = task.get_bounds() | |
| bounds = u_bounds + [(-np.pi, np.pi)] # theta bounds | |
| # Ensure initial guess is inside bounds | |
| x0_clamped = np.zeros_like(x0) | |
| for i, (b_min, b_max) in enumerate(bounds): | |
| x0_clamped[i] = np.clip(x0[i], b_min, b_max) | |
| options = {'maxiter': maxiter, 'ftol': 1e-4, 'disp': False} | |
| res = minimize( | |
| fun=objective, | |
| x0=x0_clamped, | |
| method='SLSQP', | |
| jac=jacobian, | |
| bounds=bounds, | |
| constraints=constraints, | |
| options=options | |
| ) | |
| if res.success: | |
| # Re-evaluate to get final q | |
| ctx.update(res.x) | |
| if ctx.last_success: | |
| return GraspOptimizationResult( | |
| success=True, | |
| q=ctx.last_q, | |
| message=res.message, | |
| iterations=res.nit, | |
| final_cost=res.fun | |
| ) | |
| else: | |
| return GraspOptimizationResult( | |
| success=False, | |
| q=None, | |
| message="SLSQP reported success, but kinematics failed at solution.", | |
| iterations=res.nit, | |
| final_cost=res.fun | |
| ) | |
| else: | |
| return GraspOptimizationResult( | |
| success=False, | |
| q=None, | |
| message=res.message, | |
| iterations=res.nit, | |
| final_cost=res.fun | |
| ) | |