Download tests/test_dynamics.py from codewithyug/certified-safe-neural-control: direct link, hf CLI and curl.
- Browser
- Download file 1.75 kB
-
https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/tests/test_dynamics.py
- Command line
-
hf download hf://codewithyug/certified-safe-neural-control/tests/test_dynamics.py
-
curl -L -o test_dynamics.py https://huggingface.co/codewithyug/certified-safe-neural-control/resolve/main/tests/test_dynamics.py
1.75 kB
| """ | |
| Unit Tests for Control-Affine Dynamics: | |
| Dubins Car (4D), Quadrotor (12D), and Manipulator (16D). | |
| """ | |
| import numpy as np | |
| import pytest | |
| from src.dynamics.toy_nav import DubinsCarSystem | |
| from src.dynamics.quadrotor_6dof import Quadrotor6DoFSystem | |
| from src.dynamics.manipulator_8dof import Manipulator8DoFSystem | |
| def test_dubins_car_dynamics(): | |
| sys = DubinsCarSystem(tau=0.5, K=1.0, dt=0.02) | |
| x = np.array([0.0, 0.0, 0.0, 1.0], dtype=np.float32) | |
| u = np.array([0.5, 1.0], dtype=np.float32) | |
| # Check drift and actuation shapes | |
| f = sys.f(x) | |
| g = sys.g(x) | |
| assert f.shape == (4,) | |
| assert g.shape == (4, 2) | |
| # Forward Euler step | |
| x_next = sys.step_euler(x, u) | |
| assert x_next.shape == (4,) | |
| assert x_next[0] > x[0] # Moved forward in x | |
| def test_quadrotor_6dof_dynamics(): | |
| sys = Quadrotor6DoFSystem(dt=0.01) | |
| x = np.zeros(12, dtype=np.float32) | |
| x[2] = 1.0 # Hover altitude at z=1.0 | |
| u = np.full(4, 2.45, dtype=np.float32) # Approx hover thrust (mg / 4 = 9.81 / 4) | |
| f = sys.f(x) | |
| g = sys.g(x) | |
| assert f.shape == (12,) | |
| assert g.shape == (12, 4) | |
| # RK4 step | |
| x_next = sys.step_rk4(x, u) | |
| assert x_next.shape == (12,) | |
| def test_manipulator_8dof_dynamics(): | |
| sys = Manipulator8DoFSystem(dt=0.01) | |
| # Neutral/home configuration safely within interior of joint limits | |
| x = np.zeros(16, dtype=np.float32) | |
| x[4] = -1.5 # elbow bent | |
| x[6] = 1.8 # wrist angle | |
| u = np.zeros(8, dtype=np.float32) | |
| f = sys.f(x) | |
| g = sys.g(x) | |
| assert f.shape == (16,) | |
| assert g.shape == (16, 8) | |
| # Check joint limit barrier | |
| barrier_val = sys.joint_limit_barrier(x) | |
| assert isinstance(barrier_val, float) | |
| assert barrier_val > 0.0 # Safe initial configuration | |