Added new util class FirstOrderFilter (#20303)

* new class FirstOrderFilter

* flick face icon

* revert ui_draw_vision_face

* new function reset()

* reset after update

* use filter to update brightness
old-commit-hash: 6db31b9a33
commatwo_master
Dean Lee 4 years ago committed by GitHub
parent 8c09067b27
commit aa1474467e
  1. 16
      selfdrive/common/util.h
  2. 13
      selfdrive/modeld/modeld.cc
  3. 9
      selfdrive/ui/qt/home.cc
  4. 3
      selfdrive/ui/qt/home.hpp

@ -161,3 +161,19 @@ struct unique_fd {
operator int() const { return fd_; } operator int() const { return fd_; }
int fd_; int fd_;
}; };
class FirstOrderFilter {
public:
FirstOrderFilter(float x0, float ts, float dt) {
k_ = (dt / ts) / (1.0 + dt / ts);
x_ = x0;
}
inline float update(float x) {
x_ = (1. - k_) * x_ + k_ * x;
return x_;
}
inline void reset(float x) { x_ = x; }
private:
float x_, k_;
};

@ -72,10 +72,7 @@ void run_model(ModelState &model, VisionIpcClient &vipc_client) {
SubMaster sm({"lateralPlan", "roadCameraState"}); SubMaster sm({"lateralPlan", "roadCameraState"});
// setup filter to track dropped frames // setup filter to track dropped frames
const float dt = 1. / MODEL_FREQ; FirstOrderFilter frame_dropped_filter(0., 10., 1. / MODEL_FREQ);
const float ts = 10.0; // filter time constant (s)
const float frame_filter_k = (dt / ts) / (1. + dt / ts);
float frames_dropped = 0;
uint32_t frame_id = 0, last_vipc_frame_id = 0; uint32_t frame_id = 0, last_vipc_frame_id = 0;
double last = 0; double last = 0;
@ -114,8 +111,12 @@ void run_model(ModelState &model, VisionIpcClient &vipc_client) {
// tracked dropped frames // tracked dropped frames
uint32_t vipc_dropped_frames = extra.frame_id - last_vipc_frame_id - 1; uint32_t vipc_dropped_frames = extra.frame_id - last_vipc_frame_id - 1;
frames_dropped = (1. - frame_filter_k) * frames_dropped + frame_filter_k * (float)std::min(vipc_dropped_frames, 10U); float frames_dropped = frame_dropped_filter.update((float)std::min(vipc_dropped_frames, 10U));
if (run_count < 10) frames_dropped = 0; // let frame drops warm up if (run_count < 10) { // let frame drops warm up
frame_dropped_filter.reset(0);
frames_dropped = 0.;
}
float frame_drop_ratio = frames_dropped / (1 + frames_dropped); float frame_drop_ratio = frames_dropped / (1 + frames_dropped);
model_publish(pm, extra.frame_id, frame_id, frame_drop_ratio, model_buf, extra.timestamp_eof, model_execution_time, model_publish(pm, extra.frame_id, frame_id, frame_drop_ratio, model_buf, extra.timestamp_eof, model_execution_time,

@ -223,7 +223,7 @@ static void handle_display_state(UIState* s, bool user_input) {
} }
} }
GLWindow::GLWindow(QWidget* parent) : QOpenGLWidget(parent) { GLWindow::GLWindow(QWidget* parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKLIGHT_TS, BACKLIGHT_DT), QOpenGLWidget(parent) {
timer = new QTimer(this); timer = new QTimer(this);
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate())); QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timerUpdate()));
@ -236,7 +236,6 @@ GLWindow::GLWindow(QWidget* parent) : QOpenGLWidget(parent) {
brightness_b = 10.0; brightness_b = 10.0;
brightness_m = 0.1; brightness_m = 0.1;
} }
smooth_brightness = BACKLIGHT_OFFROAD;
} }
GLWindow::~GLWindow() { GLWindow::~GLWindow() {
@ -265,16 +264,12 @@ void GLWindow::initializeGL() {
void GLWindow::backlightUpdate() { void GLWindow::backlightUpdate() {
// Update brightness // Update brightness
float k = (BACKLIGHT_DT / BACKLIGHT_TS) / (1.0f + BACKLIGHT_DT / BACKLIGHT_TS);
float clipped_brightness = std::min(100.0f, (ui_state.scene.light_sensor * brightness_m) + brightness_b); float clipped_brightness = std::min(100.0f, (ui_state.scene.light_sensor * brightness_m) + brightness_b);
if (!ui_state.scene.started) { if (!ui_state.scene.started) {
clipped_brightness = BACKLIGHT_OFFROAD; clipped_brightness = BACKLIGHT_OFFROAD;
} }
smooth_brightness = clipped_brightness * k + smooth_brightness * (1.0f - k); int brightness = brightness_filter.update(clipped_brightness);
int brightness = smooth_brightness;
if (!ui_state.awake) { if (!ui_state.awake) {
brightness = 0; brightness = 0;
emit screen_shutoff(); emit screen_shutoff();

@ -11,6 +11,7 @@
#include "sound.hpp" #include "sound.hpp"
#include "ui/ui.hpp" #include "ui/ui.hpp"
#include "common/util.h"
#include "widgets/offroad_alerts.hpp" #include "widgets/offroad_alerts.hpp"
// container window for onroad NVG UI // container window for onroad NVG UI
@ -46,8 +47,8 @@ private:
// TODO: make a nice abstraction to handle embedded device stuff // TODO: make a nice abstraction to handle embedded device stuff
float brightness_b = 0; float brightness_b = 0;
float brightness_m = 0; float brightness_m = 0;
float smooth_brightness = 0;
float last_brightness = 0; float last_brightness = 0;
FirstOrderFilter brightness_filter;
public slots: public slots:
void timerUpdate(); void timerUpdate();

Loading…
Cancel
Save