From ded0a8879125a0fd1e44e229aa3c7a4bddddf0d7 Mon Sep 17 00:00:00 2001 From: firestar5683 <168790843+firestar5683@users.noreply.github.com> Date: Sun, 29 Nov 2026 15:40:08 -0600 Subject: [PATCH] Use reported model confidence on the C4 rail --- .../selfdrive/ui/mici/onroad/__init__.py | 12 ++++ .../ui/mici/onroad/confidence_ball.py | 26 ++------ .../starpilot/ui/onroad_compact_widgets.py | 65 ++++--------------- openpilot/starpilot/ui/onroad_state.py | 2 + openpilot/starpilot/ui/runtime_snapshot.py | 2 + .../starpilot/ui/tests/test_onroad_parity.py | 44 +++++++++++++ .../starpilot/ui/tests/test_wheel_feedback.py | 14 ---- 7 files changed, 77 insertions(+), 88 deletions(-) diff --git a/openpilot/selfdrive/ui/mici/onroad/__init__.py b/openpilot/selfdrive/ui/mici/onroad/__init__.py index 0af3b4676e..55e6a9c6a9 100644 --- a/openpilot/selfdrive/ui/mici/onroad/__init__.py +++ b/openpilot/selfdrive/ui/mici/onroad/__init__.py @@ -3,6 +3,18 @@ from openpilot.starpilot.ui.raylib import rl SIDE_PANEL_WIDTH = 60 +def confidence_colors(value: float, *, engaged: bool, overriding: bool) -> tuple[rl.Color, rl.Color]: + if overriding: + return rl.WHITE, rl.Color(82, 82, 82, 255) + if not engaged: + return rl.Color(50, 50, 50, 255), rl.Color(13, 13, 13, 255) + if value > 0.5: + return rl.Color(0, 255, 204, 255), rl.Color(0, 255, 38, 255) + if value > 0.2: + return rl.Color(255, 200, 0, 255), rl.Color(255, 115, 0, 255) + return rl.Color(255, 0, 21, 255), rl.Color(255, 0, 89, 255) + + def blend_colors(a: rl.Color, b: rl.Color, f: float) -> rl.Color: h0, s0, v0 = (hsv0 := rl.color_to_hsv(a)).x, hsv0.y, hsv0.z h1, s1, v1 = (hsv1 := rl.color_to_hsv(b)).x, hsv1.y, hsv1.z diff --git a/openpilot/selfdrive/ui/mici/onroad/confidence_ball.py b/openpilot/selfdrive/ui/mici/onroad/confidence_ball.py index f6ed031c27..15a8324e14 100644 --- a/openpilot/selfdrive/ui/mici/onroad/confidence_ball.py +++ b/openpilot/selfdrive/ui/mici/onroad/confidence_ball.py @@ -1,6 +1,6 @@ import math from openpilot.starpilot.ui.raylib import rl -from openpilot.selfdrive.ui.mici.onroad import SIDE_PANEL_WIDTH +from openpilot.selfdrive.ui.mici.onroad import SIDE_PANEL_WIDTH, confidence_colors from openpilot.selfdrive.ui.ui_state import ui_state, UIStatus from openpilot.system.ui.widgets import Widget from openpilot.system.ui.lib.application import gui_app @@ -64,26 +64,10 @@ class ConfidenceBall(Widget): dot_height = (1 - self._confidence_filter.x) * (content_rect.height - 2 * status_dot_radius) + status_dot_radius dot_height = self._rect.y + dot_height - # confidence zones - if ui_state.status == UIStatus.ENGAGED or self._demo or \ - (ui_state.status == UIStatus.DISENGAGED and self._render_lateral_active): - if self._confidence_filter.x > 0.5: - top_dot_color = rl.Color(0, 255, 204, 255) - bottom_dot_color = rl.Color(0, 255, 38, 255) - elif self._confidence_filter.x > 0.2: - top_dot_color = rl.Color(255, 200, 0, 255) - bottom_dot_color = rl.Color(255, 115, 0, 255) - else: - top_dot_color = rl.Color(255, 0, 21, 255) - bottom_dot_color = rl.Color(255, 0, 89, 255) - - elif ui_state.status == UIStatus.OVERRIDE: - top_dot_color = rl.Color(255, 255, 255, 255) - bottom_dot_color = rl.Color(82, 82, 82, 255) - - else: - top_dot_color = rl.Color(50, 50, 50, 255) - bottom_dot_color = rl.Color(13, 13, 13, 255) + engaged = ui_state.status == UIStatus.ENGAGED or self._demo or ( + ui_state.status == UIStatus.DISENGAGED and self._render_lateral_active) + top_dot_color, bottom_dot_color = confidence_colors( + self._confidence_filter.x, engaged=engaged, overriding=not engaged and ui_state.status == UIStatus.OVERRIDE) draw_circle_gradient(content_rect.x + content_rect.width - status_dot_radius, dot_height, status_dot_radius, diff --git a/openpilot/starpilot/ui/onroad_compact_widgets.py b/openpilot/starpilot/ui/onroad_compact_widgets.py index b2fafa644f..bb3e9ead83 100644 --- a/openpilot/starpilot/ui/onroad_compact_widgets.py +++ b/openpilot/starpilot/ui/onroad_compact_widgets.py @@ -13,7 +13,7 @@ from openpilot.starpilot.ui.raylib import rl from opendbc.car.structs import car from openpilot.common.filter_simple import FirstOrderFilter -from openpilot.selfdrive.ui.mici.onroad import blend_colors +from openpilot.selfdrive.ui.mici.onroad import confidence_colors from openpilot.starpilot.ui.onroad_customization import offset as widget_offset, placement, rgba, widget_size from openpilot.starpilot.ui.onroad_state import AlertSize, ObservationKind, OnroadState, is_lane_change_notice, manual_slc_confirmation from openpilot.starpilot.ui.onroad_curve import controlling as curve_controlling @@ -103,38 +103,6 @@ def compact_wheel_warning(state: OnroadState) -> bool: return alert.user_prompt or alert.visual_alert == car.CarControl.HUDControl.VisualAlert.steerRequired -class CompactStatusFade: - def __init__(self, time_constant: float): - self._time_constant = time_constant - self.reset() - - def reset(self) -> None: - self._last_ns = self._override_ns = 0 - self._drive_frame: int | None = None - self._amount = 0.0 - - def update(self, state: OnroadState, active: bool) -> float: - now = state.observed_ns - if not active: - self.reset() - return 0.0 - if not now: - return float(not state.driver_override_active) - if state.drive_frame != self._drive_frame or now < self._last_ns: - self.reset() - self._drive_frame = state.drive_frame - if state.driver_override_active: - self._override_ns = now - enabled = not self._override_ns or now - self._override_ns >= 300_000_000 - if not enabled or not self._last_ns: - self._amount = float(enabled) - else: - fade_start = max(self._last_ns, self._override_ns + 300_000_000) if self._override_ns else self._last_ns - self._amount += (1.0 - self._amount) * (1.0 - math.exp(-(now - fade_start) / 1e9 / self._time_constant)) - self._last_ns = now - return self._amount - - class CompactHudRenderer: """Transient compact MAX, detached speed-limit sign and native steering wheel.""" @@ -281,8 +249,8 @@ class MiciSidebarWidgets: def __init__(self, fonts: BitmapFonts | None = None): self.fonts = fonts self.personality = PersonalityWidget(fonts) - self._confidence_filter = FirstOrderFilter(-0.5, 0.5, 1 / 60) - self._confidence_tint = CompactStatusFade(0.1) + from openpilot.system.ui.lib.application import gui_app + self._confidence_filter = FirstOrderFilter(-0.5, 0.5, 1 / gui_app.target_fps) self._confidence_drive_frame: int | None = None self._confidence_stamp_ns: int | None = None @@ -356,28 +324,19 @@ class MiciSidebarWidgets: def _confidence_ball(self, rect: rl.Rectangle, state: OnroadState) -> None: radius = 18 center_x, center_y = rect.x + rect.width / 2, rect.y + rect.height / 2 - active = (state.lateral_active or state.longitudinal_active) and (state.alert.size == AlertSize.NONE or is_lane_change_notice(state.alert)) stamp = state.stock_confidence_source_stamp_ns if state.stock_confidence_source_fresh else None - if (stamp is None or state.model_confidence is None or state.stock_confidence_drive_frame != self._confidence_drive_frame or - self._confidence_stamp_ns is not None and (stamp < self._confidence_stamp_ns or stamp - self._confidence_stamp_ns > 200_000_000)): + value = state.model_confidence if stamp is not None else None + if (value is None or state.stock_confidence_drive_frame != self._confidence_drive_frame or + self._confidence_stamp_ns is not None and stamp is not None and + (stamp < self._confidence_stamp_ns or stamp - self._confidence_stamp_ns > 200_000_000)): self._confidence_filter.x = -0.5 - self._confidence_tint.reset() self._confidence_drive_frame = state.stock_confidence_drive_frame self._confidence_stamp_ns = stamp - visible = active and stamp is not None and state.model_confidence is not None - tint = self._confidence_tint.update(state, visible) - if visible and state.model_confidence is not None: - confidence = self._confidence_filter.update(state.model_confidence) - if confidence > 0.5: - top, bottom = rl.Color(0, 255, 204, 255), rl.Color(0, 255, 38, 255) - elif confidence > 0.2: - top, bottom = rl.Color(255, 200, 0, 255), rl.Color(255, 115, 0, 255) - else: - top, bottom = rl.Color(255, 0, 21, 255), rl.Color(255, 0, 89, 255) - top = blend_colors(rl.WHITE, top, tint) - bottom = blend_colors(rl.Color(82, 82, 82, 255), bottom, tint) - else: - top, bottom = rl.Color(50, 50, 50, 255), rl.Color(13, 13, 13, 255) + engaged = state.model_confidence_engaged or state.lateral_active + overriding = state.model_confidence_overriding + target = value if value is not None and (engaged or overriding) else -0.5 + confidence = self._confidence_filter.update(target) + top, bottom = confidence_colors(confidence, engaged=value is not None and engaged, overriding=value is not None and overriding) rl.draw_rectangle_gradient_v(int(center_x - radius), int(center_y - radius), 2 * radius, 2 * radius, top, bottom) outer = math.ceil(radius * math.sqrt(2)) + 1 rl.draw_ring(rl.Vector2(int(center_x), int(center_y)), radius, outer, 0, 360, 20, rl.BLACK) diff --git a/openpilot/starpilot/ui/onroad_state.py b/openpilot/starpilot/ui/onroad_state.py index 9e7ae3b434..03ab390800 100644 --- a/openpilot/starpilot/ui/onroad_state.py +++ b/openpilot/starpilot/ui/onroad_state.py @@ -220,6 +220,8 @@ class OnroadState: stock_confidence_source_stamp_ns: int | None = None stock_confidence_drive_frame: int | None = None model_confidence: float | None = None + model_confidence_engaged: bool = False + model_confidence_overriding: bool = False lead_indicator_source_fresh: bool = False force_stop: bool = False # Force Stop is braking for, or holding at, a stop customization: dict = field(default_factory=default_document) diff --git a/openpilot/starpilot/ui/runtime_snapshot.py b/openpilot/starpilot/ui/runtime_snapshot.py index 4b5fd627a5..efd9323b8d 100644 --- a/openpilot/starpilot/ui/runtime_snapshot.py +++ b/openpilot/starpilot/ui/runtime_snapshot.py @@ -892,6 +892,8 @@ class RuntimeSnapshotAdapter: stock_confidence_source_stamp_ns=int(sm.logMonoTime["modelV2"]) if display_model is not None else None, stock_confidence_drive_frame=after if started else None, model_confidence=_model_confidence(display_model), + model_confidence_engaged=bool(getattr(display_selfdrive, 'enabled', False)), + model_confidence_overriding=str(getattr(display_selfdrive, 'state', '')) in ('preEnabled', 'overriding'), lead_indicator_source_fresh=display_model is not None and display_radar is not None, force_stop=bool(display_force_stop is not None and (display_force_stop.forceStopBraking or display_force_stop.forceStopHolding))) diff --git a/openpilot/starpilot/ui/tests/test_onroad_parity.py b/openpilot/starpilot/ui/tests/test_onroad_parity.py index a9c3a58c4e..f0d87185b9 100644 --- a/openpilot/starpilot/ui/tests/test_onroad_parity.py +++ b/openpilot/starpilot/ui/tests/test_onroad_parity.py @@ -52,6 +52,50 @@ def test_confidence_uses_model_probabilities_and_marks_unknown_as_unknown(): assert gradient.call_args.args[4].r == 50 +def test_stationary_confidence_ball_tracks_native_color_with_no_movement(): + from openpilot.selfdrive.ui.mici.onroad import confidence_ball + from openpilot.system.ui.lib.application import gui_app + + with patch.object(gui_app, '_target_fps', 20): + native = confidence_ball.ConfidenceBall() + widget = MiciSidebarWidgets() + rect = rl.Rectangle(476, 0, 60, 80) + native._rect = rect + model = NS(meta=NS(disengagePredictions=NS(brakeDisengageProbs=[0.05], steerOverrideProbs=[0.05]))) + native_ui = NS(status=UIStatus.ENGAGED, sm={'modelV2': model}) + colors = set() + stamp = 1_000_000_000 + with patch.object(confidence_ball, 'ui_state', native_ui), patch.object(confidence_ball, 'draw_circle_gradient') as moving, \ + patch.object(rl, 'draw_rectangle_gradient_v') as stationary, patch.object(rl, 'draw_ring'): + for status, lateral, brake, steer, count in ( + (UIStatus.ENGAGED, True, 0.05, 0.05, 30), + (UIStatus.OVERRIDE, True, 0.05, 0.05, 10), + (UIStatus.DISENGAGED, False, 0.05, 0.05, 30), + (UIStatus.DISENGAGED, True, 0.6, 0.2, 35), + (UIStatus.ENGAGED, True, 0.8, 0.8, 35), + (UIStatus.ENGAGED, True, 0.05, 0.05, 30)): + native_ui.status = status + native._render_lateral_active = lateral + model.meta.disengagePredictions.brakeDisengageProbs = [brake] + model.meta.disengagePredictions.steerOverrideProbs = [steer] + for _ in range(count): + native._update_state() + native._render(None) + widget._confidence_ball(rect, road(lateral_active=lateral, model_confidence=_model_confidence(model), + stock_confidence_source_fresh=True, stock_confidence_source_stamp_ns=stamp, + stock_confidence_drive_frame=1, model_confidence_engaged=status == UIStatus.ENGAGED, + model_confidence_overriding=status == UIStatus.OVERRIDE)) + assert widget._confidence_filter.x == native._confidence_filter.x + assert stationary.call_args.args[:4] == (488, 22, 36, 36) + color = stationary.call_args.args[4] + rgb = (color.r, color.g, color.b) + stock_color = moving.call_args.args[-2] + assert rgb == (stock_color.r, stock_color.g, stock_color.b) + colors.add(rgb) + stamp += 50_000_000 + assert colors == {(0, 255, 204), (255, 200, 0), (255, 0, 21), (255, 255, 255), (50, 50, 50)} + + def test_aol_model_geometry_uses_fresh_lateral_display_context(monkeypatch): renderer = native_model.ModelRenderer.__new__(native_model.ModelRenderer) renderer._init_markers() diff --git a/openpilot/starpilot/ui/tests/test_wheel_feedback.py b/openpilot/starpilot/ui/tests/test_wheel_feedback.py index 4d3baf0431..41cb916788 100644 --- a/openpilot/starpilot/ui/tests/test_wheel_feedback.py +++ b/openpilot/starpilot/ui/tests/test_wheel_feedback.py @@ -413,17 +413,3 @@ def test_compact_steer_warning_tints_the_engaged_wheel_without_covering_full_ale hud.render_steering_wheel(replace(state, alert=replace(alert, size=AlertSize.FULL))) hud.render_steering_wheel(replace(state, lateral_active=False)) draw.assert_not_called() - - -def test_compact_status_tint_holds_through_one_frame_override_gap(): - from openpilot.starpilot.ui.onroad_compact_widgets import CompactStatusFade - - status = CompactStatusFade(.1) - drive = OnroadState(True, True, 10, None, SpeedLimitObservation(), observed_ns=NOW, drive_frame=1) - assert status.update(drive, True) == 1.0 - override = replace(drive, observed_ns=NOW + 10_000_000, driver_override_active=True) - assert status.update(override, True) == 0.0 - assert status.update(replace(drive, observed_ns=NOW + 20_000_000), True) == 0.0 - after = replace(drive, observed_ns=NOW + 320_000_000) - assert 0 < status.update(after, True) < 1 - assert status.update(after, False) == 0.0