type hints for alerts and fix community feature alert (#2196)

* fix startup alert for community features

* type hints for alerts

* more type hints

* fix dashcam only alerts

* more type hints
old-commit-hash: a81a18205e
commatwo_master
Adeeb Shihadeh 5 years ago committed by GitHub
parent 806d0961d7
commit 8221c26fab
  1. 3
      selfdrive/car/mazda/interface.py
  2. 4
      selfdrive/controls/controlsd.py
  3. 33
      selfdrive/controls/lib/events.py
  4. 6
      selfdrive/controls/tests/test_startup.py

@ -25,9 +25,6 @@ class CarInterface(CarInterfaceBase):
ret.radarOffCan = True ret.radarOffCan = True
# Mazda port is a community feature for now
ret.communityFeature = True
ret.steerActuatorDelay = 0.1 ret.steerActuatorDelay = 0.1
ret.steerRateCost = 1.0 ret.steerRateCost = 1.0
ret.steerLimitTimer = 0.8 ret.steerLimitTimer = 0.8

@ -83,7 +83,7 @@ class Controls:
car_recognized = self.CP.carName != 'mock' car_recognized = self.CP.carName != 'mock'
# If stock camera is disconnected, we loaded car controls and it's not dashcam mode # If stock camera is disconnected, we loaded car controls and it's not dashcam mode
controller_available = self.CP.enableCamera and self.CI.CC is not None and not passive controller_available = self.CP.enableCamera and self.CI.CC is not None and not passive and not self.CP.dashcamOnly
community_feature_disallowed = self.CP.communityFeature and not community_feature_toggle community_feature_disallowed = self.CP.communityFeature and not community_feature_toggle
self.read_only = not car_recognized or not controller_available or \ self.read_only = not car_recognized or not controller_available or \
self.CP.dashcamOnly or community_feature_disallowed self.CP.dashcamOnly or community_feature_disallowed
@ -138,7 +138,7 @@ class Controls:
self.events.add(EventName.internetConnectivityNeeded, static=True) self.events.add(EventName.internetConnectivityNeeded, static=True)
if community_feature_disallowed: if community_feature_disallowed:
self.events.add(EventName.communityFeatureDisallowed, static=True) self.events.add(EventName.communityFeatureDisallowed, static=True)
if self.read_only and not passive: if not car_recognized:
self.events.add(EventName.carUnrecognized, static=True) self.events.add(EventName.carUnrecognized, static=True)
if hw_type == HwType.whitePanda: if hw_type == HwType.whitePanda:
self.events.add(EventName.whitePandaUnsupported, static=True) self.events.add(EventName.whitePandaUnsupported, static=True)

@ -1,6 +1,8 @@
from functools import total_ordering from functools import total_ordering
from typing import Dict, Union, Callable, Any
from cereal import log, car from cereal import log, car
import cereal.messaging as messaging
from common.realtime import DT_CTRL from common.realtime import DT_CTRL
from selfdrive.config import Conversions as CV from selfdrive.config import Conversions as CV
from selfdrive.locationd.calibrationd import MIN_SPEED_FILTER from selfdrive.locationd.calibrationd import MIN_SPEED_FILTER
@ -97,18 +99,18 @@ class Events:
@total_ordering @total_ordering
class Alert: class Alert:
def __init__(self, def __init__(self,
alert_text_1, alert_text_1: str,
alert_text_2, alert_text_2: str,
alert_status, alert_status,
alert_size, alert_size,
alert_priority, alert_priority,
visual_alert, visual_alert,
audible_alert, audible_alert,
duration_sound, duration_sound: float,
duration_hud_alert, duration_hud_alert: float,
duration_text, duration_text: float,
alert_rate=0., alert_rate: float = 0.,
creation_delay=0.): creation_delay: float = 0.):
self.alert_type = "" self.alert_type = ""
self.alert_text_1 = alert_text_1 self.alert_text_1 = alert_text_1
@ -131,14 +133,14 @@ class Alert:
tst = car.CarControl.new_message() tst = car.CarControl.new_message()
tst.hudControl.visualAlert = self.visual_alert tst.hudControl.visualAlert = self.visual_alert
def __str__(self): def __str__(self) -> str:
return self.alert_text_1 + "/" + self.alert_text_2 + " " + str(self.alert_priority) + " " + str( return self.alert_text_1 + "/" + self.alert_text_2 + " " + str(self.alert_priority) + " " + str(
self.visual_alert) + " " + str(self.audible_alert) self.visual_alert) + " " + str(self.audible_alert)
def __gt__(self, alert2): def __gt__(self, alert2) -> bool:
return self.alert_priority > alert2.alert_priority return self.alert_priority > alert2.alert_priority
def __eq__(self, alert2): def __eq__(self, alert2) -> bool:
return self.alert_priority == alert2.alert_priority return self.alert_priority == alert2.alert_priority
class NoEntryAlert(Alert): class NoEntryAlert(Alert):
@ -174,7 +176,7 @@ class EngagementAlert(Alert):
# ********** alert callback functions ********** # ********** alert callback functions **********
def below_steer_speed_alert(CP, sm, metric): def below_steer_speed_alert(CP: car.CarParams, sm: messaging.SubMaster, metric: bool) -> Alert:
speed = int(round(CP.minSteerSpeed * (CV.MS_TO_KPH if metric else CV.MS_TO_MPH))) speed = int(round(CP.minSteerSpeed * (CV.MS_TO_KPH if metric else CV.MS_TO_MPH)))
unit = "km/h" if metric else "mph" unit = "km/h" if metric else "mph"
return Alert( return Alert(
@ -183,7 +185,7 @@ def below_steer_speed_alert(CP, sm, metric):
AlertStatus.userPrompt, AlertSize.mid, AlertStatus.userPrompt, AlertSize.mid,
Priority.MID, VisualAlert.steerRequired, AudibleAlert.none, 0., 0.4, .3) Priority.MID, VisualAlert.steerRequired, AudibleAlert.none, 0., 0.4, .3)
def calibration_incomplete_alert(CP, sm, metric): def calibration_incomplete_alert(CP: car.CarParams, sm: messaging.SubMaster, metric: bool) -> Alert:
speed = int(MIN_SPEED_FILTER * (CV.MS_TO_KPH if metric else CV.MS_TO_MPH)) speed = int(MIN_SPEED_FILTER * (CV.MS_TO_KPH if metric else CV.MS_TO_MPH))
unit = "km/h" if metric else "mph" unit = "km/h" if metric else "mph"
return Alert( return Alert(
@ -192,7 +194,7 @@ def calibration_incomplete_alert(CP, sm, metric):
AlertStatus.normal, AlertSize.mid, AlertStatus.normal, AlertSize.mid,
Priority.LOWEST, VisualAlert.none, AudibleAlert.none, 0., 0., .2) Priority.LOWEST, VisualAlert.none, AudibleAlert.none, 0., 0., .2)
def no_gps_alert(CP, sm, metric): def no_gps_alert(CP: car.CarParams, sm: messaging.SubMaster, metric: bool) -> Alert:
gps_integrated = sm['health'].hwType in [log.HealthData.HwType.uno, log.HealthData.HwType.dos] gps_integrated = sm['health'].hwType in [log.HealthData.HwType.uno, log.HealthData.HwType.dos]
return Alert( return Alert(
"Poor GPS reception", "Poor GPS reception",
@ -200,13 +202,13 @@ def no_gps_alert(CP, sm, metric):
AlertStatus.normal, AlertSize.mid, AlertStatus.normal, AlertSize.mid,
Priority.LOWER, VisualAlert.none, AudibleAlert.none, 0., 0., .2, creation_delay=300.) Priority.LOWER, VisualAlert.none, AudibleAlert.none, 0., 0., .2, creation_delay=300.)
def wrong_car_mode_alert(CP, sm, metric): def wrong_car_mode_alert(CP: car.CarParams, sm: messaging.SubMaster, metric: bool) -> Alert:
text = "Cruise Mode Disabled" text = "Cruise Mode Disabled"
if CP.carName == "honda": if CP.carName == "honda":
text = "Main Switch Off" text = "Main Switch Off"
return NoEntryAlert(text, duration_hud_alert=0.) return NoEntryAlert(text, duration_hud_alert=0.)
EVENTS = { EVENTS: Dict[int, Dict[str, Union[Alert, Callable[[Any, messaging.SubMaster, bool], Alert]]]] = {
# ********** events with no alerts ********** # ********** events with no alerts **********
# ********** events only containing alerts displayed in all states ********** # ********** events only containing alerts displayed in all states **********
@ -287,7 +289,6 @@ EVENTS = {
EventName.communityFeatureDisallowed: { EventName.communityFeatureDisallowed: {
# LOW priority to overcome Cruise Error # LOW priority to overcome Cruise Error
ET.PERMANENT: Alert( ET.PERMANENT: Alert(
"",
"Community Feature Detected", "Community Feature Detected",
"Enable Community Features in Developer Settings", "Enable Community Features in Developer Settings",
AlertStatus.normal, AlertSize.mid, AlertStatus.normal, AlertSize.mid,

@ -26,11 +26,11 @@ class TestStartup(unittest.TestCase):
# community supported car # community supported car
(EventName.startupMaster, HYUNDAI.KIA_STINGER, True), (EventName.startupMaster, HYUNDAI.KIA_STINGER, True),
(EventName.startupMaster, HYUNDAI.KIA_STINGER, False), (EventName.communityFeatureDisallowed, HYUNDAI.KIA_STINGER, False),
# dashcamOnly car # dashcamOnly car
(EventName.startupMaster, MAZDA.CX5, True), (EventName.startupNoControl, MAZDA.CX5, True),
(EventName.startupMaster, MAZDA.CX5, False), (EventName.startupNoControl, MAZDA.CX5, False),
# unrecognized car # unrecognized car
(EventName.startupNoCar, None, True), (EventName.startupNoCar, None, True),

Loading…
Cancel
Save