common function get_alert

pull/22796/head
deanlee 4 years ago
parent a34db9c542
commit 4d863da6fc
  1. 19
      selfdrive/ui/qt/onroad.cc
  2. 12
      selfdrive/ui/soundd.cc
  3. 25
      selfdrive/ui/ui.h

@ -40,24 +40,13 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) {
} }
void OnroadWindow::updateState(const UIState &s) { void OnroadWindow::updateState(const UIState &s) {
SubMaster &sm = *(s.sm);
QColor bgColor = bg_colors[s.status]; QColor bgColor = bg_colors[s.status];
if (sm.updated("controlsState")) { auto alert = get_alert(*(s.sm), s.scene.started_frame);
const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState(); if (alert) {
alerts->updateAlert({QString::fromStdString(cs.getAlertText1()), if (alert->type == "controlsUnresponsive") {
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
bgColor = bg_colors[STATUS_ALERT]; bgColor = bg_colors[STATUS_ALERT];
alerts->updateAlert(CONTROLS_UNRESPONSIVE_ALERT, bgColor);
} }
alerts->updateAlert(*alert, bgColor);
} }
if (bg != bgColor) { if (bg != bgColor) {
// repaint border // repaint border

@ -59,12 +59,12 @@ public:
} }
} }
auto cs = sm["controlsState"].getControlsState(); auto alert = get_alert(sm, 0);
if (sm.updated("controlsState")) { if (alert) {
setAlert(cs.getAlertType().cStr(), cs.getAlertSound()); setAlert((*alert).type, (*alert).sound);
} else if (sm.rcv_frame("controlsState") > 0 && cs.getEnabled() && } else {
((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT)) { // stop alert.
setAlert(CONTROLS_UNRESPONSIVE_ALERT.type, CONTROLS_UNRESPONSIVE_ALERT.sound); setAlert("", AudibleAlert::NONE);
} }
} }

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <QObject> #include <QObject>
@ -26,6 +27,8 @@
#define COLOR_YELLOW nvgRGBA(218, 202, 37, 255) #define COLOR_YELLOW nvgRGBA(218, 202, 37, 255)
#define COLOR_RED nvgRGBA(201, 34, 49, 255) #define COLOR_RED nvgRGBA(201, 34, 49, 255)
const int UI_FREQ = 20; // Hz
typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert; typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert;
// TODO: this is also hardcoded in common/transformations/camera.py // 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", const Alert CONTROLS_UNRESPONSIVE_ALERT = {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive",
"controlsUnresponsive", cereal::ControlsState::AlertSize::FULL, "controlsUnresponsive", cereal::ControlsState::AlertSize::FULL,
AudibleAlert::CHIME_WARNING_REPEAT}; AudibleAlert::CHIME_WARNING_REPEAT};
const int CONTROLS_TIMEOUT = 5; inline std::optional<Alert> 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 bdr_s = 30;
const int header_h = 420; const int header_h = 420;
const int footer_h = 280; const int footer_h = 280;
const int UI_FREQ = 20; // Hz
typedef enum UIStatus { typedef enum UIStatus {
STATUS_DISENGAGED, STATUS_DISENGAGED,
STATUS_ENGAGED, STATUS_ENGAGED,

Loading…
Cancel
Save