Skip to content

Commit f1a718f

Browse files
committed
GPS - Add processNMEA() with override, hacky but works
1 parent 0376181 commit f1a718f

File tree

5 files changed

+86
-15
lines changed

5 files changed

+86
-15
lines changed

src/components/gps/controller.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,7 @@
1818
/*!
1919
* @brief Constructor for GPSController.
2020
*/
21-
GPSController::GPSController() {
22-
_gps_model = new GPSModel();
23-
_nmea_buff.head = 0;
24-
_nmea_buff.tail = 0;
25-
_nmea_buff.maxlen = MAX_NMEA_SENTENCES;
26-
}
21+
GPSController::GPSController() { _gps_model = new GPSModel(); }
2722

2823
/*!
2924
* @brief Destructor for GPSController.
@@ -200,4 +195,5 @@ void GPSController::update() {
200195
}
201196
drv->SetPollPeriodPrv(cur_time);
202197
}
203-
}
198+
}
199+
}

src/components/gps/hardware.cpp

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
#include "hardware.h"
1616

17+
// Define static member
18+
GPSHardware *GPSHardware::current_instance = nullptr;
19+
1720
/*!
1821
* @brief Constructor
1922
*/
@@ -22,6 +25,8 @@ GPSHardware::GPSHardware() {
2225
_driver_type = GPS_DRV_NONE;
2326
_nmea_baud_rate = DEFAULT_MTK_NMEA_BAUD_RATE;
2427
_nmea_update_rate = DEFAULT_MTK_NMEA_UPDATE_RATE;
28+
current_instance = this;
29+
is_nmea_valid = false;
2530
}
2631

2732
/*!
@@ -38,6 +43,9 @@ GPSHardware::~GPSHardware() {
3843
delete _sfe_gps;
3944
_sfe_gps = nullptr;
4045
}
46+
if (current_instance == this) {
47+
current_instance = nullptr;
48+
}
4149
}
4250

4351
/*!
@@ -558,7 +566,18 @@ void GPSHardware::PollStoreSentences() {
558566
}
559567
}
560568
} else if (_driver_type == GPS_DRV_UBLOX) {
561-
// TODO!
569+
// Read from the GPS module for update_rate milliseconds
570+
ulong update_rate = 1000 / _nmea_update_rate;
571+
ulong start_time = millis();
572+
WS_DEBUG_PRINTLN("[gps] Polling u-blox GPS driver for new sentences...");
573+
while (millis() - start_time < update_rate) {
574+
WS_DEBUG_PRINT("...");
575+
_sfe_gps->checkUblox();
576+
}
577+
WS_DEBUG_PRINTLN("[gps] Polling complete for u-blox GPS driver!");
578+
WS_DEBUG_PRINT("Did We get a NMEA sentence? ");
579+
WS_DEBUG_PRINTLN(is_nmea_valid);
580+
// MONDAY: There is a crash right here!
562581
} else {
563582
WS_DEBUG_PRINTLN("[gps] ERROR: Unsupported GPS driver type for polling!");
564583
}
@@ -620,6 +639,7 @@ int GPSHardware::NmeaBufPop(char *sentence) {
620639
bool GPSHardware::ParseNMEASentence(char *sentence) {
621640
if (!sentence)
622641
return false;
642+
623643
if (_driver_type == GPS_DRV_MTK) {
624644
// Parse the NMEA sentence using Adafruit_GPS
625645
return _ada_gps->parse(sentence);
@@ -881,3 +901,50 @@ float GPSHardware::GetGeoidHeight() {
881901

882902
return 0.0f;
883903
}
904+
905+
// TODO: Untested, leaving for MONDAY
906+
/*!
907+
* @brief Handles incoming NMEA characters for UBlox GPS processing
908+
* @param incoming The incoming NMEA character
909+
*/
910+
void GPSHardware::HandleNmeaChar(char incoming) {
911+
static char nmea_buffer[256];
912+
static uint8_t nmea_buf_idx = 0;
913+
914+
// Add character to buffer
915+
if (nmea_buf_idx < sizeof(nmea_buffer) - 1) {
916+
nmea_buffer[nmea_buf_idx++] = incoming;
917+
}
918+
919+
// Check for sentence termination
920+
if (incoming == '\n' || incoming == '\r') {
921+
// Null terminate
922+
nmea_buffer[nmea_buf_idx] = '\0';
923+
924+
// Push complete NMEA sentence if valid
925+
if (nmea_buf_idx > 0 && nmea_buffer[0] == '$') {
926+
NmeaBufPush(nmea_buffer);
927+
is_nmea_valid = true; // Mark NMEA as valid
928+
WS_DEBUG_PRINT("[gps] Received NMEA sentence: ");
929+
WS_DEBUG_PRINTLN(nmea_buffer);
930+
}
931+
932+
// Reset for next sentence
933+
nmea_buf_idx = 0;
934+
}
935+
936+
// Safety reset if buffer gets too full
937+
if (nmea_buf_idx >= sizeof(nmea_buffer) - 1) {
938+
nmea_buf_idx = 0;
939+
}
940+
}
941+
942+
/*!
943+
* @brief Global override of the weak processNMEA function from UBlox library
944+
* @param incoming The incoming NMEA character from the UBlox GPS
945+
*/
946+
void DevUBLOXGNSS::processNMEA(char incoming) {
947+
if (GPSHardware::current_instance) {
948+
GPSHardware::current_instance->HandleNmeaChar(incoming);
949+
}
950+
}

src/components/gps/hardware.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ class GPSHardware {
9494
void ReadDiscardBuffer();
9595
void PollStoreSentences();
9696
bool ParseNMEASentence(char *sentence);
97+
void HandleNmeaChar(char incoming);
9798
// Datetime getters
9899
uint8_t GetHour();
99100
uint8_t GetMinute();
@@ -115,6 +116,10 @@ class GPSHardware {
115116
float GetAngle();
116117
float GetGeoidHeight();
117118

119+
// Static instance for global processNMEA override
120+
static GPSHardware *current_instance;
121+
bool is_nmea_valid;
122+
118123
private:
119124
bool QueryModuleType();
120125
bool DetectMtkUart();

src/components/gps/model.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,19 @@ bool GPSModel::ProcessNMEASentence(char *sentence, GPSHardware *drv) {
204204
wippersnapper_gps_GPSDateTime datetime = CreateGpsDatetime(
205205
drv->GetHour(), drv->GetMinute(), drv->GetSeconds(),
206206
drv->GetMilliseconds(), drv->GetDay(), drv->GetMonth(), drv->GetYear());
207-
207+
char lat_dir = drv->GetLatDir();
208+
char lon_dir = drv->GetLonDir();
208209
if (sentence[3] == 'R' && sentence[4] == 'M' && sentence[5] == 'C') {
209210
// Process RMC sentence
210-
if (!AddGpsEventRMC(datetime, drv->GetFix(), drv->GetLat(),
211-
&drv->GetLatDir(), drv->GetLon(), &drv->GetLonDir(),
212-
drv->GetSpeed(), drv->GetAngle()))
211+
if (!AddGpsEventRMC(datetime, drv->GetFix(), drv->GetLat(), &lat_dir,
212+
drv->GetLon(), &lon_dir, drv->GetSpeed(),
213+
drv->GetAngle()))
213214
return false;
214215
} else if (sentence[3] == 'G' && sentence[4] == 'G' && sentence[5] == 'A') {
215216
// Process GGA sentence
216-
if (!AddGpsEventGGA(datetime, drv->GetFix(), drv->GetLat(),
217-
&drv->GetLatDir(), drv->GetLon(), &drv->GetLonDir(),
218-
drv->GetNumSats(), drv->GetHDOP(), drv->GetAltitude(),
217+
if (!AddGpsEventGGA(datetime, drv->GetFix(), drv->GetLat(), &lat_dir,
218+
drv->GetLon(), &lon_dir, drv->GetNumSats(),
219+
drv->GetHDOP(), drv->GetAltitude(),
219220
drv->GetGeoidHeight()))
220221
return false;
221222
} else {

src/components/gps/model.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <protos/gps.pb.h>
1919
#define MAX_COUNT_RMC_GGA 10 ///< Maximum number of RMC or GGA responses
2020

21+
class GPSHardware; ///< Forward declaration
22+
2123
/*!
2224
@brief Provides an interface for creating, encoding, and parsing
2325
messages from GPS.proto.

0 commit comments

Comments
 (0)