2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #define AO_GPS_LEADER 2
24 static __code char ao_gps_header[] = "GP";
26 __xdata uint8_t ao_gps_mutex;
27 static __data char ao_gps_char;
28 static __data uint8_t ao_gps_cksum;
29 static __data uint8_t ao_gps_error;
31 __pdata uint16_t ao_gps_tick;
32 __xdata struct ao_telemetry_location ao_gps_data;
33 __xdata struct ao_telemetry_satellite ao_gps_tracking_data;
35 static __pdata uint16_t ao_gps_next_tick;
36 static __pdata struct ao_telemetry_location ao_gps_next;
37 static __pdata uint8_t ao_gps_date_flags;
38 static __pdata struct ao_telemetry_satellite ao_gps_tracking_next;
40 #define STQ_S 0xa0, 0xa1
41 #define STQ_E 0x0d, 0x0a
42 #define SKYTRAQ_MSG_2(id,a,b) \
43 STQ_S, 0, 3, id, a,b, (id^a^b), STQ_E
44 #define SKYTRAQ_MSG_3(id,a,b,c) \
45 STQ_S, 0, 4, id, a,b,c, (id^a^b^c), STQ_E
46 #define SKYTRAQ_MSG_8(id,a,b,c,d,e,f,g,h) \
47 STQ_S, 0, 9, id, a,b,c,d,e,f,g,h, (id^a^b^c^d^e^f^g^h), STQ_E
48 #define SKYTRAQ_MSG_14(id,a,b,c,d,e,f,g,h,i,j,k,l,m,n) \
49 STQ_S, 0,15, id, a,b,c,d,e,f,g,h,i,j,k,l,m,n, \
50 (id^a^b^c^d^e^f^g^h^i^j^k^l^m^n), STQ_E
52 static __code uint8_t ao_gps_config[] = {
53 SKYTRAQ_MSG_8(0x08, 1, 1, 1, 1, 1, 1, 1, 0), /* configure nmea */
61 /* attributes (0 = update to sram, 1 = update flash too) */
63 SKYTRAQ_MSG_2(0x3c, 0x00, 0x00), /* configure navigation mode */
64 /* 0 = car, 1 = pedestrian */
65 /* 0 = update to sram, 1 = update sram + flash */
75 c = ao_serial_getchar();
81 ao_gps_skip_field(void)
85 if (c == ',' || c == '*' || c == '\n')
95 if (c == ',' || c == '.' || c == '*')
99 __data static uint8_t ao_gps_num_width;
102 ao_gps_decimal(uint8_t max_width)
108 if (ao_gps_char == '-') {
113 ao_gps_num_width = 0;
114 while (ao_gps_num_width < max_width) {
115 uint8_t c = ao_gps_char;
116 if (c < (uint8_t) '0' || (uint8_t) '9' < c)
118 v = v * 10 + (uint8_t) (c - (uint8_t) '0');
134 ao_gps_num_width = 0;
135 while (ao_gps_num_width < 2) {
136 uint8_t c = ao_gps_char;
138 if ((uint8_t) '0' <= c && c <= (uint8_t) '9')
140 else if ((uint8_t) 'A' <= c && c <= (uint8_t) 'F')
142 else if ((uint8_t) 'a' <= c && c <= (uint8_t) 'f')
146 v = (v << 4) | (c + d);
154 ao_gps_parse_pos(uint8_t deg_width) __reentrant
156 static __pdata uint16_t d;
157 static __pdata uint8_t m;
158 static __pdata uint16_t f;
161 d = ao_gps_decimal(deg_width);
162 m = ao_gps_decimal(2);
165 f = ao_gps_decimal(4);
166 while (ao_gps_num_width < 4) {
175 return d * 10000000l + (m * 10000l + f) * 50 / 3;
179 ao_gps_parse_flag(char no_c, char yes_c)
183 if (ao_gps_char == yes_c)
185 else if (ao_gps_char == no_c)
198 /* Now read the data into the gps data record
200 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
204 * 025149.000 time (02:51:49.000 GMT)
205 * 4528.1723,N Latitude 45°28.1723' N
206 * 12244.2480,W Longitude 122°44.2480' W
212 * 4 = Real Time Kinematic
214 * 6 = estimated (dead reckoning)
215 * 7 = Manual input mode
216 * 8 = Simulation mode
217 * 05 Number of satellites (5)
218 * 2.0 Horizontal dilution
219 * 103.5,M Altitude, 103.5M above msl
220 * -19.5,M Height of geoid above WGS84 ellipsoid
221 * ? time in seconds since last DGPS update
222 * 0000 DGPS station ID
226 ao_gps_next_tick = ao_time();
227 ao_gps_next.flags = AO_GPS_RUNNING | ao_gps_date_flags;
228 ao_gps_next.hour = ao_gps_decimal(2);
229 ao_gps_next.minute = ao_gps_decimal(2);
230 ao_gps_next.second = ao_gps_decimal(2);
231 ao_gps_skip_field(); /* skip seconds fraction */
233 ao_gps_next.latitude = ao_gps_parse_pos(2);
234 if (ao_gps_parse_flag('N', 'S'))
235 ao_gps_next.latitude = -ao_gps_next.latitude;
236 ao_gps_next.longitude = ao_gps_parse_pos(3);
237 if (ao_gps_parse_flag('E', 'W'))
238 ao_gps_next.longitude = -ao_gps_next.longitude;
240 i = ao_gps_decimal(0xff);
242 ao_gps_next.flags |= AO_GPS_VALID;
244 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
245 if (i > AO_GPS_NUM_SAT_MASK)
246 i = AO_GPS_NUM_SAT_MASK;
247 ao_gps_next.flags |= i;
250 i = ao_gps_decimal(0xff);
253 if (ao_gps_char == '.')
254 i = (i + ((uint8_t) ao_gps_decimal(1) >> 1));
257 ao_gps_next.hdop = i;
260 ao_gps_next.altitude = ao_gps_decimal(0xff);
261 ao_gps_skip_field(); /* skip any fractional portion */
263 /* Skip remaining fields */
264 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
268 if (ao_gps_char == '*') {
269 uint8_t cksum = ao_gps_cksum ^ '*';
270 if (cksum != ao_gps_hex())
275 ao_mutex_get(&ao_gps_mutex);
276 ao_gps_tick = ao_gps_next_tick;
277 ao_xmemcpy(&ao_gps_data, &ao_gps_next, sizeof (ao_gps_data));
278 ao_mutex_put(&ao_gps_mutex);
279 ao_wakeup(&ao_gps_data);
289 /* Now read the data into the GPS tracking data record
291 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
293 * Satellites in view data
295 * 3 Total number of GSV messages
296 * 1 Sequence number of current GSV message
297 * 12 Total sats in view (0-12)
305 c = ao_gps_decimal(1); /* total messages */
306 i = ao_gps_decimal(1); /* message sequence */
308 ao_gps_tracking_next.channels = 0;
310 done = (uint8_t) c == i;
312 ao_gps_skip_field(); /* sats in view */
313 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
314 i = ao_gps_tracking_next.channels;
315 c = ao_gps_decimal(2); /* SVID */
316 if (i < AO_MAX_GPS_TRACKING)
317 ao_gps_tracking_next.sats[i].svid = c;
319 ao_gps_skip_field(); /* elevation */
321 ao_gps_skip_field(); /* azimuth */
322 c = ao_gps_decimal(2); /* C/N0 */
323 if (i < AO_MAX_GPS_TRACKING) {
324 if ((ao_gps_tracking_next.sats[i].c_n_1 = c) != 0)
325 ao_gps_tracking_next.channels = i + 1;
328 if (ao_gps_char == '*') {
329 uint8_t cksum = ao_gps_cksum ^ '*';
330 if (cksum != ao_gps_hex())
336 ao_gps_tracking_next.channels = 0;
338 ao_mutex_get(&ao_gps_mutex);
339 ao_xmemcpy(&ao_gps_tracking_data, &ao_gps_tracking_next,
340 sizeof(ao_gps_tracking_data));
341 ao_mutex_put(&ao_gps_mutex);
342 ao_wakeup(&ao_gps_tracking_data);
351 /* Parse the RMC record to read out the current date */
353 /* $GPRMC,111636.932,A,2447.0949,N,12100.5223,E,000.0,000.0,030407,,,A*61
355 * Recommended Minimum Specific GNSS Data
357 * 111636.932 UTC time 11:16:36.932
358 * A Data Valid (V = receiver warning)
360 * N North/south indicator
361 * 12100.5223 Longitude
362 * E East/west indicator
363 * 000.0 Speed over ground
364 * 000.0 Course over ground
365 * 030407 UTC date (ddmmyy format)
368 * A = autonomous mode
369 * D = differential mode
370 * E = estimated (dead reckoning) mode
371 * M = manual input mode
376 for (i = 0; i < 8; i++) {
380 a = ao_gps_decimal(2);
381 c = ao_gps_decimal(2);
382 i = ao_gps_decimal(2);
383 /* Skip remaining fields */
384 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
388 if (ao_gps_char == '*') {
389 uint8_t cksum = ao_gps_cksum ^ '*';
390 if (cksum != ao_gps_hex())
395 ao_gps_next.year = i;
396 ao_gps_next.month = c;
398 ao_gps_date_flags = AO_GPS_DATE_VALID;
402 #define ao_skytraq_sendstruct(s) ao_skytraq_sendbytes((s), sizeof(s))
405 ao_skytraq_sendbytes(__code uint8_t *b, uint8_t l)
410 ao_delay(AO_MS_TO_TICKS(500));
411 ao_serial_putchar(c);
416 ao_gps_nmea_parse(void)
423 for (a = 0; a < AO_GPS_LEADER; a++) {
425 if (ao_gps_char != ao_gps_header[a])
437 if (ao_gps_char != ',')
440 if (a == (uint8_t) 'G' && b == (uint8_t) 'G' && c == (uint8_t) 'A') {
442 } else if (a == (uint8_t) 'G' && b == (uint8_t) 'S' && c == (uint8_t) 'V') {
444 } else if (a == (uint8_t) 'R' && b == (uint8_t) 'M' && c == (uint8_t) 'C') {
450 ao_gps(void) __reentrant
452 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
454 /* give skytraq time to boot in case of cold start */
455 ao_delay(AO_MS_TO_TICKS(2000));
457 ao_skytraq_sendstruct(ao_gps_config);
460 /* Locate the begining of the next record */
461 if (ao_serial_getchar() == '$') {
468 __xdata struct ao_task ao_gps_task;
471 gps_dump(void) __reentrant
474 ao_mutex_get(&ao_gps_mutex);
475 printf ("Date: %02d/%02d/%02d\n", ao_gps_data.year, ao_gps_data.month, ao_gps_data.day);
476 printf ("Time: %02d:%02d:%02d\n", ao_gps_data.hour, ao_gps_data.minute, ao_gps_data.second);
477 printf ("Lat/Lon: %ld %ld\n", ao_gps_data.latitude, ao_gps_data.longitude);
478 printf ("Alt: %d\n", ao_gps_data.altitude);
479 printf ("Flags: 0x%x\n", ao_gps_data.flags);
480 printf ("Sats: %d", ao_gps_tracking_data.channels);
481 for (i = 0; i < ao_gps_tracking_data.channels; i++)
483 ao_gps_tracking_data.sats[i].svid,
484 ao_gps_tracking_data.sats[i].c_n_1);
486 ao_mutex_put(&ao_gps_mutex);
489 __code struct ao_cmds ao_gps_cmds[] = {
490 { gps_dump, "g\0Display GPS" },
497 ao_add_task(&ao_gps_task, ao_gps, "gps");
498 ao_cmd_register(&ao_gps_cmds[0]);