Local preservation snapshot, not a new deployment or simulation acceptance.
105 lines
4.2 KiB
Python
105 lines
4.2 KiB
Python
"""Audit contact-band envelopes against immutable native motion recordings.
|
|
|
|
No simulator ground truth enters navigation. This offline qualifier projects
|
|
the measured 3-D motion of the nominal bottom bands, including pitch/roll.
|
|
"""
|
|
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import math
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
from navigation.footprint import Footprint
|
|
|
|
from k1link.simulation.ai_polygon.run_inputs import robot_geometry
|
|
|
|
|
|
def phase_metrics(rows, robot):
|
|
geometry = robot_geometry(robot)
|
|
monitor = Footprint(geometry)
|
|
v, w = rows[0]["request"]
|
|
bounds = monitor.command_bounds(v, w)
|
|
cells, _ = monitor.contacts.cells(bounds, w)
|
|
spacing = monitor.contacts.spacing_m
|
|
admitted = {tuple(p) for p in np.rint(cells / spacing).astype(int)}
|
|
vertices = np.array(
|
|
[
|
|
[x, y, -geometry["body_contact_height_m"]]
|
|
for low, high in monitor.contacts.bands
|
|
for x in np.linspace(low[0], high[0], 5)
|
|
for y in np.linspace(low[1], high[1], 3)
|
|
]
|
|
)
|
|
dt = rows[0]["dt"]
|
|
frames, stride = math.ceil(bounds["horizon_seconds"] / dt), max(1, round(0.25 / dt))
|
|
projected = []
|
|
for row in rows:
|
|
if row["request"] != [v, w] or row["dt"] != dt:
|
|
raise ValueError("Contact qualification phase changed command or clock")
|
|
qw, qx, qy, qz = row["quaternion_wxyz"]
|
|
rotation = np.array(
|
|
[
|
|
[1 - 2 * (qy * qy + qz * qz), 2 * (qx * qy - qw * qz), 2 * (qx * qz + qw * qy)],
|
|
[2 * (qx * qy + qw * qz), 1 - 2 * (qx * qx + qz * qz), 2 * (qy * qz - qw * qx)],
|
|
[2 * (qx * qz - qw * qy), 2 * (qy * qz + qw * qx), 1 - 2 * (qx * qx + qy * qy)],
|
|
]
|
|
)
|
|
projected.append((vertices @ rotation.T)[:, :2] + row["position"][:2])
|
|
projected = np.asarray(projected)
|
|
windows = []
|
|
for start in range(0, len(rows) - frames + 1, stride):
|
|
initial = rows[start]["pre_step"]
|
|
c, s = math.cos(initial["heading"]), math.sin(initial["heading"])
|
|
points = (projected[start : start + frames] - initial["position"][:2]) @ np.array(
|
|
[[c, -s], [s, c]]
|
|
)
|
|
keys = np.unique(np.rint(points.reshape(-1, 2) / spacing).astype(int), axis=0)
|
|
outside = [p.tolist() for p in keys if tuple(p) not in admitted]
|
|
windows.append({"start_frame": start, "passed": not outside, "outside_cells": outside[:12]})
|
|
return {"windows": windows, "passed": bool(windows) and all(w["passed"] for w in windows)}
|
|
|
|
|
|
def audit_rows(rows, robots):
|
|
# The native matrix writes one frame from each concurrent case in turn.
|
|
# Consecutive-line groupby would silently split every phase into one frame.
|
|
groups = defaultdict(list)
|
|
for row in rows:
|
|
groups[(row["case"], row["phase"])].append(row)
|
|
return [
|
|
{"case": case, "phase": phase, **phase_metrics(group, robots[case])}
|
|
for (case, phase), group in groups.items()
|
|
]
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--matrix", type=Path, required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
acceptance = json.loads((args.matrix / "acceptance.json").read_text())
|
|
robots = {c["id"]: c["robot"] for c in acceptance["cases"]}
|
|
source = args.matrix / "motion.jsonl"
|
|
with source.open() as stream:
|
|
phases = audit_rows((json.loads(line) for line in stream), robots)
|
|
windows = [w for phase in phases for w in phase["windows"]]
|
|
result = {
|
|
"schema_version": "missioncore.contact-sweep-qualification/v1",
|
|
"scope": "reused native flat-ground traces; contact-band projection, not terrain admission",
|
|
"source_sha256": hashlib.sha256(source.read_bytes()).hexdigest(),
|
|
"passed": bool(phases) and all(p["passed"] for p in phases),
|
|
"phases": phases,
|
|
"windows": len(windows),
|
|
"failed_windows": sum(not w["passed"] for w in windows),
|
|
}
|
|
args.output.write_text(json.dumps(result, indent=2))
|
|
print(json.dumps({k: v for k, v in result.items() if k != "phases"}))
|
|
if not result["passed"]:
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|