Switch to native ublox api for GPS position and time

This commit is contained in:
geeksville 2020-03-14 12:46:24 -07:00
parent e93bc76ac9
commit 64109b25f2
2 changed files with 62 additions and 42 deletions

View File

@ -66,7 +66,7 @@ lib_deps =
1202 ; CRC32, explicitly needed because dependency is missing in the ble ota update lib 1202 ; CRC32, explicitly needed because dependency is missing in the ble ota update lib
Wire ; explicitly needed here because the AXP202 library forgets to add it Wire ; explicitly needed here because the AXP202 library forgets to add it
https://github.com/geeksville/arduino-fsm.git https://github.com/geeksville/arduino-fsm.git
SparkFun Ublox Arduino Library https://github.com/meshtastic/SparkFun_Ublox_Arduino_Library.git
;[env:tbeam] ;[env:tbeam]
;board = ttgo-t-beam ;board = ttgo-t-beam

View File

@ -32,19 +32,37 @@ void GPS::setup()
// note: the lib's implementation has the wrong docs for what the return val is // 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 // it is not a bool, it returns zero for success
isConnected = ublox.begin(_serial_gps); isConnected = ublox.begin(_serial_gps);
// try a second time, the ublox lib serial parsing is buggy?
// if(!isConnected) isConnected = ublox.begin(_serial_gps);
if (isConnected) if (isConnected)
{ {
DEBUG_MSG("Connected to GPS successfully\n"); DEBUG_MSG("Connected to GPS successfully, TXpin=%d\n", GPS_TX_PIN);
bool ok = ublox.setUART1Output(COM_TYPE_UBX); // Use native API bool factoryReset = false;
bool ok;
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)
ublox.factoryReset();
delay(2000);
isConnected = ublox.begin(_serial_gps);
DEBUG_MSG("Factory reset success=%d\n", isConnected);
if(isConnected) {
ublox.assumeAutoPVT(true, true); // Just parse NEMA for now
}
}
else {
ok = ublox.setUART1Output(COM_TYPE_UBX); // Use native API
assert(ok); assert(ok);
ok = ublox.setNavigationFrequency(1); //Produce one solutions per second ok = ublox.setNavigationFrequency(4); //Produce 4x/sec to keep the amount of time we stall in getPVT low
assert(ok); assert(ok);
ok = ublox.setAutoPVT(false); //ok = ublox.setAutoPVT(false); // Not implemented on NEO-6M
assert(ok); //assert(ok);
ok = ublox.setDynamicModel(DYN_MODEL_BIKE); // probably PEDESTRIAN but just in case assume bike speeds //ok = ublox.setDynamicModel(DYN_MODEL_BIKE); // probably PEDESTRIAN but just in case assume bike speeds
assert(ok); //assert(ok);
ok = ublox.saveConfiguration(); }
ok = ublox.saveConfiguration(2000);
assert(ok); assert(ok);
} }
else else
@ -122,18 +140,17 @@ void GPS::doTask()
// Consume all characters that have arrived // Consume all characters that have arrived
// getPVT automatically calls checkUblox // getPVT automatically calls checkUblox
// ublox.checkUblox(); //See if new data is available. Process bytes as they come in. ublox.checkUblox(); //See if new data is available. Process bytes as they come in.
DEBUG_MSG("numsat %d, sec %d\n", ublox.getSIV(), ublox.getSecond());
#if 0 // DEBUG_MSG("sec %d\n", ublox.getSecond());
if (ublox.getPVT()) // DEBUG_MSG("lat %d\n", ublox.getLatitude());
{ // we only notify if position has changed
isConnected = true; // We just received a packet, so we must have a GPS
if (!timeSetFromGPS) if (!timeSetFromGPS && ublox.getT())
{ {
struct timeval tv; struct timeval tv;
isConnected = true; // We just received a packet, so we must have a GPS
/* Convert to unix time /* 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). 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).
*/ */
@ -149,23 +166,26 @@ void GPS::doTask()
tv.tv_sec = res; tv.tv_sec = res;
tv.tv_usec = 0; // time.centisecond() * (10 / 1000); tv.tv_usec = 0; // time.centisecond() * (10 / 1000);
DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", ublox.getMonth(), ublox.getYear(), tv.tv_sec); DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec);
// FIXME perhapsSetRTC(&tv);
// perhapsSetRTC(&tv);
} }
DEBUG_MSG("new gps pos\n"); if (ublox.getP())
latitude = ublox.getLatitude() * 10e-7; {
longitude = ublox.getLongitude() * 10e-7; // we only notify if position has changed
isConnected = true; // We just received a packet, so we must have a GPS
latitude = ublox.getLatitude() * 1e-7;
longitude = ublox.getLongitude() * 1e-7;
altitude = ublox.getAltitude() / 1000; // in mm convert to meters 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 = true; hasValidLocation = true;
wantNewLocation = false; wantNewLocation = false;
notifyObservers(); notifyObservers();
} }
else // we didn't get a location update, go back to sleep and hope the characters show up else // we didn't get a location update, go back to sleep and hope the characters show up
wantNewLocation = true; wantNewLocation = true;
#endif
#endif #endif
// 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 // 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