nopenpilot/selfdrive/ui/ui.h

210 lines
5.2 KiB
C
Raw Normal View History

#pragma once
#include <map>
#include <memory>
#include <string>
#include <optional>
#include <QObject>
#include <QTimer>
#include <QColor>
#include <QTransform>
2020-01-17 12:05:23 -07:00
#include "nanovg.h"
#include "cereal/messaging/messaging.h"
#include "common/transformations/orientation.hpp"
#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
#define COLOR_BLACK nvgRGBA(0, 0, 0, 255)
#define COLOR_BLACK_ALPHA(x) nvgRGBA(0, 0, 0, x)
#define COLOR_WHITE nvgRGBA(255, 255, 255, 255)
#define COLOR_WHITE_ALPHA(x) nvgRGBA(255, 255, 255, x)
#define COLOR_RED_ALPHA(x) nvgRGBA(201, 34, 49, x)
#define COLOR_YELLOW nvgRGBA(218, 202, 37, 255)
#define COLOR_RED nvgRGBA(201, 34, 49, 255)
const int bdr_s = 30;
const int header_h = 420;
const int footer_h = 280;
const int UI_FREQ = 20; // Hz
typedef cereal::CarControl::HUDControl::AudibleAlert AudibleAlert;
// TODO: this is also hardcoded in common/transformations/camera.py
// TODO: choose based on frame input size
const float y_offset = Hardware::EON() ? 0.0 : 150.0;
const float ZOOM = Hardware::EON() ? 2138.5 : 2912.8;
typedef struct Rect {
int x, y, w, h;
int centerX() const { return x + w / 2; }
int centerY() const { return y + h / 2; }
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;
struct Alert {
QString text1;
QString text2;
QString type;
cereal::ControlsState::AlertSize size;
AudibleAlert sound;
bool equal(const Alert &a2) {
return text1 == a2.text1 && text2 == a2.text2 && type == a2.type && sound == a2.sound;
}
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,
AudibleAlert::WARNING_IMMEDIATE};
}
}
return {};
}
};
2020-01-17 12:05:23 -07: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 [] = {
[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
};
Torch model (#2452) * refactor draw model * rebase master * correct valid_len * rename function * rename variables * white space * rebase to master * e16c13ac-927d-455e-ae0a-81b482a2c787 * start rewriting * save proress * compiles! * oops * many fixes * seems to work * fix desires * finally cleaned * wrong std for ll * dont pulse none * compiles! * ready to test * WIP does not compile * compiles * various fixes * does something! * full 3d * not needed * draw up to 100m * fix segfault * wrong sign * fix flicker * add road edges * finish v2 packet * Added pytorch supercombo * fix rebase * no more keras * Hacky solution to the NCHW/NHWC incompatibility between SNPE and our frame data * dont break dmonitoringd, final model 229e3ce1-7259-412b-85e6-cc646d70f1d8/430 * fix hack * Revert "fix hack" This reverts commit 5550fc01a7881d065a5eddbbb42dac55ef7ec36c. * Removed axis permutation hack * Folded padding layers into conv layers * Removed the last pad layer from the dlc * Revert "Removed the last pad layer from the dlc" This reverts commit b85f24b9e1d04abf64e85901a7ff49e00d82020a. * Revert "Folded padding layers into conv layers" This reverts commit b8d1773e4e76dea481acebbfad6a6235fbb58463. * vision model: 5034ac8b-5703-4a49-948b-11c064d10880/780 temporal model: 229e3ce1-7259-412b-85e6-cc646d70f1d8/430 with permute + pool opt * fix ui drawing with clips * ./compile_torch.py 5034ac8b-5703-4a49-948b-11c064d10880/780 dfcd2375-81d8-49df-95bf-1d2d6ad86010/450 with variable history length * std::clamp * not sure how this compiled before * 2895ace6-a296-47ac-86e6-17ea800a74e5/550 * db090195-8810-42de-ab38-bb835d775d87/601 * 5m is very little * onnx runner * add onnxruntime to pipfile * run in real time without using the whole CPU * bump cereal; * add stds * set road edge opacity based on stddev * don't access the model packet in paint * convert mat.h to a c++ header file (#2499) * update tests * safety first Co-authored-by: deanlee <deanlee3@gmail.com> Co-authored-by: mitchell <mitchell@comma.ai> Co-authored-by: Comma Device <device@comma.ai> Co-authored-by: George Hotz <george@comma.ai> Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2020-11-11 21:31:46 -07:00
typedef struct {
UI: refactor model related functions (#19657) Squashed commit of the following: commit 7d7fb15e4cdd0e47fa38f949f79581dc611db015 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:31:09 2021 +0100 no extern commit 901893966e3f908435bceb8777253dfa916a3d91 Merge: 9816e01e d8e864e3 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:27:29 2021 +0100 Merge branch 'master' into refactor_model_draw commit 9816e01e6ff0284521fa18b07ec15b3900577687 Author: deanlee <deanlee3@gmail.com> Date: Wed Jan 6 04:02:08 2021 +0000 fix lead_d commit 6fa500dbce26544cc3fdeb4d9b0d2d654382a40b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 13:18:20 2021 +0000 populate model data in update_socket commit e25db7e28c1b543a04c3e837aa44431c42dbb5b2 Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:59:13 2021 +0000 update model in function commit 0834a3ffe7eaa080671f13c32dd6475ae5392d5b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:50:21 2021 +0000 continue commit 95b08679c2593eb0770a68b3cf1a59f1fb3b74f4 Author: Willem Melching <willem.melching@gmail.com> Date: Tue Jan 5 13:26:34 2021 +0100 Update selfdrive/ui/paint.cc commit 0eef065ae4f96c2ee5d97b4ccdfdbf0ae4945385 Author: Dean Lee <deanlee3@gmail.com> Date: Tue Jan 5 20:11:01 2021 +0800 Update selfdrive/ui/paint.cc Co-authored-by: Willem Melching <willem.melching@gmail.com> commit 955ad9b03a176174b86b28d5bb6f48d6e2a09281 Author: deanlee <deanlee3@gmail.com> Date: Thu Dec 31 04:45:31 2020 +0000 refactor model related functions
2021-01-06 03:34:52 -07:00
float x, y;
} vertex_data;
2020-01-17 12:05:23 -07:00
UI: refactor model related functions (#19657) Squashed commit of the following: commit 7d7fb15e4cdd0e47fa38f949f79581dc611db015 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:31:09 2021 +0100 no extern commit 901893966e3f908435bceb8777253dfa916a3d91 Merge: 9816e01e d8e864e3 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:27:29 2021 +0100 Merge branch 'master' into refactor_model_draw commit 9816e01e6ff0284521fa18b07ec15b3900577687 Author: deanlee <deanlee3@gmail.com> Date: Wed Jan 6 04:02:08 2021 +0000 fix lead_d commit 6fa500dbce26544cc3fdeb4d9b0d2d654382a40b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 13:18:20 2021 +0000 populate model data in update_socket commit e25db7e28c1b543a04c3e837aa44431c42dbb5b2 Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:59:13 2021 +0000 update model in function commit 0834a3ffe7eaa080671f13c32dd6475ae5392d5b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:50:21 2021 +0000 continue commit 95b08679c2593eb0770a68b3cf1a59f1fb3b74f4 Author: Willem Melching <willem.melching@gmail.com> Date: Tue Jan 5 13:26:34 2021 +0100 Update selfdrive/ui/paint.cc commit 0eef065ae4f96c2ee5d97b4ccdfdbf0ae4945385 Author: Dean Lee <deanlee3@gmail.com> Date: Tue Jan 5 20:11:01 2021 +0800 Update selfdrive/ui/paint.cc Co-authored-by: Willem Melching <willem.melching@gmail.com> commit 955ad9b03a176174b86b28d5bb6f48d6e2a09281 Author: deanlee <deanlee3@gmail.com> Date: Thu Dec 31 04:45:31 2020 +0000 refactor model related functions
2021-01-06 03:34:52 -07:00
typedef struct {
vertex_data v[TRAJECTORY_SIZE * 2];
UI: refactor model related functions (#19657) Squashed commit of the following: commit 7d7fb15e4cdd0e47fa38f949f79581dc611db015 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:31:09 2021 +0100 no extern commit 901893966e3f908435bceb8777253dfa916a3d91 Merge: 9816e01e d8e864e3 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:27:29 2021 +0100 Merge branch 'master' into refactor_model_draw commit 9816e01e6ff0284521fa18b07ec15b3900577687 Author: deanlee <deanlee3@gmail.com> Date: Wed Jan 6 04:02:08 2021 +0000 fix lead_d commit 6fa500dbce26544cc3fdeb4d9b0d2d654382a40b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 13:18:20 2021 +0000 populate model data in update_socket commit e25db7e28c1b543a04c3e837aa44431c42dbb5b2 Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:59:13 2021 +0000 update model in function commit 0834a3ffe7eaa080671f13c32dd6475ae5392d5b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:50:21 2021 +0000 continue commit 95b08679c2593eb0770a68b3cf1a59f1fb3b74f4 Author: Willem Melching <willem.melching@gmail.com> Date: Tue Jan 5 13:26:34 2021 +0100 Update selfdrive/ui/paint.cc commit 0eef065ae4f96c2ee5d97b4ccdfdbf0ae4945385 Author: Dean Lee <deanlee3@gmail.com> Date: Tue Jan 5 20:11:01 2021 +0800 Update selfdrive/ui/paint.cc Co-authored-by: Willem Melching <willem.melching@gmail.com> commit 955ad9b03a176174b86b28d5bb6f48d6e2a09281 Author: deanlee <deanlee3@gmail.com> Date: Thu Dec 31 04:45:31 2020 +0000 refactor model related functions
2021-01-06 03:34:52 -07:00
int cnt;
} line_vertices_data;
Torch model (#2452) * refactor draw model * rebase master * correct valid_len * rename function * rename variables * white space * rebase to master * e16c13ac-927d-455e-ae0a-81b482a2c787 * start rewriting * save proress * compiles! * oops * many fixes * seems to work * fix desires * finally cleaned * wrong std for ll * dont pulse none * compiles! * ready to test * WIP does not compile * compiles * various fixes * does something! * full 3d * not needed * draw up to 100m * fix segfault * wrong sign * fix flicker * add road edges * finish v2 packet * Added pytorch supercombo * fix rebase * no more keras * Hacky solution to the NCHW/NHWC incompatibility between SNPE and our frame data * dont break dmonitoringd, final model 229e3ce1-7259-412b-85e6-cc646d70f1d8/430 * fix hack * Revert "fix hack" This reverts commit 5550fc01a7881d065a5eddbbb42dac55ef7ec36c. * Removed axis permutation hack * Folded padding layers into conv layers * Removed the last pad layer from the dlc * Revert "Removed the last pad layer from the dlc" This reverts commit b85f24b9e1d04abf64e85901a7ff49e00d82020a. * Revert "Folded padding layers into conv layers" This reverts commit b8d1773e4e76dea481acebbfad6a6235fbb58463. * vision model: 5034ac8b-5703-4a49-948b-11c064d10880/780 temporal model: 229e3ce1-7259-412b-85e6-cc646d70f1d8/430 with permute + pool opt * fix ui drawing with clips * ./compile_torch.py 5034ac8b-5703-4a49-948b-11c064d10880/780 dfcd2375-81d8-49df-95bf-1d2d6ad86010/450 with variable history length * std::clamp * not sure how this compiled before * 2895ace6-a296-47ac-86e6-17ea800a74e5/550 * db090195-8810-42de-ab38-bb835d775d87/601 * 5m is very little * onnx runner * add onnxruntime to pipfile * run in real time without using the whole CPU * bump cereal; * add stds * set road edge opacity based on stddev * don't access the model packet in paint * convert mat.h to a c++ header file (#2499) * update tests * safety first Co-authored-by: deanlee <deanlee3@gmail.com> Co-authored-by: mitchell <mitchell@comma.ai> Co-authored-by: Comma Device <device@comma.ai> Co-authored-by: George Hotz <george@comma.ai> Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2020-11-11 21:31:46 -07:00
typedef struct UIScene {
2020-01-17 12:05:23 -07:00
mat3 view_from_calib;
bool world_objects_visible;
2020-01-17 12:05:23 -07:00
cereal::PandaState::PandaType pandaType;
UI: refactor model related functions (#19657) Squashed commit of the following: commit 7d7fb15e4cdd0e47fa38f949f79581dc611db015 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:31:09 2021 +0100 no extern commit 901893966e3f908435bceb8777253dfa916a3d91 Merge: 9816e01e d8e864e3 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:27:29 2021 +0100 Merge branch 'master' into refactor_model_draw commit 9816e01e6ff0284521fa18b07ec15b3900577687 Author: deanlee <deanlee3@gmail.com> Date: Wed Jan 6 04:02:08 2021 +0000 fix lead_d commit 6fa500dbce26544cc3fdeb4d9b0d2d654382a40b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 13:18:20 2021 +0000 populate model data in update_socket commit e25db7e28c1b543a04c3e837aa44431c42dbb5b2 Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:59:13 2021 +0000 update model in function commit 0834a3ffe7eaa080671f13c32dd6475ae5392d5b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:50:21 2021 +0000 continue commit 95b08679c2593eb0770a68b3cf1a59f1fb3b74f4 Author: Willem Melching <willem.melching@gmail.com> Date: Tue Jan 5 13:26:34 2021 +0100 Update selfdrive/ui/paint.cc commit 0eef065ae4f96c2ee5d97b4ccdfdbf0ae4945385 Author: Dean Lee <deanlee3@gmail.com> Date: Tue Jan 5 20:11:01 2021 +0800 Update selfdrive/ui/paint.cc Co-authored-by: Willem Melching <willem.melching@gmail.com> commit 955ad9b03a176174b86b28d5bb6f48d6e2a09281 Author: deanlee <deanlee3@gmail.com> Date: Thu Dec 31 04:45:31 2020 +0000 refactor model related functions
2021-01-06 03:34:52 -07:00
// modelV2
Torch model (#2452) * refactor draw model * rebase master * correct valid_len * rename function * rename variables * white space * rebase to master * e16c13ac-927d-455e-ae0a-81b482a2c787 * start rewriting * save proress * compiles! * oops * many fixes * seems to work * fix desires * finally cleaned * wrong std for ll * dont pulse none * compiles! * ready to test * WIP does not compile * compiles * various fixes * does something! * full 3d * not needed * draw up to 100m * fix segfault * wrong sign * fix flicker * add road edges * finish v2 packet * Added pytorch supercombo * fix rebase * no more keras * Hacky solution to the NCHW/NHWC incompatibility between SNPE and our frame data * dont break dmonitoringd, final model 229e3ce1-7259-412b-85e6-cc646d70f1d8/430 * fix hack * Revert "fix hack" This reverts commit 5550fc01a7881d065a5eddbbb42dac55ef7ec36c. * Removed axis permutation hack * Folded padding layers into conv layers * Removed the last pad layer from the dlc * Revert "Removed the last pad layer from the dlc" This reverts commit b85f24b9e1d04abf64e85901a7ff49e00d82020a. * Revert "Folded padding layers into conv layers" This reverts commit b8d1773e4e76dea481acebbfad6a6235fbb58463. * vision model: 5034ac8b-5703-4a49-948b-11c064d10880/780 temporal model: 229e3ce1-7259-412b-85e6-cc646d70f1d8/430 with permute + pool opt * fix ui drawing with clips * ./compile_torch.py 5034ac8b-5703-4a49-948b-11c064d10880/780 dfcd2375-81d8-49df-95bf-1d2d6ad86010/450 with variable history length * std::clamp * not sure how this compiled before * 2895ace6-a296-47ac-86e6-17ea800a74e5/550 * db090195-8810-42de-ab38-bb835d775d87/601 * 5m is very little * onnx runner * add onnxruntime to pipfile * run in real time without using the whole CPU * bump cereal; * add stds * set road edge opacity based on stddev * don't access the model packet in paint * convert mat.h to a c++ header file (#2499) * update tests * safety first Co-authored-by: deanlee <deanlee3@gmail.com> Co-authored-by: mitchell <mitchell@comma.ai> Co-authored-by: Comma Device <device@comma.ai> Co-authored-by: George Hotz <george@comma.ai> Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com>
2020-11-11 21:31:46 -07:00
float lane_line_probs[4];
float road_edge_stds[2];
line_vertices_data track_vertices;
UI: refactor model related functions (#19657) Squashed commit of the following: commit 7d7fb15e4cdd0e47fa38f949f79581dc611db015 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:31:09 2021 +0100 no extern commit 901893966e3f908435bceb8777253dfa916a3d91 Merge: 9816e01e d8e864e3 Author: Willem Melching <willem.melching@gmail.com> Date: Wed Jan 6 11:27:29 2021 +0100 Merge branch 'master' into refactor_model_draw commit 9816e01e6ff0284521fa18b07ec15b3900577687 Author: deanlee <deanlee3@gmail.com> Date: Wed Jan 6 04:02:08 2021 +0000 fix lead_d commit 6fa500dbce26544cc3fdeb4d9b0d2d654382a40b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 13:18:20 2021 +0000 populate model data in update_socket commit e25db7e28c1b543a04c3e837aa44431c42dbb5b2 Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:59:13 2021 +0000 update model in function commit 0834a3ffe7eaa080671f13c32dd6475ae5392d5b Author: deanlee <deanlee3@gmail.com> Date: Tue Jan 5 12:50:21 2021 +0000 continue commit 95b08679c2593eb0770a68b3cf1a59f1fb3b74f4 Author: Willem Melching <willem.melching@gmail.com> Date: Tue Jan 5 13:26:34 2021 +0100 Update selfdrive/ui/paint.cc commit 0eef065ae4f96c2ee5d97b4ccdfdbf0ae4945385 Author: Dean Lee <deanlee3@gmail.com> Date: Tue Jan 5 20:11:01 2021 +0800 Update selfdrive/ui/paint.cc Co-authored-by: Willem Melching <willem.melching@gmail.com> commit 955ad9b03a176174b86b28d5bb6f48d6e2a09281 Author: deanlee <deanlee3@gmail.com> Date: Thu Dec 31 04:45:31 2020 +0000 refactor model related functions
2021-01-06 03:34:52 -07:00
line_vertices_data lane_line_vertices[4];
line_vertices_data road_edge_vertices[2];
2021-06-04 00:06:14 -06:00
bool dm_active, engageable;
// lead
vertex_data lead_vertices[2];
float light_sensor, accel_sensor, gyro_sensor;
bool started, ignition, is_metric, longitudinal_control, end_to_end;
uint64_t started_frame;
2020-01-17 12:05:23 -07:00
} UIScene;
typedef struct UIState {
int fb_w = 0, fb_h = 0;
2020-01-17 12:05:23 -07:00
NVGcontext *vg;
// 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
UIStatus status;
UIScene scene = {};
2020-01-17 12:05:23 -07:00
bool awake;
bool has_prime = false;
2020-01-17 12:05:23 -07:00
QTransform car_space_transform;
bool wide_camera;
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;
bool awake = false;
2021-04-29 12:18:59 -06:00
int awake_timeout = 0;
float accel_prev = 0;
float gyro_prev = 0;
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);
};