add logic for GPS single acquisition on bootup fixed-position

This commit is contained in:
code8buster 2023-01-21 06:32:41 -05:00
parent c628c70db2
commit 54b4b67e5c
2 changed files with 129 additions and 114 deletions

View File

@ -21,7 +21,8 @@ GPS *gps;
/// only init that port once. /// only init that port once.
static bool didSerialInit; static bool didSerialInit;
bool GPS::getACK(uint8_t c, uint8_t i) { bool GPS::getACK(uint8_t c, uint8_t i)
{
uint8_t b; uint8_t b;
uint8_t ack = 0; uint8_t ack = 0;
const uint8_t ackP[2] = {c, i}; const uint8_t ackP[2] = {c, i};
@ -50,8 +51,7 @@ bool GPS::getACK(uint8_t c, uint8_t i) {
b = _serial_gps->read(); b = _serial_gps->read();
if (b == buf[ack]) { if (b == buf[ack]) {
ack++; ack++;
} } else {
else {
ack = 0; ack = 0;
} }
} }
@ -78,13 +78,13 @@ int GPS::getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t
int c = _serial_gps->read(); int c = _serial_gps->read();
switch (ubxFrameCounter) { switch (ubxFrameCounter) {
case 0: case 0:
//ubxFrame 'μ' // ubxFrame 'μ'
if (c == 0xB5) { if (c == 0xB5) {
ubxFrameCounter++; ubxFrameCounter++;
} }
break; break;
case 1: case 1:
//ubxFrame 'b' // ubxFrame 'b'
if (c == 0x62) { if (c == 0x62) {
ubxFrameCounter++; ubxFrameCounter++;
} else { } else {
@ -92,7 +92,7 @@ int GPS::getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t
} }
break; break;
case 2: case 2:
//Class // Class
if (c == requestedClass) { if (c == requestedClass) {
ubxFrameCounter++; ubxFrameCounter++;
} else { } else {
@ -100,7 +100,7 @@ int GPS::getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t
} }
break; break;
case 3: case 3:
//Message ID // Message ID
if (c == requestedID) { if (c == requestedID) {
ubxFrameCounter++; ubxFrameCounter++;
} else { } else {
@ -108,12 +108,12 @@ int GPS::getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t
} }
break; break;
case 4: case 4:
//Payload lenght lsb // Payload lenght lsb
needRead = c; needRead = c;
ubxFrameCounter++; ubxFrameCounter++;
break; break;
case 5: case 5:
//Payload lenght msb // Payload lenght msb
needRead |= (c << 8); needRead |= (c << 8);
ubxFrameCounter++; ubxFrameCounter++;
break; break;
@ -149,20 +149,20 @@ bool GPS::setupGPS()
_serial_gps->setRxBufferSize(2048); // the default is 256 _serial_gps->setRxBufferSize(2048); // the default is 256
#endif #endif
// if the overrides are not dialled in, set them from the board definitions, if they exist // if the overrides are not dialled in, set them from the board definitions, if they exist
#if defined(GPS_RX_PIN) #if defined(GPS_RX_PIN)
if (!config.position.rx_gpio) if (!config.position.rx_gpio)
config.position.rx_gpio = GPS_RX_PIN; config.position.rx_gpio = GPS_RX_PIN;
#endif #endif
#if defined(GPS_TX_PIN) #if defined(GPS_TX_PIN)
if (!config.position.tx_gpio) if (!config.position.tx_gpio)
config.position.tx_gpio = GPS_TX_PIN; config.position.tx_gpio = GPS_TX_PIN;
#endif #endif
// ESP32 has a special set of parameters vs other arduino ports // ESP32 has a special set of parameters vs other arduino ports
#if defined(ARCH_ESP32) #if defined(ARCH_ESP32)
if(config.position.rx_gpio) if (config.position.rx_gpio)
_serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, config.position.rx_gpio, config.position.tx_gpio); _serial_gps->begin(GPS_BAUDRATE, SERIAL_8N1, config.position.rx_gpio, config.position.tx_gpio);
#else #else
_serial_gps->begin(GPS_BAUDRATE); _serial_gps->begin(GPS_BAUDRATE);
@ -173,13 +173,13 @@ if (!config.position.tx_gpio)
*/ */
gnssModel = probe(); gnssModel = probe();
if(gnssModel == GNSS_MODEL_MTK){ if (gnssModel == GNSS_MODEL_MTK) {
/* /*
* t-beam-s3-core uses the same L76K GNSS module as t-echo. * t-beam-s3-core uses the same L76K GNSS module as t-echo.
* Unlike t-echo, L76K uses 9600 baud rate for communication by default. * Unlike t-echo, L76K uses 9600 baud rate for communication by default.
* */ * */
// _serial_gps->begin(9600); //The baud rate of 9600 has been initialized at the beginning of setupGPS, this line is the redundant part // _serial_gps->begin(9600); //The baud rate of 9600 has been initialized at the beginning of setupGPS, this line
// delay(250); // is the redundant part delay(250);
// Initialize the L76K Chip, use GPS + GLONASS // Initialize the L76K Chip, use GPS + GLONASS
_serial_gps->write("$PCAS04,5*1C\r\n"); _serial_gps->write("$PCAS04,5*1C\r\n");
@ -191,7 +191,7 @@ if (!config.position.tx_gpio)
_serial_gps->write("$PCAS11,3*1E\r\n"); _serial_gps->write("$PCAS11,3*1E\r\n");
delay(250); delay(250);
}else if(gnssModel == GNSS_MODEL_UBLOX){ } else if (gnssModel == GNSS_MODEL_UBLOX) {
/* /*
tips: NMEA Only should not be set here, otherwise initializing Ublox gnss module again after tips: NMEA Only should not be set here, otherwise initializing Ublox gnss module again after
@ -210,7 +210,8 @@ if (!config.position.tx_gpio)
// ublox-M10S can be compatible with UBLOX traditional protocol, so the following sentence settings are also valid // ublox-M10S can be compatible with UBLOX traditional protocol, so the following sentence settings are also valid
// disable GGL // disable GGL
byte _message_GGL[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x05, 0x3A}; byte _message_GGL[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01,
0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x05, 0x3A};
_serial_gps->write(_message_GGL, sizeof(_message_GGL)); _serial_gps->write(_message_GGL, sizeof(_message_GGL));
if (!getACK(0x06, 0x01)) { if (!getACK(0x06, 0x01)) {
LOG_WARN("Unable to disable NMEA GGL.\n"); LOG_WARN("Unable to disable NMEA GGL.\n");
@ -218,7 +219,8 @@ if (!config.position.tx_gpio)
} }
// disable GSA // disable GSA
byte _message_GSA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x06, 0x41}; byte _message_GSA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02,
0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x06, 0x41};
_serial_gps->write(_message_GSA, sizeof(_message_GSA)); _serial_gps->write(_message_GSA, sizeof(_message_GSA));
if (!getACK(0x06, 0x01)) { if (!getACK(0x06, 0x01)) {
LOG_WARN("Unable to disable NMEA GSA.\n"); LOG_WARN("Unable to disable NMEA GSA.\n");
@ -226,7 +228,8 @@ if (!config.position.tx_gpio)
} }
// disable GSV // disable GSV
byte _message_GSV[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x07, 0x48}; byte _message_GSV[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03,
0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x07, 0x48};
_serial_gps->write(_message_GSV, sizeof(_message_GSV)); _serial_gps->write(_message_GSV, sizeof(_message_GSV));
if (!getACK(0x06, 0x01)) { if (!getACK(0x06, 0x01)) {
LOG_WARN("Unable to disable NMEA GSV.\n"); LOG_WARN("Unable to disable NMEA GSV.\n");
@ -234,7 +237,8 @@ if (!config.position.tx_gpio)
} }
// disable VTG // disable VTG
byte _message_VTG[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x05, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x09, 0x56}; byte _message_VTG[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x05,
0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x09, 0x56};
_serial_gps->write(_message_VTG, sizeof(_message_VTG)); _serial_gps->write(_message_VTG, sizeof(_message_VTG));
if (!getACK(0x06, 0x01)) { if (!getACK(0x06, 0x01)) {
LOG_WARN("Unable to disable NMEA VTG.\n"); LOG_WARN("Unable to disable NMEA VTG.\n");
@ -242,7 +246,8 @@ if (!config.position.tx_gpio)
} }
// enable RMC // enable RMC
byte _message_RMC[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x09, 0x54}; byte _message_RMC[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x09, 0x54};
_serial_gps->write(_message_RMC, sizeof(_message_RMC)); _serial_gps->write(_message_RMC, sizeof(_message_RMC));
if (!getACK(0x06, 0x01)) { if (!getACK(0x06, 0x01)) {
LOG_WARN("Unable to enable NMEA RMC.\n"); LOG_WARN("Unable to enable NMEA RMC.\n");
@ -250,7 +255,8 @@ if (!config.position.tx_gpio)
} }
// enable GGA // enable GGA
byte _message_GGA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x05, 0x38}; byte _message_GGA[] = {0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x00,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x05, 0x38};
_serial_gps->write(_message_GGA, sizeof(_message_GGA)); _serial_gps->write(_message_GGA, sizeof(_message_GGA));
if (!getACK(0x06, 0x01)) { if (!getACK(0x06, 0x01)) {
LOG_WARN("Unable to enable NMEA GGA.\n"); LOG_WARN("Unable to enable NMEA GGA.\n");
@ -270,9 +276,9 @@ bool GPS::setup()
#endif #endif
#ifdef HAS_PMU #ifdef HAS_PMU
if(config.position.gps_enabled){ if (config.position.gps_enabled) {
setGPSPower(true); setGPSPower(true);
} }
#endif #endif
#ifdef PIN_GPS_RESET #ifdef PIN_GPS_RESET
@ -289,7 +295,7 @@ if(config.position.gps_enabled){
notifyDeepSleepObserver.observe(&notifyDeepSleep); notifyDeepSleepObserver.observe(&notifyDeepSleep);
notifyGPSSleepObserver.observe(&notifyGPSSleep); notifyGPSSleepObserver.observe(&notifyGPSSleep);
} }
if (config.position.gps_enabled==false) { if (config.position.gps_enabled == false && config.position.fixed_position == false) {
setAwake(false); setAwake(false);
doGPSpowersave(false); doGPSpowersave(false);
} }
@ -396,7 +402,8 @@ uint32_t GPS::getSleepTime() const
uint32_t t = config.position.gps_update_interval; uint32_t t = config.position.gps_update_interval;
bool gps_enabled = config.position.gps_enabled; bool gps_enabled = config.position.gps_enabled;
if (!gps_enabled) // We'll not need the GPS thread to wake up again after first acq. with fixed position.
if (!gps_enabled || config.position.fixed_position)
t = UINT32_MAX; // Sleep forever now t = UINT32_MAX; // Sleep forever now
if (t == UINT32_MAX) if (t == UINT32_MAX)
@ -425,9 +432,9 @@ int32_t GPS::runOnce()
// if we have received valid NMEA claim we are connected // if we have received valid NMEA claim we are connected
setConnected(); setConnected();
} else { } else {
if((config.position.gps_enabled == 1) && (gnssModel == GNSS_MODEL_UBLOX)){ if ((config.position.gps_enabled == 1) && (gnssModel == GNSS_MODEL_UBLOX)) {
// reset the GPS on next bootup // reset the GPS on next bootup
if(devicestate.did_gps_reset && (millis() > 60000) && !hasFlow()) { if (devicestate.did_gps_reset && (millis() > 60000) && !hasFlow()) {
LOG_DEBUG("GPS is not communicating, trying factory reset on next bootup.\n"); LOG_DEBUG("GPS is not communicating, trying factory reset on next bootup.\n");
devicestate.did_gps_reset = false; devicestate.did_gps_reset = false;
nodeDB.saveDeviceStateToDisk(); nodeDB.saveDeviceStateToDisk();
@ -492,6 +499,14 @@ int32_t GPS::runOnce()
// If state has changed do a publish // If state has changed do a publish
publishUpdate(); publishUpdate();
if (!(fixeddelayCtr >= 20) && config.position.fixed_position && hasValidLocation) {
fixeddelayCtr++;
// LOG_DEBUG("Our delay counter is %d\n", fixeddelayCtr);
if (fixeddelayCtr >= 20) {
doGPSpowersave(false);
forceWake(false);
}
}
// 9600bps is approx 1 byte per msec, so considering our buffer size we never need to wake more often than 200ms // 9600bps is approx 1 byte per msec, so considering our buffer size we never need to wake more often than 200ms
// if not awake we can run super infrquently (once every 5 secs?) to see if we need to wake. // if not awake we can run super infrquently (once every 5 secs?) to see if we need to wake.
return isAwake ? GPS_THREAD_INTERVAL : 5000; return isAwake ? GPS_THREAD_INTERVAL : 5000;
@ -547,7 +562,7 @@ GnssModel_t GPS::probe()
* The GNSS module information variable is temporarily placed inside the function body, * The GNSS module information variable is temporarily placed inside the function body,
* if it needs to be used elsewhere, it can be moved to the outside * if it needs to be used elsewhere, it can be moved to the outside
* */ * */
struct uBloxGnssModelInfo info ; struct uBloxGnssModelInfo info;
memset(&info, 0, sizeof(struct uBloxGnssModelInfo)); memset(&info, 0, sizeof(struct uBloxGnssModelInfo));
@ -564,7 +579,7 @@ GnssModel_t GPS::probe()
// Get module info , If the correct header is returned, // Get module info , If the correct header is returned,
// it can be determined that it is the MTK chip // it can be determined that it is the MTK chip
int index = ver.indexOf("$"); int index = ver.indexOf("$");
if(index != -1){ if (index != -1) {
ver = ver.substring(index); ver = ver.substring(index);
if (ver.startsWith("$GPTXT,01,01,02")) { if (ver.startsWith("$GPTXT,01,01,02")) {
LOG_INFO("L76K GNSS init succeeded, using L76K GNSS Module\n"); LOG_INFO("L76K GNSS init succeeded, using L76K GNSS Module\n");
@ -574,7 +589,6 @@ GnssModel_t GPS::probe()
} }
} }
uint8_t cfg_rate[] = {0xB5, 0x62, 0x06, 0x08, 0x00, 0x00, 0x0E, 0x30}; uint8_t cfg_rate[] = {0xB5, 0x62, 0x06, 0x08, 0x00, 0x00, 0x0E, 0x30};
_serial_gps->write(cfg_rate, sizeof(cfg_rate)); _serial_gps->write(cfg_rate, sizeof(cfg_rate));
// Check that the returned response class and message ID are correct // Check that the returned response class and message ID are correct
@ -611,27 +625,27 @@ GnssModel_t GPS::probe()
} }
LOG_DEBUG("Module Info : \n"); LOG_DEBUG("Module Info : \n");
LOG_DEBUG("Soft version: %s\n",info.swVersion); LOG_DEBUG("Soft version: %s\n", info.swVersion);
LOG_DEBUG("Hard version: %s\n",info.hwVersion); LOG_DEBUG("Hard version: %s\n", info.hwVersion);
LOG_DEBUG("Extensions:%d\n",info.extensionNo); LOG_DEBUG("Extensions:%d\n", info.extensionNo);
for (int i = 0; i < info.extensionNo; i++) { for (int i = 0; i < info.extensionNo; i++) {
LOG_DEBUG(" %s\n",info.extension[i]); LOG_DEBUG(" %s\n", info.extension[i]);
} }
memset(buffer,0,sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
//tips: extensionNo field is 0 on some 6M GNSS modules // tips: extensionNo field is 0 on some 6M GNSS modules
for (int i = 0; i < info.extensionNo; ++i) { for (int i = 0; i < info.extensionNo; ++i) {
if (!strncmp(info.extension[i], "OD=", 3)) { if (!strncmp(info.extension[i], "OD=", 3)) {
strncpy((char *)buffer, &(info.extension[i][3]), sizeof(buffer)); strncpy((char *)buffer, &(info.extension[i][3]), sizeof(buffer));
LOG_DEBUG("GetModel:%s\n",(char *)buffer); LOG_DEBUG("GetModel:%s\n", (char *)buffer);
} }
} }
} }
if (strlen((char*)buffer)) { if (strlen((char *)buffer)) {
LOG_INFO("UBlox GNSS init succeeded, using UBlox %s GNSS Module\n" , buffer); LOG_INFO("UBlox GNSS init succeeded, using UBlox %s GNSS Module\n", buffer);
}else{ } else {
LOG_INFO("UBlox GNSS init succeeded, using UBlox GNSS Module\n"); LOG_INFO("UBlox GNSS init succeeded, using UBlox GNSS Module\n");
} }
@ -662,8 +676,7 @@ GPS *createGps()
new_gps->setup(); new_gps->setup();
return new_gps; return new_gps;
} }
} } else {
else{
GPS *new_gps = new NMEAGPS(); GPS *new_gps = new NMEAGPS();
new_gps->setup(); new_gps->setup();
return new_gps; return new_gps;

View File

@ -4,19 +4,18 @@
#include "Observer.h" #include "Observer.h"
#include "concurrency/OSThread.h" #include "concurrency/OSThread.h"
struct uBloxGnssModelInfo { struct uBloxGnssModelInfo {
char swVersion[30]; char swVersion[30];
char hwVersion[10]; char hwVersion[10];
uint8_t extensionNo; uint8_t extensionNo;
char extension[10][30]; char extension[10][30];
} ; };
typedef enum{ typedef enum {
GNSS_MODEL_MTK, GNSS_MODEL_MTK,
GNSS_MODEL_UBLOX, GNSS_MODEL_UBLOX,
GNSS_MODEL_UNKONW, GNSS_MODEL_UNKONW,
}GnssModel_t; } GnssModel_t;
// Generate a string representation of DOP // Generate a string representation of DOP
const char *getDOPString(uint32_t dop); const char *getDOPString(uint32_t dop);
@ -78,7 +77,7 @@ class GPS : private concurrency::OSThread
/// Return true if we are connected to a GPS /// Return true if we are connected to a GPS
bool isConnected() const { return hasGPS; } bool isConnected() const { return hasGPS; }
bool isPowerSaving() const { return !config.position.gps_enabled;} bool isPowerSaving() const { return !config.position.gps_enabled; }
/** /**
* Restart our lock attempt - try to get and broadcast a GPS reading ASAP * Restart our lock attempt - try to get and broadcast a GPS reading ASAP
@ -169,12 +168,15 @@ class GPS : private concurrency::OSThread
int getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID); int getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID);
// delay counter to allow more sats before fixed position stops GPS thread
uint8_t fixeddelayCtr = 0;
protected: protected:
GnssModel_t gnssModel = GNSS_MODEL_UNKONW; GnssModel_t gnssModel = GNSS_MODEL_UNKONW;
}; };
// Creates an instance of the GPS class. // Creates an instance of the GPS class.
// Returns the new instance or null if the GPS is not present. // Returns the new instance or null if the GPS is not present.
GPS* createGps(); GPS *createGps();
extern GPS *gps; extern GPS *gps;