Files

100 lines
4.8 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="bounded-wall-catchup"):
"""Allow one acquisition period of scheduling jitter before dropping time.
A clamp of exactly one sensor period assumes the entire synchronous loop
fits that period. RGB/range acquisition also consumes wall time; a loop just
over 100ms was clamped to six 60Hz steps on every update and could never
catch up. Two acquisition periods provide bounded headroom while keeping
every native physics step and its fresh-command gate. This does not queue
sensor frames or change physics dt, the sensor cadence, or wall leases.
The earlier clamps remain explicit qualification modes. Noncontinuous
execution retains its original one-Hz clamp.
"""
if type(continuous) is not bool:
raise ValueError("Invalid scheduling continuous mode")
if catch_up_mode not in ("bounded-wall-catchup", "sensor-period-catchup", "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"
sensor_period = effective_mode == "sensor-period-catchup"
bounded_wall = effective_mode == "bounded-wall-catchup"
minimum = (max(1, math.ceil(capture_hz / 2)) if bounded_wall else
math.ceil(capture_hz) if sensor_period else 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=("two-acquisition-periods" if bounded_wall else "ceil-sensor-rate" if sensor_period else "ceil-main-loop-rate" if render_period else "legacy-one-hz-clamp"),
native_rate_rounding=("up-to-integer" if bounded_wall or sensor_period or 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