Files
NODEDC_MISSION_CORE/simulation/ai-polygon/scheduling_policy.py
T

96 lines
4.4 KiB
Python

"""Explicit bounded catch-up policy; no clocks, native settings or I/O are changed.
PhysX consumes variable wall-timeline demand using its unchanged physics dt.
The native minFrameRate limits catch-up work between main-loop updates.
Demand beyond that clamp is discarded; this is slower simulated progression,
not evidence of real-time performance. Only completed physics steps count as
physical simulation time. Acquisition timestamps and freshness stay on wall time.
"""
import math
SCHEMA = "missioncore.simulation-scheduling/v1"
def _positive(value, name):
if type(value) not in (int, float) or not math.isfinite(value) or value <= 0:
raise ValueError("Invalid scheduling " + name)
return value
def build_scheduling_policy(*, continuous, physics_dt, capture_hz, main_loop_hz,
catch_up_mode="render-period-catchup"):
"""Derive a native integer clamp from the declared main-loop cadence.
For fractional main-loop rates, round only the native setting upward;
the requested loop and sensor cadences are unchanged. The ceil step bound is a
conservative runtime guard, not a promise that every update executes that
many steps. 60Hz physics/30Hz main loop gives an exact two-step cap.
Explicit legacy-wall-catchup permits a same-build continuous A/B comparison;
noncontinuous execution always retains the legacy one-Hz clamp.
Existing execution-input validation owns the supported capture-rate range.
"""
if type(continuous) is not bool:
raise ValueError("Invalid scheduling continuous mode")
if catch_up_mode not in ("render-period-catchup", "legacy-wall-catchup"):
raise ValueError("Invalid scheduling catch-up mode")
physics_dt = _positive(physics_dt, "physics dt")
capture_hz = _positive(capture_hz, "capture rate")
main_loop_hz = _positive(main_loop_hz, "main-loop rate")
physics_hz = _positive(1 / physics_dt, "physics rate")
effective_mode = catch_up_mode if continuous else "legacy-wall-catchup"
render_period = effective_mode == "render-period-catchup"
minimum = math.ceil(main_loop_hz) if render_period else 1
maximum_steps = math.ceil(physics_hz / minimum)
return dict(
schema_version=SCHEMA,
mode="variable-wall-timeline-with-bounded-physics-catch-up",
continuous=continuous,
catch_up_mode=effective_mode,
requested_catch_up_mode=catch_up_mode,
use_fixed_time_stepping=False,
play_every_frame=False,
physics_dt_seconds=physics_dt,
physics_hz=physics_hz,
capture_hz=capture_hz,
main_loop_hz=main_loop_hz,
min_simulation_frame_rate_hz=minimum,
max_physics_steps_per_update=maximum_steps,
max_requested_physics_interval_seconds=1 / minimum,
max_integrated_seconds_per_update=maximum_steps * physics_dt,
derivation=("ceil-main-loop-rate" if render_period else "legacy-one-hz-clamp"),
native_rate_rounding=("up-to-integer" if render_period else "none"),
step_bound_rounding="ceil-conservative-verify-native-counts",
overload_policy="discard-excess-wall-time-demand-and-report-lower-rtf",
simulation_clock="sum-completed-physics-dt",
freshness_clock="monotonic-wall-time-with-original-capture-timestamps",
step_obligations="retain-every-executed-pre-post-and-native-audit",
)
class CaptureCadence:
"""Admit only the current rendered frame, retaining the requested phase.
Render jitter may skip deadlines but must not round the capture period up
to an integer number of render frames. Missed slots are discarded; neither
acquisition timestamps nor physics clocks are synthesized or advanced.
"""
def __init__(self, hz, started_at):
self.period = 1 / _positive(hz, "capture rate")
self.next_at = float(started_at) + self.period
if not math.isfinite(self.next_at):
raise ValueError("Invalid capture start")
def due(self, captured_at):
if not math.isfinite(captured_at):
raise ValueError("Invalid capture timestamp")
if captured_at < self.next_at:
return False
missed = math.floor((captured_at - self.next_at) / self.period)
self.next_at += (missed + 1) * self.period
# Retain a strict future deadline even at floating-point boundaries.
if self.next_at <= captured_at:
self.next_at += self.period
return True