From 61fda7b9cc0460fbf1eeee110aa4c81dffe24f2a Mon Sep 17 00:00:00 2001 From: whoisdomi Date: Fri, 13 Nov 2026 13:39:30 -0600 Subject: [PATCH] CSC rebase and UI fixes --- .../starpilot/conditional_mode/projection.py | 14 +++++++++++++- .../conditional_mode/tests/test_projection.py | 11 +++++++++++ openpilot/starpilot/lead_tracking.py | 2 +- .../starpilot/tests/test_lead_tracking.py | 11 +++++++++++ openpilot/starpilot/ui/onroad_cem_gauge.py | 9 +++++---- openpilot/starpilot/ui/onroad_conditional.py | 12 ++++++++++++ openpilot/starpilot/ui/onroad_curve.py | 8 ++++---- .../ui/tests/test_onroad_cem_gauge.py | 18 +++++++++++++----- 8 files changed, 70 insertions(+), 15 deletions(-) diff --git a/openpilot/starpilot/conditional_mode/projection.py b/openpilot/starpilot/conditional_mode/projection.py index b884c11229..97dff2cad6 100644 --- a/openpilot/starpilot/conditional_mode/projection.py +++ b/openpilot/starpilot/conditional_mode/projection.py @@ -220,11 +220,23 @@ def _horizon(model) -> float | None: finite = tuple(value for value in parsed if value is not None) if any(b < a for a, b in zip(finite, finite[1:], strict=False)): return None - return finite[-1] + return min(finite[-1] + _bend_excess(model, finite), 500.0) except (AttributeError, TypeError, ValueError, OverflowError): return None +def _bend_excess(model, distances: tuple[float, ...]) -> float: + """Arc length minus forward reach; a bend alone must not read as a shortening plan.""" + try: + offsets = tuple(model.position.y) + except (AttributeError, TypeError, ValueError, OverflowError): + return 0.0 + if len(offsets) != len(distances) or any(_number(value, low=-500.0, high=500.0) is None for value in offsets): + return 0.0 + return sum(math.hypot(x1 - x0, y1 - y0) - (x1 - x0) + for x0, x1, y0, y1 in zip(distances, distances[1:], offsets, offsets[1:], strict=False)) + + def _standstill_path_extent(model) -> float | None: """Uncommitted standstill hold only; too lenient for stop acquisition or release.""" try: diff --git a/openpilot/starpilot/conditional_mode/tests/test_projection.py b/openpilot/starpilot/conditional_mode/tests/test_projection.py index 7aeba16daa..1bc14bc5af 100644 --- a/openpilot/starpilot/conditional_mode/tests/test_projection.py +++ b/openpilot/starpilot/conditional_mode/tests/test_projection.py @@ -533,6 +533,17 @@ class TestSceneProjector(unittest.TestCase): self.assertIsNone(repeated.scene.lead) self.assertEqual(projector.lead_detector.filter_value, previous_filter) + def test_bend_does_not_shorten_the_stop_horizon(self): + from openpilot.starpilot.conditional_mode.projection import _horizon + model = messaging.new_message('modelV2').modelV2 + angles = [1.25 * index / 32 for index in range(33)] + model.position.x = [200. * math.sin(angle) for angle in angles] + model.position.y = [200. * (1. - math.cos(angle)) for angle in angles] + self.assertLess(model.position.x[-1], 190.) + self.assertAlmostEqual(_horizon(model), 250., delta=.5) + model.position.y = [0.] * 33 + self.assertEqual(_horizon(model), model.position.x[-1]) + def test_stationary_clear_endpoint_does_not_relax_spatial_path_validation(self): from openpilot.starpilot.conditional_mode.projection import _horizon, _standstill_clear_horizon from openpilot.starpilot.conditional_mode.tests.test_stop_commit_transport import CLEAR_97_X diff --git a/openpilot/starpilot/lead_tracking.py b/openpilot/starpilot/lead_tracking.py index 9197c5d019..a805736658 100644 --- a/openpilot/starpilot/lead_tracking.py +++ b/openpilot/starpilot/lead_tracking.py @@ -64,7 +64,7 @@ def _path_at_distance(model: object, distance_m: float) -> tuple[float, float] | if len(xs) != 33 or len(ys) != 33: return None distances = tuple(_number(x, 0.0, 500.0) for x in xs) - offsets = tuple(_number(y, -50.0, 50.0) for y in ys) + offsets = tuple(_number(y, -500.0, 500.0) for y in ys) if any(value is None for value in distances) or any(value is None for value in offsets): return None clean_xs = tuple(value for value in distances if value is not None) diff --git a/openpilot/starpilot/tests/test_lead_tracking.py b/openpilot/starpilot/tests/test_lead_tracking.py index 69bafb5ea1..27cce29507 100644 --- a/openpilot/starpilot/tests/test_lead_tracking.py +++ b/openpilot/starpilot/tests/test_lead_tracking.py @@ -214,6 +214,17 @@ class TestLeadDetector(unittest.TestCase): else: self.assertIsNone(observation.tracked) + def test_curved_path_keeps_lead_state_known(self): + event = messaging.new_message('modelV2', valid=True) + xs = [245. * index / 32 for index in range(33)] + event.modelV2.position.x = xs + event.modelV2.position.y = [x * x / 460. for x in xs] + curved = messaging.log_from_bytes(event.to_bytes()).modelV2 + detector = LeadDetector() + clear = [step(detector, tick, lead=wire_lead(present=False, distance=0, speed=0), model=curved, speed=24.6) + for tick in range(3)] + self.assertTrue(all(observation.tracked is False and observation.following is False for observation in clear)) + def test_serialized_model_origin_roundoff_only(self): detector = LeadDetector() roundoff = step(detector, 0, model=wire_model(origin=-6.64888977208733e-11)) diff --git a/openpilot/starpilot/ui/onroad_cem_gauge.py b/openpilot/starpilot/ui/onroad_cem_gauge.py index aed1f84804..0665d0a6d0 100644 --- a/openpilot/starpilot/ui/onroad_cem_gauge.py +++ b/openpilot/starpilot/ui/onroad_cem_gauge.py @@ -11,8 +11,8 @@ from openpilot.starpilot.ui.raylib import rl from openpilot.common.filter_simple import FirstOrderFilter from openpilot.starpilot.ui.appearance_preferences import CameraViewChoice -from openpilot.starpilot.ui.onroad_conditional import reason, status -from openpilot.starpilot.ui.onroad_curve import controlling, glow_color, intensity +from openpilot.starpilot.ui.onroad_conditional import curve_slowing, reason, status +from openpilot.starpilot.ui.onroad_curve import controlling, glow_color, glowing, intensity from openpilot.starpilot.ui.onroad_customization import CEM_GAUGE_FRAME, CEM_GAUGE_INSET, placement from openpilot.starpilot.ui.onroad_state import AlertSize, OnroadState from openpilot.starpilot.ui.presentation import BitmapFonts, FontRole @@ -142,9 +142,10 @@ def gauge_data(state: OnroadState, *, lead_stopped: bool = False) -> AetherGauge return AetherGaugeData('STOP', color=COLOR_FORCE_STOP, indicator_type=IndicatorType.STOP_LIGHT, indicator_extra='red') curve = state.curve - if controlling(state) or current == 'cem_curve': + curve_glow = glowing(state) + if curve_glow or curve_slowing(state): speed = state.speed_mps - target = curve.ceiling_mps if controlling(state) and curve is not None else None + target = curve.ceiling_mps if curve_glow and controlling(state) and curve is not None else None shown = min(speed, target) if speed is not None and target is not None else speed curvature = curve.road_curvature if curve is not None else None preview = state.visual_preview diff --git a/openpilot/starpilot/ui/onroad_conditional.py b/openpilot/starpilot/ui/onroad_conditional.py index caa9404bdb..302acc1660 100644 --- a/openpilot/starpilot/ui/onroad_conditional.py +++ b/openpilot/starpilot/ui/onroad_conditional.py @@ -14,6 +14,7 @@ _CCM_HOLD_REASONS = {4: "ccm_lead", 6: "ccm_speed"} _REASONS = {reason.value: (reason.name[4:] if reason.name.startswith(('CEM_', 'CCM_')) else reason.name).replace('_', ' ') for reason in Reason} +CURVE_SLOWING_DELTA_MPS = 1.0 _HIDDEN_REASONS = frozenset((Reason.SCENE_UNAVAILABLE.value, Reason.UNAVAILABLE.value)) @@ -32,6 +33,17 @@ def reason(state: OnroadState) -> str | None: return value.reason +def curve_slowing(state: OnroadState) -> bool: + """CEM Curves holding the car under the driver's cruise speed.""" + if reason(state) != 'cem_curve': + return False + preview = state.visual_preview + if preview is not None and preview.cem_reason == 'CURVE': + return state.alert.size != AlertSize.FULL + return bool(state.alert.size == AlertSize.NONE and not state.longitudinal_overridden and state.longitudinal_active and state.speed_mps is not None and state.cruise_kph is not None and + state.speed_mps < state.cruise_kph / 3.6 - CURVE_SLOWING_DELTA_MPS) + + def stop_active(state: OnroadState) -> bool: """One accepted stop state for the light icon and the conditional orange border.""" if state.alert.size == AlertSize.FULL or state.longitudinal_overridden: diff --git a/openpilot/starpilot/ui/onroad_curve.py b/openpilot/starpilot/ui/onroad_curve.py index 52ea82e952..cfdeea2802 100644 --- a/openpilot/starpilot/ui/onroad_curve.py +++ b/openpilot/starpilot/ui/onroad_curve.py @@ -8,6 +8,7 @@ import math from openpilot.starpilot.ui.raylib import rl +from openpilot.starpilot.ui.onroad_conditional import curve_slowing from openpilot.starpilot.ui.onroad_state import AlertSize, OnroadState @@ -114,12 +115,11 @@ def render_glow(rect: rl.Rectangle, state: OnroadState, *, border_width: float, active = True strength = intensity(preview_curve) else: - if not glowing(state) and not (state.show_curve_status and training(state)): + active = glowing(state) or curve_slowing(state) + if not active and not (state.show_curve_status and training(state)): return curve = state.curve - assert curve is not None - active = glowing(state) - strength = intensity(curve.road_curvature) if active else BASE_INTENSITY + strength = intensity(curve.road_curvature if curve is not None else None) if active else BASE_INTENSITY color = glow_color(strength) if active else TRAINING cycle = period(strength) fraction = max(0.0, min(1.0, (strength - BASE_INTENSITY) / (1.0 - BASE_INTENSITY))) diff --git a/openpilot/starpilot/ui/tests/test_onroad_cem_gauge.py b/openpilot/starpilot/ui/tests/test_onroad_cem_gauge.py index b0374c7a49..474f84de64 100644 --- a/openpilot/starpilot/ui/tests/test_onroad_cem_gauge.py +++ b/openpilot/starpilot/ui/tests/test_onroad_cem_gauge.py @@ -78,14 +78,22 @@ def test_current_radar_metrics_use_native_units(): def test_curve_selected_source_retains_speed_units_and_csc_reduction(): curve = CurveObservation(True, 15., 15., True, True, False, 100., 2., 30., 'curve', 'selected', 'saved', True, False, .005) - sample = state('cem_curve', effective=True, curve=curve, metric=True) + sample = state('cem_curve', effective=True, curve=curve, metric=True, show_curve_status=True) data = gauge_data(sample) assert data.indicator_type is IndicatorType.ROAD_CURVE and data.indicator_value == .005 assert (data.text, data.unit, data.reduction_text) == ('54', 'km/h', '-36') assert (gauge_data(replace(sample, metric=False)).text, gauge_data(replace(sample, metric=False)).unit) == ('34', 'mph') - perception = state('cem_curve', curve=curve) - assert gauge_data(perception).text == '45' # Actual ego speed; no CSC control authority. - assert gauge_data(perception).reduction_text == '' + cem_only = replace(sample, metric=False, show_curve_status=False) + assert (gauge_data(cem_only).text, gauge_data(cem_only).reduction_text) == ('45', '') + assert gauge_data(replace(cem_only, speed_mps=24.5)) is None + assert gauge_data(state('cem_curve', curve=curve)) is None + hidden = replace(sample, show_curve_status=False) + assert (gauge_data(hidden).text, gauge_data(hidden).reduction_text) == ('72', '') + assert gauge_data(replace(state('no_trigger', effective=True, curve=curve), show_curve_status=False)) is None + assert gauge_data(replace(state('no_trigger', effective=True, curve=curve), show_curve_status=True)).reduction_text == '-22' + held = replace(state('no_trigger', effective=True, curve=replace(curve, applied=False, controlling=False)), show_curve_status=True) + assert (gauge_data(held).text, gauge_data(held).reduction_text) == ('45', '') + assert gauge_data(replace(held, curve=replace(held.curve, glow=False))) is None @pytest.mark.parametrize('changes', [{'alert': OnroadAlert(size=AlertSize.FULL, critical=True)}, @@ -267,7 +275,7 @@ def test_gauge_draws_only_for_visible_reasons(projection, kind): sample = replace(sample, conditional_perception=None) elif kind == 'curve_control': curve = CurveObservation(True, 15., 15., True, True, False, 100., 2., 30., 'curve', 'selected', 'saved', True, False, .005) - sample = replace(sample, curve=curve) + sample = replace(sample, curve=curve, show_curve_status=True) gauge = AetherGauge(Mock()) with patch.object(gauge, '_render_unified_road') as draw: gauge.render(sample)