WIP add support for i2C GPS

This commit is contained in:
geeksville 2020-07-10 11:43:14 -07:00
parent 5b07d454b1
commit 1415f2bed7
5 changed files with 45 additions and 17 deletions

View File

@ -5,6 +5,7 @@
#include <assert.h>
#include <time.h>
// If we have a serial GPS port it will not be null
#ifdef GPS_RX_PIN
HardwareSerial _serial_gps_real(GPS_SERIAL_NUM);
HardwareSerial *GPS::_serial_gps = &_serial_gps_real;
@ -15,6 +16,8 @@ HardwareSerial *GPS::_serial_gps = &Serial1;
HardwareSerial *GPS::_serial_gps = NULL;
#endif
uint8_t GPS::i2cAddress = 0;
bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep
GPS *gps;

View File

@ -1,8 +1,8 @@
#pragma once
#include "Observer.h"
#include "GPSStatus.h"
#include "../concurrency/PeriodicTask.h"
#include "GPSStatus.h"
#include "Observer.h"
#include "sys/time.h"
/// If we haven't yet set our RTC this boot, set it from a GPS derived time
@ -30,12 +30,17 @@ class GPS : public Observable<void *>
protected:
bool hasValidLocation = false; // default to false, until we complete our first read
public:
/** If !NULL we will use this serial port to construct our GPS */
static HardwareSerial *_serial_gps;
public:
/** If !0 we will attempt to connect to the GPS over I2C */
static uint8_t i2cAddress;
int32_t latitude = 0, longitude = 0; // as an int mult by 1e-7 to get value as double
int32_t altitude = 0;
uint32_t dop = 0; // Diminution of position; PDOP where possible (UBlox), HDOP otherwise (TinyGPS) in 10^2 units (needs scaling before use)
uint32_t dop = 0; // Diminution of position; PDOP where possible (UBlox), HDOP otherwise (TinyGPS) in 10^2 units (needs
// scaling before use)
uint32_t heading = 0; // Heading of motion, in degrees * 10^-5
uint32_t numSatellites = 0;

View File

@ -7,23 +7,35 @@ UBloxGPS::UBloxGPS() : concurrency::PeriodicTask()
notifySleepObserver.observe(&notifySleep);
}
bool UBloxGPS::tryConnect()
{
isConnected = false;
if (_serial_gps)
isConnected = ublox.begin(*_serial_gps);
if (!isConnected && i2cAddress)
isConnected = ublox.begin(Wire, i2cAddress);
return isConnected;
}
bool UBloxGPS::setup()
{
if (_serial_gps) {
#ifdef GPS_RX_PIN
_serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
#else
_serial_gps->begin(GPS_BAUDRATE);
#endif
// _serial_gps.setRxBufferSize(1024); // the default is 256
}
// 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
isConnected = ublox.begin(*_serial_gps);
// try a second time, the ublox lib serial parsing is buggy?
if (!isConnected)
isConnected = ublox.begin(*_serial_gps);
if (!tryConnect())
tryConnect();
if (isConnected) {
DEBUG_MSG("Connected to UBLOX GPS successfully\n");
@ -35,7 +47,7 @@ bool UBloxGPS::setup()
// GPS_TX connected)
ublox.factoryReset();
delay(3000);
isConnected = ublox.begin(*_serial_gps);
tryConnect();
DEBUG_MSG("Factory reset success=%d\n", isConnected);
ok = ublox.saveConfiguration(3000);
assert(ok);
@ -131,7 +143,8 @@ The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of s
wantNewLocation = true;
// Notify any status instances that are observing us
const meshtastic::GPSStatus status = meshtastic::GPSStatus(hasLock(), isConnected, latitude, longitude, altitude, dop, heading, numSatellites);
const meshtastic::GPSStatus status =
meshtastic::GPSStatus(hasLock(), isConnected, latitude, longitude, altitude, dop, heading, numSatellites);
newStatus.notifyObservers(&status);
// Once we have sent a location once we only poll the GPS rarely, otherwise check back every 1s until we have something over

View File

@ -38,4 +38,7 @@ class UBloxGPS : public GPS, public concurrency::PeriodicTask
/// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs
/// always returns 0 to indicate okay to sleep
int prepareSleep(void *unused);
/// Attempt to connect to our GPS, returns false if no gps is present
bool tryConnect();
};

View File

@ -269,9 +269,13 @@ void setup()
// assume NEMA at 9600 baud.
DEBUG_MSG("ERROR: No UBLOX GPS found, hoping that NEMA might work\n");
delete gps;
if(GPS::_serial_gps) {
// dumb NEMA access only work for serial GPSes)
gps = new NEMAGPS();
gps->setup();
}
}
#else
gps = new NEMAGPS();
gps->setup();