ui: share alert logic between comma four and comma 3X (#39223)

This commit is contained in:
Shane Smiskol
2026-11-28 12:06:41 -06:00
committed by firestar5683
parent 4ccf2359b6
commit 112dd3f0bd
2 changed files with 44 additions and 76 deletions
@@ -4,12 +4,10 @@ from typing import NamedTuple
from openpilot.starpilot.ui.raylib import rl
import random
import string
from dataclasses import dataclass
from openpilot.cereal import messaging, log
from opendbc.car.structs import car
from openpilot.selfdrive.ui.ui_state import ui_state
from openpilot.common.filter_simple import BounceFilter, FirstOrderFilter
from openpilot.common.hardware import COMMA_HARDWARE
from openpilot.selfdrive.ui.onroad.alert_renderer import Alert, get_alert
from openpilot.system.ui.lib.application import gui_app, FontWeight, TextAlignment
from openpilot.system.ui.widgets import Widget
from openpilot.system.ui.widgets.label import UnifiedLabel
@@ -22,9 +20,6 @@ ALERT_MARGIN = 18
ALERT_FONT_SMALL = 66 - 50
ALERT_FONT_BIG = 88 - 40
SELFDRIVE_STATE_TIMEOUT = 5 # Seconds
SELFDRIVE_UNRESPONSIVE_TIMEOUT = 10 # Seconds
# Constants
ALERT_COLORS = {
AlertStatus.normal: rl.Color(0, 0, 0, 255),
@@ -54,16 +49,6 @@ class AlertLayout(NamedTuple):
icon: IconLayout | None
@dataclass
class Alert:
text1: str = ""
text2: str = ""
size: int = 0
status: int = 0
visual_alert: int = car.CarControl.HUDControl.VisualAlert.none
alert_type: str = ""
# Pre-defined alert instances
ALERT_STARTUP_PENDING = Alert(
text1="openpilot Unavailable",
@@ -121,38 +106,14 @@ class AlertRenderer(Widget):
def get_alert(self, sm: messaging.SubMaster) -> Alert | None:
"""Generate the current alert based on selfdrive state."""
ss = sm['selfdriveState']
recv_frame = sm.recv_frame['selfdriveState']
if self._alert_drive_frame != ui_state.started_frame:
self._alert_drive_frame = ui_state.started_frame
self._prev_alert = None
self._fade_time = None
self._alpha_filter.x = 0
# Check if selfdriveState messages have stopped arriving
if not sm.updated['selfdriveState']:
time_since_onroad = time.monotonic() - ui_state.started_time
# 1. Never received selfdriveState since going onroad
waiting_for_startup = recv_frame < ui_state.started_frame
if waiting_for_startup and time_since_onroad > 10:
return ALERT_STARTUP_PENDING
# 2. Lost communication with selfdriveState after receiving it
if COMMA_HARDWARE and not waiting_for_startup:
ss_missing = time.monotonic() - sm.recv_time['selfdriveState']
if ss_missing > SELFDRIVE_STATE_TIMEOUT:
if ss.enabled and (ss_missing - SELFDRIVE_STATE_TIMEOUT) < SELFDRIVE_UNRESPONSIVE_TIMEOUT:
return ALERT_CRITICAL_TIMEOUT
return ALERT_CRITICAL_REBOOT
# No alert if size is none
if ss.alertSize == 0 or recv_frame < ui_state.started_frame:
return None
# Return current alert
return Alert(text1=ss.alertText1, text2=ss.alertText2, size=ss.alertSize.raw, status=ss.alertStatus.raw,
visual_alert=ss.alertHudVisual, alert_type=ss.alertType)
return get_alert(sm, startup_alert=ALERT_STARTUP_PENDING, timeout_alert=ALERT_CRITICAL_TIMEOUT,
reboot_alert=ALERT_CRITICAL_REBOOT)
def will_render(self) -> tuple[Alert | None, bool]:
alert = self.get_alert(ui_state.sm)
+41 -34
View File
@@ -2,6 +2,7 @@ import time
from openpilot.starpilot.ui.raylib import rl
from dataclasses import dataclass
from openpilot.cereal import messaging, log
from opendbc.car.structs import car
from openpilot.selfdrive.ui.ui_state import ui_state
from openpilot.common.hardware import COMMA_HARDWARE
from openpilot.system.ui.lib.application import gui_app, FontWeight, TextAlignment, TextAlignmentVertical
@@ -44,6 +45,8 @@ class Alert:
text2: str = ""
size: int = 0
status: int = 0
visual_alert: int = car.CarControl.HUDControl.VisualAlert.none
alert_type: str = ""
# Pre-defined alert instances
@@ -69,6 +72,43 @@ ALERT_CRITICAL_REBOOT = Alert(
)
def get_alert(sm: messaging.SubMaster, *, startup_alert=ALERT_STARTUP_PENDING,
timeout_alert=ALERT_CRITICAL_TIMEOUT, reboot_alert=ALERT_CRITICAL_REBOOT) -> Alert | None:
"""Generate the current alert based on selfdrive state."""
ss = sm['selfdriveState']
# Check if selfdriveState messages have stopped arriving
recv_frame = sm.recv_frame['selfdriveState']
if not sm.updated['selfdriveState']:
time_since_onroad = time.monotonic() - ui_state.started_time
# 1. Never received selfdriveState since going onroad
waiting_for_startup = recv_frame < ui_state.started_frame
if waiting_for_startup and time_since_onroad > 10:
return startup_alert
# 2. Lost communication with selfdriveState after receiving it
if COMMA_HARDWARE and not waiting_for_startup:
ss_missing = time.monotonic() - sm.recv_time['selfdriveState']
if ss_missing > SELFDRIVE_STATE_TIMEOUT:
if ss.enabled and (ss_missing - SELFDRIVE_STATE_TIMEOUT) < SELFDRIVE_UNRESPONSIVE_TIMEOUT:
return timeout_alert
return reboot_alert
# No alert if size is none
if ss.alertSize == 0:
return None
# Don't get old alert
if recv_frame < ui_state.started_frame:
return None
# Return current alert
return Alert(text1=ss.alertText1, text2=ss.alertText2, size=ss.alertSize.raw, status=ss.alertStatus.raw,
visual_alert=ss.alertHudVisual, alert_type=ss.alertType)
class AlertRenderer(Widget):
def __init__(self):
super().__init__()
@@ -81,41 +121,8 @@ class AlertRenderer(Widget):
self._full_text2_label = Label("", font_size=ALERT_FONT_BIG, text_alignment=TextAlignment.CENTER,
text_alignment_vertical=TextAlignmentVertical.TOP)
def get_alert(self, sm: messaging.SubMaster) -> Alert | None:
"""Generate the current alert based on selfdrive state."""
ss = sm['selfdriveState']
# Check if selfdriveState messages have stopped arriving
recv_frame = sm.recv_frame['selfdriveState']
if not sm.updated['selfdriveState']:
time_since_onroad = time.monotonic() - ui_state.started_time
# 1. Never received selfdriveState since going onroad
waiting_for_startup = recv_frame < ui_state.started_frame
if waiting_for_startup and time_since_onroad > 10:
return ALERT_STARTUP_PENDING
# 2. Lost communication with selfdriveState after receiving it
if COMMA_HARDWARE and not waiting_for_startup:
ss_missing = time.monotonic() - sm.recv_time['selfdriveState']
if ss_missing > SELFDRIVE_STATE_TIMEOUT:
if ss.enabled and (ss_missing - SELFDRIVE_STATE_TIMEOUT) < SELFDRIVE_UNRESPONSIVE_TIMEOUT:
return ALERT_CRITICAL_TIMEOUT
return ALERT_CRITICAL_REBOOT
# No alert if size is none
if ss.alertSize == 0:
return None
# Don't get old alert
if recv_frame < ui_state.started_frame:
return None
# Return current alert
return Alert(text1=ss.alertText1, text2=ss.alertText2, size=ss.alertSize.raw, status=ss.alertStatus.raw)
def _render(self, rect: rl.Rectangle):
alert = self.get_alert(ui_state.sm)
alert = get_alert(ui_state.sm)
if not alert:
return