Files
NODEDC_MISSION_CORE/simulation/ai-polygon/qualify_scene_contact.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

143 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Worker-only contact replay on a retained episode's exact collision asset.
This isolates physical support from perception/planning. It never supplies a
route or collision truth to inference and never writes to the original run.
"""
import argparse
import hashlib
import json
import math
import sys
import time
from datetime import UTC, datetime
from pathlib import Path
from isaacsim import SimulationApp
parser = argparse.ArgumentParser()
parser.add_argument("--run", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--mode", choices=("straight", "replay"), default="straight")
parser.add_argument("--seconds", type=float, default=40)
args = parser.parse_args()
if not 2 <= args.seconds <= 120 or args.output.exists():
raise ValueError("Expected a new report and 2–120 seconds of simulation")
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "src"))
app = SimulationApp({"headless": True, "hide_ui": True})
try:
import omni.timeline
import omni.usd
from isaacsim.core.simulation_manager import SimulationManager
from pxr import UsdGeom
from rover_profile import PROFILE, ROBOT_DOCUMENT, create_rover, open_drive
from terrain import install_terrain
from k1link.simulation.ai_polygon.mission_policy import inclination
run = json.loads(args.run.read_text())
settings = run["world"]["settings"]
stage = omni.usd.get_context().get_stage()
UsdGeom.SetStageUpAxis(stage, UsdGeom.Tokens.z)
UsdGeom.SetStageMetersPerUnit(stage, 1)
height, terrain = install_terrain(stage, run["terrain_manifest"], run["world"])
create_rover(
stage,
[*settings["spawn_xy"], height],
settings["heading_degrees"],
terrain["initial_ground_normal"],
)
SimulationManager.setup_simulation(dt=1 / 60, device="cpu")
omni.timeline.get_timeline_interface().play()
for _ in range(30):
app.update()
drive = open_drive(contact_filter_paths=["/World/Terrain"])
robot = drive.body
playback = []
if args.mode == "replay":
playback = [
json.loads(x) for x in (args.run.parent / "motion.jsonl").read_text().splitlines()
]
trace, cursor, stopped = [], 0, False
started = time.monotonic()
for step in range(round(args.seconds * 60)):
if time.monotonic() - started > 180:
raise TimeoutError("Scene-contact qualification exceeded its wall deadline")
p, q = robot.get_world_poses()
p, q = p.numpy()[0].tolist(), q.numpy()[0].tolist()
pose = p + q[1:] + q[:1]
tilt = inclination(pose)
stopped |= tilt >= PROFILE["stop_tilt_degrees"]
v, w = settings["max_speed_mps"], 0.0
if playback:
while (
cursor + 1 < len(playback)
and playback[cursor + 1]["simulation_time_ns"] / 1e9 <= step / 60
):
cursor += 1
v, w = playback[cursor]["applied_speed_mps"], playback[cursor]["applied_yaw_rate_rps"]
# End with two seconds of the ordinary zero-command hold on this mesh.
holding = step / 60 >= args.seconds - 2
drive.command(0 if stopped or holding else v, 0 if stopped or holding else w)
drive.step(1 / 60)
if step % 10 == 0:
trace.append(
dict(
time_s=step / 60,
pose=pose,
tilt_degrees=tilt,
stopped=stopped,
holding=holding,
side_actual_mps=drive.side_speeds(),
held_sides=drive.metrics.get("held_sides"),
drivetrain={key: drive.metrics.get(key) for key in (
"normal_load_n", "motor_force_n", "native_normal_force_n",
"reported_normal_force_n", "force_n", "torque_nm", "native_contact_count")},
loaded_native_contacts=[c for c in drive.metrics.get("native_contacts", [])
if c["load_n"] > 1],
)
)
SimulationManager.step(steps=1)
report = dict(
schema_version="missioncore.scene-contact-qualification/v1",
utc=datetime.now(UTC).isoformat(),
monotonic=time.monotonic(),
elapsed_wall_s=time.monotonic() - started,
run_sha256=hashlib.sha256(args.run.read_bytes()).hexdigest(),
mode=args.mode,
contact_model=PROFILE.get("contact_model", "wheels"),
source_sha256={
str(path.relative_to(Path(__file__).resolve().parents[2])): hashlib.sha256(
path.read_bytes()
).hexdigest()
for path in [
Path(__file__),
*[
Path(__file__).parent / name
for name in (
"rover_profile.py",
"tracked_rover.py",
"track_dynamics.py",
"terrain.py",
"robots/dcd006-v020.json",
)
],
]
},
robot=ROBOT_DOCUMENT,
wheel_collision=PROFILE["wheel_collision"],
seconds=args.seconds,
profile=PROFILE,
terrain=terrain,
trace=trace,
displacement_m=math.dist(trace[0]["pose"][:2], trace[-1]["pose"][:2]),
max_tilt_degrees=max(x["tilt_degrees"] for x in trace),
stopped=stopped,
)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(report, indent=2), encoding="utf-8")
print(json.dumps({k: v for k, v in report.items() if k not in ("trace", "terrain", "profile")}))
finally:
app.close()