@@ -127,6 +127,22 @@ void GPSController::update() {
127
127
return ; // bail-out!
128
128
129
129
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
+ }
130
146
131
147
// TODO: Commented out due to parsing failures, stability issue (failed to
132
148
// parse NMEA acks for this) Perform a keep-alive check by sending an
@@ -142,125 +158,76 @@ void GPSController::update() {
142
158
continue ; // Not yet elapsed, skip this driver
143
159
144
160
// 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 ();
146
162
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
152
163
for (size_t i = 0 ; i < bytes_avail; i++) {
153
- drv-> GetAdaGps () ->read ();
164
+ ada_gps ->read ();
154
165
}
155
166
}
156
167
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 ();
160
171
}
161
172
162
173
// 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
-
167
174
// Read from the GPS module for update_rate milliseconds
175
+ ulong update_rate = 1000 / drv->GetNmeaUpdateRate ();
168
176
ulong start_time = millis ();
169
- int read_calls = 0 ;
170
177
171
178
WS_DEBUG_PRINT (" [gps] Reading GPS data for " );
172
179
WS_DEBUG_PRINT (update_rate);
173
- WS_DEBUG_PRINTLN (" milliseconds ..." );
180
+ WS_DEBUG_PRINTLN (" ms ..." );
174
181
while (millis () - start_time < update_rate) {
175
- char c = drv->GetAdaGps ()->read ();
176
- read_calls++;
177
-
182
+ char c = ada_gps->read ();
178
183
// Check if we have a new NMEA sentence
179
- if (drv-> GetAdaGps () ->newNMEAreceived ()) {
184
+ if (ada_gps ->newNMEAreceived ()) {
180
185
// 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 ());
183
188
}
184
189
}
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 ();
188
190
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
194
192
char nmea_sentence[MAX_LEN_NMEA_SENTENCE];
195
- // Pop until we have no more sentences in the buffer
193
+ bool has_gps_event = false ;
196
194
while (NmeaBufPop (nmea_sentence) != -1 ) {
197
195
// Parse the NMEA sentence
198
196
WS_DEBUG_PRINT (" [gps] Parsing NMEA sentence: " );
199
197
WS_DEBUG_PRINTLN (nmea_sentence);
200
- if (!drv-> GetAdaGps ()-> parse (nmea_sentence))
198
+ if (!ada_gps-> parse (nmea_sentence)) {
201
199
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 ;
225
203
}
226
204
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 );
228
209
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 );
237
210
_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 );
243
213
} 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 );
252
214
_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 );
259
218
} else {
260
219
WS_DEBUG_PRINTLN (
261
220
" [gps] WARNING - Parsed sentence is not type RMC or GGA!" );
262
221
}
263
222
}
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
+
264
231
// Encode and publish to IO
265
232
WS_DEBUG_PRINT (" [gps] Encoding and publishing GPSEvent to IO..." );
266
233
bool did_encode = _gps_model->EncodeGPSEvent ();
0 commit comments