From a17ad80f105b82105d9e96fd5436b7b5879719bf Mon Sep 17 00:00:00 2001 From: Marco <49691247+marcoSchr@users.noreply.github.com> Date: Sun, 31 Mar 2024 12:46:42 +0200 Subject: [PATCH] GPS: using 16-bit integers for altitude and speed --- lib/minmea/include/minmea.h | 13 +++++++++++++ openrtx/include/core/gps.h | 4 ++-- openrtx/src/core/gps.c | 8 ++++---- openrtx/src/core/voicePromptUtils.c | 4 ++-- openrtx/src/ui/default/ui.c | 11 +++++------ openrtx/src/ui/default/ui_menu.c | 2 +- 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/lib/minmea/include/minmea.h b/lib/minmea/include/minmea.h index 187235d4..da9aa54c 100644 --- a/lib/minmea/include/minmea.h +++ b/lib/minmea/include/minmea.h @@ -258,6 +258,19 @@ static inline float minmea_tocoord(struct minmea_float *f) */ int minmea_tofixedpoint(struct minmea_float *f); + +/** + * Convert a fixed-point value to an integer value. + * Returns zero for "unknown" values. + */ +static inline int minmea_toint(struct minmea_float *f) +{ + if(f->scale == 0) + return 0; + + return f->value / f->scale; +} + #ifdef __cplusplus } #endif diff --git a/openrtx/include/core/gps.h b/openrtx/include/core/gps.h index c681d720..448058d3 100644 --- a/openrtx/include/core/gps.h +++ b/openrtx/include/core/gps.h @@ -50,8 +50,8 @@ typedef struct uint32_t active_sats; // Bitmap representing which sats are part of the fix int32_t latitude; // Latitude coordinates int32_t longitude; // Longitude coordinates - float altitude; // Antenna altitude above mean sea level (geoid) in m - float speed; // Ground speed in km/h + int16_t altitude; // Antenna altitude above mean sea level (geoid) in m + uint16_t speed; // Ground speed in km/h float tmg_mag; // Course over ground, degrees, magnetic float tmg_true; // Course over ground, degrees, true } diff --git a/openrtx/src/core/gps.c b/openrtx/src/core/gps.c index 1b16d15c..af4ad891 100644 --- a/openrtx/src/core/gps.c +++ b/openrtx/src/core/gps.c @@ -26,7 +26,7 @@ #include #include -#define KNOTS2KMH 1.852f +#define KNOTS2KMH(x) ((((int) x) * 1852) / 1000) static char sentence[2*MINMEA_MAX_LENGTH]; static bool gpsEnabled = false; @@ -102,7 +102,7 @@ void gps_task() } gps_data.tmg_true = minmea_tofloat(&frame.course); - gps_data.speed = minmea_tofloat(&frame.speed) * KNOTS2KMH; + gps_data.speed = KNOTS2KMH(minmea_toint(&frame.speed)); } break; @@ -113,7 +113,7 @@ void gps_task() { gps_data.fix_quality = frame.fix_quality; gps_data.satellites_tracked = frame.satellites_tracked; - gps_data.altitude = minmea_tofloat(&frame.altitude); + gps_data.altitude = minmea_toint(&frame.altitude); } } break; @@ -166,7 +166,7 @@ void gps_task() struct minmea_sentence_vtg frame; if (minmea_parse_vtg(&frame, sentence)) { - gps_data.speed = minmea_tofloat(&frame.speed_kph); + gps_data.speed = minmea_toint(&frame.speed_kph); gps_data.tmg_mag = minmea_tofloat(&frame.magnetic_track_degrees); gps_data.tmg_true = minmea_tofloat(&frame.true_track_degrees); } diff --git a/openrtx/src/core/voicePromptUtils.c b/openrtx/src/core/voicePromptUtils.c index 1982962a..f18b7d9b 100644 --- a/openrtx/src/core/voicePromptUtils.c +++ b/openrtx/src/core/voicePromptUtils.c @@ -673,7 +673,7 @@ void vp_announceGPSInfo(vpGPSInfoFlags_t gpsInfoFlags) if ((gpsInfoFlags & vpGPSSpeed) != 0) { // speed/altitude: - sniprintf(buffer, 16, "%4.1fkm/h", state.gps_data.speed); + sniprintf(buffer, 16, "%dkm/h", state.gps_data.speed); vp_queuePrompt(PROMPT_SPEED); vp_queueString(buffer, vpAnnounceCommonSymbols | vpAnnounceLessCommonSymbols); @@ -683,7 +683,7 @@ void vp_announceGPSInfo(vpGPSInfoFlags_t gpsInfoFlags) { vp_queuePrompt(PROMPT_ALTITUDE); - sniprintf(buffer, 16, "%4.1fm", state.gps_data.altitude); + sniprintf(buffer, 16, "%dm", state.gps_data.altitude); vp_queueString(buffer, vpAnnounceCommonSymbols); addSilenceIfNeeded(flags); } diff --git a/openrtx/src/ui/default/ui.c b/openrtx/src/ui/default/ui.c index 934db42f..e219a684 100644 --- a/openrtx/src/ui/default/ui.c +++ b/openrtx/src/ui/default/ui.c @@ -63,6 +63,7 @@ #include #include +#include #include #include #include @@ -1269,8 +1270,8 @@ void ui_saveState() } #ifdef CONFIG_GPS -static float priorGPSSpeed = 0; -static float priorGPSAltitude = 0; +static uint16_t priorGPSSpeed = 0; +static int16_t priorGPSAltitude = 0; static float priorGPSDirection = 500; // impossible value init. static uint8_t priorGPSFixQuality= 0; static uint8_t priorGPSFixType = 0; @@ -1300,15 +1301,13 @@ static vpGPSInfoFlags_t GetGPSDirectionOrSpeedChanged() priorGPSFixType = state.gps_data.fix_type; } - float speedDiff=fabs(state.gps_data.speed - priorGPSSpeed); - if (speedDiff >= 1) + if (state.gps_data.speed != priorGPSSpeed) { whatChanged |= vpGPSSpeed; priorGPSSpeed = state.gps_data.speed; } - float altitudeDiff = fabs(state.gps_data.altitude - priorGPSAltitude); - if (altitudeDiff >= 5) + if (state.gps_data.altitude != priorGPSAltitude) { whatChanged |= vpGPSAltitude; priorGPSAltitude = state.gps_data.altitude; diff --git a/openrtx/src/ui/default/ui_menu.c b/openrtx/src/ui/default/ui_menu.c index 2908d932..d1a425af 100644 --- a/openrtx/src/ui/default/ui_menu.c +++ b/openrtx/src/ui/default/ui_menu.c @@ -707,7 +707,7 @@ void _ui_drawMenuGPS() gfx_print(layout.line2_pos, layout.top_font, TEXT_ALIGN_RIGHT, color_white, "%d.%.6d", longitude_int, longitude_dec); gfx_print(layout.bottom_pos, layout.bottom_font, TEXT_ALIGN_CENTER, - color_white, "S %4.1fkm/h A %4.1fm", + color_white, "S %dkm/h A %dm", last_state.gps_data.speed, last_state.gps_data.altitude); }