File size: 4,842 Bytes
0210a65 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | """
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()
|