Merge pull request #352 from cyberman54/development
Fix PPS time sync start bug & improve gps time sync accuracy
This commit is contained in:
commit
ae471ecc9e
@ -9,7 +9,7 @@
|
|||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#endif
|
#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
|
#define NMEA_BUFFERTIME 50 // 50ms safety time regardless
|
||||||
|
|
||||||
extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
|
extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#if(HAS_GPS)
|
#if (HAS_GPS)
|
||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
|
|
||||||
@ -11,6 +11,9 @@ TaskHandle_t GpsTask;
|
|||||||
|
|
||||||
#ifdef GPS_SERIAL
|
#ifdef GPS_SERIAL
|
||||||
HardwareSerial GPS_Serial(1); // use UART #1
|
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
|
#endif
|
||||||
|
|
||||||
// initialize and configure GPS
|
// initialize and configure GPS
|
||||||
@ -77,22 +80,26 @@ void gps_read() {
|
|||||||
time_t get_gpstime(void) {
|
time_t get_gpstime(void) {
|
||||||
|
|
||||||
// set time to wait for arrive next recent NMEA time record
|
// 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;
|
time_t t = 0;
|
||||||
|
uint32_t time_age = gps.time.age();
|
||||||
|
|
||||||
if ((gps.time.age() < gpsDelay_ms) && gps.time.isValid() &&
|
if ((time_age < gpsDelay_ms) && gps.time.isValid() && gps.date.isValid() &&
|
||||||
gps.date.isValid() && gps.time.isUpdated()) {
|
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());
|
|
||||||
|
|
||||||
t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(),
|
t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(),
|
||||||
gps.time.hour(), gps.time.minute(), gps.time.second());
|
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);
|
return timeIsValid(t);
|
||||||
} // get_gpstime()
|
} // get_gpstime()
|
||||||
|
@ -20,9 +20,7 @@ HardwareSerial IF482(2); // use UART #2 (#1 may be in use for serial GPS)
|
|||||||
|
|
||||||
Ticker timesyncer;
|
Ticker timesyncer;
|
||||||
|
|
||||||
void timeSync() {
|
void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
|
||||||
xTaskNotifyFromISR(irqHandlerTask, TIMESYNC_IRQ, eSetBits, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
time_t timeProvider(void) {
|
time_t timeProvider(void) {
|
||||||
|
|
||||||
@ -116,6 +114,7 @@ void timepulse_start(void) {
|
|||||||
timerAttachInterrupt(ppsIRQ, &CLOCKIRQ, true);
|
timerAttachInterrupt(ppsIRQ, &CLOCKIRQ, true);
|
||||||
timerAlarmEnable(ppsIRQ);
|
timerAlarmEnable(ppsIRQ);
|
||||||
#endif
|
#endif
|
||||||
|
now(); // refresh sysTime to pps
|
||||||
|
|
||||||
// start cyclic time sync
|
// start cyclic time sync
|
||||||
timeSync(); // init systime by RTC or GPS or LORA
|
timeSync(); // init systime by RTC or GPS or LORA
|
||||||
@ -213,7 +212,7 @@ void clock_init(void) {
|
|||||||
void clock_loop(void *taskparameter) { // ClockTask
|
void clock_loop(void *taskparameter) { // ClockTask
|
||||||
|
|
||||||
// caveat: don't use now() in this task, it will cause a race condition
|
// 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
|
#define nextmin(t) (t + DCF77_FRAME_SIZE + 1) // next minute
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user