Local preservation snapshot, not a new deployment or simulation acceptance.
274 lines
11 KiB
Python
274 lines
11 KiB
Python
"""Whole-body geometric admission, separate from the mission's point radius."""
|
|
|
|
import importlib
|
|
import math
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from k1link.simulation.ai_polygon.mission_policy import WaypointMission
|
|
from k1link.simulation.ai_polygon.robots import select_robot
|
|
|
|
|
|
@pytest.fixture
|
|
def monitor(monkeypatch):
|
|
monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/ai-polygon/navigation"))
|
|
return importlib.import_module("footprint").Footprint(select_robot("dcd006-v020")["physics"])
|
|
|
|
|
|
def ground(xmax=2, ymax=2, slope=0):
|
|
return np.array(
|
|
[
|
|
[x, y, x * slope, 0]
|
|
for x in np.arange(-2, xmax + 0.001, 0.05)
|
|
for y in np.arange(-2, ymax + 0.001, 0.05)
|
|
]
|
|
)
|
|
|
|
|
|
def pose(x=0, y=0, yaw=0):
|
|
return [x, y, 0.37, 0, 0, math.sin(yaw / 2), math.cos(yaw / 2)]
|
|
|
|
|
|
def test_obstacle_absence_cannot_stand_in_for_observed_support(monitor):
|
|
empty = monitor.assess_pose([], pose(), 0.37)
|
|
assert empty["collision_free"] and not empty["support_known"] and not empty["admitted"]
|
|
full = monitor.assess_pose(ground(), pose(), 0.37)
|
|
assert full["admitted"] and full["support_samples"] == full["supported_samples"]
|
|
strip = ground()[np.abs(ground()[:, 1]) < 0.01]
|
|
assert not monitor.assess_pose(strip, pose(), 0.37)["support_known"]
|
|
|
|
|
|
def test_nearby_ground_cannot_extrapolate_support_over_a_ledge_or_hole(monitor):
|
|
platform = ground(ymax=0.5)
|
|
assert monitor.assess_pose(platform, pose(), 0.37)["admitted"]
|
|
# Nearest return is <15cm from the corner; it lies on only one side.
|
|
assert not monitor.assess_pose(platform, pose(y=0.15), 0.37)["admitted"]
|
|
p = ground()
|
|
hole = p[np.linalg.norm(p[:, :2] - [0.25, 0.1], axis=1) > 0.25]
|
|
assert not monitor.assess_pose(hole, pose(), 0.37)["admitted"]
|
|
|
|
|
|
def test_same_arrival_point_can_fit_one_heading_but_not_another(monitor):
|
|
lane = ground(ymax=0.5)
|
|
lane = lane[lane[:, 1] >= -0.5 - 1e-6]
|
|
assert monitor.assess_pose(lane, pose(), 0.37)["admitted"]
|
|
assert not monitor.assess_pose(lane, pose(yaw=math.pi / 2), 0.37)["admitted"]
|
|
obstacle = np.vstack((ground(), [[0.55, 0.42, 0.25, 0.25]]))
|
|
result = monitor.assess_pose(obstacle, pose(), 0.37)
|
|
assert result["support_known"] and not result["collision_free"] and not result["admitted"]
|
|
|
|
|
|
def test_supported_grade_uses_physical_slope_and_contact_height(monitor):
|
|
for degrees, expected in ((20, True), (28, False)):
|
|
a = math.radians(degrees)
|
|
inclined = [0, 0, 0.37 / math.cos(a), 0, -math.sin(a / 2), 0, math.cos(a / 2)]
|
|
assert (
|
|
monitor.assess_pose(ground(slope=math.tan(a)), inclined, 0.37)["admitted"] == expected
|
|
)
|
|
|
|
|
|
def test_arrival_requires_same_observation_and_does_not_expand_radius():
|
|
mission = WaypointMission([[0, 0]])
|
|
current = pose(x=0.399)
|
|
mission.update(current, 1, lambda *_: None)
|
|
assert mission.arrival_candidate(current) and mission.fault is None
|
|
for observed, seconds, index, admitted in (
|
|
(current, 0, 0, True),
|
|
(pose(x=0.3), 1, 0, True),
|
|
(current, 1, 1, True),
|
|
(current, 1, 0, False),
|
|
):
|
|
assert not mission.confirm_arrival(observed, seconds, index, admitted=admitted)
|
|
assert mission.confirm_arrival(current, 1, 0, admitted=True)
|
|
assert mission.fault == "goal-reached"
|
|
outside = WaypointMission([[0, 0]])
|
|
outside.update(pose(x=0.401), 1, lambda *_: None)
|
|
assert not outside.confirm_arrival(pose(x=0.401), 1, 0, admitted=True)
|
|
|
|
|
|
def test_rejected_body_receipts_do_not_reset_stall_or_advance_waypoints():
|
|
mission = WaypointMission([[0, 0], [3, 0]])
|
|
for t in range(34):
|
|
mission.update(pose(), t, lambda *_: None)
|
|
assert not mission.confirm_arrival(pose(), t, 0, admitted=False)
|
|
assert mission.index == 0 and mission.fault == "stuck"
|
|
|
|
|
|
@pytest.fixture
|
|
def arrival_adapter(monkeypatch):
|
|
from types import SimpleNamespace
|
|
|
|
from k1link.simulation.ai_polygon.run_inputs import sensor_rig
|
|
from k1link.simulation.ai_polygon.source_projection import PinholeSource
|
|
|
|
monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/ai-polygon"))
|
|
from navigation_client import ComposedInference
|
|
|
|
adapter = ComposedInference.__new__(ComposedInference)
|
|
robot = select_robot("dcd006-v020")
|
|
adapter.geometry = robot["physics"]
|
|
adapter.source = PinholeSource(sensor_rig(robot, 0.8))
|
|
adapter.mission = WaypointMission([[0, 0], [3, 0]])
|
|
adapter.inputs = {"sha256": "a" * 64}
|
|
adapter.max_speed = 0.2
|
|
adapter.environment = "park"
|
|
adapter.goal = None # A camera blind strip cannot bypass observed-body admission.
|
|
_, intent = adapter.mission.update(pose(), 1, lambda *_: None)
|
|
inputs = {
|
|
"source.lidar": ground()[:, :3],
|
|
"source.pose": pose(),
|
|
"source.simulation-time": 1,
|
|
"source.range-rays": {},
|
|
"source.camera.calibration": {
|
|
"origin": [0.38, 0, 0.8],
|
|
"rotation": np.eye(3).reshape(-1).tolist(),
|
|
"intrinsics": [533, 533, 400, 300],
|
|
"body_contact_height_m": 0.37,
|
|
},
|
|
"navigation.intent": intent,
|
|
"navigation.surface": np.ones((512, 512), dtype=bool),
|
|
"navigation.permissions": np.ones((512, 512), dtype=bool),
|
|
"detection.boxes": [],
|
|
}
|
|
replies, requests = [], []
|
|
|
|
def request(path, value):
|
|
assert path == "/plan"
|
|
requests.append(value)
|
|
return replies.pop(0)
|
|
|
|
adapter.navigation = SimpleNamespace(request=request)
|
|
return adapter, inputs, replies, requests
|
|
|
|
|
|
def arrival_reply(inputs):
|
|
return {
|
|
"status": "arrival-accepted",
|
|
"speed_mps": 0.2, # The mission transition must always produce a stop.
|
|
"yaw_rate_rps": 0.1,
|
|
"path": [],
|
|
"arrival": {
|
|
"admitted": True,
|
|
"observation": {
|
|
"pose": inputs["source.pose"].copy(),
|
|
"target_xy": [0, 0],
|
|
"simulation_time_seconds": inputs["source.simulation-time"],
|
|
"execution_inputs_sha256": "a" * 64,
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def test_live_adapter_waits_for_body_receipt_then_stops_and_advances_once(arrival_adapter):
|
|
adapter, inputs, replies, requests = arrival_adapter
|
|
for status in ("waiting-for-plan", "blocked"):
|
|
replies.append({"status": status, "speed_mps": 0, "yaw_rate_rps": 0, "path": []})
|
|
assert adapter.navigate(inputs)["motion.command"]["speed_mps"] == 0
|
|
assert adapter.mission.index == 0
|
|
replies.append(arrival_reply(inputs))
|
|
command = adapter.navigate(inputs)["motion.command"]
|
|
assert command["speed_mps"] == command["yaw_rate_rps"] == 0
|
|
assert adapter.mission.index == 1 and adapter.mission.fault is None
|
|
assert all(r["arrival_target"] == [0, 0] for r in requests)
|
|
assert adapter.navigation_evidence["mission"]["waypoint"] == 1
|
|
|
|
|
|
@pytest.mark.parametrize("invalid", ["missing", "denied", "pose", "clock", "target", "inputs"])
|
|
def test_live_adapter_rejects_unbound_or_stale_receipt(arrival_adapter, invalid):
|
|
adapter, inputs, replies, _ = arrival_adapter
|
|
reply = arrival_reply(inputs)
|
|
if invalid == "missing":
|
|
reply.pop("arrival")
|
|
elif invalid == "denied":
|
|
reply["arrival"]["admitted"] = False
|
|
else:
|
|
key, value = {
|
|
"pose": ("pose", pose(x=0.1)),
|
|
"clock": ("simulation_time_seconds", 0),
|
|
"target": ("target_xy", [3, 0]),
|
|
"inputs": ("execution_inputs_sha256", "b" * 64),
|
|
}[invalid]
|
|
reply["arrival"]["observation"][key] = value
|
|
replies.append(reply)
|
|
with pytest.raises(ValueError, match="body arrival receipt"):
|
|
adapter.navigate(inputs)
|
|
assert adapter.mission.index == 0 and adapter.mission.fault is None
|
|
|
|
|
|
def test_arrival_region_can_offer_observed_body_pose_without_radius_relaxation(monitor):
|
|
# Initial under-body blind strip: the waypoint centre is unsupported, but
|
|
# an entire body fits farther ahead inside the same arrival circle.
|
|
platform = ground()
|
|
platform = platform[platform[:, 0] >= -0.25 - 1e-6]
|
|
assert not monitor.assess_pose(platform, pose(), 0.37)["admitted"]
|
|
selected = monitor.find_arrival_pose(platform, pose(x=-0.3), [0, 0], 0.37)
|
|
assert selected is not None and np.linalg.norm(selected[:2]) <= 0.4
|
|
assert selected[3:] == pose()[3:]
|
|
assert monitor.assess_pose(platform, selected, 0.37)["admitted"]
|
|
# No sampling or nearest-point extrapolation can create a destination
|
|
# when the whole admissible arrival region is unsupported.
|
|
assert monitor.find_arrival_pose([], pose(), [0, 0], 0.37) is None
|
|
assert monitor.find_arrival_pose(platform, pose(), [-1, 0], 0.37) is None
|
|
|
|
|
|
def test_body_support_requires_positive_unexpired_surface_permission(monitor, monkeypatch):
|
|
monkeypatch.syspath_prepend(str(Path(__file__).parents[1] / "simulation/ai-polygon/navigation"))
|
|
from semantic_surface import SemanticSurface
|
|
|
|
memory = SemanticSurface()
|
|
terrain = ground()
|
|
assert not monitor.assess_pose(
|
|
terrain, pose(), 0.37, support_terrain=memory.permitted_terrain(terrain, 0)
|
|
)["admitted"]
|
|
samples = terrain.copy()
|
|
samples[:, 3] = 1
|
|
memory.observe(samples, 1)
|
|
assert monitor.assess_pose(
|
|
terrain, pose(), 0.37, support_terrain=memory.permitted_terrain(terrain, 20)
|
|
)["admitted"]
|
|
assert not monitor.assess_pose(
|
|
terrain, pose(), 0.37, support_terrain=memory.permitted_terrain(terrain, 32)
|
|
)["admitted"]
|
|
# A newly forbidden cell cannot retain an older positive permit.
|
|
samples[:, 3] = 0
|
|
memory.observe(samples, 33)
|
|
assert len(memory.permitted_terrain(terrain, 33)) == 0
|
|
|
|
|
|
def test_near_goal_without_current_visual_candidate_only_asks_for_body_validation(arrival_adapter):
|
|
adapter, inputs, replies, requests = arrival_adapter
|
|
inputs["source.pose"] = pose(x=-0.65)
|
|
_, inputs["navigation.intent"] = adapter.mission.update(
|
|
inputs["source.pose"], 2, lambda *_: None
|
|
)
|
|
inputs["source.simulation-time"] = 2
|
|
inputs["navigation.surface"][:] = False
|
|
replies.append({"status": "blocked", "speed_mps": 0, "yaw_rate_rps": 0, "path": []})
|
|
assert adapter.navigate(inputs)["motion.command"]["speed_mps"] == 0
|
|
assert requests[0]["arrival_target"] == [0, 0]
|
|
assert requests[0]["semantic_points"] == [] # Low confidence cannot create positive permission.
|
|
assert adapter.mission.index == 0
|
|
replies.append(arrival_reply(inputs))
|
|
with pytest.raises(ValueError, match="mission observation"):
|
|
adapter.navigate(inputs) # A server cannot expand the unchanged arrival radius.
|
|
|
|
|
|
def test_body_goal_admission_keeps_task_identity_when_visual_candidates_reappear(arrival_adapter):
|
|
adapter, inputs, replies, requests = arrival_adapter
|
|
inputs["source.pose"] = pose(x=-0.65)
|
|
_, inputs["navigation.intent"] = adapter.mission.update(
|
|
inputs["source.pose"], 2, lambda *_: None
|
|
)
|
|
inputs["source.simulation-time"] = 2
|
|
replies.append({"status": "blocked", "speed_mps": 0, "yaw_rate_rps": 0, "path": []})
|
|
adapter.navigate(inputs)
|
|
# A newly visible intermediate candidate lies beyond the task region.
|
|
# CMU must continue receiving the bound task target for revalidation.
|
|
adapter.goal = [0.8, 0.6, 0]
|
|
replies.append({"status": "blocked", "speed_mps": 0, "yaw_rate_rps": 0, "path": []})
|
|
adapter.navigate(inputs)
|
|
assert requests[-1]["arrival_target"] == [0, 0]
|
|
assert adapter.mission.index == 0
|