2020-02-06 15:39:21 +00:00
|
|
|
|
|
|
|
#include "GPS.h"
|
2020-02-19 15:58:51 +00:00
|
|
|
#include "time.h"
|
|
|
|
#include <sys/time.h>
|
2020-02-23 01:40:31 +00:00
|
|
|
#include "configuration.h"
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-02-06 16:18:20 +00:00
|
|
|
// stuff that really should be in in the instance instead...
|
2020-02-06 15:39:21 +00:00
|
|
|
HardwareSerial _serial_gps(GPS_SERIAL_NUM);
|
2020-02-20 02:51:17 +00:00
|
|
|
uint32_t timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time
|
|
|
|
uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock
|
2020-02-20 04:02:57 +00:00
|
|
|
|
2020-02-21 20:24:35 +00:00
|
|
|
RTC_DATA_ATTR bool timeSetFromGPS; // We only reset our time once per _boot_ after that point just run from the internal clock (even across sleeps)
|
2020-02-06 15:39:21 +00:00
|
|
|
|
|
|
|
GPS gps;
|
2020-02-21 16:41:36 +00:00
|
|
|
bool hasValidLocation; // default to false, until we complete our first read
|
2020-02-23 01:40:31 +00:00
|
|
|
bool wantNewLocation = true;
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-02-21 20:24:35 +00:00
|
|
|
GPS::GPS() : PeriodicTask()
|
2020-02-06 15:39:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPS::setup()
|
2020-02-19 16:17:28 +00:00
|
|
|
{
|
2020-02-23 03:08:16 +00:00
|
|
|
readFromRTC(); // read the main CPU RTC at first
|
2020-02-19 16:17:28 +00:00
|
|
|
|
|
|
|
#ifdef GPS_RX_PIN
|
|
|
|
_serial_gps.begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
|
2020-02-23 03:08:16 +00:00
|
|
|
ublox.enableDebugging(Serial);
|
|
|
|
|
|
|
|
// note: the lib's implementation has the wrong docs for what the return val is
|
|
|
|
// it is not a bool, it returns zero for success
|
|
|
|
bool errCode = ublox.begin(_serial_gps);
|
|
|
|
assert(!errCode); // FIXME, report hw failure on screen
|
|
|
|
|
|
|
|
bool ok = ublox.setUART1Output(COM_TYPE_UBX); // Use native API
|
|
|
|
assert(ok);
|
|
|
|
ok = ublox.setNavigationFrequency(1); //Produce one solutions per second
|
|
|
|
assert(ok);
|
|
|
|
ok = ublox.setAutoPVT(true);
|
|
|
|
assert(ok);
|
|
|
|
// ublox.saveConfiguration();
|
|
|
|
assert(ok);
|
2020-02-19 16:17:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GPS::readFromRTC()
|
2020-02-06 15:39:21 +00:00
|
|
|
{
|
2020-02-19 15:58:51 +00:00
|
|
|
struct timeval tv; /* btw settimeofday() is helpfull here too*/
|
|
|
|
|
|
|
|
if (!gettimeofday(&tv, NULL))
|
|
|
|
{
|
|
|
|
uint32_t now = millis();
|
|
|
|
|
2020-02-20 04:02:57 +00:00
|
|
|
DEBUG_MSG("Read RTC time as %ld (cur millis %u) valid=%d\n", tv.tv_sec, now, timeSetFromGPS);
|
2020-02-19 15:58:51 +00:00
|
|
|
timeStartMsec = now;
|
2020-02-19 18:53:09 +00:00
|
|
|
zeroOffsetSecs = tv.tv_sec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If we haven't yet set our RTC this boot, set it from a GPS derived time
|
|
|
|
void GPS::perhapsSetRTC(const struct timeval *tv)
|
|
|
|
{
|
|
|
|
if (!timeSetFromGPS)
|
|
|
|
{
|
|
|
|
timeSetFromGPS = true;
|
|
|
|
DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec);
|
|
|
|
settimeofday(tv, NULL);
|
|
|
|
readFromRTC();
|
2020-02-19 15:58:51 +00:00
|
|
|
}
|
2020-02-06 15:39:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 02:51:17 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2020-02-06 16:18:20 +00:00
|
|
|
// for the time being we need to rapidly read from the serial port to prevent overruns
|
2020-02-06 15:39:21 +00:00
|
|
|
void GPS::loop()
|
|
|
|
{
|
|
|
|
PeriodicTask::loop();
|
2020-02-21 16:41:36 +00:00
|
|
|
}
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-02-21 16:41:36 +00:00
|
|
|
uint32_t GPS::getTime()
|
|
|
|
{
|
|
|
|
return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GPS::getValidTime()
|
|
|
|
{
|
|
|
|
return timeSetFromGPS ? getTime() : 0;
|
|
|
|
}
|
|
|
|
|
2020-02-21 20:24:35 +00:00
|
|
|
/// Returns true if we think the board can enter deep or light sleep now (we might be trying to get a GPS lock)
|
|
|
|
bool GPS::canSleep()
|
|
|
|
{
|
|
|
|
return !wantNewLocation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
|
|
|
|
void GPS::prepareSleep()
|
|
|
|
{
|
|
|
|
// discard all rx serial bytes so we don't try to parse them when we come back
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:20:47 +00:00
|
|
|
void GPS::doTask()
|
2020-02-21 16:41:36 +00:00
|
|
|
{
|
2020-02-06 16:18:20 +00:00
|
|
|
#ifdef GPS_RX_PIN
|
2020-02-21 16:41:36 +00:00
|
|
|
// Consume all characters that have arrived
|
|
|
|
|
2020-02-23 03:08:16 +00:00
|
|
|
ublox.checkUblox(); //See if new data is available. Process bytes as they come in.
|
2020-02-06 16:18:20 +00:00
|
|
|
|
2020-02-23 03:08:16 +00:00
|
|
|
if (ublox.getPVT())
|
|
|
|
{ // we only notify if position has changed
|
|
|
|
if (!timeSetFromGPS)
|
2020-02-06 16:18:20 +00:00
|
|
|
{
|
2020-02-19 16:17:28 +00:00
|
|
|
struct timeval tv;
|
2020-02-06 16:18:20 +00:00
|
|
|
|
2020-02-23 03:08:16 +00:00
|
|
|
DEBUG_MSG("Got time from GPS month=%d, year=%d\n", ublox.getMonth(), ublox.getYear());
|
2020-02-23 01:40:31 +00:00
|
|
|
|
2020-02-20 02:51:17 +00:00
|
|
|
/* 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;
|
2020-02-23 03:08:16 +00:00
|
|
|
t.tm_sec = ublox.getSecond();
|
|
|
|
t.tm_min = ublox.getMinute();
|
|
|
|
t.tm_hour = ublox.getHour();
|
|
|
|
t.tm_mday = ublox.getDay();
|
|
|
|
t.tm_mon = ublox.getMonth() - 1;
|
|
|
|
t.tm_year = ublox.getYear() - 1900;
|
2020-02-20 02:51:17 +00:00
|
|
|
t.tm_isdst = false;
|
|
|
|
time_t res = mktime(&t);
|
|
|
|
tv.tv_sec = res;
|
|
|
|
tv.tv_usec = 0; // time.centisecond() * (10 / 1000);
|
2020-02-06 16:18:20 +00:00
|
|
|
|
2020-02-23 03:08:16 +00:00
|
|
|
// FIXME
|
|
|
|
// perhapsSetRTC(&tv);
|
2020-02-06 15:39:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 03:08:16 +00:00
|
|
|
DEBUG_MSG("new gps pos\n");
|
|
|
|
latitude = ublox.getLatitude() * 10e-7;
|
|
|
|
longitude = ublox.getLongitude() * 10e-7;
|
|
|
|
altitude = ublox.getAltitude() / 1000; // in mm convert to meters
|
2020-02-21 16:41:36 +00:00
|
|
|
hasValidLocation = true;
|
2020-02-21 20:24:35 +00:00
|
|
|
wantNewLocation = false;
|
2020-02-06 16:18:20 +00:00
|
|
|
notifyObservers();
|
|
|
|
}
|
2020-02-21 20:24:35 +00:00
|
|
|
else // we didn't get a location update, go back to sleep and hope the characters show up
|
|
|
|
wantNewLocation = true;
|
2020-02-23 03:08:16 +00:00
|
|
|
#endif
|
2020-02-21 16:41:36 +00:00
|
|
|
|
2020-02-21 20:24:35 +00:00
|
|
|
// Once we have sent a location once we only poll the GPS rarely, otherwise check back every 100ms until we have something over the serial
|
|
|
|
setPeriod(hasValidLocation && !wantNewLocation ? 30 * 1000 : 100);
|
2020-02-06 15:39:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 01:40:31 +00:00
|
|
|
void GPS::startLock()
|
2020-02-22 21:14:10 +00:00
|
|
|
{
|
2020-02-22 22:56:19 +00:00
|
|
|
DEBUG_MSG("Looking for GPS lock\n");
|
2020-02-22 21:14:10 +00:00
|
|
|
wantNewLocation = true;
|
|
|
|
setPeriod(1);
|
|
|
|
}
|
|
|
|
|