From fd052d15fb027e5509128b5533d2a65a0fa1ade7 Mon Sep 17 00:00:00 2001 From: firestar5683 <168790843+firestar5683@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:55:38 -0600 Subject: [PATCH] Retain fresh braking proof for confirmed stop signs --- .../starpilot/longitudinal/force_stop.py | 23 ++++- .../longitudinal/force_stop_runtime.py | 1 + .../test_ranged_sign_acquisition_history.py | 95 +++++++++++++++++++ prebuilt.json | 2 +- 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 openpilot/starpilot/longitudinal/tests/test_ranged_sign_acquisition_history.py diff --git a/openpilot/starpilot/longitudinal/force_stop.py b/openpilot/starpilot/longitudinal/force_stop.py index 3779186a69..fca71e9c29 100644 --- a/openpilot/starpilot/longitudinal/force_stop.py +++ b/openpilot/starpilot/longitudinal/force_stop.py @@ -15,6 +15,7 @@ SIGN_MAX_SPEED_MPS = 17.88 SIGN_MODEL_AGREE_M = 50.0 SIGN_SEED_M = 27.0 SIGN_ESTIMATE_HOLD_NS = 2_000_000_000 +SIGN_BRAKING_PROOF_NS = 250_000_000 # Over a long model path a sign is only believed if stopping at its estimate would be a normal stop. SIGN_LONG_PATH_MAX_DECEL = 2.5 SIGN_APPROACH_DECEL = 1.0 @@ -80,6 +81,8 @@ class StopFrame: actuator_delay_s: float | None = None vision_source_ns: int = 0 always_hold: bool = False + acceleration_ns: int = 0 + sample_ns: int = 0 @dataclass(frozen=True) @@ -119,6 +122,7 @@ class ForceStop: self.standstill_reason = None self.sign_confirmed = False self.ranged_stop = False + self.braking_samples: tuple[tuple[int, float], ...] = () self.previously_enabled = False self.distance = 0.0 self.distance_cap = 0.0 @@ -215,6 +219,23 @@ class ForceStop: model_active = available and horizon < (108.0 if self.activation_gate and frame.light else 100.0) self.activation_gate = model_active and frame.light spatial_permitted = available and now >= self.override_until_ns and not frame.driving_in_curve and not turn + # Keep source-bound delivered braking while a lead eases during sign acquisition. + braking = frame.acceleration_mps2 + braking_proof = braking if braking is not None and math.isfinite(braking) else 0.0 + if (spatial_permitted and not frame.standstill and not frame.pedal_override and not frame.resume_requested and + braking is not None and math.isfinite(braking) and braking < 0 and + type(frame.acceleration_ns) is int and type(frame.sample_ns) is int and + now <= frame.sample_ns <= now + 150_000_000 and + 0 < frame.acceleration_ns <= frame.sample_ns and frame.sample_ns - frame.acceleration_ns <= 150_000_000): + self.braking_samples = tuple(sample for sample in self.braking_samples + if 0 <= frame.sample_ns - sample[0] <= SIGN_BRAKING_PROOF_NS) + if not self.braking_samples or frame.acceleration_ns > self.braking_samples[-1][0]: + self.braking_samples = (*self.braking_samples, (frame.acceleration_ns, braking))[-6:] + elif frame.acceleration_ns < self.braking_samples[-1][0] or braking != self.braking_samples[-1][1]: + self.braking_samples = () + braking_proof = min((accel for _, accel in self.braking_samples), default=braking_proof) + else: + self.braking_samples = () permitted = spatial_permitted and not frame.tracking_lead and not lead model_path = frame.light and model_active and permitted and not curved plausible = sign_estimate is None or speed * speed <= 2 * SIGN_LONG_PATH_MAX_DECEL * max(sign_estimate, 1.0) @@ -226,7 +247,7 @@ class ForceStop: frame.acceleration_mps2 is not None and frame.acceleration_mps2 < 0 and frame.actuator_delay_s is not None and 0 <= frame.actuator_delay_s <= 2 - DT_MDL and distance_target(speed, frame.acceleration_mps2, current_range, frame.actuator_delay_s + DT_MDL) >= - max(-SIGN_LONG_PATH_MAX_DECEL, frame.acceleration_mps2)) + max(-SIGN_LONG_PATH_MAX_DECEL, braking_proof)) # Retain sufficient existing braking for a confirmed sign, never for a light alone. sign_path = (frame.sign and spatial_permitted and (permitted or ranged_sign) and speed < SIGN_MAX_SPEED_MPS and (horizon < SIGN_MODEL_AGREE_M or self.forcing and plausible)) diff --git a/openpilot/starpilot/longitudinal/force_stop_runtime.py b/openpilot/starpilot/longitudinal/force_stop_runtime.py index c47e11531e..0c4d9a9539 100644 --- a/openpilot/starpilot/longitudinal/force_stop_runtime.py +++ b/openpilot/starpilot/longitudinal/force_stop_runtime.py @@ -207,6 +207,7 @@ class ForceStopRuntime: dashboard_sign=dashboard_sign, vision_distance_m=vision_distance, always_hold=self.always_hold, acceleration_mps2=float(car.aEgo), actuator_delay_s=float(cp.longitudinalActuatorDelay), vision_source_ns=int(vision_sign) if vision_fresh else 0, + acceleration_ns=int(sm.logMonoTime['carState']), sample_ns=now_ns, moving_preview=bool(perception_available and not model.action.shouldStop and len(model.velocity.x) == len(model.position.x) and math.isfinite(model.velocity.x[-1]))) diff --git a/openpilot/starpilot/longitudinal/tests/test_ranged_sign_acquisition_history.py b/openpilot/starpilot/longitudinal/tests/test_ranged_sign_acquisition_history.py new file mode 100644 index 0000000000..c7de93586f --- /dev/null +++ b/openpilot/starpilot/longitudinal/tests/test_ranged_sign_acquisition_history.py @@ -0,0 +1,95 @@ +from dataclasses import replace +import pytest +from openpilot.starpilot.longitudinal.force_stop import ForceStop, vehicle_tune +from openpilot.starpilot.longitudinal.tests.test_ranged_sign_lead import approach + + +def easing_stop(): + owner = ForceStop() + tune = vehicle_tune('HYUNDAI_IONIQ_6') + speed, distance = 3.67, 8.3 + for tick, accel in enumerate([-1.6, -1.4, -1.2, -1.0, -.9]): + sample = approach(model_ns=100_000_000_000 + tick * 50_000_000, + vision_source_ns=99_500_000_000, speed_mps=speed, + acceleration_ns=100_000_000_000 + tick * 50_000_000, + sample_ns=100_001_000_000 + tick * 50_000_000, + vision_distance_m=distance, acceleration_mps2=accel) + plan = owner.step(sample, tune) + speed += accel * .05 + distance -= speed * .05 + return owner, plan, sample + + +def test_easing_brake_retains_valid_stop_before_lead_departs(): + owner, plan, sample = easing_stop() + assert plan.forcing and owner.sign_confirmed + assert plan.obstacle_m is not None and plan.obstacle_m < sample.vision_distance_m + sample = replace(sample, model_ns=sample.model_ns + 50_000_000, + sign=False, vision_distance_m=None, tracking_lead=False, + lead_present=False, horizon_m=80., raw_model_stopped=False) + plan = owner.step(sample, vehicle_tune('HYUNDAI_IONIQ_6')) + assert plan.forcing + sample = replace(sample, model_ns=sample.model_ns + 50_000_000, + speed_mps=0., standstill=True) + plan = owner.step(sample, vehicle_tune('HYUNDAI_IONIQ_6')) + assert plan.forcing and plan.manual_hold and plan.should_stop + + +def test_old_braking_cannot_authorize_a_new_stop(): + owner = ForceStop() + tune = vehicle_tune('HYUNDAI_IONIQ_6') + sample = approach(sign=False, vision_distance_m=None, acceleration_ns=100_000_000_000, sample_ns=100_001_000_000) + owner.step(sample, tune) + for tick in range(1, 11): + plan = owner.step(replace(sample, model_ns=sample.model_ns + tick * 50_000_000, + acceleration_ns=sample.model_ns + tick * 50_000_000, + sample_ns=sample.model_ns + tick * 50_000_000 + 1_000_000, + acceleration_mps2=-.1, sign=tick>=4, + vision_distance_m=12.3 if tick>=4 else None), tune) + assert not plan.forcing + + +@pytest.mark.parametrize('veto', ['perception', 'pedal', 'resume', 'standstill', 'curve']) +def test_braking_history_is_cleared_by_loss_or_override(veto): + owner = ForceStop() + tune = vehicle_tune('HYUNDAI_IONIQ_6') + sample = approach(sign=False, vision_distance_m=None, acceleration_ns=100_000_000_000, sample_ns=100_001_000_000) + owner.step(sample, tune) + changes = {'perception': {'perception_available': False}, + 'pedal': {'pedal_override': True}, + 'resume': {'resume_requested': True}, + 'standstill': {'standstill': True, 'speed_mps': 0.}, + 'curve': {'driving_in_curve': True}}[veto] + owner.step(replace(sample, model_ns=sample.model_ns + 50_000_000, + acceleration_ns=sample.model_ns + 50_000_000, + sample_ns=sample.model_ns + 51_000_000, **changes), tune) + assert not owner.braking_samples + + +def test_clock_discontinuity_cannot_reuse_braking(): + owner = ForceStop() + tune = vehicle_tune('HYUNDAI_IONIQ_6') + sample = approach(sign=False, vision_distance_m=None, acceleration_ns=100_000_000_000, sample_ns=100_001_000_000) + owner.step(sample, tune) + owner.step(replace(sample, model_ns=sample.model_ns + 300_000_000, + acceleration_ns=sample.model_ns + 300_000_000, + sample_ns=sample.model_ns + 301_000_000), tune) + assert not owner.braking_samples + + +@pytest.mark.parametrize('kind', ['repeated', 'future', 'changed']) +def test_bad_acceleration_source_cannot_refresh_braking_history(kind): + owner = ForceStop() + tune = vehicle_tune('HYUNDAI_IONIQ_6') + first = approach(sign=False, vision_distance_m=None, acceleration_ns=100_000_000_000, sample_ns=100_001_000_000) + owner.step(first, tune) + assert owner.braking_samples + for tick in range(1, 11): + now = first.model_ns + tick * 50_000_000 + source = now + 100_000_000 if kind == 'future' else first.acceleration_ns + sample = replace(first, model_ns=now, sample_ns=now+1_000_000, acceleration_ns=source, + acceleration_mps2=-.1, sign=True, vision_distance_m=12.3) + if kind == 'repeated': + sample = replace(sample, acceleration_mps2=first.acceleration_mps2, sign=False, vision_distance_m=None) + assert not owner.step(sample, tune).forcing + assert not owner.braking_samples diff --git a/prebuilt.json b/prebuilt.json index 385bee4cdd..9c3b618fd8 100644 --- a/prebuilt.json +++ b/prebuilt.json @@ -1 +1 @@ -{"protected_inventory_sha256": "48ce18260232b938ce0d7998312f078f06e60cc9e57aee84e375f8dd5f92712b", "schema_version": 1} +{"protected_inventory_sha256": "f089595996edcd6baa183a52a99d8bc9a20083803af88f5e3c510c08633adf55", "schema_version": 1}