759 lines
34 KiB
Python
759 lines
34 KiB
Python
import importlib.util
|
|
import subprocess
|
|
import sys
|
|
from functools import partial
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from k1link.observatory.modular_composition import CompositionError
|
|
from k1link.simulation.ai_polygon.composition import compose, registry
|
|
from k1link.simulation.ai_polygon.terrain_contract import terrain_matches
|
|
|
|
ROOT = Path(__file__).resolve().parents[1] / "simulation/ai-polygon"
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def runtime_imports(monkeypatch):
|
|
# Match the shipped adapter's script directory without depending on which
|
|
# other test happened to add it to sys.path first.
|
|
monkeypatch.syspath_prepend(str(ROOT))
|
|
|
|
|
|
def module(name):
|
|
spec = importlib.util.spec_from_file_location(name.replace("/", "."), ROOT / (name + ".py"))
|
|
result = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(result)
|
|
from k1link.simulation.ai_polygon.robots import select_robot
|
|
from k1link.simulation.ai_polygon.run_inputs import sensor_rig
|
|
from k1link.simulation.ai_polygon.source_projection import PinholeSource
|
|
|
|
robot = select_robot("dcd006-v020")
|
|
geometry = robot["physics"]
|
|
if name == "navigation/footprint":
|
|
return result.Footprint(geometry)
|
|
if name == "navigation_client":
|
|
source = PinholeSource(sensor_rig(robot, 0.8))
|
|
return SimpleNamespace(
|
|
**{
|
|
**vars(result),
|
|
"project": partial(result.project, source=source),
|
|
"visual_goal": partial(result.visual_goal, source=source, geometry=geometry),
|
|
"recovery_goal": partial(result.recovery_goal, geometry=geometry),
|
|
"corner_approach": partial(
|
|
result.corner_approach, source=source, geometry=geometry
|
|
),
|
|
}
|
|
)
|
|
if name == "navigation/terrain_costs":
|
|
return SimpleNamespace(
|
|
**{
|
|
**vars(result),
|
|
"underbody_support_costs": partial(
|
|
result.underbody_support_costs, geometry=geometry
|
|
),
|
|
}
|
|
)
|
|
return result
|
|
|
|
|
|
def test_local_route_receipt_is_bounded_and_binds_the_exact_observation():
|
|
receipt = module("navigation/route_observation").route_observation
|
|
terrain = np.array([[x, 0, 0, 1] for x in range(25)] + [[0.5, 0, 0, 0]])
|
|
pose = np.array([0, 0, 0.37, 0, 0, 0, 1])
|
|
value = {"simulation_time_seconds": 4, "execution_inputs_sha256": "a" * 64}
|
|
result = receipt(terrain, pose, value, 0.15)
|
|
assert len(result["near_obstacles"]) == 12
|
|
assert [p[0] for p in result["near_obstacles"]] == list(range(12))
|
|
assert result["observation"] == {"pose": pose.tolist(), **value}
|
|
assert receipt(np.array([[0, 0, 0, 0]]), pose, value, 0.15)["near_obstacles"] == []
|
|
|
|
|
|
def test_grouped_semantic_barriers_preserve_per_return_expiry_and_boundaries():
|
|
memory = module("navigation/semantic_surface").SemanticSurface()
|
|
samples = np.array([[0, 0, 0, 0], [-0.001, 0, 0, 1], [0.201, 0, 0, 1], [0.202, 0, 1, 0]])
|
|
memory.observe(samples, 10)
|
|
rng = np.random.default_rng(20261003)
|
|
terrain = rng.uniform(-0.5, 0.5, (2000, 4))
|
|
original = terrain.copy()
|
|
for now in (10, 39.999, 40, 40.001):
|
|
expected = terrain.copy()
|
|
blocked = []
|
|
for point in terrain:
|
|
cell = tuple(memory.cell_keys(point[None])[0])
|
|
permission, observed = memory.cells.get(cell, (1, now))
|
|
blocked.append(permission == 0 and observed >= now - 30)
|
|
expected[blocked, 3] = np.maximum(expected[blocked, 3], 1)
|
|
actual, count = memory.apply(terrain, now)
|
|
assert count == sum(blocked) and np.array_equal(expected, actual)
|
|
assert np.array_equal(original, terrain)
|
|
|
|
|
|
def test_semantic_snapshot_serializes_numpy_cells_without_refreshing_ttl():
|
|
import json
|
|
|
|
memory = module("navigation/semantic_surface").SemanticSurface()
|
|
memory.observe([[-1.01, 0.21, 0, 1]], 10)
|
|
assert json.loads(json.dumps(memory.snapshot(42))) == [[-6, 1, 0, 1, 32.0]]
|
|
terrain = np.array([[-1.01, 0.21, 0, 0]])
|
|
assert len(memory.permitted_terrain(terrain, 42)) == 0
|
|
|
|
|
|
@pytest.mark.parametrize('shift', [[0,0,0], [83.11,-24.03,2.17]])
|
|
def test_wall_labels_do_not_project_onto_observed_sidewalk_below(shift):
|
|
memory = module('navigation/semantic_surface').SemanticSurface(fuse_observations=True)
|
|
xyz = np.array([[1.01,.01,1.278], [1.01,.01,1.55], [1.01,.01,2.8]])+shift
|
|
terrain = np.c_[xyz, [0.,.3,1.5]]
|
|
# Many wall returns in the same XY column used to outvote the sidewalk.
|
|
samples = np.r_[np.c_[xyz[:1], [1.]], np.tile(np.c_[xyz[1:], [0.,0.]], (20,1))]
|
|
for frame in range(8):
|
|
memory.observe(samples, frame, evidence_id=str(frame))
|
|
output, count = memory.apply(terrain, 8)
|
|
assert output[:,3].tolist() == [0.,1.,1.5] and count == 2
|
|
np.testing.assert_array_equal(memory.permitted_terrain(terrain,8),terrain[:1])
|
|
# A new excluded measurement on that same floor still blocks it.
|
|
for frame in range(8,11):
|
|
memory.observe(np.c_[xyz[:1], [0.]], frame, evidence_id=str(frame))
|
|
assert memory.apply(terrain,11)[0][0,3] == 1
|
|
|
|
|
|
def test_allowed_upper_surface_cannot_permit_excluded_or_unseen_lower_floor():
|
|
memory = module('navigation/semantic_surface').SemanticSurface()
|
|
memory.observe([[1,0,1,1],[1,0,0,0]],0)
|
|
terrain=np.array([[1,0,1,0],[1,0,0,0],[1,0,-.5,0]],float)
|
|
assert memory.apply(terrain,0)[0][:,3].tolist() == [0,1,0]
|
|
assert memory.permitted_terrain(terrain,0).tolist() == [[1,0,1,0]]
|
|
|
|
|
|
def test_simulation_composition_has_causal_dependencies_and_separate_authority():
|
|
graph = compose(ROOT)
|
|
assert graph.as_dict()["execution"]["mode"] == "worker-local-simulation"
|
|
motion = graph.nodes[-1]
|
|
assert motion.module.group == "motion"
|
|
assert dict(motion.inputs)["navigation.surface"] == "simulation-waypoint-mission"
|
|
assert dict(motion.inputs)["detection.boxes"] == "simulation-rf-detr"
|
|
assert motion.module.state_policy == "causal-reset-at-source-start"
|
|
selection = graph.selection_document()
|
|
selection["selections"] = [r for r in selection["selections"] if r["group"] != "segmentation"]
|
|
with pytest.raises(CompositionError, match="segmentation.known"):
|
|
compose(ROOT, selection)
|
|
|
|
|
|
def test_surface_providers_are_interchangeable_in_the_shared_constructor():
|
|
selection = compose(ROOT).selection_document()
|
|
reference = next(m for m in registry(ROOT).modules if m.module_id == "simulation-ddrnet-goose")
|
|
for row in selection["selections"]:
|
|
if row["group"] == "segmentation":
|
|
row.update(module_id=reference.module_id, module_sha256=reference.sha256)
|
|
graph = compose(ROOT, selection)
|
|
policy = next(n for n in graph.nodes if n.module.group == "policy")
|
|
assert dict(policy.inputs)["segmentation.surface"] == reference.module_id
|
|
|
|
|
|
def test_composition_rejects_stale_module_identity():
|
|
selection = compose(ROOT).selection_document()
|
|
selection["selections"][0]["module_sha256"] = "0" * 64
|
|
with pytest.raises(CompositionError, match="not installed"):
|
|
compose(ROOT, selection)
|
|
|
|
|
|
def test_shared_constructor_import_needs_no_core_or_third_party_runtime():
|
|
# -S removes site-packages, as in the minimal Windows coordinator. Loading
|
|
# a contract must not load the POSIX-only artifact gateway through __init__.
|
|
source = str(ROOT.parents[1] / "src")
|
|
result = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-S",
|
|
"-c",
|
|
f"import sys; sys.path.insert(0, {source!r}); "
|
|
"from k1link.observatory.modular_composition import ModuleRegistry; "
|
|
"from k1link.simulation.ai_polygon.composition import compose; "
|
|
"assert 'k1link.artifact_gateway' not in sys.modules",
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
assert result.returncode == 0, result.stderr
|
|
|
|
|
|
def test_semantic_goal_uses_range_and_correct_square_camera_crop():
|
|
nav = module("navigation_client")
|
|
points = np.array(
|
|
[[x, y, 0] for x in np.linspace(1.5, 3, 20) for y in np.linspace(-0.3, 0.3, 9)],
|
|
dtype=np.float32,
|
|
)
|
|
pose = [0, 0, 0.27, 0, 0, 0, 1]
|
|
calibration = {
|
|
"origin": [0.38, 0, 0.8],
|
|
"rotation": np.eye(3).reshape(-1).tolist(),
|
|
"intrinsics": [800 * 24 / 36, 800 * 24 / 36, 400, 300],
|
|
}
|
|
leaves = np.ones((512, 512), dtype=bool)
|
|
goal = nav.visual_goal(leaves, points, pose, calibration)
|
|
assert goal is not None and 1.5 < goal[0] < 3 and abs(goal[1]) < 0.3
|
|
assert nav.visual_goal(np.zeros_like(leaves), points, pose, calibration) is None
|
|
assert nav.visual_goal(leaves, points + [0, 0, 2], pose, calibration) is None
|
|
# A previously valid goal cannot authorize motion through newly unknown RGB.
|
|
assert nav.visual_goal(np.zeros_like(leaves), points, pose, calibration, goal) is None
|
|
# An explicit-route waypoint entering the camera blind strip is retained,
|
|
# but losing all current visual surface support still forbids movement.
|
|
close = [0.65, 0, 0]
|
|
assert nav.visual_goal(leaves, points, pose, calibration, close, target=[0.65, 0]) == close
|
|
assert (
|
|
nav.visual_goal(np.zeros_like(leaves), points, pose, calibration, close, target=[0.65, 0])
|
|
is None
|
|
)
|
|
|
|
|
|
def test_ground_placement_uses_actual_triangle_intersection():
|
|
terrain = module("terrain")
|
|
vertices = np.array([[0, 0, 0], [1, 0, 0.2], [0, 1, 0]], dtype=np.float32)
|
|
faces = np.array([[0, 1, 2]], dtype=np.int32)
|
|
assert terrain.ground_intersections(vertices, faces, 0.25, 0.25)[0] == pytest.approx(0.05)
|
|
assert len(terrain.ground_intersections(vertices, faces, 0.9, 0.9)) == 0
|
|
|
|
|
|
def test_local_goal_ranks_permitted_candidates_instead_of_rejecting_best_outside():
|
|
nav = module("navigation_client")
|
|
points = np.array(
|
|
[[x, y, 0] for x in np.linspace(1.5, 3.3, 40) for y in np.linspace(-0.3, 0.3, 9)]
|
|
)
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
calibration = {
|
|
"origin": [0.38, 0, 0.8],
|
|
"rotation": np.eye(3).reshape(-1).tolist(),
|
|
"intrinsics": [800 * 24 / 36, 800 * 24 / 36, 400, 300],
|
|
}
|
|
surface = np.ones((512, 512), bool)
|
|
baseline = nav.visual_goal(surface, points, pose, calibration, target=[6, 0])
|
|
assert baseline[0] > 2.6
|
|
def allowed(candidates):
|
|
return candidates[:, 0] <= 2.6
|
|
selected = nav.visual_goal(
|
|
surface, points, pose, calibration, prior=baseline, target=[6, 0], candidate_mask=allowed
|
|
)
|
|
assert selected is not None and selected[0] <= 2.6
|
|
assert (
|
|
nav.visual_goal(
|
|
np.zeros_like(surface), points, pose, calibration, target=[6, 0], candidate_mask=allowed
|
|
)
|
|
is None
|
|
)
|
|
assert (
|
|
nav.visual_goal(
|
|
surface,
|
|
points,
|
|
pose,
|
|
calibration,
|
|
target=[6, 0],
|
|
candidate_mask=lambda candidates: np.zeros(len(candidates), bool),
|
|
)
|
|
is None
|
|
)
|
|
|
|
|
|
def test_observed_route_goal_keeps_task_position_and_cannot_run_away_from_it():
|
|
choose = module("navigation_client").visual_goal
|
|
points = np.array(
|
|
[[x, y, 0] for x in np.arange(1.2, 3.1, 0.05) for y in np.arange(-0.5, 0.51, 0.05)]
|
|
)
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
calibration = {
|
|
"origin": [0.38, 0, 0.8],
|
|
"rotation": np.eye(3).reshape(-1).tolist(),
|
|
"intrinsics": [800 * 24 / 36, 800 * 24 / 36, 400, 300],
|
|
"body_contact_height_m": 0.37,
|
|
}
|
|
surface = np.ones((512, 512), dtype=bool)
|
|
target = [2.03, 0.27]
|
|
assert choose(surface, points, pose, calibration, target=target) == pytest.approx([*target, 0])
|
|
# The close, already observed waypoint can enter the camera blind strip.
|
|
advanced = [1.5, 0, 0.37, 0, 0, 0, 1]
|
|
camera = {**calibration, "origin": [1.88, 0, 0.8]}
|
|
prior = [*target, 0]
|
|
# A rejected centre may select observed ground inside the arrival region;
|
|
# an unconstrained pixel median must never move it outside that region.
|
|
changed = surface.copy()
|
|
_, u, v, visible = module("navigation_client").project(np.asarray([prior]), calibration)
|
|
assert visible.all()
|
|
changed[int(v[0] * 512 / 600), int((u[0] - 100) * 512 / 600)] = False
|
|
alternative = choose(changed, points, pose, calibration, prior, target)
|
|
assert alternative is not None
|
|
assert np.linalg.norm(np.asarray(alternative[:2]) - target) <= 0.25
|
|
assert choose(surface, points, pose, calibration, prior, target) == prior
|
|
assert choose(surface, points + [1.5, 0, 0], advanced, camera, prior, target) == prior
|
|
# The actual camera loses nearby ground beyond the old hardcoded 0.8 m.
|
|
advanced = [1.1, 0, 0.37, 0, 0, 0, 1]
|
|
camera = {**calibration, "origin": [1.48, 0, 0.8]}
|
|
assert choose(surface, points + [1.1, 0, 0], advanced, camera, prior, target) == prior
|
|
assert (
|
|
choose(surface, points + [1.1, 0, 0], advanced, camera, alternative, target) == alternative
|
|
)
|
|
assert choose(np.zeros_like(surface), points, pose, calibration, target=target) is None
|
|
# Clear road ahead is not permission to drive away from a missed waypoint.
|
|
assert choose(surface, points, pose, calibration, target=[-1, 0]) is None
|
|
# A distant task may still use an observed local goal towards it.
|
|
far = choose(surface, points, pose, calibration, target=[8, 0])
|
|
assert far is not None and 1.2 <= far[0] <= 3.1
|
|
|
|
|
|
def test_offset_arrival_goal_survives_blind_strip_with_stopping_reserve():
|
|
nav = module("navigation_client")
|
|
points = np.array(
|
|
[[x, y, 0] for x in np.arange(1.5, 3.1, 0.05) for y in np.arange(-0.3, 0.31, 0.05)]
|
|
)
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
calibration = {
|
|
"origin": [0.62, 0, 0.8],
|
|
"rotation": np.eye(3).reshape(-1).tolist(),
|
|
"intrinsics": [800 * 24 / 36, 800 * 24 / 36, 400, 300],
|
|
}
|
|
surface = np.ones((512, 512), dtype=bool)
|
|
target = [0.8, 0]
|
|
# A measured local goal 27 cm beyond the corner still fits the 40 cm
|
|
# arrival disc, including the follower's 8 cm stopping tolerance.
|
|
prior = [1.07, 0, 0]
|
|
assert nav.visual_goal(surface, points, pose, calibration, prior, target) == prior
|
|
# Missing current road observations cannot be replaced by remembered intent.
|
|
assert nav.visual_goal(np.zeros_like(surface), points, pose, calibration, prior, target) is None
|
|
# A farther goal leaves too little stopping reserve and must not be retained.
|
|
assert nav.visual_goal(surface, points, pose, calibration, [1.13, 0, 0], target) is None
|
|
# A goal with a visible, explicitly forbidden side is rejected even if its
|
|
# other contact projections have entered the camera blind strip.
|
|
camera = {**calibration, "origin": [0.62, 0, 0.25]}
|
|
_, u, v, visible = nav.project(np.array([[1.07, -0.4525, 0], prior, [1.07, 0.4525, 0]]), camera)
|
|
assert visible.any() and not visible.all()
|
|
denied = surface.copy()
|
|
from k1link.simulation.ai_polygon.robots import select_robot
|
|
from k1link.simulation.ai_polygon.run_inputs import sensor_rig
|
|
from k1link.simulation.ai_polygon.source_projection import PinholeSource
|
|
|
|
source = PinholeSource(sensor_rig(select_robot("dcd006-v020"), 0.8))
|
|
denied[source.indices(u[visible], v[visible])] = False
|
|
assert nav.visual_goal(denied, points, pose, camera, prior, target) is None
|
|
|
|
|
|
def test_collision_identity_follows_geometry_and_tile_coverage_not_camera_or_start():
|
|
settings = dict(
|
|
meters_per_unit=1,
|
|
rotation_degrees=[-90, 0, 180],
|
|
spawn_xy=[0, 0],
|
|
ground_z=0,
|
|
camera_height_m=0.8,
|
|
max_speed_mps=0.15,
|
|
)
|
|
terrain = dict(source_sha256="a" * 64, generator_sha256="b" * 64, settings=settings)
|
|
world = dict(sha256="a" * 64, settings={**settings, "spawn_xy": [1, 1], "camera_height_m": 1})
|
|
assert terrain_matches(terrain, world, "b" * 64)
|
|
assert not terrain_matches(terrain, world, "c" * 64)
|
|
assert not terrain_matches(terrain, {**world, "sha256": "d" * 64})
|
|
for change in ({"spawn_xy": [20, 0]}, {"meters_per_unit": 2}, {"rotation_degrees": [0, 0, 0]}):
|
|
assert not terrain_matches(terrain, {**world, "settings": {**world["settings"], **change}})
|
|
|
|
|
|
def test_paired_full_scene_does_not_inherit_generated_tile_bounds():
|
|
settings = dict(meters_per_unit=1, rotation_degrees=[90, 0, 0], spawn_xy=[0, 0], ground_z=5)
|
|
terrain = dict(
|
|
generator="paired-source",
|
|
source_sha256="a" * 64,
|
|
collider_sha256="b" * 64,
|
|
settings=settings,
|
|
)
|
|
world = dict(
|
|
sha256="a" * 64,
|
|
collider_sha256="b" * 64,
|
|
settings={**settings, "spawn_xy": [210, 30], "ground_z": 1.5},
|
|
)
|
|
assert terrain_matches(terrain, world)
|
|
assert not terrain_matches(terrain, {**world, "collider_sha256": "c" * 64})
|
|
assert not terrain_matches(
|
|
terrain, {**world, "settings": {**world["settings"], "meters_per_unit": 2}}
|
|
)
|
|
assert not terrain_matches({**terrain, "generator": "generated-tile"}, world)
|
|
|
|
|
|
def test_square_footprint_fits_straight_corridor_and_rejects_corner_sweep():
|
|
check = module("navigation/footprint").swept_footprint_clear
|
|
pose = [0, 0, 0.27, 0, 0, 0, 1]
|
|
path = [[0, 0, 0], [0.5, 0, 0], [1, 0, 0]]
|
|
walls = np.array([[x, y, 0.5, 0.5] for x in np.arange(-1, 2, 0.1) for y in [-0.65, 0.65]])
|
|
assert check(path, walls, pose)
|
|
walls[:, 1] *= 0.45 / 0.65
|
|
assert not check(path, walls, pose)
|
|
# A diagonal turn sweeps a square corner into this obstacle, even though
|
|
# the chassis at its initial and final straight poses does not contain it.
|
|
assert not check([[0, 0, 0], [0.5, 0.5, 0]], [[0.7, 0, 0.5, 0.5]], pose)
|
|
|
|
|
|
def test_command_monitor_covers_deadman_braking_distance_and_rotation():
|
|
check = module("navigation/footprint").command_footprint_clear
|
|
pose = [0, 0, 0.27, 0, 0, 0, 1]
|
|
assert check(0.15, 0, [[1.5, 0, 0.5, 0.5]], pose)
|
|
assert not check(0.15, 0, [[0.7, 0, 0.5, 0.5]], pose)
|
|
assert not check(0, 0.8, [[0.7, 0, 0.5, 0.5]], pose)
|
|
|
|
|
|
def test_command_sweep_covers_recorded_understeer_and_retains_straight_corridor():
|
|
monitor = module("navigation/footprint")
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
# Reconstructed body corner from v0.20-m130-mu0.80, left-arc at 1.9 s.
|
|
# The ideal .2 m/s, .2 rad/s arc previously admitted this virtual obstacle.
|
|
obstacle = [[0.8994258851, -0.3573475900, 0.5, 0.5]]
|
|
assert not monitor.command_footprint_clear(0.2, 0.2, obstacle, pose)
|
|
walls = [[x, y, 0.5, 0.5] for x in np.arange(-1, 2, 0.05) for y in (-0.52, 0.52)]
|
|
assert monitor.command_footprint_clear(0.2, 0, walls, pose)
|
|
assert not monitor.command_footprint_clear(0.2, 0.2, walls, pose)
|
|
assert not monitor.command_footprint_clear(0, 0.8, walls, pose)
|
|
# Turning error cannot excuse an obstacle within the initial true body.
|
|
assert not monitor.command_footprint_clear(0.2, 0.2, [[0, 0, 0.5, 0.5]], pose)
|
|
|
|
|
|
def test_command_envelope_contains_incomplete_responses_and_scales_monotonically():
|
|
monitor = module("navigation/footprint")
|
|
for speed in (-0.3, 0, 0.3):
|
|
for yaw in (-0.8, 0, 0.8):
|
|
b = monitor.command_bounds(speed, yaw)
|
|
lower, upper = np.array(b["lower_xy_m"]), np.array(b["upper_xy_m"])
|
|
for response_v in (0, 0.3, 1):
|
|
for response_w in (0, 0.2, 1):
|
|
t = np.linspace(0, b["horizon_seconds"], 101)
|
|
v, w = speed * response_v, yaw * response_w
|
|
a = w * t
|
|
x = v * np.sin(a) / w if w else v * t
|
|
y = v * (1 - np.cos(a)) / w if w else np.zeros_like(t)
|
|
for cx in (-monitor.half_length, monitor.half_length):
|
|
for cy in (-monitor.half_width, monitor.half_width):
|
|
points = np.column_stack(
|
|
(
|
|
x + cx * np.cos(a) - cy * np.sin(a),
|
|
y + cx * np.sin(a) + cy * np.cos(a),
|
|
)
|
|
)
|
|
assert np.all(points >= lower - 1e-9)
|
|
assert np.all(points <= upper + 1e-9)
|
|
for scale in (0.2, 0.5, 0.8):
|
|
small = monitor.command_bounds(speed * scale, yaw * scale)
|
|
assert np.all(np.array(small["lower_xy_m"]) >= lower - 1e-9)
|
|
assert np.all(np.array(small["upper_xy_m"]) <= upper + 1e-9)
|
|
assert np.all(np.array(small["discrepancy_xy_m"]) <= b["discrepancy_xy_m"])
|
|
|
|
|
|
@pytest.mark.parametrize("speed,yaw", [(float("nan"), 0), (0, float("inf"))])
|
|
def test_command_sweep_rejects_nonfinite_commands(speed, yaw):
|
|
with pytest.raises(ValueError):
|
|
module("navigation/footprint").command_footprint_clear(
|
|
speed, yaw, [], [0, 0, 0.37, 0, 0, 0, 1]
|
|
)
|
|
|
|
|
|
def test_smooth_slope_is_distinct_from_a_step_or_vertical_terrain():
|
|
costs = module("navigation/terrain_costs").supported_slope_costs
|
|
xy = np.array([[x, y] for x in np.arange(-1, 1.01, 0.1) for y in np.arange(-1, 1.01, 0.1)])
|
|
slope = np.column_stack((xy, xy[:, 0] * np.tan(np.radians(20)), np.full(len(xy), 0.15)))
|
|
normalized, corrected = costs(slope)
|
|
assert corrected > len(slope) * 0.9
|
|
assert normalized[len(slope) // 2, 3] == 0
|
|
assert np.array_equal(normalized[:, :3], slope[:, :3])
|
|
# A 15 cm ledge across the initial footprint cannot become a traversable ramp.
|
|
step = slope.copy()
|
|
step[:, 2] = np.where(step[:, 0] >= 0, 0.15, 0)
|
|
assert costs(step)[1] == 0
|
|
cliff = slope.copy()
|
|
cliff[:, 2] = np.where(cliff[:, 0] >= 0, -0.4, 0)
|
|
assert costs(cliff)[1] == 0
|
|
steep = slope.copy()
|
|
steep[:, 2] = steep[:, 0] * np.tan(np.radians(35))
|
|
assert costs(steep)[1] == 0
|
|
assert costs(slope[np.abs(slope[:, 1]) < 0.01])[1] == 0 # Unobserved lateral support.
|
|
|
|
|
|
def test_underbody_support_does_not_clear_future_terrain_walls_or_drops():
|
|
correct = module("navigation/terrain_costs").underbody_support_costs
|
|
terrain = np.array(
|
|
[
|
|
[-0.375, -0.28, 0.066, 0.103], # Low return already under the chassis.
|
|
[0.55, 0, 0.066, 0.103], # Low return beneath the real leading edge.
|
|
[0.75, 0, 0.066, 0.103], # Never change future terrain from body pose.
|
|
[0, 0, 0.12, 0.12], # A real step within the footprint remains blocked.
|
|
[0, 0, 0.5, 0.5],
|
|
[0, 0, -0.4, 0.4],
|
|
]
|
|
)
|
|
original = terrain.copy()
|
|
result, count = correct(terrain, [0, 0, 0.37, 0, 0, 0, 1])
|
|
assert count == 2 and result[:2, 3] == pytest.approx([0.066, 0.066])
|
|
assert np.array_equal(result[2:], original[2:])
|
|
assert np.array_equal(terrain, original) # Never erase the causal raw map.
|
|
# Rotate both observations and the measured chassis; the result must agree.
|
|
yaw = np.pi / 2
|
|
rotated = terrain.copy()
|
|
rotated[:, :2] = terrain[:, :2] @ np.array([[0, 1], [-1, 0]]) + [3, 4]
|
|
pose = [3, 4, 0.37, 0, 0, np.sin(yaw / 2), np.cos(yaw / 2)]
|
|
assert np.allclose(correct(rotated, pose)[0][:, 3], result[:, 3])
|
|
assert correct(terrain, [0, 0, 0.37, 0, np.sin(np.pi / 12), 0, np.cos(np.pi / 12)])[1] == 0
|
|
|
|
|
|
def test_retreat_requires_observed_full_width_support_and_no_drop_or_step():
|
|
choose = module("navigation_client").recovery_goal
|
|
points = np.array(
|
|
[[x, y, 0.0] for x in np.arange(-1.5, -0.39, 0.05) for y in np.arange(-0.85, 0.86, 0.05)]
|
|
)
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
calibration = {"body_contact_height_m": 0.37}
|
|
assert choose(points, pose, calibration) == pytest.approx([-0.65, 0, 0])
|
|
assert choose(points[points[:, 1] > -0.2], pose, calibration) is None
|
|
assert choose(points[points[:, 0] < -0.9], pose, calibration) is None
|
|
for height in (-0.4, 0.15):
|
|
discontinuous = points.copy()
|
|
discontinuous[points[:, 0] < -0.9, 2] = height
|
|
assert choose(discontinuous, pose, calibration) is None
|
|
slope = points.copy()
|
|
slope[:, 2] = slope[:, 0] * np.tan(np.radians(10))
|
|
assert choose(slope, pose, calibration) is not None
|
|
assert choose(points, pose, calibration, [-0.65, 0.3, 0]) is None
|
|
|
|
|
|
def test_reverse_monitor_checks_behind_the_body():
|
|
check = module("navigation/footprint").command_footprint_clear
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
assert check(-0.1, 0, [[-1.5, 0, 0.5, 0.5]], pose)
|
|
assert not check(-0.1, 0, [[-0.65, 0, 0.5, 0.5]], pose)
|
|
|
|
|
|
def test_legacy_coordinate_migration_is_an_exact_rigid_rotation():
|
|
import json
|
|
import struct
|
|
|
|
migrate = module("navigation/migrate_terrain_coordinates")
|
|
positions = [[1.0, -2.0, -3.0], [2.0, -2.0, -3.0], [1.0, -1.0, -3.0]]
|
|
binary = struct.pack("<9f3I", *(v for p in positions for v in p), 0, 1, 2)
|
|
document = {
|
|
"nodes": [{"mesh": 0}],
|
|
"meshes": [{"primitives": [{"attributes": {"POSITION": 0}, "indices": 1}]}],
|
|
"accessors": [
|
|
{
|
|
"bufferView": 0,
|
|
"componentType": 5126,
|
|
"type": "VEC3",
|
|
"count": 3,
|
|
"min": [1, -2, -3],
|
|
"max": [2, -1, -3],
|
|
},
|
|
{"bufferView": 1, "componentType": 5125, "type": "SCALAR", "count": 3},
|
|
],
|
|
"bufferViews": [{"byteOffset": 0, "byteLength": 36}, {"byteOffset": 36, "byteLength": 12}],
|
|
}
|
|
raw = json.dumps(document).encode()
|
|
raw += b" " * ((-len(raw)) % 4)
|
|
glb = (
|
|
struct.pack("<III", 0x46546C67, 2, 28 + len(raw) + len(binary))
|
|
+ struct.pack("<II", len(raw), 0x4E4F534A)
|
|
+ raw
|
|
+ struct.pack("<II", len(binary), 0x004E4942)
|
|
+ binary
|
|
)
|
|
corrected = migrate.rotate_glb(glb)
|
|
length = struct.unpack_from("<I", corrected, 12)[0]
|
|
result = np.array(struct.unpack_from("<9f", corrected, 28 + length)).reshape(-1, 3)
|
|
assert np.array_equal(result, np.array(positions) * [-1, 1, -1])
|
|
assert np.array_equal(
|
|
result[:, [0, 2, 1]] * [1, -1, 1], [[-1, -3, -2], [-2, -3, -2], [-1, -3, -1]]
|
|
)
|
|
assert struct.unpack_from("<3I", corrected, 28 + length + 36) == (0, 1, 2)
|
|
|
|
|
|
def test_voxel_quantized_grade_does_not_become_a_wall_but_ledge_remains():
|
|
costs = module("navigation/terrain_costs").supported_slope_costs
|
|
xy = np.array([[x, y] for x in np.arange(-1, 1.01, 0.06) for y in np.arange(-1, 1.01, 0.06)])
|
|
height = np.round(xy[:, 0] * np.tan(np.radians(20)) / 0.06) * 0.06
|
|
surface = np.column_stack((xy, height, np.full(len(xy), 0.15)))
|
|
assert costs(surface)[1] > len(surface) * 0.8
|
|
for discontinuity in (0.12, 0.15, -0.4):
|
|
ledge = surface.copy()
|
|
ledge[:, 2] = np.where(xy[:, 0] >= 0, discontinuity, 0)
|
|
corrected, _ = costs(ledge)
|
|
near_edge = np.abs(xy[:, 0]) < 0.12
|
|
assert np.all(corrected[near_edge, 3] > 0.1)
|
|
stone = surface.copy()
|
|
stone[:, 2] = 0
|
|
stone[(abs(xy[:, 0]) < 0.12) & (abs(xy[:, 1]) < 0.12), 2] = 0.15
|
|
assert costs(stone)[1] == 0
|
|
|
|
|
|
def test_grade_fit_cannot_bridge_an_unobserved_gap():
|
|
costs = module("navigation/terrain_costs").supported_slope_costs
|
|
points = np.array(
|
|
[
|
|
[x, y, 0 if x < 0 else 0.15, 0.15]
|
|
for x in [-0.3, -0.2, 0.2, 0.3]
|
|
for y in np.arange(-0.3, 0.31, 0.1)
|
|
]
|
|
)
|
|
assert costs(points)[1] == 0
|
|
|
|
|
|
def test_existing_reserve_overlap_only_allows_departure_not_approach_or_body_overlap():
|
|
check = module("navigation/footprint").command_footprint_clear
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
behind = [[-0.61, 0, 0.5, 0.5]]
|
|
assert check(0.15, 0, behind, pose)
|
|
assert not check(-0.1, 0, behind, pose)
|
|
assert not check(0, 0.35, behind, pose)
|
|
assert not check(0.15, 0, [[-0.49, 0, 0.5, 0.5]], pose)
|
|
assert not check(0.15, 0, [[0.53, 0, 0.5, 0.5]], pose)
|
|
# A longer admitted camera age must also enlarge the collision envelope.
|
|
assert not check(0.15, 0, [[0.80, 0, 0.5, 0.5]], pose)
|
|
|
|
|
|
def test_terrain_fit_cache_invalidates_when_a_new_obstacle_is_observed():
|
|
costs = module("navigation/terrain_costs")
|
|
normalize = costs.TerrainCostNormalizer()
|
|
xy = np.array([[x, y] for x in np.arange(-1, 1.01, 0.1) for y in np.arange(-1, 1.01, 0.1)])
|
|
grade = np.column_stack((xy, xy[:, 0] * np.tan(np.radians(20)), np.full(len(xy), 0.15)))
|
|
clear, count = normalize(grade)
|
|
assert count > len(grade) * 0.9
|
|
assert np.array_equal(normalize(grade)[0], clear)
|
|
changed = np.vstack((grade, [0.02, 0.02, 0.3, 0.3]))
|
|
cached, count = normalize(changed)
|
|
fresh, expected_count = costs.supported_slope_costs(changed)
|
|
assert np.array_equal(cached, fresh) and count == expected_count
|
|
assert cached[len(grade) // 2, 3] > 0.1
|
|
assert np.array_equal(normalize(grade)[0], clear)
|
|
|
|
|
|
def test_velocity_regulation_keeps_the_selected_arc_and_its_braking_clearance():
|
|
monitor = module("navigation/footprint")
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
hazard = [[0.834, -0.08, 0.15, 0.15]]
|
|
speed, yaw, scale = monitor.regulate_command(0.15, -0.245, hazard, pose)
|
|
assert 0.2 <= scale < 1
|
|
assert speed / yaw == pytest.approx(0.15 / -0.245)
|
|
assert monitor.command_footprint_clear(speed, yaw, hazard, pose)
|
|
assert monitor.regulate_command(0.15, 0, [[0.56, 0, 0.2, 0.2]], pose) == (0, 0, 0)
|
|
assert monitor.regulate_command(0.15, 0, [[0.49, 0, 0.2, 0.2]], pose) == (0, 0, 0)
|
|
reverse, _, scale = monitor.regulate_command(-0.1, 0, [[-0.68, 0, 0.2, 0.2]], pose)
|
|
assert 0.2 <= scale < 1 and reverse < 0
|
|
assert monitor.command_footprint_clear(reverse, 0, [[-0.68, 0, 0.2, 0.2]], pose)
|
|
|
|
|
|
def test_range_fan_rotation_preserves_both_mounts_and_first_hit_occlusion():
|
|
from types import SimpleNamespace
|
|
|
|
sensor = module("terrain").RangeSensor.__new__(module("terrain").RangeSensor)
|
|
sensor.directions = np.array([[1.0, 0, 0], [0, 1, 0], [0, 0, -1]])
|
|
sensor.rear_directions = np.array([[-1.0, 0, 0]])
|
|
calls = []
|
|
hits = iter(
|
|
[
|
|
{
|
|
"hit": True,
|
|
"rigidBody": "/World/Ground",
|
|
"collision": "/World/Ground",
|
|
"position": [1, 2, 3],
|
|
},
|
|
{"hit": True, "rigidBody": "/World/Rover/chassis", "position": [7, 7, 7]},
|
|
{"hit": False},
|
|
{
|
|
"hit": True,
|
|
"rigidBody": "/World/Obstacle",
|
|
"collision": "/World/Obstacle/Shape",
|
|
"position": [4, 5, 6],
|
|
},
|
|
]
|
|
)
|
|
|
|
def cast(origin, direction, distance, **kwargs):
|
|
calls.append((origin, direction, distance, kwargs))
|
|
return next(hits)
|
|
|
|
sensor.query = SimpleNamespace(raycast_closest=cast)
|
|
transform = np.array([[0, 1, 0, 0], [-1, 0, 0, 0], [0, 0, 1, 0], [9, 8, 7, 1]])
|
|
result = sensor.capture([1, 0, 0.8], transform, [-1, 0, 0.8])
|
|
assert result.tolist() == [[1, 2, 3], [4, 5, 6]]
|
|
assert sensor.hit_paths == {"/World/Ground": 1, "/World/Obstacle/Shape": 1}
|
|
# Exact per-beam mounts, max-range no-echo, and no ray behind self-occlusion.
|
|
assert sensor.rays["origins"][sensor.rays["origin_indices"]].tolist() == pytest.approx(
|
|
np.array([[1, 0, 0.8], [1, 0, 0.8], [-1, 0, 0.8]])
|
|
)
|
|
assert sensor.rays["ends"].tolist() == pytest.approx(
|
|
np.array([[1, 2, 3], [1, 0, -7.2], [4, 5, 6]])
|
|
)
|
|
assert [c[1] for c in calls] == [(0, 1, 0), (-1, 0, 0), (0, 0, -1), (0, -1, 0)]
|
|
assert [c[0] for c in calls] == [(1, 0, 0.8)] * 3 + [(-1, 0, 0.8)]
|
|
assert all(c[2:] == (8.0, {"bothSides": True}) for c in calls)
|
|
sensor.query = SimpleNamespace(raycast_closest=lambda *a, **k: {"hit": False})
|
|
assert len(sensor.capture([1, 0, 0.8], transform)) == 0
|
|
assert sensor.hit_paths == {}
|
|
|
|
|
|
def test_snapshot_patch_requires_exact_upstream_anchors():
|
|
patch = module("navigation/patch_snapshot").patch
|
|
source = "\n".join(
|
|
[
|
|
"bool clearingCloud = false;",
|
|
' nh->declare_parameter<double>("scanVoxelSize", scanVoxelSize);',
|
|
' nh->get_parameter("scanVoxelSize", scanVoxelSize);',
|
|
" // stack registered laser scans",
|
|
]
|
|
)
|
|
result = patch(source)
|
|
assert "bool inputIsMapSnapshot = false;" in result
|
|
assert result.index("terrainVoxelCloud[ind]->clear()") < result.index(
|
|
"// stack registered laser scans"
|
|
)
|
|
with pytest.raises(ValueError, match="already present"):
|
|
patch(result)
|
|
with pytest.raises(ValueError, match="anchor"):
|
|
patch(source.replace("bool clearingCloud = false;", ""))
|
|
|
|
|
|
def test_blind_corner_approach_uses_fresh_measured_direction_not_ground_permission():
|
|
nav = module("navigation_client")
|
|
points = np.array([[x, y, 0] for x in np.arange(1.5, 2.01, 0.05) for y in (-0.1, 0, 0.1)])
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
calibration = {
|
|
"origin": [0.62, 0, 0.8],
|
|
"rotation": np.eye(3).reshape(-1).tolist(),
|
|
"intrinsics": [800 * 14 / 36, 800 * 14 / 36, 400, 300],
|
|
}
|
|
surface = np.ones((512, 512), dtype=bool)
|
|
original = points.copy()
|
|
result = nav.corner_approach(surface, points, pose, calibration, [0.8, 0])
|
|
assert result["goal"] == [0.8, 0, 0] and result["max_speed_mps"] == 0.1
|
|
assert any(np.array_equal(point, result["observed_direction_point"]) for point in points)
|
|
assert np.array_equal(points, original) and result["authority"].startswith(
|
|
"declared-corner-intent-only"
|
|
)
|
|
# The front-mounted camera may already be past the target while the body
|
|
# centre still approaches it. Current forward observations remain required.
|
|
assert nav.corner_approach(surface, points, pose, calibration, [0.45, 0]) is not None
|
|
for target in (None, [-0.8, 0], [0, 0.8], [1.3, 0], [0.4, 0]):
|
|
assert nav.corner_approach(surface, points, pose, calibration, target) is None
|
|
assert nav.corner_approach(surface, points[:2], pose, calibration, [0.8, 0]) is None
|
|
assert nav.corner_approach(np.zeros_like(surface), points, pose, calibration, [0.8, 0]) is None
|
|
assert nav.corner_approach(surface, points + [0, 1, 0], pose, calibration, [0.8, 0]) is None
|
|
# A currently visible corner uses ordinary semantic/range admission. This
|
|
# fallback cannot replace a visible rejection with an inferred road label.
|
|
assert (
|
|
nav.corner_approach(surface, points, pose, {**calibration, "origin": [0, 0, 0.1]}, [0.8, 0])
|
|
is None
|
|
)
|
|
|
|
|
|
def test_mounted_semantics_retains_occluded_near_ground_and_barriers_until_observed_again():
|
|
memory = module("navigation/semantic_surface").SemanticSurface(retain_near=True)
|
|
pose = [0, 0, 0.37, 0, 0, 0, 1]
|
|
memory.observe([[0, 0, 0, 1], [1, 0, 0, 0], [8, 0, 0, 1]], 0, pose)
|
|
terrain = np.array([[0, 0, 0, 0], [1, 0, 0, 0], [8, 0, 0, 0]], float)
|
|
assert len(memory.permitted_terrain(terrain, 60)) == 1
|
|
assert memory.apply(terrain, 60)[0][1, 3] == 1
|
|
assert memory.cells[(0, 0, 0)][1] == 0 # Retention never rewrites capture time.
|
|
memory.observe([[0, 0, 0, 0], [1, 0, 0, 1]], 61, pose)
|
|
assert memory.apply(terrain, 61)[0][0, 3] == 1
|
|
assert memory.permitted_terrain(terrain, 61)[0, 0] == 1
|
|
memory.observe([], 100, [10, 0, 0.37, 0, 0, 0, 1])
|
|
assert len(memory.permitted_terrain(terrain, 100)) == 0
|