2019-04-14 14:35:07 +02:00
|
|
|
#if (HAS_GPS)
|
2018-06-08 22:41:37 +02:00
|
|
|
|
|
|
|
#include "globals.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
2019-02-27 00:52:27 +01:00
|
|
|
static const char TAG[] = __FILE__;
|
2018-06-09 17:59:59 +02:00
|
|
|
|
2018-09-20 13:23:22 +02:00
|
|
|
TinyGPSPlus gps;
|
2018-07-19 21:53:56 +02:00
|
|
|
gpsStatus_t gps_status;
|
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 =
|
|
|
|
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
|
|
|
|
2018-11-25 16:05:30 +01:00
|
|
|
int ret = 1;
|
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;
|
|
|
|
}
|
|
|
|
|
2018-06-12 19:48:21 +02:00
|
|
|
#if defined GPS_SERIAL
|
2018-09-20 19:36:32 +02:00
|
|
|
GPS_Serial.begin(GPS_SERIAL);
|
2018-11-25 16:05:30 +01:00
|
|
|
ESP_LOGI(TAG, "Using serial GPS");
|
2018-10-20 09:58:53 +02:00
|
|
|
#elif defined GPS_I2C
|
|
|
|
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);
|
2018-09-20 19:36:32 +02:00
|
|
|
Wire.write(0x00); // dummy write
|
|
|
|
ret = Wire.endTransmission(); // check if chip is seen on i2c bus
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
ESP_LOGE(TAG,
|
|
|
|
"Quectel L76 GPS chip not found on i2c bus, bus error %d. "
|
|
|
|
"Stopping GPS-Task.",
|
|
|
|
ret);
|
2018-11-25 16:05:30 +01:00
|
|
|
ret = 0;
|
2018-09-20 19:36:32 +02:00
|
|
|
} else {
|
2018-11-25 16:05:30 +01:00
|
|
|
ESP_LOGI(TAG, "Quectel L76 GPS chip found");
|
2018-09-20 17:33:52 +02:00
|
|
|
}
|
2018-06-12 19:48:21 +02:00
|
|
|
#endif
|
|
|
|
|
2018-11-25 16:05:30 +01:00
|
|
|
return ret;
|
|
|
|
} // 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
|
|
|
|
|
|
|
|
/* to come */
|
|
|
|
|
|
|
|
#elif defined GPS_I2C
|
|
|
|
|
|
|
|
/* to come */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return rslt;
|
|
|
|
}
|
|
|
|
|
2019-04-15 12:57:55 +02:00
|
|
|
// 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();
|
2019-07-27 13:35:12 +02:00
|
|
|
gps_store.altitude = (int16_t)gps.altitude.meters();
|
2018-11-25 16:05:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-15 12:57:55 +02:00
|
|
|
// store current GPS timedate in struct
|
|
|
|
void IRAM_ATTR gps_storetime(gpsStatus_t &gps_store) {
|
|
|
|
|
2019-07-07 16:00:48 +02:00
|
|
|
if (gps.time.isUpdated() && gps.date.isValid() && (gps.time.age() < 1000)) {
|
2019-04-15 12:57:55 +02:00
|
|
|
|
2019-07-16 21:21:15 +02:00
|
|
|
// nmea telegram serial delay compensation; not sure if we need this?
|
2019-07-21 19:21:11 +02:00
|
|
|
/*
|
2019-07-16 21:21:15 +02:00
|
|
|
if (gps.time.age() > nmea_txDelay_ms)
|
|
|
|
gps_store.timedate.Second = gps.time.second() + 1;
|
|
|
|
else
|
|
|
|
gps_store.timedate.Second = gps.time.second();
|
2019-07-21 19:21:11 +02:00
|
|
|
*/
|
2019-07-07 16:00:48 +02:00
|
|
|
|
|
|
|
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();
|
2019-04-15 12:57:55 +02:00
|
|
|
gps_store.timedate.Year =
|
|
|
|
CalendarYrToTm(gps.date.year()); // year offset from 1970 in microTime.h
|
2019-07-07 16:00:48 +02:00
|
|
|
|
2019-04-15 12:57:55 +02:00
|
|
|
} else
|
|
|
|
gps_store.timedate = {0};
|
|
|
|
}
|
|
|
|
|
|
|
|
// function to fetch current time from struct; note: this is costly
|
|
|
|
time_t get_gpstime(gpsStatus_t value) {
|
|
|
|
|
|
|
|
time_t t = timeIsValid(makeTime(value.timedate));
|
|
|
|
|
2019-07-28 18:42:22 +02:00
|
|
|
// show NMEA data in debug mode, useful for debugging GPS
|
2019-07-07 16:00:48 +02:00
|
|
|
ESP_LOGD(
|
2019-04-15 12:57:55 +02:00
|
|
|
TAG,
|
|
|
|
"GPS time: %d | GPS NMEA data: passed %d / failed: %d / with fix: %d", t,
|
|
|
|
gps.passedChecksum(), gps.failedChecksum(), gps.sentencesWithFix());
|
2019-02-17 22:12:14 +01:00
|
|
|
|
2019-04-15 12:57:55 +02:00
|
|
|
return t;
|
2019-04-14 14:35:07 +02:00
|
|
|
|
2019-01-28 23:59:52 +01:00
|
|
|
} // get_gpstime()
|
|
|
|
|
2018-11-25 16:05:30 +01:00
|
|
|
// GPS serial feed FreeRTos Task
|
|
|
|
void gps_loop(void *pvParameters) {
|
|
|
|
|
|
|
|
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
|
|
|
|
|
2018-06-12 19:48:21 +02:00
|
|
|
while (1) {
|
|
|
|
|
2018-11-19 00:41:15 +01:00
|
|
|
if (cfg.payloadmask && GPS_DATA) {
|
2018-06-12 19:48:21 +02:00
|
|
|
#if defined 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
|
2018-11-19 00:41:15 +01:00
|
|
|
} // if
|
2018-06-12 19:48:21 +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
|
|
|
|
2018-06-08 22:41:37 +02:00
|
|
|
#endif // HAS_GPS
|