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
|
|
|
|
2018-09-20 13:23:22 +02:00
|
|
|
TinyGPSPlus gps;
|
2018-10-03 16:24:45 +02:00
|
|
|
TaskHandle_t GpsTask;
|
2019-01-26 12:32:58 +01:00
|
|
|
HardwareSerial GPS_Serial(1); // use UART #1
|
2018-06-09 13:18:59 +02:00
|
|
|
|
2022-02-13 15:41:42 +01:00
|
|
|
// Ublox UBX packet data
|
|
|
|
|
|
|
|
// UBX CFG-PRT packet
|
|
|
|
byte CFG_PRT[] = {
|
|
|
|
0xB5, // sync char 1
|
|
|
|
0x62, // sync char 2
|
|
|
|
0x06, // class
|
|
|
|
0x00, // id
|
|
|
|
0x14, // length
|
|
|
|
0x00, // .
|
|
|
|
0x01, // portID (UART 1)
|
|
|
|
0x00, // reserved
|
|
|
|
0x00, // txReady
|
|
|
|
0x00, // .
|
|
|
|
0b11010000, // UART mode: 8N1
|
|
|
|
0b00001000, // .
|
|
|
|
0x00, // .
|
|
|
|
0x00, // .
|
|
|
|
(byte)GPS_BAUDRATE, // baudrate
|
|
|
|
(byte)(GPS_BAUDRATE >> 8), // .
|
|
|
|
(byte)(GPS_BAUDRATE >> 16), // .
|
|
|
|
(byte)(GPS_BAUDRATE >> 24), // .
|
|
|
|
0b00000011, // input protocols: NMEA + UBX
|
|
|
|
0b00000000, // .
|
|
|
|
0b00000010, // output protocols: NMEA
|
|
|
|
0x00000000, // .
|
|
|
|
0x00, // reserved
|
|
|
|
0x00, // .
|
|
|
|
0x00, // .
|
|
|
|
0x00 // .
|
|
|
|
};
|
|
|
|
|
|
|
|
// Array of two bytes for CFG-MSG packets payload.
|
2022-02-13 18:41:22 +01:00
|
|
|
byte CFG_MSG_CID[][2] = {{0xF0, 0x01}, {0xF0, 0x02}, {0xF0, 0x03}, {0xF0, 0x05},
|
|
|
|
{0xF0, 0x06}, {0xF0, 0x07}, {0xF0, 0x08}, {0xF0, 0x09},
|
|
|
|
{0xF0, 0x0A}, {0xF0, 0x0E}, {0xF1, 0x00}, {0xF1, 0x03},
|
|
|
|
{0xF1, 0x04}, {0xF1, 0x05}, {0xF1, 0x06}};
|
2022-02-13 15:41:42 +01:00
|
|
|
|
|
|
|
// UBX CFG-MSG packet
|
|
|
|
byte CFG_MSG[] = {
|
|
|
|
0xB5, // sync char 1
|
|
|
|
0x62, // sync char 2
|
|
|
|
0x06, // class
|
|
|
|
0x01, // id
|
|
|
|
0x03, // length
|
|
|
|
0x00, // .
|
|
|
|
0x00, // payload (first byte from messages array element)
|
|
|
|
0x00, // payload (second byte from messages array element)
|
|
|
|
0x00 // payload (zero to disable message)
|
|
|
|
};
|
|
|
|
|
|
|
|
// UBX CFG-CFG packet
|
|
|
|
byte CFG_CFG[] = {
|
|
|
|
0xB5, // sync char 1
|
|
|
|
0x62, // sync char 2
|
|
|
|
0x06, // class
|
|
|
|
0x09, // id
|
|
|
|
0x0D, // length
|
|
|
|
0x00, // .
|
|
|
|
0b00011111, // clearmask
|
|
|
|
0b00000110, // .
|
|
|
|
0x00, // .
|
|
|
|
0x00, // .
|
|
|
|
0x00, // savemask
|
|
|
|
0x00, // .
|
|
|
|
0x00, // .
|
|
|
|
0x00, // .
|
|
|
|
0b00011111, // loadmask
|
|
|
|
0b00000110, // .
|
|
|
|
0x00, // .
|
|
|
|
0x00, // .
|
|
|
|
0b00010001 // devicemask
|
|
|
|
};
|
|
|
|
|
2022-02-06 18:29:30 +01:00
|
|
|
// helper functions to send UBX commands to ublox gps chip
|
|
|
|
|
|
|
|
void sendPacket(byte *packet, byte len) {
|
|
|
|
|
|
|
|
uint8_t CK_A = 0;
|
|
|
|
uint8_t CK_B = 0;
|
|
|
|
|
2022-02-06 18:33:17 +01:00
|
|
|
for (int i = 0; i < len; i++)
|
2022-02-06 18:29:30 +01:00
|
|
|
GPS_Serial.write(packet[i]);
|
|
|
|
|
2022-02-06 18:33:17 +01:00
|
|
|
// calculate and send Fletcher checksum
|
2022-02-06 18:29:30 +01:00
|
|
|
for (int i = 2; i < len; i++) {
|
|
|
|
CK_A += packet[i];
|
|
|
|
CK_B += CK_A;
|
|
|
|
}
|
|
|
|
GPS_Serial.write(CK_A);
|
|
|
|
GPS_Serial.write(CK_B);
|
|
|
|
}
|
|
|
|
|
2022-02-13 15:41:42 +01:00
|
|
|
void restoreDefaults() { sendPacket(CFG_CFG, sizeof(CFG_CFG)); }
|
|
|
|
void changeBaudrate() { sendPacket(CFG_PRT, sizeof(CFG_PRT)); }
|
2022-02-12 16:26:41 +01:00
|
|
|
|
2022-02-06 18:29:30 +01:00
|
|
|
void disableNmea() {
|
|
|
|
|
2022-02-12 18:15:19 +01:00
|
|
|
// tinygps++ processes only $GPGGA/$GNGGA and $GPRMC/$GNRMC
|
2022-02-12 16:26:41 +01:00
|
|
|
// thus, we disable all other NMEA messages
|
2022-02-06 18:29:30 +01:00
|
|
|
|
2022-02-13 15:41:42 +01:00
|
|
|
byte packetSize = sizeof(CFG_MSG);
|
2022-02-06 18:29:30 +01:00
|
|
|
|
|
|
|
// Offset to the place where payload starts.
|
|
|
|
byte payloadOffset = 6;
|
|
|
|
|
|
|
|
// Iterate over the messages array.
|
2022-02-13 15:41:42 +01:00
|
|
|
for (byte i = 0; i < sizeof(CFG_MSG_CID) / sizeof(*CFG_MSG_CID); i++) {
|
2022-02-06 18:29:30 +01:00
|
|
|
// Copy two bytes of payload to the packet buffer.
|
2022-02-13 15:41:42 +01:00
|
|
|
for (byte j = 0; j < sizeof(*CFG_MSG_CID); j++) {
|
|
|
|
CFG_MSG[payloadOffset + j] = CFG_MSG_CID[i][j];
|
2022-02-06 18:29:30 +01:00
|
|
|
}
|
2022-02-13 15:41:42 +01:00
|
|
|
sendPacket(CFG_MSG, packetSize);
|
2022-02-06 18:29:30 +01: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
|
|
|
|
2021-01-07 22:07:55 +01:00
|
|
|
ESP_LOGI(TAG, "Opening serial GPS");
|
2022-02-12 16:26:41 +01:00
|
|
|
|
2018-09-20 19:36:32 +02:00
|
|
|
GPS_Serial.begin(GPS_SERIAL);
|
2022-02-12 16:26:41 +01:00
|
|
|
|
2022-02-06 20:23:34 +01:00
|
|
|
restoreDefaults();
|
2022-02-12 16:26:41 +01:00
|
|
|
delay(100);
|
2022-02-06 20:23:34 +01:00
|
|
|
|
2022-02-13 15:41:42 +01:00
|
|
|
changeBaudrate();
|
2022-02-06 18:29:30 +01:00
|
|
|
delay(100);
|
|
|
|
GPS_Serial.flush();
|
|
|
|
GPS_Serial.updateBaudRate(GPS_BAUDRATE);
|
|
|
|
|
|
|
|
disableNmea();
|
2019-02-09 13:02:38 +01:00
|
|
|
|
2022-02-06 18:29:30 +01:00
|
|
|
return 1;
|
2019-02-09 13:02:38 +01:00
|
|
|
|
2022-02-06 18:29:30 +01:00
|
|
|
} // gps_init()
|
2019-02-09 13:02:38 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
// function to poll UTC time from GPS NMEA data; note: this is costly
|
2022-02-13 15:41:42 +01:00
|
|
|
time_t get_gpstime(uint16_t *msec = 0) {
|
2019-08-04 15:17:50 +02:00
|
|
|
|
2022-02-13 15:41:42 +01:00
|
|
|
const uint16_t txDelay =
|
2022-02-13 18:41:22 +01:00
|
|
|
70U * 1000 / (GPS_BAUDRATE / 9); // serial tx of 70 NMEA chars
|
2019-08-04 15:17:50 +02:00
|
|
|
|
2020-10-04 23:13:17 +02:00
|
|
|
// did we get a current date & time?
|
2022-02-13 15:41:42 +01:00
|
|
|
if (gps.time.age() < 1000) {
|
2020-10-05 13:40:22 +02:00
|
|
|
|
2022-02-12 16:26:41 +01:00
|
|
|
// convert tinygps time format to struct tm format
|
2022-01-13 23:30:18 +01:00
|
|
|
struct tm gps_tm = {0};
|
2022-02-12 16:26:41 +01:00
|
|
|
gps_tm.tm_sec = gps.time.second();
|
|
|
|
gps_tm.tm_min = gps.time.minute();
|
|
|
|
gps_tm.tm_hour = gps.time.hour();
|
|
|
|
gps_tm.tm_mday = gps.date.day();
|
|
|
|
gps_tm.tm_mon = gps.date.month() - 1; // 1-12 -> 0-11
|
|
|
|
gps_tm.tm_year = gps.date.year() - 1900; // 2000+ -> years since 1900
|
2022-01-26 16:58:05 +01:00
|
|
|
|
2022-01-13 23:30:18 +01:00
|
|
|
// convert UTC tm to time_t epoch
|
2022-01-26 16:58:05 +01:00
|
|
|
gps_tm.tm_isdst = 0; // UTC has no DST
|
2022-01-13 23:30:18 +01:00
|
|
|
time_t t = mkgmtime(&gps_tm);
|
2020-10-05 13:40:22 +02:00
|
|
|
|
2022-02-12 16:26:41 +01:00
|
|
|
#ifdef GPS_INT
|
|
|
|
// if we have a recent GPS PPS pulse, sync on top of next second
|
2022-02-13 18:41:22 +01:00
|
|
|
uint16_t ppsDiff = millis() - lastPPS;
|
|
|
|
if (ppsDiff < 1000)
|
|
|
|
*msec = ppsDiff;
|
2022-02-12 16:26:41 +01:00
|
|
|
else {
|
|
|
|
ESP_LOGD(TAG, "no PPS from GPS");
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-12 18:15:19 +01:00
|
|
|
#else
|
2022-02-13 18:41:22 +01:00
|
|
|
// best guess for sync on top of next second
|
|
|
|
*msec = gps.time.centisecond() * 10U + txDelay;
|
2022-02-12 16:26:41 +01:00
|
|
|
#endif
|
2020-10-05 13:40:22 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2022-02-12 18:15:19 +01:00
|
|
|
// feed GPS decoder with serial NMEA data from GPS device
|
2018-06-12 19:48:21 +02:00
|
|
|
while (1) {
|
2022-01-26 22:45:23 +01:00
|
|
|
while (cfg.payloadmask & GPS_DATA) {
|
2022-02-12 18:15:19 +01:00
|
|
|
|
|
|
|
while (GPS_Serial.available())
|
|
|
|
gps.encode(GPS_Serial.read());
|
|
|
|
|
2022-02-09 12:24:11 +01:00
|
|
|
delay(5);
|
2022-02-12 16:26:41 +01:00
|
|
|
}
|
2022-02-09 12:24:11 +01:00
|
|
|
delay(1000);
|
2022-02-12 16:26:41 +01:00
|
|
|
} // infinite while 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
|