From 4d863da6fce1e3e166cea9400cb28c56b1c0ca1d Mon Sep 17 00:00:00 2001 From: deanlee Date: Sat, 6 Nov 2021 06:59:56 +0800 Subject: [PATCH] common function get_alert --- selfdrive/ui/qt/onroad.cc | 19 ++++--------------- selfdrive/ui/soundd.cc | 12 ++++++------ selfdrive/ui/ui.h | 25 ++++++++++++++++++++++--- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/selfdrive/ui/qt/onroad.cc b/selfdrive/ui/qt/onroad.cc index 7c3a610eed..2f846b725b 100644 --- a/selfdrive/ui/qt/onroad.cc +++ b/selfdrive/ui/qt/onroad.cc @@ -40,24 +40,13 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) { } void OnroadWindow::updateState(const UIState &s) { - SubMaster &sm = *(s.sm); QColor bgColor = bg_colors[s.status]; - if (sm.updated("controlsState")) { - const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); - alerts->updateAlert({QString::fromStdString(cs.getAlertText1()), - QString::fromStdString(cs.getAlertText2()), - QString::fromStdString(cs.getAlertType()), - cs.getAlertSize(), cs.getAlertSound()}, bgColor); - } else if ((sm.frame - s.scene.started_frame) > 5 * UI_FREQ) { - // Handle controls timeout - if (sm.rcv_frame("controlsState") < s.scene.started_frame) { - // car is started, but controlsState hasn't been seen at all - alerts->updateAlert(CONTROLS_WAITING_ALERT, bgColor); - } else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) { - // car is started, but controls is lagging or died + auto alert = get_alert(*(s.sm), s.scene.started_frame); + if (alert) { + if (alert->type == "controlsUnresponsive") { bgColor = bg_colors[STATUS_ALERT]; - alerts->updateAlert(CONTROLS_UNRESPONSIVE_ALERT, bgColor); } + alerts->updateAlert(*alert, bgColor); } if (bg != bgColor) { // repaint border diff --git a/selfdrive/ui/soundd.cc b/selfdrive/ui/soundd.cc index b36514d141..f14414366a 100644 --- a/selfdrive/ui/soundd.cc +++ b/selfdrive/ui/soundd.cc @@ -59,12 +59,12 @@ public: } } - auto cs = sm["controlsState"].getControlsState(); - if (sm.updated("controlsState")) { - setAlert(cs.getAlertType().cStr(), cs.getAlertSound()); - } else if (sm.rcv_frame("controlsState") > 0 && cs.getEnabled() && - ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT)) { - setAlert(CONTROLS_UNRESPONSIVE_ALERT.type, CONTROLS_UNRESPONSIVE_ALERT.sound); + auto alert = get_alert(sm, 0); + if (alert) { + setAlert((*alert).type, (*alert).sound); + } else { + // stop alert. + setAlert("", AudibleAlert::NONE); } } diff --git a/selfdrive/ui/ui.h b/selfdrive/ui/ui.h index 6bea6ad610..6693423c0e 100644 --- a/selfdrive/ui/ui.h +++ b/selfdrive/ui/ui.h @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -26,6 +27,8 @@ #define COLOR_YELLOW nvgRGBA(218, 202, 37, 255) #define COLOR_RED nvgRGBA(201, 34, 49, 255) +const int UI_FREQ = 20; // Hz + typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert; // TODO: this is also hardcoded in common/transformations/camera.py @@ -62,14 +65,30 @@ const Alert CONTROLS_WAITING_ALERT = {"openpilot Unavailable", "Waiting for cont const Alert CONTROLS_UNRESPONSIVE_ALERT = {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive", "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, AudibleAlert::CHIME_WARNING_REPEAT}; -const int CONTROLS_TIMEOUT = 5; +inline std::optional get_alert(const SubMaster &sm, uint64_t started_frame) { + const int CONTROLS_TIMEOUT = 5; + + if (sm.updated("controlsState")) { + const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); + return Alert{cs.getAlertText1().cStr(), cs.getAlertText2().cStr(), cs.getAlertType().cStr(), + cs.getAlertSize(), cs.getAlertSound()}; + } else if ((sm.frame - started_frame) > 5 * UI_FREQ) { + // Handle controls timeout + if (sm.rcv_frame("controlsState") < started_frame) { + // car is started, but controlsState hasn't been seen at all + return CONTROLS_WAITING_ALERT; + } else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) { + // car is started, but controls is lagging or died + return CONTROLS_UNRESPONSIVE_ALERT; + } + } + return std::nullopt; +} const int bdr_s = 30; const int header_h = 420; const int footer_h = 280; -const int UI_FREQ = 20; // Hz - typedef enum UIStatus { STATUS_DISENGAGED, STATUS_ENGAGED,