Download src/dynamics/toy_nav.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 1.84 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/src/dynamics/toy_nav.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/src/dynamics/toy_nav.py
-
curl -L -o toy_nav.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/src/dynamics/toy_nav.py
1.84 kB
| """ | |
| 2D/3D Nonlinear Navigation: Dubins Car Dynamics | |
| State: x = [p_x, p_y, theta, v]^T in R^4 | |
| Control: u = [omega, a]^T in R^2 (steering rate, linear acceleration) | |
| """ | |
| import numpy as np | |
| from src.dynamics.base import ControlAffineSystem | |
| class DubinsCarSystem(ControlAffineSystem): | |
| """ | |
| Dubins Car / Unicycle model: | |
| dot{p_x} = v * cos(theta) | |
| dot{p_y} = v * sin(theta) | |
| dot{theta} = omega | |
| dot{v} = -(1 / tau) * v + (K / tau) * a | |
| """ | |
| def __init__( | |
| self, | |
| tau: float = 0.5, | |
| K: float = 1.0, | |
| dt: float = 0.02, | |
| omega_max: float = 1.5, | |
| a_max: float = 2.0, | |
| ): | |
| u_min = np.array([-omega_max, -a_max], dtype=np.float32) | |
| u_max = np.array([omega_max, a_max], dtype=np.float32) | |
| super().__init__(state_dim=4, control_dim=2, u_min=u_min, u_max=u_max, dt=dt) | |
| self.tau = tau | |
| self.K = K | |
| def f(self, x: np.ndarray) -> np.ndarray: | |
| px, py, theta, v = x | |
| f_vec = np.zeros(4, dtype=np.float32) | |
| f_vec[0] = v * np.cos(theta) | |
| f_vec[1] = v * np.sin(theta) | |
| f_vec[2] = 0.0 | |
| f_vec[3] = -(1.0 / self.tau) * v | |
| return f_vec | |
| def g(self, x: np.ndarray) -> np.ndarray: | |
| g_mat = np.zeros((4, 2), dtype=np.float32) | |
| g_mat[2, 0] = 1.0 | |
| g_mat[3, 1] = self.K / self.tau | |
| return g_mat | |
| def ground_truth_barrier( | |
| self, | |
| x: np.ndarray, | |
| obstacle_pos: np.ndarray, | |
| obstacle_radius: float, | |
| safety_margin: float = 0.1, | |
| ) -> float: | |
| """ | |
| Analytical distance-based barrier: h(x) = ||p - p_obs||^2 - (R_obs + margin)^2 | |
| """ | |
| p = x[:2] | |
| dist_sq = np.sum((p - obstacle_pos) ** 2) | |
| r_safe_sq = (obstacle_radius + safety_margin) ** 2 | |
| return float(dist_sq - r_safe_sq) | |