Merge pull request #352 from cyberman54/development

Fix PPS time sync start bug & improve gps time sync accuracy
This commit is contained in:
Verkehrsrot 2019-04-14 14:37:17 +02:00 committed by GitHub
commit ae471ecc9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 16 deletions

View File

@ -9,7 +9,7 @@
#include <Wire.h>
#endif
#define NMEA_FRAME_SIZE 80 // NEMA has a maxium of 80 bytes per record
#define NMEA_FRAME_SIZE 82 // NEMA has a maxium of 82 bytes per record
#define NMEA_BUFFERTIME 50 // 50ms safety time regardless
extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe

View File

@ -1,4 +1,4 @@
#if(HAS_GPS)
#if (HAS_GPS)
#include "globals.h"
@ -11,6 +11,9 @@ TaskHandle_t GpsTask;
#ifdef GPS_SERIAL
HardwareSerial GPS_Serial(1); // use UART #1
static TickType_t gps_txDelay = tx_Ticks(NMEA_FRAME_SIZE, GPS_SERIAL);
#else
static TickType_t gps_txDelay = 0;
#endif
// initialize and configure GPS
@ -77,22 +80,26 @@ void gps_read() {
time_t get_gpstime(void) {
// set time to wait for arrive next recent NMEA time record
static const uint32_t gpsDelay_ms = 500;
static const uint32_t gpsDelay_ms = 1000 - gps_txDelay / portTICK_PERIOD_MS;
time_t t = 0;
uint32_t time_age = gps.time.age();
if ((gps.time.age() < gpsDelay_ms) && gps.time.isValid() &&
gps.date.isValid() && gps.time.isUpdated()) {
gps.time.value(); // trigger isUpdated()
ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d",
gps.time.age(),
(gps.time.isValid() && gps.date.isValid()) ? "yes" : "no",
gps.time.second());
if ((time_age < gpsDelay_ms) && gps.time.isValid() && gps.date.isValid() &&
gps.time.isUpdated()) {
t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second());
if (time_age < (gpsDelay_ms / 2))
t--;
ESP_LOGD(TAG, "GPS time age: %dms", time_age);
#ifndef GPS_INT
// wait until top of second with millisecond precision
vTaskDelay(pdMS_TO_TICKS(1000 - time_age) - gps_txDelay);
#endif
}
return timeIsValid(t);
} // get_gpstime()

View File

@ -20,9 +20,7 @@ HardwareSerial IF482(2); // use UART #2 (#1 may be in use for serial GPS)
Ticker timesyncer;
void timeSync() {
xTaskNotifyFromISR(irqHandlerTask, TIMESYNC_IRQ, eSetBits, NULL);
}
void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
time_t timeProvider(void) {
@ -116,6 +114,7 @@ void timepulse_start(void) {
timerAttachInterrupt(ppsIRQ, &CLOCKIRQ, true);
timerAlarmEnable(ppsIRQ);
#endif
now(); // refresh sysTime to pps
// start cyclic time sync
timeSync(); // init systime by RTC or GPS or LORA
@ -213,7 +212,7 @@ void clock_init(void) {
void clock_loop(void *taskparameter) { // ClockTask
// caveat: don't use now() in this task, it will cause a race condition
// due to concurrent access to i2c bus for setting rtc via SyncProvider!
// due to concurrent access to i2c bus for setting rtc!
#define nextmin(t) (t + DCF77_FRAME_SIZE + 1) // next minute