Fix possible edge case in GPS detection

This commit is contained in:
Jonathan Bennett 2023-09-01 16:23:52 -05:00
parent 5bd861f3d8
commit 5d6f0ea6c4
2 changed files with 15 additions and 16 deletions

View File

@ -51,21 +51,20 @@ bool GPS::getACK(const char *message, int waitMillis)
{ {
uint8_t buffer[768] = {0}; uint8_t buffer[768] = {0};
uint8_t b; uint8_t b;
int bytesRead; int bytesRead = 0;
uint32_t startTimeout = millis() + waitMillis; uint32_t startTimeout = millis() + waitMillis;
while (millis() < startTimeout) { while (millis() < startTimeout) {
bytesRead = 0; if (_serial_gps->available()) {
while ((_serial_gps->available()) && (millis() < startTimeout)) {
b = _serial_gps->read(); b = _serial_gps->read();
buffer[bytesRead] = b; buffer[bytesRead] = b;
bytesRead++; bytesRead++;
if ((bytesRead == 768) || (b == '\r')) if ((bytesRead == 768) || (b == '\r')) {
break; if (strnstr((char *)buffer, message, bytesRead) != nullptr) {
// Get module info , If the correct header is returned, return true;
// it can be determined that it is the MTK chip } else {
} bytesRead = 0;
if (strnstr((char *)buffer, message, bytesRead) != nullptr) { }
return true; }
} }
} }
return false; return false;
@ -92,7 +91,7 @@ bool GPS::getACK(uint8_t class_id, uint8_t msg_id, int waitMillis)
while (millis() - startTime < waitMillis) { while (millis() - startTime < waitMillis) {
if (ack > 9) { if (ack > 9) {
LOG_INFO("Got ACK for class %02X message %02X in %d millis.\n", class_id, msg_id, millis() - startTime); // LOG_INFO("Got ACK for class %02X message %02X in %d millis.\n", class_id, msg_id, millis() - startTime);
return true; // ACK received return true; // ACK received
} }
if (_serial_gps->available()) { if (_serial_gps->available()) {
@ -108,7 +107,7 @@ bool GPS::getACK(uint8_t class_id, uint8_t msg_id, int waitMillis)
} }
} }
} }
LOG_WARN("No response for class %02X message %02X\n", class_id, msg_id); // LOG_WARN("No response for class %02X message %02X\n", class_id, msg_id);
return false; // No response received within timeout return false; // No response received within timeout
} }
@ -121,7 +120,7 @@ bool GPS::getACK(uint8_t class_id, uint8_t msg_id, int waitMillis)
* @param requestedID: request message ID constant * @param requestedID: request message ID constant
* @retval length of payload message * @retval length of payload message
*/ */
int GPS::getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID, int waitMillis) int GPS::getACK(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID, int waitMillis)
{ {
uint16_t ubxFrameCounter = 0; uint16_t ubxFrameCounter = 0;
uint32_t startTime = millis(); uint32_t startTime = millis();
@ -894,7 +893,7 @@ GnssModel_t GPS::probe(int serialSpeed)
clearBuffer(); clearBuffer();
_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
if (!getAck(buffer, sizeof(buffer), 0x06, 0x08, 1500)) { if (!getACK(0x06, 0x08, 500)) {
LOG_WARN("Failed to find UBlox & MTK GNSS Module using baudrate %d\n", serialSpeed); LOG_WARN("Failed to find UBlox & MTK GNSS Module using baudrate %d\n", serialSpeed);
return GNSS_MODEL_UNKNOWN; return GNSS_MODEL_UNKNOWN;
} }
@ -930,7 +929,7 @@ GnssModel_t GPS::probe(int serialSpeed)
clearBuffer(); clearBuffer();
_serial_gps->write(_message_MONVER, sizeof(_message_MONVER)); _serial_gps->write(_message_MONVER, sizeof(_message_MONVER));
uint16_t len = getAck(buffer, sizeof(buffer), 0x0A, 0x04, 1200); uint16_t len = getACK(buffer, sizeof(buffer), 0x0A, 0x04, 1200);
if (len) { if (len) {
// LOG_DEBUG("monver reply size = %d\n", len); // LOG_DEBUG("monver reply size = %d\n", len);
uint16_t position = 0; uint16_t position = 0;

View File

@ -172,7 +172,7 @@ class GPS : private concurrency::OSThread
String getNMEA(); String getNMEA();
GnssModel_t probe(int serialSpeed); GnssModel_t probe(int serialSpeed);
int getAck(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID, int waitMillis); int getACK(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t requestedID, int waitMillis);
bool getACK(uint8_t c, uint8_t i, int waitMillis); bool getACK(uint8_t c, uint8_t i, int waitMillis);
bool getACK(const char *message, int waitMillis); bool getACK(const char *message, int waitMillis);
// delay counter to allow more sats before fixed position stops GPS thread // delay counter to allow more sats before fixed position stops GPS thread