2020-06-15 16:26:38 -06:00
|
|
|
#pragma once
|
2020-08-20 09:16:44 -06:00
|
|
|
|
2020-09-03 17:32:55 -06:00
|
|
|
#include <map>
|
2021-01-11 07:52:58 -07:00
|
|
|
#include <memory>
|
2021-05-08 23:15:17 -06:00
|
|
|
#include <string>
|
2021-11-17 14:46:38 -07:00
|
|
|
#include <optional>
|
2021-05-08 23:15:17 -06:00
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QTimer>
|
2021-06-01 21:59:41 -06:00
|
|
|
#include <QColor>
|
2021-11-17 04:24:37 -07:00
|
|
|
#include <QTransform>
|
2020-01-17 12:05:23 -07:00
|
|
|
#include "nanovg.h"
|
|
|
|
|
2021-05-08 23:15:17 -06:00
|
|
|
#include "cereal/messaging/messaging.h"
|
2021-02-09 18:23:46 -07:00
|
|
|
#include "common/transformations/orientation.hpp"
|
2021-05-08 23:15:17 -06:00
|
|
|
#include "selfdrive/camerad/cameras/camera_common.h"
|
|
|
|
#include "selfdrive/common/mat.h"
|
|
|
|
#include "selfdrive/common/modeldata.h"
|
|
|
|
#include "selfdrive/common/params.h"
|
|
|
|
#include "selfdrive/common/util.h"
|
2021-04-29 12:18:59 -06:00
|
|
|
|
2020-03-27 12:00:42 -06:00
|
|
|
#define COLOR_BLACK nvgRGBA(0, 0, 0, 255)
|
|
|
|
#define COLOR_BLACK_ALPHA(x) nvgRGBA(0, 0, 0, x)
|
2020-03-09 20:13:32 -06:00
|
|
|
#define COLOR_WHITE nvgRGBA(255, 255, 255, 255)
|
2020-03-27 12:00:42 -06:00
|
|
|
#define COLOR_WHITE_ALPHA(x) nvgRGBA(255, 255, 255, x)
|
2021-03-24 02:10:31 -06:00
|
|
|
#define COLOR_RED_ALPHA(x) nvgRGBA(201, 34, 49, x)
|
2020-03-09 20:13:32 -06:00
|
|
|
#define COLOR_YELLOW nvgRGBA(218, 202, 37, 255)
|
|
|
|
#define COLOR_RED nvgRGBA(201, 34, 49, 255)
|
|
|
|
|
2021-11-09 15:18:29 -07:00
|
|
|
const int bdr_s = 30;
|
|
|
|
const int header_h = 420;
|
|
|
|
const int footer_h = 280;
|
|
|
|
|
|
|
|
const int UI_FREQ = 20; // Hz
|
2021-07-16 18:30:00 -06:00
|
|
|
typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert;
|
|
|
|
|
2021-09-14 12:48:34 -06:00
|
|
|
// TODO: this is also hardcoded in common/transformations/camera.py
|
2021-06-01 21:59:41 -06:00
|
|
|
// TODO: choose based on frame input size
|
2021-10-13 13:44:36 -06:00
|
|
|
const float y_offset = Hardware::EON() ? 0.0 : 150.0;
|
|
|
|
const float ZOOM = Hardware::EON() ? 2138.5 : 2912.8;
|
2021-06-01 21:59:41 -06:00
|
|
|
|
2020-09-12 19:35:36 -06:00
|
|
|
typedef struct Rect {
|
|
|
|
int x, y, w, h;
|
|
|
|
int centerX() const { return x + w / 2; }
|
2021-01-08 12:30:37 -07:00
|
|
|
int centerY() const { return y + h / 2; }
|
2020-09-12 19:35:36 -06:00
|
|
|
int right() const { return x + w; }
|
|
|
|
int bottom() const { return y + h; }
|
|
|
|
bool ptInRect(int px, int py) const {
|
|
|
|
return px >= x && px < (x + w) && py >= y && py < (y + h);
|
|
|
|
}
|
|
|
|
} Rect;
|
2020-08-20 09:16:44 -06:00
|
|
|
|
2021-11-09 15:18:29 -07:00
|
|
|
struct Alert {
|
2021-07-16 18:30:00 -06:00
|
|
|
QString text1;
|
|
|
|
QString text2;
|
|
|
|
QString type;
|
|
|
|
cereal::ControlsState::AlertSize size;
|
|
|
|
AudibleAlert sound;
|
2021-08-25 10:33:27 -06:00
|
|
|
bool equal(const Alert &a2) {
|
2021-11-12 16:04:57 -07:00
|
|
|
return text1 == a2.text1 && text2 == a2.text2 && type == a2.type && sound == a2.sound;
|
2021-07-16 18:30:00 -06:00
|
|
|
}
|
|
|
|
|
2021-11-09 15:18:29 -07:00
|
|
|
static Alert get(const SubMaster &sm, uint64_t started_frame) {
|
|
|
|
if (sm.updated("controlsState")) {
|
|
|
|
const cereal::ControlsState::Reader &cs = sm["controlsState"].getControlsState();
|
|
|
|
return {cs.getAlertText1().cStr(), cs.getAlertText2().cStr(),
|
|
|
|
cs.getAlertType().cStr(), cs.getAlertSize(),
|
|
|
|
cs.getAlertSound()};
|
|
|
|
} else if ((sm.frame - started_frame) > 5 * UI_FREQ) {
|
|
|
|
const int CONTROLS_TIMEOUT = 5;
|
|
|
|
// Handle controls timeout
|
|
|
|
if (sm.rcv_frame("controlsState") < started_frame) {
|
|
|
|
// car is started, but controlsState hasn't been seen at all
|
|
|
|
return {"openpilot Unavailable", "Waiting for controls to start",
|
|
|
|
"controlsWaiting", cereal::ControlsState::AlertSize::MID,
|
|
|
|
AudibleAlert::NONE};
|
|
|
|
} else if ((nanos_since_boot() - sm.rcv_time("controlsState")) / 1e9 > CONTROLS_TIMEOUT) {
|
|
|
|
// car is started, but controls is lagging or died
|
|
|
|
return {"TAKE CONTROL IMMEDIATELY", "Controls Unresponsive",
|
|
|
|
"controlsUnresponsive", cereal::ControlsState::AlertSize::FULL,
|
2021-11-30 20:47:33 -07:00
|
|
|
AudibleAlert::WARNING_IMMEDIATE};
|
2021-11-09 15:18:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
};
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-09-03 17:32:55 -06:00
|
|
|
typedef enum UIStatus {
|
|
|
|
STATUS_DISENGAGED,
|
|
|
|
STATUS_ENGAGED,
|
|
|
|
STATUS_WARNING,
|
|
|
|
STATUS_ALERT,
|
|
|
|
} UIStatus;
|
|
|
|
|
2021-06-07 01:04:16 -06:00
|
|
|
const QColor bg_colors [] = {
|
2021-06-01 21:59:41 -06:00
|
|
|
[STATUS_DISENGAGED] = QColor(0x17, 0x33, 0x49, 0xc8),
|
|
|
|
[STATUS_ENGAGED] = QColor(0x17, 0x86, 0x44, 0xf1),
|
|
|
|
[STATUS_WARNING] = QColor(0xDA, 0x6F, 0x25, 0xf1),
|
|
|
|
[STATUS_ALERT] = QColor(0xC9, 0x22, 0x31, 0xf1),
|
2020-01-17 12:05:23 -07:00
|
|
|
};
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
typedef struct {
|
2021-01-06 03:34:52 -07:00
|
|
|
float x, y;
|
|
|
|
} vertex_data;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2021-01-06 03:34:52 -07:00
|
|
|
typedef struct {
|
2021-02-08 04:06:14 -07:00
|
|
|
vertex_data v[TRAJECTORY_SIZE * 2];
|
2021-01-06 03:34:52 -07:00
|
|
|
int cnt;
|
|
|
|
} line_vertices_data;
|
|
|
|
|
2020-11-11 21:31:46 -07:00
|
|
|
typedef struct UIScene {
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2021-02-09 18:23:46 -07:00
|
|
|
mat3 view_from_calib;
|
2020-09-03 17:32:55 -06:00
|
|
|
bool world_objects_visible;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2021-02-16 22:39:32 -07:00
|
|
|
cereal::PandaState::PandaType pandaType;
|
2020-06-05 16:33:45 -06:00
|
|
|
|
2021-01-06 03:34:52 -07:00
|
|
|
// modelV2
|
2020-11-11 21:31:46 -07:00
|
|
|
float lane_line_probs[4];
|
|
|
|
float road_edge_stds[2];
|
2021-02-08 04:06:14 -07:00
|
|
|
line_vertices_data track_vertices;
|
2021-01-06 03:34:52 -07:00
|
|
|
line_vertices_data lane_line_vertices[4];
|
|
|
|
line_vertices_data road_edge_vertices[2];
|
2021-01-28 07:53:24 -07:00
|
|
|
|
2021-06-04 00:06:14 -06:00
|
|
|
bool dm_active, engageable;
|
|
|
|
|
2021-01-28 07:53:24 -07:00
|
|
|
// lead
|
|
|
|
vertex_data lead_vertices[2];
|
2021-03-03 09:40:50 -07:00
|
|
|
|
|
|
|
float light_sensor, accel_sensor, gyro_sensor;
|
2021-03-24 02:10:31 -06:00
|
|
|
bool started, ignition, is_metric, longitudinal_control, end_to_end;
|
2021-03-03 09:40:50 -07:00
|
|
|
uint64_t started_frame;
|
2020-01-17 12:05:23 -07:00
|
|
|
} UIScene;
|
|
|
|
|
|
|
|
typedef struct UIState {
|
2021-09-08 14:13:27 -06:00
|
|
|
int fb_w = 0, fb_h = 0;
|
2020-01-17 12:05:23 -07:00
|
|
|
NVGcontext *vg;
|
|
|
|
|
2021-01-16 14:50:06 -07:00
|
|
|
// images
|
|
|
|
std::map<std::string, int> images;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2021-04-29 12:18:59 -06:00
|
|
|
std::unique_ptr<SubMaster> sm;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2020-09-03 17:32:55 -06:00
|
|
|
UIStatus status;
|
2021-09-08 14:13:27 -06:00
|
|
|
UIScene scene = {};
|
2020-01-17 12:05:23 -07:00
|
|
|
|
|
|
|
bool awake;
|
2021-10-26 10:59:17 -06:00
|
|
|
bool has_prime = false;
|
2020-01-17 12:05:23 -07:00
|
|
|
|
2021-11-17 04:24:37 -07:00
|
|
|
QTransform car_space_transform;
|
2021-04-07 11:12:35 -06:00
|
|
|
bool wide_camera;
|
2021-11-15 23:11:17 -07:00
|
|
|
|
|
|
|
float running_time;
|
2020-01-17 12:05:23 -07:00
|
|
|
} UIState;
|
|
|
|
|
2021-04-29 12:18:59 -06:00
|
|
|
|
|
|
|
class QUIState : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
QUIState(QObject* parent = 0);
|
|
|
|
|
|
|
|
// TODO: get rid of this, only use signal
|
|
|
|
inline static UIState ui_state = {0};
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void uiUpdate(const UIState &s);
|
|
|
|
void offroadTransition(bool offroad);
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void update();
|
|
|
|
|
|
|
|
private:
|
|
|
|
QTimer *timer;
|
|
|
|
bool started_prev = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// device management class
|
|
|
|
|
|
|
|
class Device : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
Device(QObject *parent = 0);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// auto brightness
|
|
|
|
const float accel_samples = 5*UI_FREQ;
|
|
|
|
|
2021-11-19 21:40:29 -07:00
|
|
|
bool awake = false;
|
2021-04-29 12:18:59 -06:00
|
|
|
int awake_timeout = 0;
|
|
|
|
float accel_prev = 0;
|
|
|
|
float gyro_prev = 0;
|
2021-11-26 06:59:23 -07:00
|
|
|
int last_brightness = 0;
|
2021-04-29 12:18:59 -06:00
|
|
|
FirstOrderFilter brightness_filter;
|
|
|
|
|
|
|
|
QTimer *timer;
|
|
|
|
|
|
|
|
void updateBrightness(const UIState &s);
|
|
|
|
void updateWakefulness(const UIState &s);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void displayPowerChanged(bool on);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void setAwake(bool on, bool reset);
|
|
|
|
void update(const UIState &s);
|
|
|
|
};
|