Files
firmware/src/gps/UBloxGPS.cpp

211 lines
6.4 KiB
C++
Raw Normal View History

2020-05-04 11:15:05 -07:00
#include "UBloxGPS.h"
#include "error.h"
#include "sleep.h"
2020-05-04 11:15:05 -07:00
#include <assert.h>
2020-10-05 14:43:44 +08:00
UBloxGPS::UBloxGPS() {}
2020-05-04 11:15:05 -07:00
2020-07-10 11:43:14 -07:00
bool UBloxGPS::tryConnect()
{
isConnected = false;
if (_serial_gps)
isConnected = ublox.begin(*_serial_gps);
if (!isConnected && i2cAddress) {
extern bool neo6M; // Super skanky - if we are talking to the device i2c we assume it is a neo7 on a RAK815, which
// supports the newer API
neo6M = true;
2020-07-10 11:43:14 -07:00
isConnected = ublox.begin(Wire, i2cAddress);
}
2020-07-10 11:43:14 -07:00
return isConnected;
}
bool UBloxGPS::setupGPS()
2020-05-04 11:15:05 -07:00
{
2020-07-10 11:43:14 -07:00
if (_serial_gps) {
2020-05-04 11:15:05 -07:00
#ifdef GPS_RX_PIN
2020-07-10 11:43:14 -07:00
_serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
2020-05-04 11:15:05 -07:00
#else
2020-07-10 11:43:14 -07:00
_serial_gps->begin(GPS_BAUDRATE);
2020-05-04 11:15:05 -07:00
#endif
2020-07-10 11:43:14 -07:00
// _serial_gps.setRxBufferSize(1024); // the default is 256
}
2020-05-04 11:15:05 -07:00
2020-10-05 14:43:44 +08:00
ublox.enableDebugging(Serial);
2020-05-04 11:15:05 -07:00
// try a second time, the ublox lib serial parsing is buggy?
// see https://github.com/meshtastic/Meshtastic-device/issues/376
for (int i = 0; (i < 3) && !tryConnect(); i++)
delay(500);
2020-05-04 11:15:05 -07:00
if (isConnected) {
DEBUG_MSG("Connected to UBLOX GPS successfully\n");
if (!setUBXMode())
recordCriticalError(UBloxInitFailed); // Don't halt the boot if saving the config fails, but do report the bug
2020-05-04 11:15:05 -07:00
return true;
} else {
return false;
}
}
bool UBloxGPS::setUBXMode()
{
if (_serial_gps) {
if (!ublox.setUART1Output(COM_TYPE_UBX, 1000)) // Use native API
return false;
}
if (i2cAddress) {
if (!ublox.setI2COutput(COM_TYPE_UBX, 1000))
return false;
}
if (!ublox.setNavigationFrequency(1, 1000)) // Produce 4x/sec to keep the amount of time we stall in getPVT low
return false;
// 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);
// per https://github.com/meshtastic/Meshtastic-device/issues/376 powerSaveMode might not work with the marginal
// TTGO antennas
// if (!ublox.powerSaveMode(true, 2000)) // use power save mode, the default timeout (1100ms seems a bit too tight)
// return false;
if (!ublox.saveConfiguration(3000))
return false;
return true;
}
/**
* Reset our GPS back to factory settings
*
* @return true for success
*/
bool UBloxGPS::factoryReset()
{
bool ok = false;
// It is useful to force back into factory defaults (9600baud, NMEA to test the behavior of boards that don't have
// GPS_TX connected)
ublox.factoryReset();
delay(5000);
tryConnect(); // sets isConnected
// try a second time, the ublox lib serial parsing is buggy?
for (int i = 0; (i < 3) && !tryConnect(); i++)
delay(500);
DEBUG_MSG("GPS Factory reset success=%d\n", isConnected);
if (isConnected)
ok = setUBXMode();
return ok;
}
2020-10-05 14:43:44 +08:00
/** Idle processing while GPS is looking for lock */
void UBloxGPS::whileActive()
{
// If we don't have a fix (a quick check), don't try waiting for a solution)
fixType = ublox.getFixType(maxWait());
DEBUG_MSG("GPS fix type %d\n", fixType);
}
2020-10-01 09:11:54 -07: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 UBloxGPS::lookForTime()
2020-05-04 11:15:05 -07:00
{
if (fixType >= 2) {
if (ublox.moduleQueried.gpsSecond) {
/* 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(0);
t.tm_min = ublox.getMinute(0);
t.tm_hour = ublox.getHour(0);
t.tm_mday = ublox.getDay(0);
t.tm_mon = ublox.getMonth(0) - 1;
t.tm_year = ublox.getYear(0) - 1900;
t.tm_isdst = false;
perhapsSetRTC(t);
return true;
} else {
ublox.getT(maxWait()); // ask for new time data - hopefully ready when we come back
}
2020-10-01 09:11:54 -07:00
}
return false;
2020-05-04 11:15:05 -07:00
}
2020-10-01 09:11:54 -07: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 UBloxGPS::lookForLocation()
2020-05-04 11:15:05 -07:00
{
2020-10-01 09:11:54 -07:00
bool foundLocation = false;
2020-05-04 11:15:05 -07:00
2020-10-01 09:11:54 -07:00
// we only notify if position has changed due to a new fix
if ((fixType >= 3 && fixType <= 4)) {
if (ublox.moduleQueried.latitude) // rd fixes only
{
latitude = ublox.getLatitude(0);
longitude = ublox.getLongitude(0);
altitude = ublox.getAltitudeMSL(0) / 1000; // in mm convert to meters
dop = ublox.getPDOP(0); // PDOP (an accuracy metric) is reported in 10^2 units so we have to scale down when we use it
// Note: heading is only currently implmented in the ublox for the 8m chipset - therefore
// don't read it here - it will generate an ignored getPVT command on the 6ms
// heading = ublox.getHeading(0);
numSatellites = ublox.getSIV(0);
// bogus lat lon is reported as 0 or 0 (can be bogus just for one)
// Also: apparently when the GPS is initially reporting lock it can output a bogus latitude > 90 deg!
foundLocation =
(latitude != 0) && (longitude != 0) && (latitude <= 900000000 && latitude >= -900000000) && (numSatellites > 0);
} else {
// Ask for a new position fix - hopefully it will have results ready by next time
ublox.getSIV(maxWait());
ublox.getPDOP(maxWait());
ublox.getP(maxWait());
}
2020-10-05 14:43:44 +08:00
}
2020-10-01 09:11:54 -07:00
return foundLocation;
2020-05-04 11:15:05 -07:00
}
2020-10-01 09:11:54 -07:00
bool UBloxGPS::whileIdle()
2020-05-04 11:15:05 -07:00
{
2020-10-01 09:11:54 -07:00
// if using i2c or serial look too see if any chars are ready
2020-10-05 14:43:44 +08:00
return ublox.checkUblox(); // See if new data is available. Process bytes as they come in.
2020-05-04 11:15:05 -07:00
}
2020-10-01 09:11:54 -07:00
/// If possible force the GPS into sleep/low power mode
/// Note: ublox doesn't need a wake method, because as soon as we send chars to the GPS it will wake up
2020-10-05 14:43:44 +08:00
void UBloxGPS::sleep()
{
// won't work on 6M
// ublox.powerOff();
setGPSPower(false);
2020-10-05 14:43:44 +08:00
}
void UBloxGPS::wake()
{
setGPSPower(true);
// Give time for the GPS to boot
delay(200);
}