Skip to content

Commit c6924af

Browse files
committed
GPS - Refactor within update() for redundancy and memory checks
1 parent 528ff03 commit c6924af

File tree

3 files changed

+58
-84
lines changed

3 files changed

+58
-84
lines changed

src/components/gps/controller.cpp

Lines changed: 50 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ void GPSController::update() {
127127
return; // bail-out!
128128

129129
for (GPSHardware *drv : _gps_drivers) {
130+
// Get the GPS driver interface from the hardware instance
131+
Adafruit_GPS *ada_gps = nullptr;
132+
if (drv->GetDriverType() == GPS_DRV_MTK) {
133+
// Interface shouldn't matter here because we already set it up in the
134+
// initialization phase, so we can just grab the Adafruit_GPS instance
135+
Adafruit_GPS *ada_gps = drv->GetAdaGps();
136+
if (ada_gps == nullptr) {
137+
WS_DEBUG_PRINTLN(
138+
"[gps] ERROR: Can't read - GPS instance not initialized!");
139+
continue;
140+
}
141+
} else {
142+
WS_DEBUG_PRINTLN(
143+
"[gps] ERROR: Unsupported GPS driver type, skipping update()!");
144+
continue;
145+
}
130146

131147
// TODO: Commented out due to parsing failures, stability issue (failed to
132148
// parse NMEA acks for this) Perform a keep-alive check by sending an
@@ -142,125 +158,76 @@ void GPSController::update() {
142158
continue; // Not yet elapsed, skip this driver
143159

144160
// Discard the GPS buffer before we attempt to do a fresh read
145-
size_t bytes_avail = drv->GetAdaGps()->available();
161+
size_t bytes_avail = ada_gps->available();
146162
if (bytes_avail > 0) {
147-
// TODO: Remove these two WS_DEBUG_PRINTs!
148-
WS_DEBUG_PRINT("[gps] Discarding GPS data: ");
149-
WS_DEBUG_PRINTLN(bytes_avail);
150-
// Read the available data from the GPS module
151-
// and discard it
152163
for (size_t i = 0; i < bytes_avail; i++) {
153-
drv->GetAdaGps()->read();
164+
ada_gps->read();
154165
}
155166
}
156167

157-
// Unset the received flag
158-
if (drv->GetAdaGps()->newNMEAreceived()) {
159-
drv->GetAdaGps()->lastNMEA();
168+
// Unset the RX flag
169+
if (ada_gps->newNMEAreceived()) {
170+
ada_gps->lastNMEA();
160171
}
161172

162173
// Let's attempt to get a sentence from the GPS module
163-
// Convert the NMEA update rate to milliseconds
164-
// TODO: This should be stored as a member within the hardware class
165-
ulong update_rate = 1000 / drv->GetNmeaUpdateRate();
166-
167174
// Read from the GPS module for update_rate milliseconds
175+
ulong update_rate = 1000 / drv->GetNmeaUpdateRate();
168176
ulong start_time = millis();
169-
int read_calls = 0;
170177

171178
WS_DEBUG_PRINT("[gps] Reading GPS data for ");
172179
WS_DEBUG_PRINT(update_rate);
173-
WS_DEBUG_PRINTLN(" milliseconds...");
180+
WS_DEBUG_PRINTLN(" ms...");
174181
while (millis() - start_time < update_rate) {
175-
char c = drv->GetAdaGps()->read();
176-
read_calls++;
177-
182+
char c = ada_gps->read();
178183
// Check if we have a new NMEA sentence
179-
if (drv->GetAdaGps()->newNMEAreceived()) {
184+
if (ada_gps->newNMEAreceived()) {
180185
// If we have a new sentence, push it to the buffer
181-
char *last_nmea = drv->GetAdaGps()->lastNMEA();
182-
NmeaBufPush(drv->GetAdaGps()->lastNMEA());
186+
char *last_nmea = ada_gps->lastNMEA();
187+
NmeaBufPush(ada_gps->lastNMEA());
183188
}
184189
}
185-
WS_DEBUG_PRINTLN("[gps] Finished reading GPS data.");
186-
// We are done reading for this period, create a new GPSEvent message
187-
_gps_model->CreateGPSEvent();
188190

189-
// TODO: This is for debugging purposes only, remove later!
190-
WS_DEBUG_PRINT("[gps] Read ");
191-
WS_DEBUG_PRINT(read_calls);
192-
WS_DEBUG_PRINTLN(" times from GPS module.");
193-
// Pop off the buffer and parse
191+
// Parse each NMEA sentence in the buffer
194192
char nmea_sentence[MAX_LEN_NMEA_SENTENCE];
195-
// Pop until we have no more sentences in the buffer
193+
bool has_gps_event = false;
196194
while (NmeaBufPop(nmea_sentence) != -1) {
197195
// Parse the NMEA sentence
198196
WS_DEBUG_PRINT("[gps] Parsing NMEA sentence: ");
199197
WS_DEBUG_PRINTLN(nmea_sentence);
200-
if (!drv->GetAdaGps()->parse(nmea_sentence))
198+
if (!ada_gps->parse(nmea_sentence)) {
201199
continue; // Skip parsing this sentence if parsing failed
202-
203-
// Print the parsed data for debugging
204-
Serial.print("Fix: ");
205-
Serial.print((int)drv->GetAdaGps()->fix);
206-
Serial.print(" quality: ");
207-
Serial.println((int)drv->GetAdaGps()->fixquality);
208-
if (drv->GetAdaGps()->fix) {
209-
Serial.print("Location: ");
210-
Serial.print(drv->GetAdaGps()->latitude, 4);
211-
Serial.print(drv->GetAdaGps()->lat);
212-
Serial.print(", ");
213-
Serial.print(drv->GetAdaGps()->longitude, 4);
214-
Serial.println(drv->GetAdaGps()->lon);
215-
Serial.print("Speed (knots): ");
216-
Serial.println(drv->GetAdaGps()->speed);
217-
Serial.print("Angle: ");
218-
Serial.println(drv->GetAdaGps()->angle);
219-
Serial.print("Altitude: ");
220-
Serial.println(drv->GetAdaGps()->altitude);
221-
Serial.print("Satellites: ");
222-
Serial.println((int)drv->GetAdaGps()->satellites);
223-
Serial.print("Antenna status: ");
224-
Serial.println((int)drv->GetAdaGps()->antenna);
200+
} else {
201+
_gps_model->CreateGPSEvent();
202+
has_gps_event = true;
225203
}
226204

227-
// now, let's build the model from the sentence
205+
// Build the GPSEvent message from the sentence
206+
wippersnapper_gps_GPSDateTime datetime = _gps_model->CreateGpsDatetime(
207+
ada_gps->hour, ada_gps->minute, ada_gps->seconds,
208+
ada_gps->milliseconds, ada_gps->day, ada_gps->month, ada_gps->year);
228209
if (strncmp(nmea_sentence, "$GPRMC", 6) == 0) {
229-
WS_DEBUG_PRINT(
230-
"[gps] Adding RMC to GPSEvent..."); // TODO: This is for debug,
231-
// remove in production!
232-
wippersnapper_gps_GPSDateTime datetime = _gps_model->CreateGpsDatetime(
233-
drv->GetAdaGps()->hour, drv->GetAdaGps()->minute,
234-
drv->GetAdaGps()->seconds, drv->GetAdaGps()->milliseconds,
235-
drv->GetAdaGps()->day, drv->GetAdaGps()->month,
236-
drv->GetAdaGps()->year);
237210
_gps_model->AddGpsEventRMC(
238-
datetime, drv->GetAdaGps()->fix, drv->GetAdaGps()->latitude,
239-
&drv->GetAdaGps()->lat, drv->GetAdaGps()->longitude,
240-
&drv->GetAdaGps()->lon, drv->GetAdaGps()->speed,
241-
drv->GetAdaGps()->angle);
242-
WS_DEBUG_PRINTLN("added!"); // TODO: THIS IS FOR DEBUG, REMOVE IN PROD
211+
datetime, ada_gps->fix, ada_gps->latitude, &ada_gps->lat,
212+
ada_gps->longitude, &ada_gps->lon, ada_gps->speed, ada_gps->angle);
243213
} else if (strncmp(nmea_sentence, "$GPGGA", 6) == 0) {
244-
WS_DEBUG_PRINT(
245-
"[gps] Adding GGA to GPSEvent..."); // TODO: This is for debug,
246-
// remove in production!
247-
wippersnapper_gps_GPSDateTime datetime = _gps_model->CreateGpsDatetime(
248-
drv->GetAdaGps()->hour, drv->GetAdaGps()->minute,
249-
drv->GetAdaGps()->seconds, drv->GetAdaGps()->milliseconds,
250-
drv->GetAdaGps()->day, drv->GetAdaGps()->month,
251-
drv->GetAdaGps()->year);
252214
_gps_model->AddGpsEventGGA(
253-
datetime, drv->GetAdaGps()->fix, drv->GetAdaGps()->latitude,
254-
&drv->GetAdaGps()->lat, drv->GetAdaGps()->longitude,
255-
&drv->GetAdaGps()->lon, drv->GetAdaGps()->satellites,
256-
drv->GetAdaGps()->HDOP, drv->GetAdaGps()->altitude,
257-
drv->GetAdaGps()->geoidheight);
258-
WS_DEBUG_PRINTLN("added!"); // TODO: THIS IS FOR DEBUG, REMOVE IN PROD
215+
datetime, ada_gps->fix, ada_gps->latitude, &ada_gps->lat,
216+
ada_gps->longitude, &ada_gps->lon, ada_gps->satellites,
217+
ada_gps->HDOP, ada_gps->altitude, ada_gps->geoidheight);
259218
} else {
260219
WS_DEBUG_PRINTLN(
261220
"[gps] WARNING - Parsed sentence is not type RMC or GGA!");
262221
}
263222
}
223+
224+
// We did not create a GPSEvent because the NMEA sentences were not
225+
// GGA/RMC or parsed correctly
226+
if (!has_gps_event) {
227+
WS_DEBUG_PRINTLN("[gps] No GPSEvent created from NMEA sentences!");
228+
continue;
229+
}
230+
264231
// Encode and publish to IO
265232
WS_DEBUG_PRINT("[gps] Encoding and publishing GPSEvent to IO...");
266233
bool did_encode = _gps_model->EncodeGPSEvent();

src/components/gps/hardware.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,10 @@ void GPSHardware::SetPrvKat(ulong kat_prv) {
330330
* @brief Gets the last time the GPS hardware was polled.
331331
* @returns The last time the GPS hardware was polled, in milliseconds.
332332
*/
333-
ulong GPSHardware::GetPrvKat() { return _kat_prv; }
333+
ulong GPSHardware::GetPrvKat() { return _kat_prv; }
334+
335+
/*!
336+
* @brief Returns the driver type of the GPS hardware.
337+
* @returns The driver type of the GPS hardware.
338+
*/
339+
GpsDriverType GPSHardware::GetDriverType() { return _driver_type; }

src/components/gps/hardware.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class GPSHardware {
6969
// TODO: Add SetInterface(I2C *_i2c_hardware) for I2C support here!
7070
bool Handle_GPSConfig(wippersnapper_gps_GPSConfig *gps_config);
7171
Adafruit_GPS *GetAdaGps();
72+
GpsDriverType GetDriverType();
7273

7374
private:
7475
bool QueryModuleType();

0 commit comments

Comments
 (0)