2019-04-14 14:35:07 +02:00
|
|
|
#if (HAS_GPS)
|
2018-06-08 22:41:37 +02:00
|
|
|
|
|
|
|
#include "globals.h"
|
2020-03-29 18:08:52 +02:00
|
|
|
#include "gpsread.h"
|
2018-06-08 22:41:37 +02:00
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:52:27 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2018-06-09 17:59:59 +02:00
|
|
|
|
2020-09-29 17:27:52 +02:00
|
|
|
// we use NMEA ZDA sentence field 1 for time synchronization
|
|
|
|
// ZDA gives time for preceding pps pulse
|
|
|
|
// downsight is that it does not have a constant offset
|
|
|
|
// thus precision is only +/- 1 second
|
|
|
|
|
2018-09-20 13:23:22 +02:00
|
|
|
TinyGPSPlus gps;
|
2020-10-04 23:13:17 +02:00
|
|
|
TinyGPSCustom gpstime(gps, "GPZDA", 1); // field 1 = UTC time (hhmmss.ss)
|
|
|
|
TinyGPSCustom gpsday(gps, "GPZDA", 2); // field 2 = day (01..31)
|
|
|
|
TinyGPSCustom gpsmonth(gps, "GPZDA", 3); // field 3 = month (01..12)
|
|
|
|
TinyGPSCustom gpsyear(gps, "GPZDA", 4); // field 4 = year (4-digit)
|
2019-08-04 15:17:50 +02:00
|
|
|
static const String ZDA_Request = "$EIGPQ,ZDA*39\r\n";
|
2018-10-03 16:24:45 +02:00
|
|
|
TaskHandle_t GpsTask;
|
2018-07-19 21:53:56 +02:00
|
|
|
|
2018-11-25 16:05:30 +01:00
|
|
|
#ifdef GPS_SERIAL
|
2019-01-26 12:32:58 +01:00
|
|
|
HardwareSerial GPS_Serial(1); // use UART #1
|
2019-04-14 22:54:27 +02:00
|
|
|
static uint16_t nmea_txDelay_ms =
|
2019-08-03 14:01:25 +02:00
|
|
|
(tx_Ticks(NMEA_FRAME_SIZE, GPS_SERIAL) / portTICK_PERIOD_MS);
|
2019-04-14 14:35:07 +02:00
|
|
|
#else
|
2019-04-14 22:54:27 +02:00
|
|
|
static uint16_t nmea_txDelay_ms = 0;
|
2018-11-25 16:05:30 +01:00
|
|
|
#endif
|
2018-06-09 13:18:59 +02:00
|
|
|
|
2018-11-25 16:05:30 +01:00
|
|
|
// initialize and configure GPS
|
|
|
|
int gps_init(void) {
|
2018-06-12 19:48:21 +02:00
|
|
|
|
2019-02-09 13:02:38 +01:00
|
|
|
if (!gps_config()) {
|
|
|
|
ESP_LOGE(TAG, "GPS chip initializiation error");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-08-04 15:17:50 +02:00
|
|
|
#ifdef GPS_SERIAL
|
2021-01-07 22:07:55 +01:00
|
|
|
ESP_LOGI(TAG, "Opening serial GPS");
|
2018-09-20 19:36:32 +02:00
|
|
|
GPS_Serial.begin(GPS_SERIAL);
|
2018-10-20 09:58:53 +02:00
|
|
|
#elif defined GPS_I2C
|
2021-01-07 22:07:55 +01:00
|
|
|
ESP_LOGI(TAG, "Opening I2C GPS");
|
2018-10-20 09:58:53 +02:00
|
|
|
Wire.begin(GPS_I2C, 400000); // I2C connect to GPS device with 400 KHz
|
2018-09-20 17:33:52 +02:00
|
|
|
Wire.beginTransmission(GPS_ADDR);
|
2021-01-07 22:07:55 +01:00
|
|
|
Wire.write(0x00); // dummy write
|
|
|
|
if (Wire.endTransmission()) {
|
|
|
|
ESP_LOGE(TAG, "Quectel L76 GPS chip not found");
|
|
|
|
return 0;
|
|
|
|
} else
|
2018-11-25 16:05:30 +01:00
|
|
|
ESP_LOGI(TAG, "Quectel L76 GPS chip found");
|
2018-06-12 19:48:21 +02:00
|
|
|
#endif
|
|
|
|
|
2021-01-07 22:07:55 +01:00
|
|
|
return 1;
|
2018-11-25 16:05:30 +01:00
|
|
|
} // gps_init()
|
|
|
|
|
2019-02-09 13:02:38 +01:00
|
|
|
// detect gps chipset type and configure it with device specific settings
|
|
|
|
int gps_config() {
|
|
|
|
int rslt = 1; // success
|
|
|
|
#if defined GPS_SERIAL
|
|
|
|
|
2019-08-04 15:17:50 +02:00
|
|
|
/* insert user configuration here, if needed */
|
2019-02-09 13:02:38 +01:00
|
|
|
|
|
|
|
#elif defined GPS_I2C
|
|
|
|
|
2019-08-04 15:17:50 +02:00
|
|
|
/* insert user configuration here, if needed */
|
2019-02-09 13:02:38 +01:00
|
|
|
|
|
|
|
#endif
|
|
|
|
return rslt;
|
|
|
|
}
|
|
|
|
|
2019-04-15 12:57:55 +02:00
|
|
|
// store current GPS location data in struct
|
2019-07-28 23:51:24 +02:00
|
|
|
void gps_storelocation(gpsStatus_t *gps_store) {
|
|
|
|
if (gps.location.isUpdated() && gps.location.isValid() &&
|
2019-08-04 15:17:50 +02:00
|
|
|
(gps.location.age() < 1500)) {
|
2020-01-04 19:12:50 +01:00
|
|
|
gps_store->latitude = (int32_t)(gps.location.lat() * 1e6);
|
|
|
|
gps_store->longitude = (int32_t)(gps.location.lng() * 1e6);
|
2019-07-28 23:51:24 +02:00
|
|
|
gps_store->satellites = (uint8_t)gps.satellites.value();
|
|
|
|
gps_store->hdop = (uint16_t)gps.hdop.value();
|
|
|
|
gps_store->altitude = (int16_t)gps.altitude.meters();
|
|
|
|
}
|
2018-11-25 16:05:30 +01:00
|
|
|
}
|
|
|
|
|
2020-01-03 09:59:10 +01:00
|
|
|
bool gps_hasfix() {
|
|
|
|
// adapted from source:
|
|
|
|
// https://github.com/hottimuc/Lora-TTNMapper-T-Beam/blob/master/fromV08/gps.cpp
|
|
|
|
return (gps.location.isValid() && gps.location.age() < 4000 &&
|
|
|
|
gps.hdop.isValid() && gps.hdop.value() <= 600 &&
|
|
|
|
gps.hdop.age() < 4000 && gps.altitude.isValid() &&
|
|
|
|
gps.altitude.age() < 4000);
|
|
|
|
}
|
|
|
|
|
2020-10-04 23:17:39 +02:00
|
|
|
// function to poll current time from GPS data; note: this is costly
|
2020-03-11 23:47:33 +01:00
|
|
|
time_t get_gpstime(uint16_t *msec) {
|
2019-08-04 15:17:50 +02:00
|
|
|
|
2020-09-29 17:27:52 +02:00
|
|
|
// poll NMEA ZDA sentence
|
2019-08-04 15:17:50 +02:00
|
|
|
#ifdef GPS_SERIAL
|
|
|
|
GPS_Serial.print(ZDA_Request);
|
2019-08-18 17:44:42 +02:00
|
|
|
// wait for gps NMEA answer
|
2020-10-04 23:13:17 +02:00
|
|
|
// vTaskDelay(tx_Ticks(NMEA_FRAME_SIZE, GPS_SERIAL));
|
2019-08-04 15:17:50 +02:00
|
|
|
#elif defined GPS_I2C
|
|
|
|
Wire.print(ZDA_Request);
|
|
|
|
#endif
|
|
|
|
|
2020-10-04 23:13:17 +02:00
|
|
|
// did we get a current date & time?
|
2021-01-03 18:25:11 +01:00
|
|
|
if (gpstime.isValid()) {
|
2019-04-15 12:57:55 +02:00
|
|
|
|
2020-10-05 13:40:22 +02:00
|
|
|
time_t t = 0;
|
2019-08-04 15:17:50 +02:00
|
|
|
tmElements_t tm;
|
2019-08-18 17:44:42 +02:00
|
|
|
uint32_t delay_ms =
|
|
|
|
gpstime.age() + nmea_txDelay_ms + NMEA_COMPENSATION_FACTOR;
|
2020-10-05 13:40:22 +02:00
|
|
|
uint32_t zdatime = atof(gpstime.value());
|
|
|
|
|
|
|
|
// convert time to maketime format and make time
|
|
|
|
tm.Second = zdatime % 100; // second
|
|
|
|
tm.Minute = (zdatime / 100) % 100; // minute
|
|
|
|
tm.Hour = zdatime / 10000; // hour
|
|
|
|
tm.Day = atoi(gpsday.value()); // day
|
|
|
|
tm.Month = atoi(gpsmonth.value()); // month
|
|
|
|
tm.Year = CalendarYrToTm(atoi(gpsyear.value())); // year offset from 1970
|
|
|
|
t = makeTime(tm);
|
2019-04-15 12:57:55 +02:00
|
|
|
|
2021-01-30 17:54:34 +01:00
|
|
|
ESP_LOGD(TAG, "GPS time/date = %02d:%02d:%02d / %02d.%02d.%2d", tm.Hour,
|
2021-03-31 09:44:23 +02:00
|
|
|
tm.Minute, tm.Second, tm.Day, tm.Month, tm.Year + 1970);
|
2020-10-05 13:40:22 +02:00
|
|
|
|
|
|
|
// add protocol delay with millisecond precision
|
|
|
|
t += delay_ms / 1000 - 1; // whole seconds
|
|
|
|
*msec = delay_ms % 1000; // fractional seconds
|
|
|
|
|
|
|
|
return t;
|
2019-08-04 15:17:50 +02:00
|
|
|
}
|
2019-04-15 12:57:55 +02:00
|
|
|
|
2020-10-05 13:40:22 +02:00
|
|
|
ESP_LOGD(TAG, "no valid GPS time");
|
|
|
|
|
|
|
|
return 0;
|
2019-04-14 14:35:07 +02:00
|
|
|
|
2020-03-11 23:47:33 +01:00
|
|
|
} // get_gpstime()
|
2019-01-28 23:59:52 +01:00
|
|
|
|
2020-03-11 23:47:33 +01:00
|
|
|
time_t get_gpstime(void) {
|
2019-08-04 15:17:50 +02:00
|
|
|
uint16_t msec;
|
2020-03-11 23:47:33 +01:00
|
|
|
return get_gpstime(&msec);
|
2019-08-04 15:17:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-25 16:05:30 +01:00
|
|
|
// GPS serial feed FreeRTos Task
|
|
|
|
void gps_loop(void *pvParameters) {
|
|
|
|
|
2020-10-30 12:24:16 +01:00
|
|
|
_ASSERT((uint32_t)pvParameters == 1); // FreeRTOS check
|
2018-11-25 16:05:30 +01:00
|
|
|
|
2018-06-12 19:48:21 +02:00
|
|
|
while (1) {
|
|
|
|
|
2019-09-01 00:11:20 +02:00
|
|
|
if (cfg.payloadmask & GPS_DATA) {
|
2019-08-04 15:17:50 +02:00
|
|
|
#ifdef GPS_SERIAL
|
2018-09-20 19:36:32 +02:00
|
|
|
// feed GPS decoder with serial NMEA data from GPS device
|
|
|
|
while (GPS_Serial.available()) {
|
|
|
|
gps.encode(GPS_Serial.read());
|
2018-06-12 19:48:21 +02:00
|
|
|
}
|
2018-10-20 09:58:53 +02:00
|
|
|
#elif defined GPS_I2C
|
2018-09-20 19:36:32 +02:00
|
|
|
Wire.requestFrom(GPS_ADDR, 32); // caution: this is a blocking call
|
|
|
|
while (Wire.available()) {
|
|
|
|
gps.encode(Wire.read());
|
2018-12-19 12:32:25 +01:00
|
|
|
delay(2); // 2ms delay according L76 datasheet
|
2018-06-12 19:48:21 +02:00
|
|
|
}
|
2018-09-20 19:36:32 +02:00
|
|
|
#endif
|
2020-09-27 22:49:41 +02:00
|
|
|
|
2020-10-05 13:40:22 +02:00
|
|
|
// (only) while device time is not set or unsynched, and we have a valid
|
|
|
|
// GPS time, we trigger a device time update to poll time from GPS
|
2021-03-31 09:44:23 +02:00
|
|
|
if ((timeSource == _unsynced || timeSource == _set) &&
|
2021-04-07 21:21:06 +02:00
|
|
|
(gpstime.isUpdated() && gpstime.isValid() && gpstime.age() < 1000)) {
|
2020-10-09 22:45:44 +02:00
|
|
|
now();
|
2020-09-29 07:59:53 +02:00
|
|
|
calibrateTime();
|
2020-10-09 22:45:44 +02:00
|
|
|
}
|
2020-09-27 22:49:41 +02:00
|
|
|
|
2018-11-19 00:41:15 +01:00
|
|
|
} // if
|
2018-06-12 19:48:21 +02:00
|
|
|
|
2020-10-04 23:13:17 +02:00
|
|
|
// show NMEA data in verbose mode, useful only for debugging GPS, very noisy
|
2020-01-03 09:59:10 +01:00
|
|
|
// ESP_LOGV(TAG, "GPS NMEA data: passed %u / failed: %u / with fix: %u",
|
2019-09-23 15:39:58 +02:00
|
|
|
// gps.passedChecksum(), gps.failedChecksum(),
|
|
|
|
// gps.sentencesWithFix());
|
2019-07-28 23:51:24 +02:00
|
|
|
|
2018-12-19 12:32:25 +01:00
|
|
|
delay(2); // yield to CPU
|
2018-06-12 19:48:21 +02:00
|
|
|
|
|
|
|
} // end of infinite loop
|
2018-06-09 19:20:34 +02:00
|
|
|
|
|
|
|
} // gps_loop()
|
2018-06-12 19:48:21 +02:00
|
|
|
|
2021-01-03 18:25:11 +01:00
|
|
|
#endif // HAS_GPS
|