2020-02-06 15:39:21 +00:00
|
|
|
|
|
|
|
#include "GPS.h"
|
2020-03-18 16:16:44 +00:00
|
|
|
#include "configuration.h"
|
2020-02-19 15:58:51 +00:00
|
|
|
#include "time.h"
|
2020-04-15 03:22:27 +00:00
|
|
|
#include <assert.h>
|
2020-02-19 15:58:51 +00:00
|
|
|
#include <sys/time.h>
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-04-15 03:22:27 +00:00
|
|
|
#ifdef GPS_RX_PIN
|
2020-02-06 15:39:21 +00:00
|
|
|
HardwareSerial _serial_gps(GPS_SERIAL_NUM);
|
2020-04-15 03:22:27 +00:00
|
|
|
#else
|
|
|
|
// Assume NRF52
|
2020-04-23 23:20:07 +00:00
|
|
|
HardwareSerial &_serial_gps = Serial1;
|
2020-04-15 03:22:27 +00:00
|
|
|
#endif
|
2020-02-20 04:02:57 +00:00
|
|
|
|
2020-04-05 02:16:30 +00:00
|
|
|
bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep
|
2020-02-06 15:39:21 +00:00
|
|
|
|
|
|
|
GPS gps;
|
2020-03-14 01:44:14 +00:00
|
|
|
|
|
|
|
// stuff that really should be in in the instance instead...
|
2020-03-18 16:16:44 +00:00
|
|
|
static uint32_t
|
|
|
|
timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time
|
2020-03-14 01:44:14 +00:00
|
|
|
static uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock
|
|
|
|
|
|
|
|
static bool wantNewLocation = true;
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-03-18 16:16:44 +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-04-25 17:59:40 +00:00
|
|
|
PeriodicTask::setup();
|
|
|
|
|
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-04-23 23:20:07 +00:00
|
|
|
#else
|
|
|
|
_serial_gps.begin(GPS_BAUDRATE);
|
|
|
|
#endif
|
2020-03-14 23:31:12 +00:00
|
|
|
// _serial_gps.setRxBufferSize(1024); // the default is 256
|
2020-03-14 03:30:48 +00:00
|
|
|
// ublox.enableDebugging(Serial);
|
2020-02-23 03:08:16 +00:00
|
|
|
|
|
|
|
// 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
|
2020-03-14 03:30:48 +00:00
|
|
|
isConnected = ublox.begin(_serial_gps);
|
2020-03-14 19:46:24 +00:00
|
|
|
|
|
|
|
// try a second time, the ublox lib serial parsing is buggy?
|
2020-03-19 02:15:51 +00:00
|
|
|
if (!isConnected)
|
|
|
|
isConnected = ublox.begin(_serial_gps);
|
2020-03-14 19:46:24 +00:00
|
|
|
|
2020-03-18 16:16:44 +00:00
|
|
|
if (isConnected) {
|
2020-04-23 23:20:07 +00:00
|
|
|
DEBUG_MSG("Connected to GPS successfully\n");
|
2020-03-14 19:46:24 +00:00
|
|
|
|
|
|
|
bool factoryReset = false;
|
|
|
|
bool ok;
|
2020-03-18 16:16:44 +00:00
|
|
|
if (factoryReset) {
|
|
|
|
// It is useful to force back into factory defaults (9600baud, NEMA to test the behavior of boards that don't have
|
|
|
|
// GPS_TX connected)
|
2020-03-14 19:46:24 +00:00
|
|
|
ublox.factoryReset();
|
|
|
|
delay(2000);
|
|
|
|
isConnected = ublox.begin(_serial_gps);
|
|
|
|
DEBUG_MSG("Factory reset success=%d\n", isConnected);
|
2020-03-18 16:16:44 +00:00
|
|
|
if (isConnected) {
|
2020-03-14 19:46:24 +00:00
|
|
|
ublox.assumeAutoPVT(true, true); // Just parse NEMA for now
|
|
|
|
}
|
2020-03-18 16:16:44 +00:00
|
|
|
} else {
|
2020-03-14 23:31:12 +00:00
|
|
|
ok = ublox.setUART1Output(COM_TYPE_UBX, 500); // Use native API
|
2020-03-14 19:46:24 +00:00
|
|
|
assert(ok);
|
2020-03-18 16:16:44 +00:00
|
|
|
ok = ublox.setNavigationFrequency(1, 500); // Produce 4x/sec to keep the amount of time we stall in getPVT low
|
2020-03-14 19:46:24 +00:00
|
|
|
assert(ok);
|
2020-03-18 16:16:44 +00:00
|
|
|
// ok = ublox.setAutoPVT(false); // Not implemented on NEO-6M
|
|
|
|
// assert(ok);
|
|
|
|
// ok = ublox.setDynamicModel(DYN_MODEL_BIKE); // probably PEDESTRIAN but just in case assume bike speeds
|
|
|
|
// assert(ok);
|
|
|
|
ok = ublox.powerSaveMode(); // use power save mode
|
2020-03-14 20:33:53 +00:00
|
|
|
assert(ok);
|
2020-03-14 19:46:24 +00:00
|
|
|
}
|
|
|
|
ok = ublox.saveConfiguration(2000);
|
2020-03-14 03:30:48 +00:00
|
|
|
assert(ok);
|
2020-03-18 16:16:44 +00:00
|
|
|
} else {
|
2020-03-14 03:30:48 +00:00
|
|
|
// Some boards might have only the TX line from the GPS connected, in that case, we can't configure it at all. Just
|
|
|
|
// assume NEMA at 9600 baud.
|
|
|
|
DEBUG_MSG("ERROR: No bidirectional GPS found, hoping that it still might work\n");
|
|
|
|
|
|
|
|
// tell lib, we are expecting the module to send PVT messages by itself to our Rx pin
|
2020-03-18 16:16:44 +00:00
|
|
|
// you can set second parameter to "false" if you want to control the parsing and eviction of the data (need to call
|
|
|
|
// checkUblox cyclically)
|
2020-03-14 03:30:48 +00:00
|
|
|
ublox.assumeAutoPVT(true, true);
|
|
|
|
}
|
2020-02-19 16:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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*/
|
|
|
|
|
2020-03-18 16:16:44 +00:00
|
|
|
if (!gettimeofday(&tv, NULL)) {
|
2020-02-19 15:58:51 +00:00
|
|
|
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)
|
|
|
|
{
|
2020-03-18 16:16:44 +00:00
|
|
|
if (!timeSetFromGPS) {
|
2020-02-19 18:53:09 +00:00
|
|
|
timeSetFromGPS = true;
|
|
|
|
DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec);
|
2020-04-15 03:22:27 +00:00
|
|
|
#ifndef NO_ESP32
|
2020-02-19 18:53:09 +00:00
|
|
|
settimeofday(tv, NULL);
|
2020-04-15 03:22:27 +00:00
|
|
|
#else
|
|
|
|
assert(0);
|
|
|
|
#endif
|
2020-02-19 18:53:09 +00:00
|
|
|
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-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()
|
|
|
|
{
|
2020-03-14 05:34:44 +00:00
|
|
|
return true; // we leave GPS on during sleep now, so sleep is okay !wantNewLocation;
|
2020-02-21 20:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
|
|
|
|
void GPS::prepareSleep()
|
|
|
|
{
|
2020-03-15 23:46:28 +00:00
|
|
|
if (isConnected)
|
|
|
|
ublox.powerOff();
|
2020-02-21 20:24:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:20:47 +00:00
|
|
|
void GPS::doTask()
|
2020-02-21 16:41:36 +00:00
|
|
|
{
|
2020-03-18 16:16:44 +00:00
|
|
|
uint8_t fixtype = 3; // If we are only using the RX pin, assume we have a 3d fix
|
|
|
|
|
|
|
|
if (isConnected) {
|
2020-03-15 23:46:28 +00:00
|
|
|
// Consume all characters that have arrived
|
2020-02-21 16:41:36 +00:00
|
|
|
|
2020-03-15 23:46:28 +00:00
|
|
|
// getPVT automatically calls checkUblox
|
2020-03-18 16:16:44 +00:00
|
|
|
ublox.checkUblox(); // See if new data is available. Process bytes as they come in.
|
2020-03-14 19:46:24 +00:00
|
|
|
|
2020-03-15 23:46:28 +00:00
|
|
|
// If we don't have a fix (a quick check), don't try waiting for a solution)
|
2020-03-19 01:34:22 +00:00
|
|
|
// Hmmm my fix type reading returns zeros for fix, which doesn't seem correct, because it is still sptting out positions
|
|
|
|
// turn off for now
|
|
|
|
// fixtype = ublox.getFixType();
|
2020-03-15 23:46:28 +00:00
|
|
|
DEBUG_MSG("fix type %d\n", fixtype);
|
2020-03-18 16:16:44 +00:00
|
|
|
}
|
2020-03-14 23:31:12 +00:00
|
|
|
|
2020-03-18 16:16:44 +00:00
|
|
|
// DEBUG_MSG("sec %d\n", ublox.getSecond());
|
|
|
|
// DEBUG_MSG("lat %d\n", ublox.getLatitude());
|
|
|
|
|
|
|
|
// any fix that has time
|
2020-03-19 01:34:22 +00:00
|
|
|
if (!timeSetFromGPS && ublox.getT()) {
|
2020-03-18 16:16:44 +00:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* 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 = 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;
|
|
|
|
t.tm_isdst = false;
|
|
|
|
time_t res = mktime(&t);
|
|
|
|
tv.tv_sec = res;
|
|
|
|
tv.tv_usec = 0; // time.centisecond() * (10 / 1000);
|
|
|
|
|
|
|
|
DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec);
|
2020-04-05 01:47:41 +00:00
|
|
|
if (t.tm_year < 0 || t.tm_year >= 300)
|
|
|
|
DEBUG_MSG("Ignoring invalid GPS time\n");
|
|
|
|
else
|
|
|
|
perhapsSetRTC(&tv);
|
2020-03-18 16:16:44 +00:00
|
|
|
}
|
2020-02-06 15:39:21 +00:00
|
|
|
|
2020-03-18 16:16:44 +00:00
|
|
|
if ((fixtype >= 3 && fixtype <= 4) && ublox.getP()) // rd fixes only
|
|
|
|
{
|
|
|
|
// we only notify if position has changed
|
|
|
|
latitude = ublox.getLatitude() * 1e-7;
|
|
|
|
longitude = ublox.getLongitude() * 1e-7;
|
|
|
|
altitude = ublox.getAltitude() / 1000; // in mm convert to meters
|
|
|
|
DEBUG_MSG("new gps pos lat=%f, lon=%f, alt=%d\n", latitude, longitude, altitude);
|
|
|
|
|
|
|
|
hasValidLocation = (latitude != 0) || (longitude != 0); // bogus lat lon is reported as 0,0
|
|
|
|
if (hasValidLocation) {
|
|
|
|
wantNewLocation = false;
|
2020-04-10 19:18:48 +00:00
|
|
|
notifyObservers(NULL);
|
2020-03-18 16:16:44 +00:00
|
|
|
// ublox.powerOff();
|
2020-03-14 23:31:12 +00:00
|
|
|
}
|
2020-03-18 16:16:44 +00:00
|
|
|
} else // we didn't get a location update, go back to sleep and hope the characters show up
|
|
|
|
wantNewLocation = true;
|
2020-03-14 23:31:12 +00:00
|
|
|
|
2020-03-18 16:16:44 +00:00
|
|
|
// Once we have sent a location once we only poll the GPS rarely, otherwise check back every 1s until we have something over
|
|
|
|
// the serial
|
2020-03-15 01:45:24 +00:00
|
|
|
setPeriod(hasValidLocation && !wantNewLocation ? 30 * 1000 : 10 * 1000);
|
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);
|
|
|
|
}
|