Download example_usage.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 2.18 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/example_usage.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/example_usage.py
-
curl -L -o example_usage.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/example_usage.py
2.18 kB
| """ | |
| Certified-Safe Neural Control: Quickstart Demonstration | |
| Loads the pretrained Neural CBF and filters unsafe nominal control inputs in real-time. | |
| """ | |
| import numpy as np | |
| from src.pipeline import CertifiedSafetyPipeline | |
| def main(): | |
| print("=" * 70) | |
| print("🛡️ CERTIFIED-SAFE NEURAL CONTROL: QUICKSTART") | |
| print("=" * 70) | |
| # 1. Load pretrained Certified Safety Pipeline | |
| print("[1] Loading pretrained 4D Mobile Robot (Dubins) Neural CBF...") | |
| pipeline = CertifiedSafetyPipeline.from_pretrained( | |
| checkpoint_path="checkpoints/cbf_dubins.pt", | |
| system_type="dubins", | |
| ) | |
| print(" [+] Neural CBF, OSQP Filter, and Conformal Calibrator Initialized!") | |
| # 2. Define an unsafe state & nominal control command | |
| # Robot at (1.5, 0.0), heading 0.0 rad, velocity 1.5 m/s, directly facing obstacle at (2.5, 0.0) | |
| state = np.array([1.5, 0.0, 0.0, 1.5], dtype=np.float32) | |
| # Unsafe nominal policy requests zero steering and max forward acceleration | |
| u_nominal = np.array([0.0, 1.5], dtype=np.float32) | |
| print("\n[2] Evaluating Control Input:") | |
| print(f" Current Robot State: Position=({state[0]:.2f}, {state[1]:.2f})m, Heading={state[2]:.2f}rad, Vel={state[3]:.2f}m/s") | |
| print(f" Unfiltered Nominal u: Steering={u_nominal[0]:+.2f} rad/s, Accel={u_nominal[1]:+.2f} m/s^2 (UNSAFE: Drives into obstacle)") | |
| # 3. Filter control action in real-time (< 1.5 ms) | |
| u_safe, info = pipeline.filter_action(state=state, u_nominal=u_nominal) | |
| print("\n[3] Certified Safety Filter Output:") | |
| print(f" Certified Safe u*: Steering={u_safe[0]:+.2f} rad/s, Accel={u_safe[1]:+.2f} m/s^2 (CERTIFIED SAFE: Active avoidance)") | |
| print(f" Barrier Value h(x): {info['h_val']:+.4f}") | |
| print(f" Conformal Margin gamma:{info['gamma_t']:+.4f}") | |
| print(f" QP Solve Time: {info['solve_time_ms']:.2f} ms (Frequency: ~{1000.0/max(info['solve_time_ms'], 0.1):.0f} Hz)") | |
| print(f" Optimization Status: {info['status']}") | |
| print("\n" + "=" * 70) | |
| print("🎉 SUCCESS: Unsafe control command was certified safe in real time!") | |
| print("=" * 70) | |
| if __name__ == "__main__": | |
| main() | |