Esempio n. 1
0
  def test_duration(self):
    """
      Enforce that an alert lasts for max(alert duration, duration the alert is added)
    """
    for duration in range(1, 100):
      alert = None
      while not isinstance(alert, Alert):
        event = random.choice([e for e in EVENTS.values() if len(e)])
        alert = random.choice(list(event.values()))

      alert.duration = duration

      # check two cases:
      # - alert is added to AM for <= the alert's duration
      # - alert is added to AM for > alert's duration
      for greater in (True, False):
        if greater:
          add_duration = duration + random.randint(1, 10)
        else:
          add_duration = random.randint(1, duration)
        show_duration = max(duration, add_duration)

        AM = AlertManager()
        for frame in range(duration+10):
          if frame < add_duration:
            AM.add_many(frame, [alert, ])
          current_alert = AM.process_alerts(frame, {})

          shown = current_alert is not None
          should_show = frame <= show_duration
          self.assertEqual(shown, should_show, msg=f"{frame=} {add_duration=} {duration=}")
Esempio n. 2
0
def cycle_alerts(duration=200, is_metric=False):
    alerts = list(EVENTS.keys())
    print(alerts)

    alerts = [
        EventName.preDriverDistracted, EventName.promptDriverDistracted,
        EventName.driverDistracted
    ]

    CP = CarInterface.get_params("HONDA CIVIC 2016 TOURING")
    sm = messaging.SubMaster([
        'deviceState', 'pandaState', 'roadCameraState', 'modelV2',
        'liveCalibration', 'driverMonitoringState', 'longitudinalPlan',
        'lateralPlan', 'liveLocationKalman'
    ])

    controls_state = messaging.pub_sock('controlsState')
    deviceState = messaging.pub_sock('deviceState')

    idx, last_alert_millis = 0, 0

    events = Events()
    AM = AlertManager()

    frame = 0

    while 1:
        if frame % duration == 0:
            idx = (idx + 1) % len(alerts)
            events.clear()
            events.add(alerts[idx])

        current_alert_types = [
            ET.PERMANENT, ET.USER_DISABLE, ET.IMMEDIATE_DISABLE,
            ET.SOFT_DISABLE, ET.PRE_ENABLE, ET.NO_ENTRY, ET.ENABLE, ET.WARNING
        ]
        a = events.create_alerts(current_alert_types, [CP, sm, is_metric])
        AM.add_many(frame, a)
        AM.process_alerts(frame)

        dat = messaging.new_message()
        dat.init('controlsState')

        dat.controlsState.alertText1 = AM.alert_text_1
        dat.controlsState.alertText2 = AM.alert_text_2
        dat.controlsState.alertSize = AM.alert_size
        dat.controlsState.alertStatus = AM.alert_status
        dat.controlsState.alertBlinkingRate = AM.alert_rate
        dat.controlsState.alertType = AM.alert_type
        dat.controlsState.alertSound = AM.audible_alert
        controls_state.send(dat.to_bytes())

        dat = messaging.new_message()
        dat.init('deviceState')
        dat.deviceState.started = True
        deviceState.send(dat.to_bytes())

        frame += 1
        time.sleep(0.01)
Esempio n. 3
0
  def test_events_defined(self):
    # Ensure all events in capnp schema are defined in events.py
    events = car.CarEvent.EventName.schema.enumerants

    for name, e in events.items():
      if not name.endswith("DEPRECATED"):
        fail_msg = "%s @%d not in EVENTS" % (name, e)
        self.assertTrue(e in EVENTS.keys(), msg=fail_msg)
Esempio n. 4
0
    def test_alert_text_length(self):
        font_path = os.path.join(BASEDIR, "selfdrive/assets/fonts")
        regular_font_path = os.path.join(font_path, "opensans_semibold.ttf")
        bold_font_path = os.path.join(font_path, "opensans_semibold.ttf")
        semibold_font_path = os.path.join(font_path, "opensans_semibold.ttf")

        max_text_width = 1920 - 300  # full screen width is useable, minus sidebar
        # TODO: get exact scale factor. found this empirically, works well enough
        font_scale_factor = 1.85  # factor to scale from nanovg units to PIL

        draw = ImageDraw.Draw(Image.new('RGB', (0, 0)))

        fonts = {
            AlertSize.small: [
                ImageFont.truetype(semibold_font_path,
                                   int(40 * font_scale_factor))
            ],
            AlertSize.mid: [
                ImageFont.truetype(bold_font_path,
                                   int(48 * font_scale_factor)),
                ImageFont.truetype(regular_font_path,
                                   int(36 * font_scale_factor))
            ],
        }

        alerts = []
        for event_types in EVENTS.values():
            for alert in event_types.values():
                if isinstance(alert, Alert):
                    alerts.append(alert)

        for alert in alerts:
            # for full size alerts, both text fields wrap the text,
            # so it's unlikely that they  would go past the max width
            if alert.alert_size in [AlertSize.none, AlertSize.full]:
                continue

            for i, txt in enumerate([alert.alert_text_1, alert.alert_text_2]):
                if i >= len(fonts[alert.alert_size]):
                    break

                font = fonts[alert.alert_size][i]
                w, h = draw.textsize(txt, font)
                msg = "type: %s msg: %s" % (alert.alert_type, txt)
                self.assertLessEqual(w, max_text_width, msg=msg)
Esempio n. 5
0
    def test_alert_sanity_check(self):
        for event_types in EVENTS.values():
            for event_type, a in event_types.items():
                # TODO: add callback alerts
                if not isinstance(a, Alert):
                    continue

                if a.alert_size == AlertSize.none:
                    self.assertEqual(len(a.alert_text_1), 0)
                    self.assertEqual(len(a.alert_text_2), 0)
                elif a.alert_size == AlertSize.small:
                    self.assertGreater(len(a.alert_text_1), 0)
                    self.assertEqual(len(a.alert_text_2), 0)
                elif a.alert_size == AlertSize.mid:
                    self.assertGreater(len(a.alert_text_1), 0)
                    self.assertGreater(len(a.alert_text_2), 0)
                else:
                    self.assertGreater(len(a.alert_text_1), 0)

                self.assertGreaterEqual(a.duration, 0.)

                if event_type not in (ET.WARNING, ET.PERMANENT, ET.PRE_ENABLE):
                    self.assertEqual(a.creation_delay, 0.)
Esempio n. 6
0
from PIL import Image, ImageDraw, ImageFont

from cereal import log, car
from common.basedir import BASEDIR
from common.params import Params
from selfdrive.controls.lib.events import Alert, EVENTS
from selfdrive.controls.lib.alertmanager import set_offroad_alert

AlertSize = log.ControlsState.AlertSize

OFFROAD_ALERTS_PATH = os.path.join(
    BASEDIR, "selfdrive/controls/lib/alerts_offroad.json")

# TODO: add callback alerts
ALERTS = []
for event_types in EVENTS.values():
    for alert in event_types.values():
        if isinstance(alert, Alert):
            ALERTS.append(alert)


class TestAlerts(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        with open(OFFROAD_ALERTS_PATH) as f:
            cls.offroad_alerts = json.loads(f.read())

    def test_events_defined(self):
        # Ensure all events in capnp schema are defined in events.py
        events = car.CarEvent.EventName.schema.enumerants
Esempio n. 7
0
# pylint: skip-file
# type: ignore

import argparse
import time

import cereal.messaging as messaging
from selfdrive.controls.lib.events import EVENTS, Alert


def now_millis():
    return time.time() * 1000


ALERTS = [
    a for _, et in EVENTS.items() for _, a in et.items()
    if isinstance(a, Alert)
]

#from cereal import car
#ALERTS = [a for a in ALERTS if a.audible_alert == car.CarControl.HUDControl.AudibleAlert.chimeWarningRepeat]

default_alerts = sorted(ALERTS,
                        key=lambda alert:
                        (alert.alert_size, len(alert.alert_text_2)))


def cycle_alerts(duration_millis, alerts=None):
    if alerts is None:
        alerts = default_alerts