File size: 1,754 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 | """
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
|