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.
24 #include <sys/types.h>
28 #define AO_GPS_NUM_SAT_MASK (0xf << 0)
29 #define AO_GPS_NUM_SAT_SHIFT (0)
31 #define AO_GPS_NEW_DATA 1
32 #define AO_GPS_NEW_TRACKING 2
34 #define AO_GPS_VALID (1 << 4)
35 #define AO_GPS_RUNNING (1 << 5)
36 #define AO_GPS_DATE_VALID (1 << 6)
37 #define AO_GPS_COURSE_VALID (1 << 7)
39 struct ao_telemetry_location {
47 int32_t latitude; /* degrees * 10⁷ */
48 int32_t longitude; /* degrees * 10⁷ */
49 int16_t altitude_low; /* m */
50 uint16_t ground_speed; /* cm/s */
51 uint8_t course; /* degrees / 2 */
52 uint8_t pdop; /* * 5 */
53 uint8_t hdop; /* * 5 */
54 uint8_t vdop; /* * 5 */
55 int16_t climb_rate; /* cm/s */
56 uint16_t h_error; /* m */
57 uint16_t v_error; /* m */
58 int16_t altitude_high; /* m */
61 typedef int32_t gps_alt_t;
62 #define AO_TELEMETRY_LOCATION_ALTITUDE(l) (((gps_alt_t) (l)->altitude_high << 16) | ((l)->altitude_low))
63 #define AO_GPS_ORIG_ALTITUDE(l) AO_TELEMETRY_LOCATION_ALTITUDE(l)
64 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) (((l)->altitude_high = (a) >> 16), \
65 ((l)->altitude_low = (a)))
67 #define UBLOX_SAT_STATE_ACQUIRED (1 << 0)
68 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID (1 << 1)
69 #define UBLOX_SAT_BIT_SYNC_COMPLETE (1 << 2)
70 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE (1 << 3)
71 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE (1 << 4)
72 #define UBLOX_SAT_CODE_LOCKED (1 << 5)
73 #define UBLOX_SAT_ACQUISITION_FAILED (1 << 6)
74 #define UBLOX_SAT_EPHEMERIS_AVAILABLE (1 << 7)
76 struct ao_telemetry_satellite_info {
81 #define AO_TELEMETRY_SATELLITE_MAX_SAT 12
83 struct ao_telemetry_satellite {
85 struct ao_telemetry_satellite_info sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
88 #define ao_gps_orig ao_telemetry_location
89 #define ao_gps_tracking_orig ao_telemetry_satellite
90 #define ao_gps_sat_orig ao_telemetry_satellite_info
92 extern struct ao_telemetry_location ao_gps_data;
93 extern struct ao_telemetry_satellite ao_gps_tracking_data;
98 ao_mutex_get(uint8_t *mutex)
103 ao_mutex_put(uint8_t *mutex)
107 static int ao_gps_fd;
108 static FILE *ao_gps_file;
118 sprintf (line, "\n");
120 sprintf (line, "\\%02x", ((int) c) & 0xff);
122 sprintf (line, "%c", c);
124 write(1, line, strlen(line));
128 #include <sys/time.h>
134 gettimeofday(&tv, NULL);
135 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
138 static uint8_t in_message[4096];
140 static uint16_t recv_len;
142 static void check_ublox_message(char *which, uint8_t *msg);
151 i = getc(ao_gps_file);
158 if (in_len || uc == 0xb5) {
159 in_message[in_len++] = c;
161 recv_len = in_message[4] | (in_message[5] << 8);
162 } else if (in_len > 6 && in_len == recv_len + 8) {
163 check_ublox_message("recv", in_message + 2);
171 #define MESSAGE_LEN 4096
173 static uint8_t message[MESSAGE_LEN];
174 static int message_len;
175 static uint16_t send_len;
178 ao_gps_putchar(char c)
181 uint8_t uc = (uint8_t) c;
183 if (message_len || uc == 0xb5) {
184 if (message_len < MESSAGE_LEN)
185 message[message_len++] = uc;
186 if (message_len == 6) {
187 send_len = message[4] | (message[5] << 8);
188 } else if (message_len > 6 && message_len == send_len + 8) {
189 check_ublox_message("send", message + 2);
195 i = write(ao_gps_fd, &c, 1);
198 if (i < 0 && (errno == EINTR || errno == EAGAIN))
205 #define AO_SERIAL_SPEED_4800 0
206 #define AO_SERIAL_SPEED_9600 1
207 #define AO_SERIAL_SPEED_57600 2
208 #define AO_SERIAL_SPEED_115200 3
211 ao_gps_set_speed(uint8_t speed)
214 struct termios termios;
216 printf ("\t\tset speed %d\n", speed);
218 tcgetattr(fd, &termios);
220 case AO_SERIAL_SPEED_4800:
221 cfsetspeed(&termios, B4800);
223 case AO_SERIAL_SPEED_9600:
224 cfsetspeed(&termios, B9600);
226 case AO_SERIAL_SPEED_57600:
227 cfsetspeed(&termios, B57600);
229 case AO_SERIAL_SPEED_115200:
230 cfsetspeed(&termios, B115200);
233 tcsetattr(fd, TCSAFLUSH, &termios);
234 tcflush(fd, TCIFLUSH);
239 uint8_t ao_task_minimize_latency;
241 #define ao_usb_getchar() 0
243 #include "ao_gps_print.c"
244 #include "ao_gps_show.c"
245 #include "ao_gps_ublox.c"
248 check_ublox_message(char *which, uint8_t *msg)
250 uint8_t class = msg[0];
252 uint16_t len = msg[2] | (msg[3] << 8);
254 struct ao_ublox_cksum cksum_msg = { .a = msg[4 + len],
255 .b = msg[4 + len + 1] };
256 struct ao_ublox_cksum cksum= { 0, 0 };
258 for (i = 0; i < 4 + len; i++) {
259 add_cksum(&cksum, msg[i]);
261 if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
262 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
273 case UBLOX_NAV_DOP: ;
274 struct ublox_nav_dop *nav_dop = (void *) msg;
275 printf ("\tnav-dop iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
285 case UBLOX_NAV_POSLLH: ;
286 struct ublox_nav_posllh *nav_posllh = (void *) msg;
287 printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
289 nav_posllh->lon / 1e7,
290 nav_posllh->lat / 1e7,
291 nav_posllh->height / 1e3,
292 nav_posllh->hmsl / 1e3,
293 nav_posllh->hacc / 1e3,
294 nav_posllh->vacc / 1e3);
296 case UBLOX_NAV_SOL: ;
297 struct ublox_nav_sol *nav_sol = (struct ublox_nav_sol *) msg;
298 printf ("\tnav-sol iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
299 nav_sol->itow, nav_sol->ftow, nav_sol->week,
300 nav_sol->gpsfix, nav_sol->flags);
302 case UBLOX_NAV_SVINFO: ;
303 struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
304 printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
305 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
307 for (i = 0; i < nav_svinfo->numch; i++) {
308 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
309 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
310 nav_svinfo_block->chn,
311 nav_svinfo_block->svid,
312 nav_svinfo_block->flags,
313 nav_svinfo_block->quality,
314 nav_svinfo_block->cno,
315 nav_svinfo_block->elev,
316 nav_svinfo_block->azim,
317 nav_svinfo_block->prres);
320 case UBLOX_NAV_VELNED: ;
321 struct ublox_nav_velned *nav_velned = (void *) msg;
322 printf ("\tnav-velned iTOW %9u velN %10.2f velE %10.2f velD %10.2f speed %10.2f gSpeed %10.2f heading %10.5f sAcc %10.2f cAcc %10.5f\n",
324 nav_velned->veln / 1e2,
325 nav_velned->vele / 1e2,
326 nav_velned->veld / 1e2,
327 nav_velned->speed / 1e2,
328 nav_velned->gspeed / 1e2,
329 nav_velned->heading / 1e5,
330 nav_velned->sacc / 1e5,
331 nav_velned->cacc / 1e6);
333 case UBLOX_NAV_TIMEUTC:;
334 struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
335 printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
351 printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
352 for (i = 0; i < len; i++)
353 printf (" %02x", msg[4 + i]);
354 printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
360 ao_dump_state(void *wchan)
362 if (wchan == &ao_gps_new)
368 ao_gps_open(const char *tty)
370 struct termios termios;
373 fd = open (tty, O_RDWR);
377 tcgetattr(fd, &termios);
379 cfsetspeed(&termios, B4800);
380 tcsetattr(fd, TCSAFLUSH, &termios);
383 tcflush(fd, TCIFLUSH);
389 static const struct option options[] = {
390 { .name = "tty", .has_arg = 1, .val = 'T' },
394 static void usage(char *program)
396 fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
401 main (int argc, char **argv)
403 char *tty = "/dev/ttyUSB0";
406 while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
416 ao_gps_fd = ao_gps_open(tty);
421 ao_gps_file = fdopen(ao_gps_fd, "r");