forked from SlashDevin/NeoGPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NMEAdiagnostic.ino
349 lines (272 loc) · 9.43 KB
/
NMEAdiagnostic.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <NMEAGPS.h>
//======================================================================
// Program: NMEAdiagnostic.ino
//
// Description: This program tries different baud rates until
// valid NMEA sentences are detected. Some GPS devices may
// have a binary mode that does not emit NMEA sentences. You
// may have to send a special command or use a utility program
// to configure it to emit NMEA sentences instead of binary messages.
//
// Prerequisites:
// 1) Your GPS device has been correctly powered.
// Be careful when connecting 3.3V devices.
// 2) Your GPS device is correctly connected to an Arduino serial port.
// See GPSport.h for the default connections.
//
// 'Serial' is for debug output to the Serial Monitor window.
//
//======================================================================
#include <GPSport.h>
#include <Streamers.h>
// Check configuration
#ifndef NMEAGPS_RECOGNIZE_ALL
#error You must define NMEAGPS_RECOGNIZE_ALL in NMEAGPS_cfg.h!
#endif
#ifdef NMEAGPS_IMPLICIT_MERGING
#error You must *undefine* NMEAGPS_IMPLICIT_MERGING in NMEAGPS_cfg.h! \
Please use EXPLICIT or NO_MERGING.
#endif
#ifdef NMEAGPS_INTERRUPT_PROCESSING
#error You must *NOT* define NMEAGPS_INTERRUPT_PROCESSING in NMEAGPS_cfg.h!
#endif
static NMEAGPS gps ; // This parses received characters
static gps_fix all_data ; // A composite of all GPS data fields
static uint32_t last_rx = 0UL; // The last millis() time a character was
// received from GPS.
static uint32_t valid_sentence_received = 0UL;
static bool last_sentence_received = false;
static uint32_t baudStartTime = 0UL;
static uint8_t warnings = 0;
static uint8_t errors = 0;
//--------------------------
static void hang()
{
DEBUG_PORT.println( F("\n** NMEAdiagnostic completed **\n") );
if (warnings) {
DEBUG_PORT.print( warnings );
DEBUG_PORT.print( F(" warnings") );
}
if (warnings && errors)
DEBUG_PORT.print( F(" and ") );
if (errors) {
DEBUG_PORT.print( errors );
DEBUG_PORT.print( F(" errors") );
}
if (warnings || errors)
DEBUG_PORT.println();
DEBUG_PORT.flush();
for (;;)
;
} // hang
//--------------------------
// Baud rates to check
static long baud_table[] =
{ 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400,
57600, 115200 };
static const uint8_t num_bauds = sizeof(baud_table)/sizeof(baud_table[0]);
static const uint8_t INITIAL_BAUD_INDEX = 3; // 9600
static uint8_t baud_index = INITIAL_BAUD_INDEX;
static bool triedDifferentBaud = false;
//--------------------------
static void tryBaud()
{
long baud = baud_table[baud_index];
DEBUG_PORT.print( F("\n____________________________\n\nChecking ") );
DEBUG_PORT.print( baud );
DEBUG_PORT.print( F(" baud...\n") );
DEBUG_PORT.flush();
//if (baud == 9600) baud = 17000;
gpsPort.begin( baud );
baudStartTime = millis();
} // tryBaud
//--------------------------
static void tryAnotherBaudRate()
{
gpsPort.end();
while (gpsPort.available())
gpsPort.read();
if (baud_index == INITIAL_BAUD_INDEX) {
baud_index = 0;
} else {
baud_index++;
if (baud_index == INITIAL_BAUD_INDEX)
baud_index++; // skip it, we already tried it
if (baud_index >= num_bauds) {
baud_index = INITIAL_BAUD_INDEX;
DEBUG_PORT.print( F("\n All baud rates tried!\n") );
hang();
}
}
tryBaud();
triedDifferentBaud = true;
} // tryAnotherBaudRate
//------------------------------------
static const uint16_t MAX_SAMPLE = 256;
static uint8_t someChars[ MAX_SAMPLE ];
static uint16_t someCharsIndex = 0;
static void dumpSomeChars()
{
if (someCharsIndex > 0) {
DEBUG_PORT.print( F("Received data:\n") );
const uint16_t bytes_per_line = 32;
char ascii[ bytes_per_line ];
uint8_t *ptr = &someChars[0];
for (uint16_t i=0; i<someCharsIndex; ) {
uint16_t j;
for (j=0; (i<someCharsIndex) && (j<bytes_per_line); i++, j++) {
uint8_t c = *ptr++;
if (c < 0x10)
DEBUG_PORT.print('0');
DEBUG_PORT.print( c, HEX );
if ((' ' <= c) && (c <= '~'))
ascii[ j ] = c;
else
ascii[ j ] = '.';
}
uint16_t jmax = j;
while (j++ < bytes_per_line)
DEBUG_PORT.print( F(" ") );
DEBUG_PORT.print( ' ' );
for (j=0; j<jmax; j++)
DEBUG_PORT.print( ascii[ j ] );
DEBUG_PORT.print( '\n' );
}
DEBUG_PORT.flush();
someCharsIndex = 0;
}
} // dumpSomeChars
//----------------------------------------------------------------
void displaySentences()
{
// We received one or more sentences, display the baud rate
DEBUG_PORT.print( F("\n\n**** NMEA sentence(s) detected! ****\n") );
dumpSomeChars();
DEBUG_PORT << F("\nDevice baud rate is ") <<
baud_table[ baud_index ] << '\n';
DEBUG_PORT.print( F("\nGPS data fields received:\n\n ") );
trace_header( DEBUG_PORT );
DEBUG_PORT.print( F(" ") );
trace_all( DEBUG_PORT, gps, all_data );
if (!last_sentence_received) {
warnings++;
DEBUG_PORT.print( F("\nWarning: LAST_SENTENCE_IN_INTERVAL defined to be ") );
DEBUG_PORT.print( gps.string_for( LAST_SENTENCE_IN_INTERVAL ) );
DEBUG_PORT.println( F(", but was never received.\n"
" Please use NMEAorder.ino to determine which sentences your GPS device sends, and then\n"
" use the last one for the definition in NMEAGPS_cfg.h.") );
}
} // displaySentences
//----------------------------------------------------------------
// Listen to see if the GPS device is correctly
// connected and functioning.
static void listenForSomething()
{
uint32_t current_ms = millis();
uint32_t ms_since_last_rx = current_ms - last_rx;
bool waited_long_enough = (current_ms - baudStartTime) > 1000UL;
if ((ms_since_last_rx > 5) && waited_long_enough) {
// The GPS device has not sent any characters for at least 5ms.
// See if we've been getting chars sometime during the last second.
// If not, the GPS may not be working or connected properly.
bool getting_chars = (someCharsIndex > 0);
// Try to diagnose the problem
static uint8_t tries = 1;
bool tryNext = false;
if (!getting_chars) {
if (tries++ >= 3) {
errors++;
DEBUG_PORT.println( F("\nCheck GPS device and/or connections. No data received.\n") );
tryNext = true;
}
} else if (valid_sentence_received) {
uint8_t s = valid_sentence_received/1000;
uint8_t ms = valid_sentence_received - s*1000;
DEBUG_PORT.print( F("Valid sentences were received ") );
DEBUG_PORT.print( s );
DEBUG_PORT.print( '.' );
if (ms < 100)
DEBUG_PORT.print( '0' );
if (ms < 10)
DEBUG_PORT.print( '0' );
DEBUG_PORT.print( ms );
DEBUG_PORT.println(
F(" seconds ago.\n"
" The GPS update rate may be lower than 1Hz,\n"
" or the connections may be bad." ) );
displaySentences();
hang();
} else {
DEBUG_PORT.println(
F("No valid sentences, but characters are being received.\n"
" Check baud rate or device protocol configuration.\n" ) );
dumpSomeChars();
delay( 2000 );
tryNext = true;
}
if (tryNext) {
tries = 1;
tryAnotherBaudRate();
valid_sentence_received = 0UL;
}
}
} // listenForSomething
//------------------------------------
static void GPSloop()
{
while (gpsPort.available()) {
last_rx = millis();
uint8_t c = gpsPort.read();
if (someCharsIndex < MAX_SAMPLE)
someChars[ someCharsIndex++ ] = c;
if (gps.decode( c ) == NMEAGPS::DECODE_COMPLETED) {
all_data |= gps.fix();
valid_sentence_received = last_rx;
if (gps.nmeaMessage == LAST_SENTENCE_IN_INTERVAL)
last_sentence_received = true;
DEBUG_PORT.print( F("Received ") );
DEBUG_PORT.println( gps.string_for( gps.nmeaMessage ) );
static uint8_t sentences_printed = 0;
bool long_enough = (millis() - baudStartTime > 3000);
if (long_enough ||
(
(sentences_printed++ >= 20) &&
(someCharsIndex >= MAX_SAMPLE)
) ) {
displaySentences();
hang();
}
}
}
if (!valid_sentence_received ||
(millis() - valid_sentence_received > 3000UL))
listenForSomething();
} // GPSloop
//--------------------------
void setup()
{
DEBUG_PORT.begin(9600);
while (!DEBUG_PORT)
;
DEBUG_PORT.print( F("NMEAdiagnostic.INO: started\n") );
DEBUG_PORT.println( F("Looking for GPS device on " GPS_PORT_NAME) );
if (sizeof(gps_fix) <= 2) {
warnings++;
DEBUG_PORT.print( F("\nWarning: no fields are enabled in GPSfix_cfg.h.\n Only the following information will be displayed:\n ") );
trace_header( DEBUG_PORT );
}
#if !defined( NMEAGPS_PARSE_GGA ) & !defined( NMEAGPS_PARSE_GLL ) & \
!defined( NMEAGPS_PARSE_GSA ) & !defined( NMEAGPS_PARSE_GSV ) & \
!defined( NMEAGPS_PARSE_RMC ) & !defined( NMEAGPS_PARSE_VTG ) & \
!defined( NMEAGPS_PARSE_ZDA ) & !defined( NMEAGPS_PARSE_GST )
warnings++;
DEBUG_PORT.println( F("\nWarning: no messages are enabled for parsing in NMEAGPS_cfg.h.\n No fields will be valid, including the 'status' field.") );
#endif
DEBUG_PORT.flush();
tryBaud();
} // setup
//--------------------------
void loop()
{
GPSloop();
}