GPS handling and timesync code refactored

This commit is contained in:
Verkehrsrot 2019-08-04 15:17:50 +02:00
parent 7284d7ce4d
commit 1efc9be6c3
8 changed files with 69 additions and 50 deletions

View File

@ -82,8 +82,6 @@ typedef struct {
uint8_t satellites; uint8_t satellites;
uint16_t hdop; uint16_t hdop;
int16_t altitude; int16_t altitude;
uint32_t time_age;
tmElements_t timedate;
} gpsStatus_t; } gpsStatus_t;
typedef struct { typedef struct {

View File

@ -10,18 +10,15 @@
#endif #endif
#define NMEA_FRAME_SIZE 82 // NEMA has a maxium of 82 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 extern TinyGPSPlus gps; // Make TinyGPS++ instance globally availabe
extern gpsStatus_t
gps_status; // Make struct for storing gps data globally available
extern TaskHandle_t GpsTask; extern TaskHandle_t GpsTask;
int gps_init(void); int gps_init(void);
void IRAM_ATTR gps_storetime(gpsStatus_t *gps_store); int gps_config();
void gps_storelocation(gpsStatus_t *gps_store); void gps_storelocation(gpsStatus_t *gps_store);
void gps_loop(void *pvParameters); void gps_loop(void *pvParameters);
time_t fetch_gpsTime(gpsStatus_t value, uint16_t *msec); time_t fetch_gpsTime(uint16_t *msec);
int gps_config(); time_t fetch_gpsTime(void);
#endif #endif

View File

@ -5,7 +5,12 @@
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
// we use NMEA $GPZDA sentence field 1 for time synchronization
// $GPZDA gives time for preceding pps pulse, but does not has a constant offset
TinyGPSPlus gps; TinyGPSPlus gps;
TinyGPSCustom gpstime(gps, "GPZDA", 1); // field 1 = UTC time
static const String ZDA_Request = "$EIGPQ,ZDA*39\r\n";
gpsStatus_t gps_status = {0}; gpsStatus_t gps_status = {0};
TaskHandle_t GpsTask; TaskHandle_t GpsTask;
@ -27,7 +32,7 @@ int gps_init(void) {
return 0; return 0;
} }
#if defined GPS_SERIAL #ifdef GPS_SERIAL
GPS_Serial.begin(GPS_SERIAL); GPS_Serial.begin(GPS_SERIAL);
ESP_LOGI(TAG, "Using serial GPS"); ESP_LOGI(TAG, "Using serial GPS");
#elif defined GPS_I2C #elif defined GPS_I2C
@ -55,11 +60,11 @@ int gps_config() {
int rslt = 1; // success int rslt = 1; // success
#if defined GPS_SERIAL #if defined GPS_SERIAL
/* to come */ /* insert user configuration here, if needed */
#elif defined GPS_I2C #elif defined GPS_I2C
/* to come */ /* insert user configuration here, if needed */
#endif #endif
return rslt; return rslt;
@ -68,7 +73,7 @@ int gps_config() {
// store current GPS location data in struct // store current GPS location data in struct
void gps_storelocation(gpsStatus_t *gps_store) { void gps_storelocation(gpsStatus_t *gps_store) {
if (gps.location.isUpdated() && gps.location.isValid() && if (gps.location.isUpdated() && gps.location.isValid() &&
(gps.time.age() < 1500)) { (gps.location.age() < 1500)) {
gps_store->latitude = (int32_t)(gps.location.lat() * 1e6); gps_store->latitude = (int32_t)(gps.location.lat() * 1e6);
gps_store->longitude = (int32_t)(gps.location.lng() * 1e6); gps_store->longitude = (int32_t)(gps.location.lng() * 1e6);
gps_store->satellites = (uint8_t)gps.satellites.value(); gps_store->satellites = (uint8_t)gps.satellites.value();
@ -77,34 +82,55 @@ void gps_storelocation(gpsStatus_t *gps_store) {
} }
} }
// store current GPS timedate in struct // function to fetch current time from struct; note: this is costly
void IRAM_ATTR gps_storetime(gpsStatus_t *gps_store) { time_t fetch_gpsTime(uint16_t *msec) {
if (gps.time.isUpdated() && gps.date.isValid() && (gps.time.age() < 1000)) { time_t time_sec = 0;
gps_store->time_age = gps.time.age() + nmea_txDelay_ms; // poll NMEA $GPZDA sentence
gps_store->timedate.Second = gps.time.second(); #ifdef GPS_SERIAL
gps_store->timedate.Minute = gps.time.minute(); GPS_Serial.print(ZDA_Request);
gps_store->timedate.Hour = gps.time.hour(); #elif defined GPS_I2C
gps_store->timedate.Day = gps.date.day(); Wire.print(ZDA_Request);
gps_store->timedate.Month = gps.date.month(); #endif
gps_store->timedate.Year =
// wait for gps NMEA answer
vTaskDelay(tx_Ticks(NMEA_FRAME_SIZE, GPS_SERIAL));
// did we get a current time?
if (gpstime.isUpdated() && gpstime.isValid()) {
tmElements_t tm;
String rawtime = gpstime.value();
uint32_t time_bcd = rawtime.toFloat() * 100;
uint32_t delay_ms = gpstime.age() + nmea_txDelay_ms;
uint8_t year =
CalendarYrToTm(gps.date.year()); // year offset from 1970 in microTime.h CalendarYrToTm(gps.date.year()); // year offset from 1970 in microTime.h
} else ESP_LOGD(TAG, "time [bcd]: %u", time_bcd);
gps_store->timedate = {0};
tm.Second = (time_bcd / 100) % 100; // second
tm.Minute = (time_bcd / 10000) % 100; // minute
tm.Hour = time_bcd / 1000000; // hour
tm.Day = gps.date.day(); // day
tm.Month = gps.date.month(); // month
tm.Year = year; // year
// add protocol delay to time with millisecond precision
time_sec = makeTime(tm) + delay_ms / 1000;
*msec = (delay_ms % 1000) ? delay_ms % 1000 : 0;
} }
// function to fetch current time from struct; note: this is costly return timeIsValid(time_sec);
time_t fetch_gpsTime(gpsStatus_t value, uint16_t *msec) {
*msec = 1000 - value.time_age;
time_t t = timeIsValid(makeTime(value.timedate));
ESP_LOGD(TAG, "GPS time: %d | time age: %d", t, value.time_age);
return t;
} // fetch_gpsTime() } // fetch_gpsTime()
time_t fetch_gpsTime(void) {
uint16_t msec;
return fetch_gpsTime(&msec);
}
// GPS serial feed FreeRTos Task // GPS serial feed FreeRTos Task
void gps_loop(void *pvParameters) { void gps_loop(void *pvParameters) {
@ -113,7 +139,7 @@ void gps_loop(void *pvParameters) {
while (1) { while (1) {
if (cfg.payloadmask && GPS_DATA) { if (cfg.payloadmask && GPS_DATA) {
#if defined GPS_SERIAL #ifdef GPS_SERIAL
// feed GPS decoder with serial NMEA data from GPS device // feed GPS decoder with serial NMEA data from GPS device
while (GPS_Serial.available()) { while (GPS_Serial.available()) {
gps.encode(GPS_Serial.read()); gps.encode(GPS_Serial.read());
@ -128,7 +154,7 @@ void gps_loop(void *pvParameters) {
} // if } // if
// show NMEA data in verbose mode, useful for debugging GPS // show NMEA data in verbose mode, useful for debugging GPS
ESP_LOGV(TAG, "GPS NMEA data: passed %d / failed: %d / with fix: %d", ESP_LOGV(TAG, "GPS NMEA data: passed %u / failed: %u / with fix: %u",
gps.passedChecksum(), gps.failedChecksum(), gps.passedChecksum(), gps.failedChecksum(),
gps.sentencesWithFix()); gps.sentencesWithFix());

View File

@ -421,7 +421,7 @@ void setup() {
// initialize gps time // initialize gps time
#if (HAS_GPS) #if (HAS_GPS)
gps_storetime(&gps_status); fetch_gpsTime();
#endif #endif
#if (defined HAS_IF482 || defined HAS_DCF77) #if (defined HAS_IF482 || defined HAS_DCF77)

View File

@ -253,6 +253,7 @@ void get_status(uint8_t val[]) {
void get_gps(uint8_t val[]) { void get_gps(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get gps status"); ESP_LOGI(TAG, "Remote command: get gps status");
#if (HAS_GPS) #if (HAS_GPS)
gpsStatus_t gps_status;
gps_storelocation(&gps_status); gps_storelocation(&gps_status);
payload.reset(); payload.reset();
payload.addGPS(gps_status); payload.addGPS(gps_status);

View File

@ -87,6 +87,7 @@ void sendCounter() {
case GPS_DATA: case GPS_DATA:
// send GPS position only if we have a fix // send GPS position only if we have a fix
if (gps.location.isValid()) { if (gps.location.isValid()) {
gpsStatus_t gps_status;
gps_storelocation(&gps_status); gps_storelocation(&gps_status);
payload.reset(); payload.reset();
payload.addGPS(gps_status); payload.addGPS(gps_status);

View File

@ -30,7 +30,7 @@ void calibrateTime(void) {
#if (HAS_GPS) #if (HAS_GPS)
// fetch recent time from last NMEA record // fetch recent time from last NMEA record
t = fetch_gpsTime(gps_status, &t_msec); t = fetch_gpsTime(&t_msec);
if (t) { if (t) {
timeSource = _gps; timeSource = _gps;
goto finish; goto finish;
@ -124,11 +124,6 @@ void IRAM_ATTR CLOCKIRQ(void) {
SyncToPPS(); // advance systime, see microTime.h SyncToPPS(); // advance systime, see microTime.h
// store recent gps time
#if (HAS_GPS)
gps_storetime(&gps_status);
#endif
// advance wall clock, if we have // advance wall clock, if we have
#if (defined HAS_IF482 || defined HAS_DCF77) #if (defined HAS_IF482 || defined HAS_DCF77)
xTaskNotifyFromISR(ClockTask, uint32_t(now()), eSetBits, xTaskNotifyFromISR(ClockTask, uint32_t(now()), eSetBits,

View File

@ -211,22 +211,22 @@ int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) {
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
timesource_t mytimesource) { timesource_t mytimesource) {
time_t time_to_set = (time_t)(t_sec); time_t time_to_set = (time_t)(t_sec + t_msec / 1000);
if (timeIsValid(time_to_set)) { if (timeIsValid(time_to_set)) {
ESP_LOGD(TAG, "[%0.3f] UTC epoch time: %d.%03d sec", millis() / 1000.0,
time_to_set, t_msec);
// wait until top of second with millisecond precision // wait until top of second with millisecond precision
if (t_msec) { if (t_msec % 1000) {
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec));
time_to_set++; time_to_set++;
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec % 1000));
} }
// if we got a timesource, set RTC time and calibrate RTC_INT pulse on top of ESP_LOGD(TAG, "[%0.3f] UTC epoch time: %d.%03d sec", millis() / 1000.0,
// second time_to_set, t_msec % 1000);
// if we got a timesource, set RTC time and RTC_INT pulse on top of second
#ifdef HAS_RTC #ifdef HAS_RTC
if (mytimesource != _rtc) if ((mytimesource == _gps) || (mytimesource == _lora))
set_rtctime(time_to_set); set_rtctime(time_to_set);
#endif #endif
@ -234,6 +234,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
#if (!defined GPS_INT && !defined RTC_INT) #if (!defined GPS_INT && !defined RTC_INT)
timerWrite(ppsIRQ, 0); // reset pps timer timerWrite(ppsIRQ, 0); // reset pps timer
CLOCKIRQ(); // fire clock pps, this advances time 1 sec CLOCKIRQ(); // fire clock pps, this advances time 1 sec
time_to_set--;
#endif #endif
setTime(time_to_set); // set the time on top of second setTime(time_to_set); // set the time on top of second
@ -249,8 +250,8 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
} }
} }
void timesync_init() {
// create task for timeserver handshake processing, called from main.cpp // create task for timeserver handshake processing, called from main.cpp
void timesync_init() {
xTaskCreatePinnedToCore(process_timesync_req, // task function xTaskCreatePinnedToCore(process_timesync_req, // task function
"timesync_req", // name of task "timesync_req", // name of task
2048, // stack size of task 2048, // stack size of task