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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #define AO_TICK_TYPE uint32_t
26 #include <sys/types.h>
30 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
31 #define AO_GPS_NUM_SAT_SHIFT (0)
33 #define AO_GPS_NEW_DATA 1
34 #define AO_GPS_NEW_TRACKING 2
36 #define AO_GPS_VALID (1 << 4)
37 #define AO_GPS_RUNNING (1 << 5)
38 #define AO_GPS_DATE_VALID (1 << 6)
39 #define AO_GPS_COURSE_VALID (1 << 7)
49 int32_t latitude; /* degrees * 10⁷ */
50 int32_t longitude; /* degrees * 10⁷ */
51 int16_t altitude; /* m */
52 uint16_t ground_speed; /* cm/s */
53 uint8_t course; /* degrees / 2 */
54 uint8_t pdop; /* unused */
55 uint8_t vdop; /* unused */
56 uint8_t hdop; /* * 5 */
57 int16_t climb_rate; /* cm/s */
58 uint16_t h_error; /* m */
59 uint16_t v_error; /* m */
62 #define AO_TELEMETRY_LOCATION_ALTITUDE(l) ((l)->altitude)
63 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) ((l)->altitude = (a))
65 #define SIRF_SAT_STATE_ACQUIRED (1 << 0)
66 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
67 #define SIRF_SAT_BIT_SYNC_COMPLETE (1 << 2)
68 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
69 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
70 #define SIRF_SAT_CODE_LOCKED (1 << 5)
71 #define SIRF_SAT_ACQUISITION_FAILED (1 << 6)
72 #define SIRF_SAT_EPHEMERIS_AVAILABLE (1 << 7)
74 struct ao_gps_sat_orig {
79 #define AO_MAX_GPS_TRACKING 12
81 struct ao_gps_tracking_orig {
83 struct ao_gps_sat_orig sats[AO_MAX_GPS_TRACKING];
86 #define ao_telemetry_location ao_gps_orig
87 #define ao_telemetry_satellite ao_gps_tracking_orig
88 #define ao_telemetry_satellite_info ao_gps_sat_orig
90 extern struct ao_telemetry_location ao_gps_data;
91 extern struct ao_telemetry_satellite ao_gps_tracking_data;
96 ao_mutex_get(uint8_t *mutex)
101 ao_mutex_put(uint8_t *mutex)
116 sprintf (line, "\n");
118 sprintf (line, "\\%02x", ((int) c) & 0xff);
120 sprintf (line, "%c", c);
122 write(1, line, strlen(line));
126 #define QUEUE_LEN 4096
128 static char input_queue[QUEUE_LEN];
129 int input_head, input_tail;
131 #include <sys/time.h>
137 gettimeofday(&tv, NULL);
138 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
142 check_skytraq_message(char *from, uint8_t *msg, int len)
144 uint16_t encoded_len, encoded_cksum;
149 // fwrite(msg, 1, len, stdout);
151 if (msg[0] != 0xa0 || msg[1] != 0xa2) {
152 printf ("bad header\n");
159 if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
160 printf ("bad trailer\n");
163 encoded_len = (msg[2] << 8) | msg[3];
165 /* printf ("%9d: %3d\n", get_millis(), id); */
166 if (encoded_len != len - 8) {
168 printf ("length mismatch (got %d, wanted %d)\n",
169 len - 8, encoded_len);
172 encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
174 for (i = 4; i < len - 4; i++)
175 cksum = (cksum + msg[i]) & 0x7fff;
176 if (encoded_cksum != cksum) {
177 printf ("cksum mismatch (got %04x wanted %04x)\n",
178 cksum, encoded_cksum);
207 int16_t heading_rate;
213 #define get_u8(u) u = (msg[off]); off+= 1
214 #define get_u16(u) u = (msg[off] << 8) | (msg[off + 1]); off+= 2
215 #define get_u32(u) u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
238 get_u16(heading_rate);
247 printf ("Geodetic Navigation Data (41):\n");
248 printf ("\tNav valid %04x\n", nav_valid);
249 printf ("\tNav type %04x\n", nav_type);
250 printf ("\tWeek %5d", week);
251 printf (" TOW %9d", tow);
252 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
254 hour, minute, second / 1000.0);
255 printf ("\tsats: %08x\n", sat_list);
256 printf ("\tlat: %g", lat / 1.0e7);
257 printf (" lon: %g", lon / 1.0e7);
258 printf (" alt_ell: %g", alt_ell / 100.0);
259 printf (" alt_msll: %g", alt_msl / 100.0);
260 printf (" datum: %d\n", datum);
261 printf ("\tground speed: %g", sog / 100.0);
262 printf (" course: %g", cog / 100.0);
263 printf (" climb: %g", climb_rate / 100.0);
264 printf (" heading rate: %g\n", heading_rate / 100.0);
265 printf ("\th error: %g", h_error / 100.0);
266 printf (" v error: %g", v_error / 100.0);
267 printf (" t error: %g", t_error / 100.0);
268 printf (" h vel error: %g\n", h_v_error / 100.0);
285 printf ("Measured Tracker Data (4):\n");
286 printf ("GPS week: %d\n", gps_week);
287 printf ("GPS time of week: %d\n", gps_tow);
288 printf ("channels: %d\n", channels);
289 for (j = 0; j < 12; j++) {
290 uint8_t svid, azimuth, elevation;
297 for (k = 0; k < 10; k++) {
300 printf ("Sat %3d:", svid);
301 printf (" aziumuth: %6.1f", azimuth * 1.5);
302 printf (" elevation: %6.1f", elevation * 0.5);
303 printf (" state: 0x%02x", state);
305 for (k = 0; k < 10; k++)
306 printf(" %3d", c_n[k]);
307 if (state & SIRF_SAT_STATE_ACQUIRED)
309 if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
311 if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
313 if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
315 if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
317 if (state & SIRF_SAT_CODE_LOCKED)
319 if (state & SIRF_SAT_ACQUISITION_FAILED)
321 if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
329 printf ("%s %4d:", from, encoded_len);
330 for (i = 4; i < len - 4; i++) {
331 if (((i - 4) & 0xf) == 0)
333 printf (" %3d", msg[i]);
339 static uint8_t skytraq_message[4096];
340 static int skytraq_message_len;
341 static uint8_t skytraq_in_message[4096];
342 static int skytraq_in_len;
345 ao_serial1_getchar(void)
350 while (input_head == input_tail) {
352 input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
353 if (input_tail < 0) {
354 if (errno == EINTR || errno == EAGAIN)
363 c = input_queue[input_head];
364 input_head = (input_head + 1) % QUEUE_LEN;
366 // printf ("c: %02x %c\n", uc, uc);
367 if (skytraq_in_len || uc == '$') {
368 if (skytraq_in_len < 4096)
369 skytraq_in_message[skytraq_in_len++] = uc;
371 check_skytraq_message("recv", skytraq_in_message, skytraq_in_len);
380 ao_serial1_putchar(char c)
383 uint8_t uc = (uint8_t) c;
385 if (skytraq_message_len || uc == 0xa0) {
386 if (skytraq_message_len < 4096)
387 skytraq_message[skytraq_message_len++] = uc;
389 check_skytraq_message("send", skytraq_message, skytraq_message_len);
390 skytraq_message_len = 0;
394 i = write(ao_gps_fd, &c, 1);
396 if ((uint8_t) c == 0xb3 || c == '\r') {
397 /* static const struct timespec delay = {
399 .tv_nsec = 100 * 1000 * 1000
403 // nanosleep(&delay, NULL);
407 if (i < 0 && (errno == EINTR || errno == EAGAIN))
414 #define AO_SERIAL_SPEED_4800 0
415 #define AO_SERIAL_SPEED_9600 1
416 #define AO_SERIAL_SPEED_57600 2
417 #define AO_SERIAL_SPEED_115200 3
420 ao_serial1_set_speed(uint8_t speed)
423 struct termios termios;
426 tcgetattr(fd, &termios);
428 case AO_SERIAL_SPEED_4800:
429 cfsetspeed(&termios, B4800);
431 case AO_SERIAL_SPEED_9600:
432 cfsetspeed(&termios, B9600);
434 case AO_SERIAL_SPEED_57600:
435 cfsetspeed(&termios, B57600);
437 case AO_SERIAL_SPEED_115200:
438 cfsetspeed(&termios, B115200);
441 tcsetattr(fd, TCSAFLUSH, &termios);
442 tcflush(fd, TCIFLUSH);
447 uint8_t ao_task_minimize_latency;
449 #define ao_usb_getchar() 0
451 #include "ao_gps_print.c"
452 #include "ao_gps_show.c"
453 #include "ao_gps_skytraq.c"
456 ao_dump_state(void *wchan)
458 if (wchan == &ao_gps_new)
463 ao_gps_open(const char *tty)
465 struct termios termios;
468 fd = open (tty, O_RDWR);
472 tcgetattr(fd, &termios);
474 cfsetspeed(&termios, B4800);
475 tcsetattr(fd, TCSAFLUSH, &termios);
478 tcflush(fd, TCIFLUSH);
484 static const struct option options[] = {
485 { .name = "tty", .has_arg = 1, .val = 'T' },
489 static void usage(char *program)
491 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
496 main (int argc, char **argv)
498 char *tty = "/dev/ttyUSB0";
501 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
511 ao_gps_fd = ao_gps_open(tty);