Download scripts/run_manipulator_sim.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 4.84 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/scripts/run_manipulator_sim.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/scripts/run_manipulator_sim.py
-
curl -L -o run_manipulator_sim.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/scripts/run_manipulator_sim.py
4.84 kB
| """ | |
| Phase 4: 8-DoF Manipulator Joint-Space Simulation Benchmark (16D State, 8D Control) | |
| Validates Certified Neural CBF + AdaptNC Calibrator for a 7-DoF Franka on a 1-DoF Linear Rail. | |
| """ | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| import numpy as np | |
| import torch | |
| from src.dynamics.manipulator_8dof import Manipulator8DoFSystem | |
| from src.cbf.network import NeuralCBF | |
| from src.filter.cbf_qp import CBFQPSafetyFilter | |
| from src.calibration.adapt_nc import AdaptNCCalibrator | |
| from src.eval.harness import ClosedLoopHarness | |
| from src.eval.metrics import compare_ablation_runs | |
| def main(): | |
| print("=" * 70) | |
| print("PHASE 4: 8-DoF MANIPULATOR CONTROL UNDER FRICTION & PAYLOAD SHIFT") | |
| print("=" * 70) | |
| # 1. Initialize 8-DoF Manipulator (16D state, 8D control) | |
| dt = 0.01 | |
| manip = Manipulator8DoFSystem(dt=dt, tau_rail_max=100.0, tau_joint_max=50.0) | |
| # Goal joint configuration (reaching target while respecting joint limits) | |
| q_target = np.array([0.8, 0.5, 0.2, 0.6, -1.2, 0.4, 1.8, 0.2], dtype=np.float32) | |
| def gt_barrier(x): | |
| return manip.joint_limit_barrier(x, margin=0.08) | |
| def goal_check(x): | |
| return bool(np.linalg.norm(x[:8] - q_target) < 0.25) | |
| # 2. Nominal Joint-Space PD Controller | |
| Kp = np.array([40.0] + [25.0] * 7, dtype=np.float32) | |
| Kd = np.array([8.0] + [5.0] * 7, dtype=np.float32) | |
| def nominal_policy(x, t): | |
| q = x[:8] | |
| qdot = x[8:16] | |
| tau_cmd = Kp * (q_target - q) - Kd * qdot | |
| return np.clip(tau_cmd, manip.u_min, manip.u_max) | |
| # Injected Disturbance: Sudden 40% joint friction increase and unmodeled payload torque | |
| def shift_disturbance(x, t): | |
| # Disturbance on accelerations qddot (indices 8..15) | |
| dist = np.zeros(16, dtype=np.float32) | |
| # Joint friction drag opposing motion | |
| qdot = x[8:16] | |
| dist[8:16] = -0.5 * np.sign(qdot) - 0.2 * qdot | |
| # End-effector payload weight adding downward torque on wrist joints | |
| dist[12] -= 1.2 | |
| dist[14] -= 0.8 | |
| return dist | |
| # Initial home configuration | |
| x0 = np.zeros(16, dtype=np.float32) | |
| x0[4] = -1.5 # elbow bent | |
| x0[6] = 1.5 # wrist angle | |
| # 3. Compact Neural CBF for 8-DoF Manipulator | |
| cbf_manip = NeuralCBF(state_dim=16, hidden_dims=[64, 64], activation="tanh") | |
| with torch.no_grad(): | |
| for p in cbf_manip.parameters(): | |
| p.data.normal_(0.0, 0.02) | |
| results = {} | |
| # RUN 1: Unfiltered Nominal | |
| print("\n[*] Executing Run 1: Unfiltered 8-DoF Manipulator Nominal...") | |
| harness_nom = ClosedLoopHarness(system=manip, ground_truth_barrier_fn=gt_barrier) | |
| res_nom = harness_nom.run_episode( | |
| x_init=x0, | |
| nominal_policy=nominal_policy, | |
| max_steps=250, | |
| shift_step=80, | |
| shift_disturbance_fn=shift_disturbance, | |
| goal_check_fn=goal_check, | |
| ) | |
| results["1. Unfiltered Nominal (16D)"] = res_nom["metrics"] | |
| # RUN 2: Static CBF-QP | |
| print("[*] Executing Run 2: Static 8-DoF Manipulator CBF-QP...") | |
| qp_filter_static = CBFQPSafetyFilter( | |
| control_dim=8, | |
| u_min=manip.u_min, | |
| u_max=manip.u_max, | |
| alpha_cbf=1.5, | |
| slack_penalty=1e4, | |
| ) | |
| harness_static = ClosedLoopHarness( | |
| system=manip, | |
| cbf_net=cbf_manip, | |
| safety_filter=qp_filter_static, | |
| ground_truth_barrier_fn=gt_barrier, | |
| ) | |
| res_static = harness_static.run_episode( | |
| x_init=x0, | |
| nominal_policy=nominal_policy, | |
| max_steps=250, | |
| shift_step=80, | |
| shift_disturbance_fn=shift_disturbance, | |
| goal_check_fn=goal_check, | |
| ) | |
| results["2. Static CBF-QP (16D)"] = res_static["metrics"] | |
| # RUN 3: AdaptNC Online Calibrated | |
| print("[*] Executing Run 3: AdaptNC Online Calibrated 8-DoF Manipulator...") | |
| qp_filter_adapt = CBFQPSafetyFilter( | |
| control_dim=8, | |
| u_min=manip.u_min, | |
| u_max=manip.u_max, | |
| alpha_cbf=1.5, | |
| slack_penalty=1e4, | |
| ) | |
| calibrator = AdaptNCCalibrator(alpha_risk=0.05, eta=0.04, c_max=4.0) | |
| harness_adapt = ClosedLoopHarness( | |
| system=manip, | |
| cbf_net=cbf_manip, | |
| safety_filter=qp_filter_adapt, | |
| calibrator=calibrator, | |
| ground_truth_barrier_fn=gt_barrier, | |
| ) | |
| res_adapt = harness_adapt.run_episode( | |
| x_init=x0, | |
| nominal_policy=nominal_policy, | |
| max_steps=250, | |
| shift_step=80, | |
| shift_disturbance_fn=shift_disturbance, | |
| goal_check_fn=goal_check, | |
| ) | |
| results["3. AdaptNC Calibrated (16D)"] = res_adapt["metrics"] | |
| # Summary Table | |
| print("\n" + "=" * 80) | |
| print("8-DoF MANIPULATOR BENCHMARK UNDER CLOSED-LOOP DISTRIBUTION SHIFT") | |
| print("=" * 80) | |
| print(compare_ablation_runs(results)) | |
| print("=" * 80) | |
| if __name__ == "__main__": | |
| main() | |