util.h: add unit conversion constants (#22813)

* Add unit conversion constants.

* move to common/util.h
old-commit-hash: e29cc1c2ee7b63c71a3a46d900a8f755bd0048a0
This commit is contained in:
Dean Lee
2021-11-08 17:19:34 +08:00
committed by GitHub
parent f30ffe16a2
commit 6e027a8aa6
5 changed files with 12 additions and 9 deletions
+7
View File
@@ -30,6 +30,13 @@
typedef void (*sighandler_t)(int sig);
#endif
const double MILE_TO_KM = 1.609344;
const double KM_TO_MILE = 1. / MILE_TO_KM;
const double MS_TO_KPH = 3.6;
const double MS_TO_MPH = MS_TO_KPH * KM_TO_MILE;
const double METER_TO_MILE = KM_TO_MILE / 1000.0;
const double METER_TO_FOOT = 3.28084;
void set_thread_name(const char* name);
int set_realtime_priority(int level);
+2 -2
View File
@@ -155,7 +155,7 @@ static void ui_draw_vision_maxspeed(UIState *s) {
const int SET_SPEED_NA = 255;
float maxspeed = (*s->sm)["controlsState"].getControlsState().getVCruise();
const bool is_cruise_set = maxspeed != 0 && maxspeed != SET_SPEED_NA;
if (is_cruise_set && !s->scene.is_metric) { maxspeed *= 0.6225; }
if (is_cruise_set && !s->scene.is_metric) { maxspeed *= KM_TO_MILE; }
const Rect rect = {bdr_s * 2, int(bdr_s * 1.5), 184, 202};
ui_fill_rect(s->vg, rect, COLOR_BLACK_ALPHA(100), 30.);
@@ -172,7 +172,7 @@ static void ui_draw_vision_maxspeed(UIState *s) {
}
static void ui_draw_vision_speed(UIState *s) {
const float speed = std::max(0.0, (*s->sm)["carState"].getCarState().getVEgo() * (s->scene.is_metric ? 3.6 : 2.2369363));
const float speed = std::max(0.0, (*s->sm)["carState"].getCarState().getVEgo() * (s->scene.is_metric ? MS_TO_KPH : MS_TO_MPH));
const std::string speed_str = std::to_string((int)std::nearbyint(speed));
nvgTextAlign(s->vg, NVG_ALIGN_CENTER | NVG_ALIGN_BASELINE);
ui_draw_text(s, s->fb_w/2, 210, speed_str.c_str(), 96 * 2.5, COLOR_WHITE, "sans-bold");
+3 -3
View File
@@ -381,8 +381,8 @@ void MapInstructions::updateDistance(float d) {
distance_str += " m";
}
} else {
float miles = d * METER_2_MILE;
float feet = d * METER_2_FOOT;
float miles = d * METER_TO_MILE;
float feet = d * METER_TO_FOOT;
if (feet > 500) {
distance_str.setNum(miles, 'f', 1);
@@ -597,7 +597,7 @@ void MapETA::updateETA(float s, float s_typical, float d) {
num = d / 1000.0;
distance_unit->setText("km");
} else {
num = d * METER_2_MILE;
num = d * METER_TO_MILE;
distance_unit->setText("mi");
}
-2
View File
@@ -9,8 +9,6 @@
#include "common/transformations/orientation.hpp"
#include "cereal/messaging/messaging.h"
const float METER_2_MILE = 0.000621371;
const float METER_2_FOOT = 3.28084;
#define RAD2DEG(x) ((x) * 180.0 / M_PI)
QGeoCoordinate to_QGeoCoordinate(const QMapbox::Coordinate &in);
-2
View File
@@ -9,8 +9,6 @@
#include "selfdrive/ui/qt/request_repeater.h"
#include "selfdrive/ui/qt/util.h"
const double MILE_TO_KM = 1.60934;
static QLabel* newLabel(const QString& text, const QString &type) {
QLabel* label = new QLabel(text);
label->setProperty("type", type);