clear irrelevant alerts on state transition (#2318)

* cleanup

* clear warnings

* more types

* needs refactor

* update refs

* update refs
pull/2274/head
Adeeb Shihadeh 5 years ago committed by GitHub
parent de833ecb06
commit 1c6905cac1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      selfdrive/controls/controlsd.py
  2. 37
      selfdrive/controls/lib/alertmanager.py
  3. 30
      selfdrive/controls/lib/events.py
  4. 2
      selfdrive/test/process_replay/ref_commit

@ -447,9 +447,10 @@ class Controls:
if CC.hudControl.rightLaneDepart or CC.hudControl.leftLaneDepart: if CC.hudControl.rightLaneDepart or CC.hudControl.leftLaneDepart:
self.events.add(EventName.ldw) self.events.add(EventName.ldw)
clear_event = ET.WARNING if ET.WARNING in self.current_alert_types else None
alerts = self.events.create_alerts(self.current_alert_types, [self.CP, self.sm, self.is_metric]) alerts = self.events.create_alerts(self.current_alert_types, [self.CP, self.sm, self.is_metric])
self.AM.add_many(self.sm.frame, alerts, self.enabled) self.AM.add_many(self.sm.frame, alerts, self.enabled)
self.AM.process_alerts(self.sm.frame) self.AM.process_alerts(self.sm.frame, clear_event)
CC.hudControl.visualAlert = self.AM.visual_alert CC.hudControl.visualAlert = self.AM.visual_alert
if not self.read_only: if not self.read_only:

@ -32,9 +32,6 @@ class AlertManager:
self.activealerts: List[Alert] = [] self.activealerts: List[Alert] = []
self.clear_current_alert() self.clear_current_alert()
def alert_present(self) -> bool:
return len(self.activealerts) > 0
def clear_current_alert(self) -> None: def clear_current_alert(self) -> None:
self.alert_type: str = "" self.alert_type: str = ""
self.alert_text_1: str = "" self.alert_text_1: str = ""
@ -46,34 +43,32 @@ class AlertManager:
self.alert_rate: float = 0. self.alert_rate: float = 0.
def add_many(self, frame: int, alerts: List[Alert], enabled: bool = True) -> None: def add_many(self, frame: int, alerts: List[Alert], enabled: bool = True) -> None:
for a in alerts: for alert in alerts:
self.add(frame, a, enabled=enabled) added_alert = copy.copy(alert)
added_alert.start_time = frame * DT_CTRL
def add(self, frame: int, alert: Alert, enabled: bool = True) -> None:
added_alert = copy.copy(alert)
added_alert.start_time = frame * DT_CTRL
# if new alert is higher priority, log it
if not self.alert_present() or added_alert.alert_priority > self.activealerts[0].alert_priority:
cloudlog.event('alert_add', alert_type=added_alert.alert_type, enabled=enabled)
self.activealerts.append(added_alert) # if new alert is higher priority, log it
if not len(self.activealerts) or added_alert.alert_priority > self.activealerts[0].alert_priority:
cloudlog.event('alert_add', alert_type=added_alert.alert_type, enabled=enabled)
# sort by priority first and then by start_time self.activealerts.append(added_alert)
self.activealerts.sort(key=lambda k: (k.alert_priority, k.start_time), reverse=True)
def process_alerts(self, frame: int) -> None: def process_alerts(self, frame: int, clear_event_type=None) -> None:
cur_time = frame * DT_CTRL cur_time = frame * DT_CTRL
# first get rid of all the expired alerts # first get rid of all the expired alerts
self.activealerts = [a for a in self.activealerts if a.start_time + self.activealerts = [a for a in self.activealerts if a.event_type != clear_event_type and
max(a.duration_sound, a.duration_hud_alert, a.duration_text) > cur_time] a.start_time + max(a.duration_sound, a.duration_hud_alert, a.duration_text) > cur_time]
# sort by priority first and then by start_time
self.activealerts.sort(key=lambda k: (k.alert_priority, k.start_time), reverse=True)
# start with assuming no alerts # start with assuming no alerts
self.clear_current_alert() self.clear_current_alert()
current_alert = self.activealerts[0] if self.alert_present() else None if len(self.activealerts):
if current_alert is not None: current_alert = self.activealerts[0]
self.alert_type = current_alert.alert_type self.alert_type = current_alert.alert_type
if current_alert.start_time + current_alert.duration_sound > cur_time: if current_alert.start_time + current_alert.duration_sound > cur_time:

@ -1,4 +1,4 @@
from functools import total_ordering from enum import IntEnum
from typing import Dict, Union, Callable, Any from typing import Dict, Union, Callable, Any
from cereal import log, car from cereal import log, car
@ -14,7 +14,7 @@ AudibleAlert = car.CarControl.HUDControl.AudibleAlert
EventName = car.CarEvent.EventName EventName = car.CarEvent.EventName
# Alert priorities # Alert priorities
class Priority: class Priority(IntEnum):
LOWEST = 0 LOWEST = 0
LOWER = 1 LOWER = 1
LOW = 2 LOW = 2
@ -79,6 +79,7 @@ class Events:
if DT_CTRL * (self.events_prev[e] + 1) >= alert.creation_delay: if DT_CTRL * (self.events_prev[e] + 1) >= alert.creation_delay:
alert.alert_type = f"{EVENT_NAME[e]}/{et}" alert.alert_type = f"{EVENT_NAME[e]}/{et}"
alert.event_type = et
ret.append(alert) ret.append(alert)
return ret return ret
@ -96,23 +97,21 @@ class Events:
ret.append(event) ret.append(event)
return ret return ret
@total_ordering
class Alert: class Alert:
def __init__(self, def __init__(self,
alert_text_1: str, alert_text_1: str,
alert_text_2: str, alert_text_2: str,
alert_status, alert_status: log.ControlsState.AlertStatus,
alert_size, alert_size: log.ControlsState.AlertSize,
alert_priority, alert_priority: Priority,
visual_alert, visual_alert: car.CarControl.HUDControl.VisualAlert,
audible_alert, audible_alert: car.CarControl.HUDControl.AudibleAlert,
duration_sound: float, duration_sound: float,
duration_hud_alert: float, duration_hud_alert: float,
duration_text: float, duration_text: float,
alert_rate: float = 0., alert_rate: float = 0.,
creation_delay: float = 0.): creation_delay: float = 0.):
self.alert_type = ""
self.alert_text_1 = alert_text_1 self.alert_text_1 = alert_text_1
self.alert_text_2 = alert_text_2 self.alert_text_2 = alert_text_2
self.alert_status = alert_status self.alert_status = alert_status
@ -125,24 +124,19 @@ class Alert:
self.duration_hud_alert = duration_hud_alert self.duration_hud_alert = duration_hud_alert
self.duration_text = duration_text self.duration_text = duration_text
self.start_time = 0.
self.alert_rate = alert_rate self.alert_rate = alert_rate
self.creation_delay = creation_delay self.creation_delay = creation_delay
# typecheck that enums are valid on startup self.start_time = 0.
tst = car.CarControl.new_message() self.alert_type = ""
tst.hudControl.visualAlert = self.visual_alert self.event_type = None
def __str__(self) -> str: def __str__(self) -> str:
return self.alert_text_1 + "/" + self.alert_text_2 + " " + str(self.alert_priority) + " " + str( return f"{self.alert_text_1}/{self.alert_text_2} {self.alert_priority} {self.visual_alert} {self.audible_alert}"
self.visual_alert) + " " + str(self.audible_alert)
def __gt__(self, alert2) -> bool: def __gt__(self, alert2) -> bool:
return self.alert_priority > alert2.alert_priority return self.alert_priority > alert2.alert_priority
def __eq__(self, alert2) -> bool:
return self.alert_priority == alert2.alert_priority
class NoEntryAlert(Alert): class NoEntryAlert(Alert):
def __init__(self, alert_text_2, audible_alert=AudibleAlert.chimeError, def __init__(self, alert_text_2, audible_alert=AudibleAlert.chimeError,
visual_alert=VisualAlert.none, duration_hud_alert=2.): visual_alert=VisualAlert.none, duration_hud_alert=2.):

@ -1 +1 @@
1ca0db37c9fab1328fe1f86cc884b9bc7123aaee 6f90ffa4970c48edc018ee733bf81b3231b4c463
Loading…
Cancel
Save