From 49735685464ca2f75e9894c4a6c6643ec636ddfe Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Sun, 28 Jul 2019 23:51:24 +0200 Subject: [PATCH] GPS fixes & code sanitization --- include/gpsread.h | 6 +++--- src/display.cpp | 24 +++++++++------------ src/gpsread.cpp | 52 ++++++++++++++++++++++++---------------------- src/irqhandler.cpp | 2 +- src/main.cpp | 5 +++++ src/timekeeper.cpp | 17 +++------------ 6 files changed, 49 insertions(+), 57 deletions(-) diff --git a/include/gpsread.h b/include/gpsread.h index d1452b37..17336afd 100644 --- a/include/gpsread.h +++ b/include/gpsread.h @@ -18,10 +18,10 @@ extern gpsStatus_t extern TaskHandle_t GpsTask; int gps_init(void); -void IRAM_ATTR gps_storetime(gpsStatus_t &gps_store); -void gps_storelocation(gpsStatus_t &gps_store); +void IRAM_ATTR gps_storetime(gpsStatus_t *gps_store); +void gps_storelocation(gpsStatus_t *gps_store); void gps_loop(void *pvParameters); -time_t get_gpstime(gpsStatus_t value); +time_t fetch_gpsTime(gpsStatus_t value); int gps_config(); #endif \ No newline at end of file diff --git a/src/display.cpp b/src/display.cpp index 01b1a1ba..d6cb3d3e 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -195,16 +195,13 @@ void draw_page(time_t t, uint8_t page) { // update GPS status (line 2) #if (HAS_GPS) - // have we ever got valid gps data? - if (gps.passedChecksum() > 0) { - u8x8.setCursor(9, 2); - if (!gps.location.isValid()) // if no fix then display Sats value inverse - { - u8x8.setInverseFont(1); - u8x8.printf("Sats:%.2d", gps.satellites.value()); - u8x8.setInverseFont(0); - } else - u8x8.printf("Sats:%.2d", gps.satellites.value()); + u8x8.setCursor(9, 2); + if (gps.location.age() < 1500) // if no fix then display Sats value inverse + u8x8.printf("Sats:%.2d", gps.satellites.value()); + else { + u8x8.setInverseFont(1); + u8x8.printf("Sats:%.2d", gps.satellites.value()); + u8x8.setInverseFont(0); } #endif @@ -252,12 +249,11 @@ void draw_page(time_t t, uint8_t page) { u8x8.setInverseFont(1); u8x8.printf("%c", timeState); u8x8.setInverseFont(0); - u8x8.printf(" %2d.%3s", day(t), printmonth[month(t)]); #else - u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t), - timeState, day(t), printmonth[month(t)]); + u8x8.printf("%02d:%02d:%02d%c", hour(t), minute(t), second(t), timeState); #endif // HAS_DCF77 || HAS_IF482 - + if (timeSource != _unsynced) + u8x8.printf(" %2d.%3s", day(t), printmonth[month(t)]); #else // update LoRa status display #if (HAS_LORA) u8x8.printf("%-16s", display_line6); diff --git a/src/gpsread.cpp b/src/gpsread.cpp index e5b86738..d746d127 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -66,53 +66,50 @@ int gps_config() { } // store current GPS location data in struct -void gps_storelocation(gpsStatus_t &gps_store) { - gps_store.latitude = (int32_t)(gps.location.lat() * 1e6); - gps_store.longitude = (int32_t)(gps.location.lng() * 1e6); - gps_store.satellites = (uint8_t)gps.satellites.value(); - gps_store.hdop = (uint16_t)gps.hdop.value(); - gps_store.altitude = (int16_t)gps.altitude.meters(); +void gps_storelocation(gpsStatus_t *gps_store) { + if (gps.location.isUpdated() && gps.location.isValid() && + (gps.time.age() < 1500)) { + gps_store->latitude = (int32_t)(gps.location.lat() * 1e6); + gps_store->longitude = (int32_t)(gps.location.lng() * 1e6); + gps_store->satellites = (uint8_t)gps.satellites.value(); + gps_store->hdop = (uint16_t)gps.hdop.value(); + gps_store->altitude = (int16_t)gps.altitude.meters(); + } } // store current GPS timedate in struct -void IRAM_ATTR gps_storetime(gpsStatus_t &gps_store) { +void IRAM_ATTR gps_storetime(gpsStatus_t *gps_store) { if (gps.time.isUpdated() && gps.date.isValid() && (gps.time.age() < 1000)) { // nmea telegram serial delay compensation; not sure if we need this? /* if (gps.time.age() > nmea_txDelay_ms) - gps_store.timedate.Second = gps.time.second() + 1; + gps_store->timedate.Second = gps.time.second() + 1; else - gps_store.timedate.Second = gps.time.second(); + gps_store->timedate.Second = gps.time.second(); */ - gps_store.timedate.Second = gps.time.second(); - gps_store.timedate.Minute = gps.time.minute(); - gps_store.timedate.Hour = gps.time.hour(); - gps_store.timedate.Day = gps.date.day(); - gps_store.timedate.Month = gps.date.month(); - gps_store.timedate.Year = + gps_store->timedate.Second = gps.time.second(); + gps_store->timedate.Minute = gps.time.minute(); + gps_store->timedate.Hour = gps.time.hour(); + gps_store->timedate.Day = gps.date.day(); + gps_store->timedate.Month = gps.date.month(); + gps_store->timedate.Year = CalendarYrToTm(gps.date.year()); // year offset from 1970 in microTime.h } else - gps_store.timedate = {0}; + gps_store->timedate = {0}; } // function to fetch current time from struct; note: this is costly -time_t get_gpstime(gpsStatus_t value) { +time_t fetch_gpsTime(gpsStatus_t value) { time_t t = timeIsValid(makeTime(value.timedate)); - - // show NMEA data in debug mode, useful for debugging GPS - ESP_LOGD( - TAG, - "GPS time: %d | GPS NMEA data: passed %d / failed: %d / with fix: %d", t, - gps.passedChecksum(), gps.failedChecksum(), gps.sentencesWithFix()); - + ESP_LOGD(TAG, "GPS time: %d", t); return t; -} // get_gpstime() +} // fetch_gpsTime() // GPS serial feed FreeRTos Task void gps_loop(void *pvParameters) { @@ -136,6 +133,11 @@ void gps_loop(void *pvParameters) { #endif } // if + // show NMEA data in verbose mode, useful for debugging GPS + ESP_LOGV(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d", + gps.passedChecksum(), gps.failedChecksum(), + gps.sentencesWithFix()); + delay(2); // yield to CPU } // end of infinite loop diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index fabda2f2..86177a31 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -48,7 +48,7 @@ void irqHandler(void *pvParameters) { // gps refresh buffer? #if (HAS_GPS) if (InterruptStatus & GPS_IRQ) - gps_storelocation(gps_status); + gps_storelocation(&gps_status); #endif // are cyclic tasks due? diff --git a/src/main.cpp b/src/main.cpp index a147707f..f97ca72d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -438,6 +438,11 @@ void setup() { #warning you did not specify a time source, time will not be synched #endif + // initialize gps time +#if (HAS_GPS) + gps_storetime(&gps_status); +#endif + #if (defined HAS_IF482 || defined HAS_DCF77) ESP_LOGI(TAG, "Starting Clock Controller..."); clock_init(); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 049e80aa..7cd1bbe0 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -18,10 +18,6 @@ const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; HardwareSerial IF482(2); // use UART #2 (#1 may be in use for serial GPS) #endif -#if (HAS_GPS) -static gpsStatus_t gps_pps_status; -#endif - Ticker timesyncer; void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } @@ -32,7 +28,7 @@ time_t timeProvider(void) { #if (HAS_GPS) // fetch recent time from last NMEA record - t = get_gpstime(gps_pps_status); + t = fetch_gpsTime(gps_status); if (t) { #ifdef HAS_RTC set_rtctime(t, do_mutex); // calibrate RTC @@ -44,7 +40,7 @@ time_t timeProvider(void) { } #endif -// no GPS -> fallback to RTC time while trying lora sync +// no time from GPS -> fallback to RTC time while trying lora sync #ifdef HAS_RTC t = get_rtctime(); if (t) { @@ -123,11 +119,6 @@ void timepulse_start(void) { timerAlarmEnable(ppsIRQ); #endif -// initialize gps time -#if (HAS_GPS) - gps_storetime(gps_pps_status); -#endif - // start cyclic time sync timeSync(); // init systime by RTC or GPS or LORA timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); @@ -142,9 +133,7 @@ void IRAM_ATTR CLOCKIRQ(void) { // store recent gps time, and try to get gps time if time is not synced #if (HAS_GPS) - gps_storetime(gps_pps_status); - if (timeSource == _unsynced) - timeSync(); + gps_storetime(&gps_status); #endif // advance wall clock, if we have