2020-02-06 15:39:21 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-07-10 18:43:14 +00:00
|
|
|
#include "GPSStatus.h"
|
|
|
|
#include "Observer.h"
|
2020-10-10 01:57:57 +00:00
|
|
|
#include "concurrency/OSThread.h"
|
2020-05-04 18:15:05 +00:00
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
struct uBloxGnssModelInfo {
|
|
|
|
char swVersion[30];
|
|
|
|
char hwVersion[10];
|
2022-09-15 18:43:04 +00:00
|
|
|
uint8_t extensionNo;
|
2023-01-21 13:34:29 +00:00
|
|
|
char extension[10][30];
|
|
|
|
};
|
2022-09-15 18:43:04 +00:00
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
typedef enum {
|
|
|
|
GNSS_MODEL_MTK,
|
|
|
|
GNSS_MODEL_UBLOX,
|
|
|
|
GNSS_MODEL_UNKONW,
|
|
|
|
} GnssModel_t;
|
2022-09-15 18:43:04 +00:00
|
|
|
|
2020-06-21 01:59:41 +00:00
|
|
|
// Generate a string representation of DOP
|
|
|
|
const char *getDOPString(uint32_t dop);
|
|
|
|
|
2020-02-06 15:39:21 +00:00
|
|
|
/**
|
|
|
|
* A gps class that only reads from the GPS periodically (and FIXME - eventually keeps the gps powered down except when reading)
|
2020-03-19 02:15:51 +00:00
|
|
|
*
|
2020-02-06 15:39:21 +00:00
|
|
|
* When new data is available it will notify observers.
|
|
|
|
*/
|
2020-10-10 01:57:57 +00:00
|
|
|
class GPS : private concurrency::OSThread
|
2020-02-06 15:39:21 +00:00
|
|
|
{
|
2020-10-01 16:11:54 +00:00
|
|
|
private:
|
2020-10-05 22:07:30 +00:00
|
|
|
uint32_t lastWakeStartMsec = 0, lastSleepStartMsec = 0, lastWhileActiveMsec = 0;
|
2020-10-01 16:11:54 +00:00
|
|
|
|
2021-10-24 12:38:35 +00:00
|
|
|
/**
|
|
|
|
* hasValidLocation - indicates that the position variables contain a complete
|
|
|
|
* GPS location, valid and fresh (< gps_update_interval + gps_attempt_time)
|
|
|
|
*/
|
2020-05-04 18:15:05 +00:00
|
|
|
bool hasValidLocation = false; // default to false, until we complete our first read
|
2020-02-19 16:17:28 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
bool isAwake = false; // true if we want a location right now
|
|
|
|
|
|
|
|
bool wakeAllowed = true; // false if gps must be forced to sleep regardless of what time it is
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-10 01:20:38 +00:00
|
|
|
bool shouldPublish = false; // If we've changed GPS state, this will force a publish the next loop()
|
|
|
|
|
|
|
|
bool hasGPS = false; // Do we have a GPS we are talking to
|
|
|
|
|
|
|
|
uint8_t numSatellites = 0;
|
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
CallbackObserver<GPS, void *> notifySleepObserver = CallbackObserver<GPS, void *>(this, &GPS::prepareSleep);
|
2020-10-30 09:05:32 +00:00
|
|
|
CallbackObserver<GPS, void *> notifyDeepSleepObserver = CallbackObserver<GPS, void *>(this, &GPS::prepareDeepSleep);
|
2022-12-17 22:32:20 +00:00
|
|
|
CallbackObserver<GPS, void *> notifyGPSSleepObserver = CallbackObserver<GPS, void *>(this, &GPS::prepareDeepSleep);
|
2020-10-01 16:11:54 +00:00
|
|
|
|
2020-07-10 18:43:14 +00:00
|
|
|
public:
|
|
|
|
/** If !NULL we will use this serial port to construct our GPS */
|
2020-07-10 04:27:34 +00:00
|
|
|
static HardwareSerial *_serial_gps;
|
2020-02-19 18:53:09 +00:00
|
|
|
|
2021-10-24 12:38:35 +00:00
|
|
|
Position p = Position_init_default;
|
2021-09-05 15:10:06 +00:00
|
|
|
|
2020-10-10 01:57:57 +00:00
|
|
|
GPS() : concurrency::OSThread("GPS") {}
|
|
|
|
|
2021-03-10 07:21:54 +00:00
|
|
|
virtual ~GPS();
|
2020-02-21 20:24:35 +00:00
|
|
|
|
2020-08-12 22:51:57 +00:00
|
|
|
/** We will notify this observable anytime GPS state has changed meaningfully */
|
2020-06-29 01:17:52 +00:00
|
|
|
Observable<const meshtastic::GPSStatus *> newStatus;
|
2020-06-28 04:19:49 +00:00
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
/**
|
|
|
|
* Returns true if we succeeded
|
|
|
|
*/
|
2020-10-01 16:11:54 +00:00
|
|
|
virtual bool setup();
|
2020-05-05 00:39:57 +00:00
|
|
|
|
2021-09-02 13:11:11 +00:00
|
|
|
/// Returns true if we have acquired GPS lock.
|
|
|
|
virtual bool hasLock();
|
2020-03-26 16:24:53 +00:00
|
|
|
|
2022-05-21 20:38:33 +00:00
|
|
|
/// Returns true if there's valid data flow with the chip.
|
|
|
|
virtual bool hasFlow();
|
|
|
|
|
2020-10-10 01:20:38 +00:00
|
|
|
/// Return true if we are connected to a GPS
|
|
|
|
bool isConnected() const { return hasGPS; }
|
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
bool isPowerSaving() const { return !config.position.gps_enabled; }
|
2022-12-17 22:32:20 +00:00
|
|
|
|
2020-09-29 00:04:19 +00:00
|
|
|
/**
|
2020-10-01 16:11:54 +00:00
|
|
|
* Restart our lock attempt - try to get and broadcast a GPS reading ASAP
|
|
|
|
* called after the CPU wakes from light-sleep state
|
|
|
|
*
|
|
|
|
* Or set to false, to disallow any sort of waking
|
|
|
|
* */
|
|
|
|
void forceWake(bool on);
|
|
|
|
|
2021-03-15 02:00:20 +00:00
|
|
|
// Some GPS modules (ublock) require factory reset
|
|
|
|
virtual bool factoryReset() { return true; }
|
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
protected:
|
2020-10-05 07:29:26 +00:00
|
|
|
/// Do gps chipset specific init, return true for success
|
2020-10-25 09:07:54 +00:00
|
|
|
virtual bool setupGPS();
|
2020-10-05 07:29:26 +00:00
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
/// If possible force the GPS into sleep/low power mode
|
2020-10-13 05:59:06 +00:00
|
|
|
virtual void sleep();
|
2020-10-01 16:11:54 +00:00
|
|
|
|
|
|
|
/// wake the GPS into normal operation mode
|
2020-10-13 05:59:06 +00:00
|
|
|
virtual void wake();
|
2020-10-01 16:11:54 +00:00
|
|
|
|
|
|
|
/** Subclasses should look for serial rx characters here and feed it to their GPS parser
|
|
|
|
*
|
|
|
|
* Return true if we received a valid message from the GPS
|
2020-09-29 00:04:19 +00:00
|
|
|
*/
|
2020-10-01 16:11:54 +00:00
|
|
|
virtual bool whileIdle() = 0;
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-05 22:07:30 +00:00
|
|
|
/** Idle processing while GPS is looking for lock, called once per secondish */
|
2020-10-05 06:43:44 +00:00
|
|
|
virtual void whileActive() {}
|
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
/**
|
2020-10-01 16:11:54 +00: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 time
|
|
|
|
*/
|
|
|
|
virtual bool lookForTime() = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
virtual bool lookForLocation() = 0;
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-10 01:20:38 +00:00
|
|
|
/// Record that we have a GPS
|
|
|
|
void setConnected();
|
|
|
|
|
|
|
|
void setNumSatellites(uint8_t n);
|
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
private:
|
|
|
|
/// 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);
|
2020-09-29 00:04:19 +00:00
|
|
|
|
2020-10-30 09:05:32 +00:00
|
|
|
/// Prepare the GPS for the cpu entering deep sleep, expect to be gone for at least 100s of msecs
|
|
|
|
/// always returns 0 to indicate okay to sleep
|
|
|
|
int prepareDeepSleep(void *unused);
|
|
|
|
|
2020-10-01 16:11:54 +00:00
|
|
|
/**
|
|
|
|
* Switch the GPS into a mode where we are actively looking for a lock, or alternatively switch GPS into a low power mode
|
|
|
|
*
|
|
|
|
* calls sleep/wake
|
|
|
|
*/
|
|
|
|
void setAwake(bool on);
|
2020-10-05 06:43:44 +00:00
|
|
|
|
|
|
|
/** Get how long we should stay looking for each aquisition
|
|
|
|
*/
|
|
|
|
uint32_t getWakeTime() const;
|
|
|
|
|
|
|
|
/** Get how long we should sleep between aqusition attempts
|
|
|
|
*/
|
|
|
|
uint32_t getSleepTime() const;
|
|
|
|
|
2022-05-17 17:48:48 +00:00
|
|
|
bool getACK(uint8_t c, uint8_t i);
|
|
|
|
|
2020-10-05 06:43:44 +00:00
|
|
|
/**
|
|
|
|
* Tell users we have new GPS readings
|
|
|
|
*/
|
|
|
|
void publishUpdate();
|
2020-10-10 01:57:57 +00:00
|
|
|
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual int32_t runOnce() override;
|
2022-09-15 18:43:04 +00:00
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
// Get GNSS model
|
2022-09-15 18:43:04 +00:00
|
|
|
GnssModel_t probe();
|
|
|
|
|
|
|
|
int getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
GnssModel_t gnssModel = GNSS_MODEL_UNKONW;
|
2020-02-06 15:39:21 +00:00
|
|
|
};
|
|
|
|
|
2023-01-21 13:34:29 +00:00
|
|
|
// Creates an instance of the GPS class.
|
2021-03-15 02:00:20 +00:00
|
|
|
// Returns the new instance or null if the GPS is not present.
|
2023-01-21 13:34:29 +00:00
|
|
|
GPS *createGps();
|
2021-03-15 02:00:20 +00:00
|
|
|
|
2020-05-04 18:15:05 +00:00
|
|
|
extern GPS *gps;
|