]> git.gag.com Git - fw/altos/blob - src/test/ao_gps_test_ublox.c
altos/draw: Use float for polygon coords
[fw/altos] / src / test / ao_gps_test_ublox.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #define AO_GPS_TEST
20 #define HAS_GPS 1
21 #include "ao_host.h"
22 #include <termios.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
29 #define AO_GPS_NUM_SAT_SHIFT    (0)
30
31 #define AO_GPS_NEW_DATA         1
32 #define AO_GPS_NEW_TRACKING     2
33
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)
38
39 struct ao_telemetry_location {
40         uint8_t                 year;
41         uint8_t                 month;
42         uint8_t                 day;
43         uint8_t                 hour;
44         uint8_t                 minute;
45         uint8_t                 second;
46         uint8_t                 flags;
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 */
59 };
60
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)))
66
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)
75
76 struct ao_telemetry_satellite_info {
77         uint8_t         svid;
78         uint8_t         c_n_1;
79 };
80
81 #define AO_TELEMETRY_SATELLITE_MAX_SAT  12
82
83 struct ao_telemetry_satellite {
84         uint8_t                                 channels;
85         struct ao_telemetry_satellite_info      sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
86 };
87
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
91
92 extern struct ao_telemetry_location     ao_gps_data;
93 extern struct ao_telemetry_satellite    ao_gps_tracking_data;
94
95 uint8_t ao_gps_mutex;
96
97 void
98 ao_mutex_get(uint8_t *mutex)
99 {
100 }
101
102 void
103 ao_mutex_put(uint8_t *mutex)
104 {
105 }
106
107 static int ao_gps_fd;
108 static FILE *ao_gps_file;
109
110 #if 0
111 static void
112 ao_dbg_char(char c)
113 {
114         char    line[128];
115         line[0] = '\0';
116         if (c < ' ') {
117                 if (c == '\n')
118                         sprintf (line, "\n");
119                 else
120                         sprintf (line, "\\%02x", ((int) c) & 0xff);
121         } else {
122                 sprintf (line, "%c", c);
123         }
124         write(1, line, strlen(line));
125 }
126 #endif
127
128 #include <sys/time.h>
129
130 int
131 get_millis(void)
132 {
133         struct timeval  tv;
134         gettimeofday(&tv, NULL);
135         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
136 }
137
138 static uint8_t  in_message[4096];
139 static int      in_len;
140 static uint16_t recv_len;
141
142 static void check_ublox_message(char *which, uint8_t *msg);
143
144 char
145 ao_gps_getchar(void)
146 {
147         char    c;
148         uint8_t uc;
149         int     i;
150
151         i = getc(ao_gps_file);
152         if (i == EOF) {
153                 perror("getchar");
154                 exit(1);
155         }
156         c = i;
157         uc = (uint8_t) c;
158         if (in_len || uc == 0xb5) {
159                 in_message[in_len++] = c;
160                 if (in_len == 6) {
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);
164                         in_len = 0;
165                 }
166                 
167         }
168         return c;
169 }
170
171 #define MESSAGE_LEN     4096
172
173 static uint8_t  message[MESSAGE_LEN];
174 static int      message_len;
175 static uint16_t send_len;
176
177 void
178 ao_gps_putchar(char c)
179 {
180         int     i;
181         uint8_t uc = (uint8_t) c;
182
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);
190                         message_len = 0;
191                 }
192         }
193
194         for (;;) {
195                 i = write(ao_gps_fd, &c, 1);
196                 if (i == 1)
197                         break;
198                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
199                         continue;
200                 perror("putchar");
201                 exit(1);
202         }
203 }
204
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
209
210 static void
211 ao_gps_set_speed(uint8_t speed)
212 {
213         int     fd = ao_gps_fd;
214         struct termios  termios;
215
216         printf ("\t\tset speed %d\n", speed);
217         tcdrain(fd);
218         tcgetattr(fd, &termios);
219         switch (speed) {
220         case AO_SERIAL_SPEED_4800:
221                 cfsetspeed(&termios, B4800);
222                 break;
223         case AO_SERIAL_SPEED_9600:
224                 cfsetspeed(&termios, B9600);
225                 break;
226         case AO_SERIAL_SPEED_57600:
227                 cfsetspeed(&termios, B57600);
228                 break;
229         case AO_SERIAL_SPEED_115200:
230                 cfsetspeed(&termios, B115200);
231                 break;
232         }
233         tcsetattr(fd, TCSAFLUSH, &termios);
234         tcflush(fd, TCIFLUSH);
235 }
236
237 #define ao_time() 0
238
239 uint8_t ao_task_minimize_latency;
240
241 #define ao_usb_getchar()        0
242
243 #include "ao_gps_print.c"
244 #include "ao_gps_show.c"
245 #include "ao_gps_ublox.c"
246
247 static void
248 check_ublox_message(char *which, uint8_t *msg)
249 {
250         uint8_t class = msg[0];
251         uint8_t id = msg[1];
252         uint16_t len = msg[2] | (msg[3] << 8);
253         uint16_t i;
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 };
257
258         for (i = 0; i < 4 + len; i++) {
259                 add_cksum(&cksum, msg[i]);
260         }
261         if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
262                 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
263                         which,
264                         cksum_msg.a & 0xff,
265                         cksum_msg.b & 0xff,
266                         cksum.a & 0xff,
267                         cksum.b & 0xff);
268                 return;
269         }
270         switch (class) {
271         case UBLOX_NAV:
272                 switch (id) {
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",
276                                 nav_dop->itow,
277                                 nav_dop->gdop,
278                                 nav_dop->ddop,
279                                 nav_dop->tdop,
280                                 nav_dop->vdop,
281                                 nav_dop->hdop,
282                                 nav_dop->ndop,
283                                 nav_dop->edop);
284                         return;
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",
288                                 nav_posllh->itow,
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);
295                         return;
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);
301                         return;
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);
306                         int i;
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);
318                         }
319                         return;
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",
323                                 nav_velned->itow,
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);
332                         return;
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",
336                                 nav_timeutc->itow,
337                                 nav_timeutc->tacc,
338                                 nav_timeutc->nano,
339                                 nav_timeutc->year,
340                                 nav_timeutc->month,
341                                 nav_timeutc->day,
342                                 nav_timeutc->hour,
343                                 nav_timeutc->min,
344                                 nav_timeutc->sec,
345                                 nav_timeutc->valid);
346                         return;
347                 }
348                 break;
349         }
350 #if 1
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);
355 #endif
356         printf ("\n");
357 }
358
359 void
360 ao_dump_state(void *wchan)
361 {
362         if (wchan == &ao_gps_new)
363                 ao_gps_show();
364         return;
365 }
366
367 int
368 ao_gps_open(const char *tty)
369 {
370         struct termios  termios;
371         int fd;
372
373         fd = open (tty, O_RDWR);
374         if (fd < 0)
375                 return -1;
376
377         tcgetattr(fd, &termios);
378         cfmakeraw(&termios);
379         cfsetspeed(&termios, B4800);
380         tcsetattr(fd, TCSAFLUSH, &termios);
381
382         tcdrain(fd);
383         tcflush(fd, TCIFLUSH);
384         return fd;
385 }
386
387 #include <getopt.h>
388
389 static const struct option options[] = {
390         { .name = "tty", .has_arg = 1, .val = 'T' },
391         { 0, 0, 0, 0},
392 };
393
394 static void usage(char *program)
395 {
396         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
397         exit(1);
398 }
399
400 int
401 main (int argc, char **argv)
402 {
403         char    *tty = "/dev/ttyUSB0";
404         int     c;
405
406         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
407                 switch (c) {
408                 case 'T':
409                         tty = optarg;
410                         break;
411                 default:
412                         usage(argv[0]);
413                         break;
414                 }
415         }
416         ao_gps_fd = ao_gps_open(tty);
417         if (ao_gps_fd < 0) {
418                 perror (tty);
419                 exit (1);
420         }
421         ao_gps_file = fdopen(ao_gps_fd, "r");
422         ao_gps();
423         return 0;
424 }