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. 23
      selfdrive/ui/ui.h

@ -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

@ -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);
}
}

@ -2,6 +2,7 @@
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <QObject>
@ -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};
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 header_h = 420;
const int footer_h = 280;
const int UI_FREQ = 20; // Hz
typedef enum UIStatus {
STATUS_DISENGAGED,
STATUS_ENGAGED,

Loading…
Cancel
Save