-
Notifications
You must be signed in to change notification settings - Fork 1
/
gps-ublox.cpp
345 lines (300 loc) · 8.24 KB
/
gps-ublox.cpp
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
#include "config.h"
#if GPS_UBLOX
#include <Arduino.h>
#include "gps.h"
#include "timing.h"
#include "debug.h"
#include "health.h"
#include "monitor.h"
#define GPS_BUFFER_SIZE 256
enum gps_state_t {
GPS_SYNC1, // 0
GPS_SYNC2, // 1
GPS_CLASS, // 2
GPS_ID, // 3
GPS_LENGTH1, // 4
GPS_LENGTH2, // 5
GPS_DATA, // 6
GPS_CHECKSUM1, // 7
GPS_CHECKSUM2, // 8
};
static inline int gps_can_read() {
return GPS.available();
}
static inline int gps_read() {
return GPS.read();
}
static inline void gps_write(const char *data) {
GPS.print(data);
}
static inline void gps_writebyte(const char ch) {
GPS.write(ch);
}
static inline void gps_checksum(const unsigned char ch, unsigned char *ck_a, unsigned char *ck_b) {
*ck_a += ch;
*ck_b += *ck_a;
}
static inline void gps_writebyte_check(const unsigned char ch, unsigned char *ck_a, unsigned char *ck_b) {
GPS.write(ch);
gps_checksum(ch, ck_a, ck_b);
}
void gps_write_ublox(const unsigned short packetid, const char *packet, int len) {
unsigned char ck_a = 0, ck_b = 0;
gps_writebyte(0xb5);
gps_writebyte(0x62);
gps_writebyte_check(packetid >> 8, &ck_a, &ck_b);
gps_writebyte_check(packetid & 0xff, &ck_a, &ck_b);
gps_writebyte_check(len & 0xff, &ck_a, &ck_b);
gps_writebyte_check(len >> 16, &ck_a, &ck_b);
for (int i = 0 ; i < len ; i++) {
gps_writebyte_check(packet[i], &ck_a, &ck_b);
}
gps_writebyte(ck_a);
gps_writebyte(ck_b);
}
/*
static void gps_set_serial_options() {
gps_write_tsip(0xBC,
"\x00" // Port 0
"\x0A" // Input rate 57600
"\x0A" // Output rate 57600
"\x03" // 8 bit
"\x00" // No parity
"\x00" // 1 stop bit
"\x00" // No flow control
"\x02" // Input TSIP
"\x02" // Output TSIP
"\x00" // Reserved
, 10
);
GPS.flush();
delay(500);
GPS.begin(57600, SERIAL_8N1);
GPS.flush();
}
*/
static void gps_set_serial_options() {
gps_write_ublox(0x600,
"\x01" // Port 1
"\x00" // Reserved
"\x00\x00" // txReady disabled
"",
4
);
}
static enum gps_state_t decoder_state = GPS_SYNC1;
static char gps_message_valid;
static unsigned short gps_packetid;
static unsigned int gps_payload_len;
static unsigned int gps_payload_read;
static unsigned char gps_payload[GPS_BUFFER_SIZE];
static unsigned char *gps_payload_ptr;
static unsigned char ck_a, ck_b;
void gps_handle_message();
void gps_poll() {
while (gps_can_read()) {
int ch = gps_read();
switch (decoder_state) {
case GPS_SYNC1:
// debug("S1 ");
if (ch == 0xb5)
decoder_state = GPS_SYNC2;
else {
// debug("Got garbage char "); debug_hex(ch); debug(" from UBX\r\n");
}
break;
case GPS_SYNC2:
// debug("S2 ");
if (ch == 0x62) {
ck_a = ck_b = 0;
decoder_state = GPS_CLASS;
} else {
// debug("Got garbage char "); debug_hex(ch); debug(" from UBX\r\n");
decoder_state = GPS_SYNC1;
}
break;
case GPS_CLASS:
// debug("I1 ");
gps_packetid = ch << 8;
gps_checksum(ch, &ck_a, &ck_b);
decoder_state = GPS_ID;
break;
case GPS_ID:
// debug("I2 ");
gps_packetid |= ch;
gps_checksum(ch, &ck_a, &ck_b);
decoder_state = GPS_LENGTH1;
break;
case GPS_LENGTH1:
// debug("L1 ");
gps_payload_len = ch;
gps_checksum(ch, &ck_a, &ck_b);
decoder_state = GPS_LENGTH2;
break;
case GPS_LENGTH2:
// debug("L2 ");
gps_payload_len |= ch << 8;
gps_checksum(ch, &ck_a, &ck_b);
gps_payload_ptr = gps_payload;
gps_payload_read = 0;
if (gps_payload_len < GPS_BUFFER_SIZE)
gps_message_valid = 1;
else
gps_message_valid = 0;
if (gps_payload_len > 0)
decoder_state = GPS_DATA;
else
decoder_state = GPS_CHECKSUM1;
break;
case GPS_DATA:
// debug("DD ");
if (gps_message_valid)
*(gps_payload_ptr++) = ch;
gps_checksum(ch, &ck_a, &ck_b);
gps_payload_read ++;
if (gps_payload_read == gps_payload_len)
decoder_state = GPS_CHECKSUM1;
break;
case GPS_CHECKSUM1:
// debug("C1 ");
if (ch != ck_a) {
debug("Invalid CK_A from UBX: "); debug_hex(ch); debug(" != "); debug_hex(ck_a); debug(", message was "); debug_hex(gps_packetid); debug("\r\n");
gps_message_valid = 0;
}
decoder_state = GPS_CHECKSUM2;
break;
case GPS_CHECKSUM2:
// debug("C2 ");
if (ch != ck_b) {
debug("Invalid CK_B from UBX: "); debug_hex(ch); debug(" != "); debug_hex(ck_b); debug(", message was "); debug_hex(gps_packetid); debug("\r\n");
gps_message_valid = 0;
}
if (gps_message_valid)
gps_handle_message();
decoder_state = GPS_SYNC1;
break;
}
// debug_hex(ch);
// debug("\r\n");
}
}
static int32_t pps_adjust;
void gps_message_tim_tp() {
unsigned int tow_msec = *((unsigned int *)(gps_payload));
unsigned short gps_week = *((unsigned short *)(gps_payload + 12));
char flags = gps_payload[14];
int32_t quant = *((int32_t *)(gps_payload + 8));
monitor_send("sawtooth", quant);
pps_adjust = -quant / 1000;
time_set_date(gps_week, (tow_msec / 1000), -1);
}
void gps_message_nav_sat() {
}
void gps_message_nav_status() {
static const char *fix_type_msg[] = {
"NONE", "DR", "2D", "3D", "GPS+DR", "TIME"
};
static const char *flag_msg[] = {
"FIX_OK", "DGPS", "WN_OK", "TOW_OK"
};
unsigned char fix_type = gps_payload[4];
unsigned char flags = gps_payload[5];
debug("NAV-STATUS: ");
debug(fix_type_msg[fix_type]);
for (int i = 0 ; i < sizeof(flag_msg) / sizeof(*flag_msg) ; i++) {
if (flags & 1 << i) {
debug(" ");
debug(flag_msg[i]);
}
}
debug("\r\n");
if (flags & 0x0B == 0x0B) {
health_set_gps_status(GPS_OK);
} else {
health_set_gps_status(GPS_UNLOCK);
}
health_reset_gps_watchdog();
}
void gps_message_nav_timeutc() {
unsigned short year = *((unsigned short *)(gps_payload + 12));
unsigned char month = gps_payload[14];
unsigned char day = gps_payload[15];
unsigned char hour = gps_payload[16];
unsigned char minute = gps_payload[17];
unsigned char second = gps_payload[18];
unsigned char flags = gps_payload[19];
debug("NAV-TIMEUTC: ");
debug_int(year); debug("-"); debug_int(month); debug("-"); debug_int(day);
debug(" ");
debug_int(hour); debug(":"); debug_int(minute); debug(":"); debug_int(second);
debug("\r\n");
}
void gps_message_nav_clock() {
}
void gps_message_nav_svin() {
}
#if GPS_UBLOX_TIMESTAMP
// To use this, connect the PPS output of the Due (pin 22)
// to EXTINT0 on the uBlox.
static bool have_timestamp;
static int32_t timestamp;
void gps_message_tim_tm2() {
unsigned char flags = gps_payload[1];
if (flags & 0xe0 == 0xe0) {
uint32_t ms_r = *((uint32_t *)(gps_payload + 8));
uint32_t ns_r = *((uint32_t *)(gps_payload + 12));
have_timestamp = true;
timestamp = -((ms_r % 1000) * 1000000 + ns_r) + PPSOUT_OFFSET_NS;
}
}
bool gps_get_timestamp(int32_t *dest) {
if (have_timestamp) {
*dest = timestamp + pps_adjust;
have_timestamp = false;
return true;
}
return false;
}
#else
void gps_message_tim_tm2() {
}
bool gps_get_timestamp(int32_t *dest) {
return false;
}
#endif
void gps_handle_message() {
switch (gps_packetid) {
case 0x0d01:
gps_message_tim_tp();
break;
case 0x0135:
gps_message_nav_sat();
break;
case 0x0103:
gps_message_nav_status();
break;
case 0x0121:
gps_message_nav_timeutc();
break;
case 0x0122:
gps_message_nav_clock();
break;
case 0x0d03:
gps_message_tim_tm2();
case 0x0d04:
gps_message_nav_svin();
break;
default:
debug("Got "); debug_int(gps_payload_len);
debug(" byte message, type "); debug_hex(gps_packetid);
debug("\r\n");
break;
}
}
void gps_init() {
// We don't send any configuration to the unit. It should be configured
// for fixed position, timing mode (if applicable), 1Hz positive timepulse,
// and UBX-TIM-TP, UBX-NAV-TIMEUTC, and UBX-NAV-STATUS messages at a minimum.
GPS.begin(57600, SERIAL_8N1);
}
#endif