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 const char ao_gps_header[] = "GP";
26 __xdata uint8_t ao_gps_mutex;
27 static __xdata char ao_gps_char;
28 static __xdata uint8_t ao_gps_cksum;
29 static __xdata uint8_t ao_gps_error;
31 __xdata uint16_t ao_gps_tick;
32 __xdata struct ao_gps_data ao_gps_data;
33 __xdata struct ao_gps_tracking_data ao_gps_tracking_data;
35 static __xdata uint16_t ao_gps_next_tick;
36 static __xdata struct ao_gps_data ao_gps_next;
37 static __xdata uint8_t ao_gps_date_flags;
38 static __xdata struct ao_gps_tracking_data 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 const 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 */
74 ao_gps_char = ao_serial_getchar();
75 ao_gps_cksum ^= ao_gps_char;
79 ao_gps_skip_field(void)
81 while (ao_gps_char != ',' && ao_gps_char != '*' && ao_gps_char != '\n')
88 if (ao_gps_char == ',' || ao_gps_char == '.' || ao_gps_char == '*')
92 __xdata static uint8_t ao_gps_num_width;
95 ao_gps_decimal(uint8_t max_width)
98 __xdata uint8_t neg = 0;
101 if (ao_gps_char == '-') {
106 ao_gps_num_width = 0;
107 while (ao_gps_num_width < max_width) {
108 if (ao_gps_char < '0' || '9' < ao_gps_char)
110 v = v * (int16_t) 10 + ao_gps_char - '0';
120 ao_gps_hex(uint8_t max_width)
126 ao_gps_num_width = 0;
127 while (ao_gps_num_width < max_width) {
128 if ('0' <= ao_gps_char && ao_gps_char <= '9')
129 d = ao_gps_char - '0';
130 else if ('A' <= ao_gps_char && ao_gps_char <= 'F')
131 d = ao_gps_char - 'A' + 10;
132 else if ('a' <= ao_gps_char && ao_gps_char <= 'f')
133 d = ao_gps_char - 'a' + 10;
144 ao_gps_parse_pos(uint8_t deg_width) __reentrant
150 d = ao_gps_decimal(deg_width);
151 m = ao_gps_decimal(2);
152 if (ao_gps_char == '.') {
153 f = ao_gps_decimal(4);
154 while (ao_gps_num_width < 4) {
160 if (ao_gps_char != ',')
170 ao_gps_parse_flag(char no_c, char yes_c) __reentrant
174 if (ao_gps_char == yes_c)
176 else if (ao_gps_char == no_c)
189 /* Now read the data into the gps data record
191 * $GPGGA,025149.000,4528.1723,N,12244.2480,W,1,05,2.0,103.5,M,-19.5,M,,0000*66
195 * 025149.000 time (02:51:49.000 GMT)
196 * 4528.1723,N Latitude 45°28.1723' N
197 * 12244.2480,W Longitude 122°44.2480' W
203 * 4 = Real Time Kinematic
205 * 6 = estimated (dead reckoning)
206 * 7 = Manual input mode
207 * 8 = Simulation mode
208 * 05 Number of satellites (5)
209 * 2.0 Horizontal dilution
210 * 103.5,M Altitude, 103.5M above msl
211 * -19.5,M Height of geoid above WGS84 ellipsoid
212 * ? time in seconds since last DGPS update
213 * 0000 DGPS station ID
217 ao_gps_next_tick = ao_time();
218 ao_gps_next.flags = AO_GPS_RUNNING | ao_gps_date_flags;
219 ao_gps_next.hour = ao_gps_decimal(2);
220 ao_gps_next.minute = ao_gps_decimal(2);
221 ao_gps_next.second = ao_gps_decimal(2);
222 ao_gps_skip_field(); /* skip seconds fraction */
224 ao_gps_next.latitude = ao_gps_parse_pos(2);
225 if (ao_gps_parse_flag('N', 'S'))
226 ao_gps_next.latitude = -ao_gps_next.latitude;
227 ao_gps_next.longitude = ao_gps_parse_pos(3);
228 if (ao_gps_parse_flag('E', 'W'))
229 ao_gps_next.longitude = -ao_gps_next.longitude;
231 i = ao_gps_decimal(0xff);
233 ao_gps_next.flags |= AO_GPS_VALID;
235 i = ao_gps_decimal(0xff) << AO_GPS_NUM_SAT_SHIFT;
236 if (i > AO_GPS_NUM_SAT_MASK)
237 i = AO_GPS_NUM_SAT_MASK;
238 ao_gps_next.flags |= i;
241 ao_gps_next.hdop = ao_gps_decimal(0xff);
242 if (ao_gps_next.hdop <= 50) {
243 ao_gps_next.hdop = (uint8_t) 5 * ao_gps_next.hdop;
244 if (ao_gps_char == '.')
245 ao_gps_next.hdop = (ao_gps_next.hdop +
246 ((uint8_t) ao_gps_decimal(1) >> 1));
248 ao_gps_next.hdop = 255;
251 ao_gps_next.altitude = ao_gps_decimal(0xff);
252 ao_gps_skip_field(); /* skip any fractional portion */
254 /* Skip remaining fields */
255 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
259 if (ao_gps_char == '*') {
260 uint8_t cksum = ao_gps_cksum ^ '*';
261 if (cksum != ao_gps_hex(2))
266 ao_mutex_get(&ao_gps_mutex);
267 ao_gps_tick = ao_gps_next_tick;
268 memcpy(&ao_gps_data, &ao_gps_next, sizeof (struct ao_gps_data));
269 ao_mutex_put(&ao_gps_mutex);
270 ao_wakeup(&ao_gps_data);
280 /* Now read the data into the GPS tracking data record
282 * $GPGSV,3,1,12,05,54,069,45,12,44,061,44,21,07,184,46,22,78,289,47*72<CR><LF>
284 * Satellites in view data
286 * 3 Total number of GSV messages
287 * 1 Sequence number of current GSV message
288 * 12 Total sats in view (0-12)
296 c = ao_gps_decimal(1); /* total messages */
297 i = ao_gps_decimal(1); /* message sequence */
299 ao_gps_tracking_next.channels = 0;
301 done = (uint8_t) c == i;
303 ao_gps_skip_field(); /* sats in view */
304 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
305 i = ao_gps_tracking_next.channels;
306 c = ao_gps_decimal(2); /* SVID */
307 if (i < AO_MAX_GPS_TRACKING)
308 ao_gps_tracking_next.sats[i].svid = c;
310 ao_gps_skip_field(); /* elevation */
312 ao_gps_skip_field(); /* azimuth */
313 c = ao_gps_decimal(2); /* C/N0 */
314 if (i < AO_MAX_GPS_TRACKING) {
315 if (!(ao_gps_tracking_next.sats[i].c_n_1 = c))
316 ao_gps_tracking_next.sats[i].svid = 0;
317 ao_gps_tracking_next.channels = i + 1;
320 if (ao_gps_char == '*') {
321 uint8_t cksum = ao_gps_cksum ^ '*';
322 if (cksum != ao_gps_hex(2))
328 ao_gps_tracking_next.channels = 0;
330 ao_mutex_get(&ao_gps_mutex);
331 memcpy(&ao_gps_tracking_data, &ao_gps_tracking_next,
332 sizeof(ao_gps_tracking_data));
333 ao_mutex_put(&ao_gps_mutex);
334 ao_wakeup(&ao_gps_tracking_data);
343 /* Parse the RMC record to read out the current date */
345 /* $GPRMC,111636.932,A,2447.0949,N,12100.5223,E,000.0,000.0,030407,,,A*61
347 * Recommended Minimum Specific GNSS Data
349 * 111636.932 UTC time 11:16:36.932
350 * A Data Valid (V = receiver warning)
352 * N North/south indicator
353 * 12100.5223 Longitude
354 * E East/west indicator
355 * 000.0 Speed over ground
356 * 000.0 Course over ground
357 * 030407 UTC date (ddmmyy format)
360 * A = autonomous mode
361 * D = differential mode
362 * E = estimated (dead reckoning) mode
363 * M = manual input mode
368 for (i = 0; i < 8; i++) {
372 a = ao_gps_decimal(2);
373 c = ao_gps_decimal(2);
374 i = ao_gps_decimal(2);
375 /* Skip remaining fields */
376 while (ao_gps_char != '*' && ao_gps_char != '\n' && ao_gps_char != '\r') {
380 if (ao_gps_char == '*') {
381 uint8_t cksum = ao_gps_cksum ^ '*';
382 if (cksum != ao_gps_hex(2))
387 ao_gps_next.year = i;
388 ao_gps_next.month = c;
390 ao_gps_date_flags = AO_GPS_DATE_VALID;
394 #define ao_skytraq_sendstruct(s) ao_skytraq_sendbytes((s), (s)+sizeof(s))
397 ao_skytraq_sendbytes(const uint8_t *b, const uint8_t *e)
401 ao_delay(AO_MS_TO_TICKS(500));
402 ao_serial_putchar(*b++);
407 ao_gps_nmea_parse(void)
414 for (a = 0; a < AO_GPS_LEADER; a++) {
416 if (ao_gps_char != ao_gps_header[a])
428 if (ao_gps_char != ',')
431 if (a == (uint8_t) 'G' && b == (uint8_t) 'G' && c == (uint8_t) 'A') {
433 } else if (a == (uint8_t) 'G' && b == (uint8_t) 'S' && c == (uint8_t) 'V') {
435 } else if (a == (uint8_t) 'R' && b == (uint8_t) 'M' && c == (uint8_t) 'C') {
441 ao_gps(void) __reentrant
443 ao_serial_set_speed(AO_SERIAL_SPEED_9600);
445 /* give skytraq time to boot in case of cold start */
446 ao_delay(AO_MS_TO_TICKS(2000));
448 ao_skytraq_sendstruct(ao_gps_config);
451 /* Locate the begining of the next record */
452 if (ao_serial_getchar() == '$') {
459 __xdata struct ao_task ao_gps_task;
462 gps_dump(void) __reentrant
464 ao_mutex_get(&ao_gps_mutex);
465 printf ("Date: %02d/%02d/%02d\n", ao_gps_data.year, ao_gps_data.month, ao_gps_data.day);
466 printf ("Time: %02d:%02d:%02d\n", ao_gps_data.hour, ao_gps_data.minute, ao_gps_data.second);
467 printf ("Lat/Lon: %ld %ld\n", ao_gps_data.latitude, ao_gps_data.longitude);
468 printf ("Alt: %d\n", ao_gps_data.altitude);
469 printf ("Flags: 0x%x\n", ao_gps_data.flags);
470 ao_mutex_put(&ao_gps_mutex);
473 __code struct ao_cmds ao_gps_cmds[] = {
474 { gps_dump, "g\0Display current GPS values" },
481 ao_add_task(&ao_gps_task, ao_gps, "gps");
482 ao_cmd_register(&ao_gps_cmds[0]);