2020-09-16 16:22:03 +00:00
|
|
|
#include "NMEAGPS.h"
|
2020-05-05 00:39:57 +00:00
|
|
|
#include "configuration.h"
|
2020-10-07 23:28:57 +00:00
|
|
|
#include "RTC.h"
|
2020-05-05 00:39:57 +00:00
|
|
|
|
|
|
|
static int32_t toDegInt(RawDegrees d)
|
|
|
|
{
|
|
|
|
int32_t degMult = 10000000; // 1e7
|
|
|
|
int32_t r = d.deg * degMult + d.billionths / 100;
|
|
|
|
if (d.negative)
|
|
|
|
r *= -1;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:29:26 +00:00
|
|
|
bool NMEAGPS::setupGPS()
|
2020-09-25 16:14:00 +00:00
|
|
|
{
|
2020-09-26 16:40:48 +00:00
|
|
|
#ifdef PIN_GPS_PPS
|
|
|
|
// pulse per second
|
|
|
|
// FIXME - move into shared GPS code
|
|
|
|
pinMode(PIN_GPS_PPS, INPUT);
|
|
|
|
#endif
|
|
|
|
|
2020-09-25 16:14:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
/**
|
|
|
|
* Perform any processing that should be done only while the GPS is awake and looking for a fix.
|
|
|
|
* Override this method to check for new locations
|
|
|
|
*
|
|
|
|
* @return true if we've acquired a new location
|
|
|
|
*/
|
|
|
|
bool NMEAGPS::lookForTime()
|
2020-05-05 00:39:57 +00:00
|
|
|
{
|
2020-10-01 16:11:54 +00:00
|
|
|
auto ti = reader.time;
|
|
|
|
auto d = reader.date;
|
|
|
|
if (ti.isUpdated() && ti.isValid() && d.isValid()) {
|
|
|
|
/* Convert to unix time
|
|
|
|
The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970
|
|
|
|
(midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
|
|
|
|
*/
|
|
|
|
struct tm t;
|
|
|
|
t.tm_sec = ti.second();
|
|
|
|
t.tm_min = ti.minute();
|
|
|
|
t.tm_hour = ti.hour();
|
|
|
|
t.tm_mday = d.day();
|
|
|
|
t.tm_mon = d.month() - 1;
|
|
|
|
t.tm_year = d.year() - 1900;
|
|
|
|
t.tm_isdst = false;
|
2020-10-07 23:46:20 +00:00
|
|
|
perhapsSetRTC(RTCQualityGPS, t);
|
2020-09-26 19:12:50 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
/**
|
|
|
|
* Perform any processing that should be done only while the GPS is awake and looking for a fix.
|
|
|
|
* Override this method to check for new locations
|
|
|
|
*
|
|
|
|
* @return true if we've acquired a new location
|
|
|
|
*/
|
|
|
|
bool NMEAGPS::lookForLocation()
|
|
|
|
{
|
|
|
|
bool foundLocation = false;
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
// uint8_t fixtype = reader.fixQuality();
|
|
|
|
// hasValidLocation = ((fixtype >= 1) && (fixtype <= 5));
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
if (reader.location.isUpdated()) {
|
|
|
|
if (reader.altitude.isValid())
|
|
|
|
altitude = reader.altitude.meters();
|
2020-09-16 16:18:44 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
if (reader.location.isValid()) {
|
|
|
|
auto loc = reader.location.value();
|
|
|
|
latitude = toDegInt(loc.lat);
|
|
|
|
longitude = toDegInt(loc.lng);
|
|
|
|
foundLocation = true;
|
|
|
|
}
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
// Diminution of precision (an accuracy metric) is reported in 10^2 units, so we need to scale down when we use it
|
|
|
|
if (reader.hdop.isValid()) {
|
|
|
|
dop = reader.hdop.value();
|
|
|
|
}
|
|
|
|
if (reader.course.isValid()) {
|
|
|
|
heading = reader.course.value() * 1e3; // Scale the heading (in degrees * 10^-2) to match the expected degrees * 10^-5
|
|
|
|
}
|
|
|
|
if (reader.satellites.isValid()) {
|
2020-10-10 01:20:38 +00:00
|
|
|
setNumSatellites(reader.satellites.value());
|
2020-10-01 16:11:54 +00:00
|
|
|
}
|
2020-05-05 03:02:43 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
// expect gps pos lat=37.520825, lon=-122.309162, alt=158
|
|
|
|
DEBUG_MSG("new NMEA GPS pos lat=%f, lon=%f, alt=%d, hdop=%g, heading=%f\n", latitude * 1e-7, longitude * 1e-7, altitude,
|
|
|
|
dop * 1e-2, heading * 1e-5);
|
|
|
|
}
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
return foundLocation;
|
|
|
|
}
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
bool NMEAGPS::whileIdle()
|
|
|
|
{
|
|
|
|
bool isValid = false;
|
2020-06-29 01:17:52 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
// First consume any chars that have piled up at the receiver
|
|
|
|
while (_serial_gps->available() > 0) {
|
|
|
|
int c = _serial_gps->read();
|
|
|
|
// DEBUG_MSG("%c", c);
|
|
|
|
isValid |= reader.encode(c);
|
2020-05-05 00:39:57 +00:00
|
|
|
}
|
2020-10-01 16:11:54 +00:00
|
|
|
|
|
|
|
return isValid;
|
|
|
|
}
|