Files
NODEDC_MISSION_CORE/tests/test_tracked_rover_dynamics.py
T
DCCONSTRUCTIONS d0e5f90952 Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
2026-10-08 09:59:51 +03:00

290 lines
11 KiB
Python

"""Physical invariants of the aggregate belts, separate from native Isaac rigs."""
import importlib
import json
from pathlib import Path
import numpy as np
import pytest
@pytest.fixture
def model(monkeypatch):
root = Path(__file__).parents[1] / "simulation/ai-polygon"
monkeypatch.syspath_prepend(str(root))
profile = json.loads((root / "robots/dcd006-v020.json").read_text())
return importlib.import_module("track_dynamics").TrackDynamics(profile["tracked_contact"], 100)
def support(friction=0.8):
return [
(side, np.array([x, y, -0.37]), np.array([0, 0, 1]), 981 / 10, friction)
for side, y in enumerate((0.3425, -0.3425))
for x in np.linspace(-0.35, 0.35, 5)
]
def advance(model, target, contacts, linear=(0, 0, 0), angular=(0, 0, 0), mode="drive"):
return model.step(target, contacts, np.eye(3), linear, angular, np.zeros(3), 1 / 60, mode)
def test_airborne_tracks_cannot_push_the_body(model):
for _ in range(300):
force, torque, row = advance(model, [0.3, -0.3], [])
assert np.array_equal(force, [0, 0, 0]) and np.array_equal(torque, [0, 0, 0])
assert model.belt_mps == pytest.approx([0.3, -0.3])
assert row["normal_load_n"] == [0, 0]
def test_zero_friction_removes_traction_even_with_power(model):
model.belt_mps[:] = [0.3, 0.1]
force, torque, _ = advance(model, [0.3, 0.1], support(0))
assert np.array_equal(force, [0, 0, 0]) and np.array_equal(torque, [0, 0, 0])
def test_predictor_gravity_and_support_are_not_applied_twice(model):
force, torque, _ = model.step(
[0.3, 0.3],
[],
np.eye(3),
np.zeros(3),
np.zeros(3),
np.zeros(3),
1 / 60,
external_wrench=[0, 0, -981, 0, 0, 0],
)
assert np.array_equal(force, [0, 0, 0]) and np.array_equal(torque, [0, 0, 0])
previous = model.belt_mps.copy()
with pytest.raises(ValueError, match="external predictor"):
model.step(
[0.3, 0.3],
[],
np.eye(3),
np.zeros(3),
np.zeros(3),
np.zeros(3),
1 / 60,
external_wrench=[0, 0, float("nan"), 0, 0, 0],
)
assert np.array_equal(model.belt_mps, previous)
def test_contact_is_passive_and_obeys_one_friction_circle(model):
model.belt_mps[:] = [0.4, -0.4]
force, torque, row = advance(model, [0, 0], support(), (0.1, 0.2, 0), (0, 0, 0.3), "coast")
assert row["motor_force_n"] == [0, 0]
assert row["motor_power_w"] == 0
assert row["slip_power_w"] <= 0
assert np.linalg.norm(force) <= 0.8 * 981 + 1e-8
assert np.isfinite(torque).all()
def test_motor_torque_and_power_caps_act_on_the_two_belts(model):
model.belt_mps[:] = [5, -5]
_, _, row = advance(model, [10, -10], [])
assert 0 < row["motor_power_w"] <= 1000 + 1e-8
maximum = (
model.profile["max_drive_torque_nm"] / model.profile["drive_sprocket_effective_radius_m"]
)
assert max(map(abs, row["motor_force_n"])) <= maximum
assert (
max(map(abs, row["motor_force_n"])) < maximum * 0.4
) # Power, not stall torque, limits it.
def test_brake_dissipates_rotating_belts_while_coast_preserves_airborne_momentum(model):
model.belt_mps[:] = [0.3, -0.3]
advance(model, [0, 0], [], mode="coast")
assert model.belt_mps == pytest.approx([0.3, -0.3])
for _ in range(60):
_, _, row = advance(model, [0, 0], [], mode="brake")
assert row["motor_power_w"] <= 0
assert max(abs(model.belt_mps)) < 1e-8
def test_symmetry_and_difference_produce_forces_without_a_yaw_setter(model):
model.belt_mps[:] = [0.3, 0.3]
force, torque, _ = advance(model, [0.3, 0.3], support())
assert force[0] > 0 and abs(torque[2]) < 1e-8
model.belt_mps[:] = [0.1, 0.3]
_, torque, _ = advance(model, [0.1, 0.3], support(), linear=(0.2, 0, 0))
assert torque[2] > 0
def test_unsupported_time_steps_fail_without_mutating_belts(model):
for dt in (0, -0.1, 1, float("nan")):
with pytest.raises(ValueError):
model.step([0.3, 0.3], [], np.eye(3), np.zeros(3), np.zeros(3), np.zeros(3), dt)
assert np.array_equal(model.belt_mps, [0, 0])
def test_speed_regulation_rejects_contact_load_without_unbounded_integral(model):
for _ in range(10000):
_, _, row = model.step(
[0.02, 0.02], support(), np.eye(3), np.zeros(3), np.zeros(3), np.zeros(3), 0.001
)
assert model.belt_mps == pytest.approx([0.02, 0.02], abs=1e-4)
assert min(row["motor_force_n"]) > 100
for _ in range(120):
advance(model, [50, 50], support())
cap = model.profile["max_drive_torque_nm"] / model.profile["drive_sprocket_effective_radius_m"]
assert max(abs(model.integral_force_n)) <= cap
_, _, row = advance(model, [0, 0], support(), mode="coast")
assert row["motor_force_n"] == [0, 0]
assert row["speed_integral_force_n"] == [0, 0]
def test_bad_rotation_and_contact_state_fail_before_changing_belts(model):
for rotation in (np.eye(2), np.diag([1, 1, -1]), np.eye(3) * 2):
with pytest.raises(ValueError):
model.step([0.3, 0.3], [], rotation, np.zeros(3), np.zeros(3), np.zeros(3), 1 / 60)
for point in ([0, 0], [0, float("nan"), 0]):
with pytest.raises(ValueError):
advance(model, [0.3, 0.3], [(0, point, [0, 0, 1], 100, 0.8)])
assert np.array_equal(model.belt_mps, [0, 0])
def test_native_query_callback_failure_is_not_reported_as_missing_support(monkeypatch):
from types import SimpleNamespace
root = Path(__file__).parents[1] / "simulation/ai-polygon"
monkeypatch.syspath_prepend(str(root))
cls = importlib.import_module("tracked_rover").TrackedDrive
drive = object.__new__(cls)
drive.root_path = "/World/Rover"
drive.query = SimpleNamespace(
raycast_all=lambda origin, direction, distance, callback, **kw: callback(object())
)
with pytest.raises(RuntimeError, match="Track support query failed"):
drive.ground_hit(np.zeros(3), np.array([0, 0, 1]))
def test_native_query_ignores_rover_and_copies_closest_surface(monkeypatch):
from types import SimpleNamespace
root = Path(__file__).parents[1] / "simulation/ai-polygon"
monkeypatch.syspath_prepend(str(root))
cls = importlib.import_module("tracked_rover").TrackedDrive
drive = object.__new__(cls)
drive.root_path = "/World/Rover"
def query(origin, direction, distance, callback, **kwargs):
for path, length in (
("/World/Rover/chassis/left_belt", 0),
("/World/Other", 0.03),
("/World/Ground", 0.02),
):
callback(
SimpleNamespace(
collision=path, distance=length, position=(0, 0, 0), normal=(0, 0, 1)
)
)
drive.query = SimpleNamespace(raycast_all=query)
assert drive.ground_hit(np.zeros(3), np.array([0, 0, 1]))["collision"] == "/World/Ground"
def test_turning_does_not_reclassify_previous_contact_as_chassis_gap(monkeypatch):
root = Path(__file__).parents[1] / "simulation/ai-polygon"
monkeypatch.syspath_prepend(str(root))
classify = importlib.import_module("tracked_rover").belt_contact_side
# Native frame: right belt's rear inner corner, exactly on the CAD edge.
point = np.array([-0.462, -0.2325, -0.37])
old_centre, old_rotation = np.zeros(3), np.eye(3)
yaw = 0.29 / 60
current_rotation = np.array(
[[np.cos(yaw), -np.sin(yaw), 0], [np.sin(yaw), np.cos(yaw), 0], [0, 0, 1]]
)
current_centre = np.array([0.036, -0.053, 0]) / 60
assert classify(point, current_centre, current_rotation, 0.465) is None
assert classify(point, old_centre, old_rotation, 0.465) == 1
assert classify(point * [1, -1, 1], old_centre, old_rotation, 0.465) == 0
assert classify(np.array([0, 0, -0.24]), old_centre, old_rotation, 0.465) is None
def test_held_belt_does_not_receive_second_custom_traction_force(model):
model.belt_mps[:] = [0.002, -0.002]
force, torque, row = model.step(
[0, 0],
support(),
np.eye(3),
[0.2, 0, 0],
[0, 0, 0.1],
np.zeros(3),
1 / 60,
"hold",
held_sides=[True, True],
)
assert row["belt_mps"] == [0, 0]
assert row["motor_force_n"] == [0, 0]
assert row["lock_capture_energy_j"] < 0.001
assert np.array_equal(force, [0, 0, 0]) and np.array_equal(torque, [0, 0, 0])
def test_held_side_does_not_stop_independent_driven_side(model):
for _ in range(120):
_, _, row = model.step(
[0, 0.2],
[],
np.eye(3),
np.zeros(3),
np.zeros(3),
np.zeros(3),
1 / 60,
held_sides=[True, False],
)
assert row["belt_mps"][0] == 0 and row["belt_mps"][1] > 0.19
before = model.belt_mps.copy()
with pytest.raises(ValueError, match="held side"):
model.step(
[0.2, 0.2],
[],
np.eye(3),
np.zeros(3),
np.zeros(3),
np.zeros(3),
1 / 60,
held_sides=[True, False],
)
assert np.array_equal(model.belt_mps, before)
def test_hold_material_has_finite_capacity_and_releases_when_driving(model):
coefficients = importlib.import_module("tracked_rover").hold_material_coefficients
assert coefficients(model.profile, 500, False) == (0, 0)
static, dynamic = coefficients(model.profile, 500, True)
assert static == 0.9 and dynamic == 0.8
static, dynamic = coefficients(model.profile, 5000, True)
assert 0 < dynamic <= static < 0.8
assert static * 5000 * model.profile["drive_sprocket_effective_radius_m"] == pytest.approx(50)
@pytest.mark.parametrize("sample_xs", [[], [0.2, 0.4]])
def test_irregular_support_keeps_native_loads_and_full_centre_of_pressure(model, sample_xs):
patch = importlib.import_module("tracked_rover").pressure_patch
normal = np.array([0, 0, 1.0])
records = [
(np.array([-0.3, 0.24, -0.36]), normal, 300.0, 0.7),
(np.array([0.3, 0.44, -0.34]), normal, 100.0, 0.8),
]
samples = [(x, np.array([x, 0.34, -0.35]), normal, 0.8) for x in sample_xs]
contacts, fallback = patch(0, records, samples, -0.15)
assert fallback
assert len(contacts) == len(records)
assert sum(c[3] for c in contacts) == 400
assert sum(c[1] * c[3] for c in contacts) / 400 == pytest.approx([-0.15, 0.29, -0.355])
for contact, record in zip(contacts, records, strict=True):
assert np.array_equal(contact[1], record[0])
assert contact[3:] == record[2:]
def test_resolved_pressure_line_still_preserves_measured_load_and_longitudinal_cop(model):
patch = importlib.import_module("tracked_rover").pressure_patch
normal = np.array([0, 0, 1.0])
records = [(np.array([0.1, 0.34, -0.37]), normal, 490.5, 0.8)]
samples = [(x, np.array([x, 0.34, -0.37]), normal, 0.8) for x in np.linspace(-0.3, 0.3, 9)]
contacts, fallback = patch(0, records, samples, 0.1)
assert not fallback
assert sum(c[3] for c in contacts) == pytest.approx(490.5)
assert sum(c[1][0] * c[3] for c in contacts) / 490.5 == pytest.approx(0.1, abs=1e-6)