altos: Extend GPS altitudes to at least 24 bits everywhere
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #define AO_GPS_TEST
19 #define HAS_GPS 1
20 #include "ao_host.h"
21 #include <termios.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
28 #define AO_GPS_NUM_SAT_SHIFT    (0)
29
30 #define AO_GPS_NEW_DATA         1
31 #define AO_GPS_NEW_TRACKING     2
32
33 #define AO_GPS_VALID            (1 << 4)
34 #define AO_GPS_RUNNING          (1 << 5)
35 #define AO_GPS_DATE_VALID       (1 << 6)
36 #define AO_GPS_COURSE_VALID     (1 << 7)
37
38 struct ao_telemetry_location {
39         uint8_t                 year;
40         uint8_t                 month;
41         uint8_t                 day;
42         uint8_t                 hour;
43         uint8_t                 minute;
44         uint8_t                 second;
45         uint8_t                 flags;
46         int32_t                 latitude;       /* degrees * 10⁷ */
47         int32_t                 longitude;      /* degrees * 10⁷ */
48         int16_t                 altitude_low;   /* m */
49         uint16_t                ground_speed;   /* cm/s */
50         uint8_t                 course;         /* degrees / 2 */
51         uint8_t                 pdop;           /* * 5 */
52         uint8_t                 hdop;           /* * 5 */
53         uint8_t                 vdop;           /* * 5 */
54         int16_t                 climb_rate;     /* cm/s */
55         uint16_t                h_error;        /* m */
56         uint16_t                v_error;        /* m */
57         int16_t                 altitude_high;  /* m */
58 };
59
60 typedef int32_t         gps_alt_t;
61 #define AO_TELEMETRY_LOCATION_ALTITUDE(l)       (((gps_alt_t) (l)->altitude_high << 16) | ((l)->altitude_low))
62 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) (((l)->altitude_high = (a) >> 16), \
63                                                  ((l)->altitude_low = (a)))
64
65 #define UBLOX_SAT_STATE_ACQUIRED                (1 << 0)
66 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID     (1 << 1)
67 #define UBLOX_SAT_BIT_SYNC_COMPLETE             (1 << 2)
68 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE        (1 << 3)
69 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE       (1 << 4)
70 #define UBLOX_SAT_CODE_LOCKED                   (1 << 5)
71 #define UBLOX_SAT_ACQUISITION_FAILED            (1 << 6)
72 #define UBLOX_SAT_EPHEMERIS_AVAILABLE           (1 << 7)
73
74 struct ao_telemetry_satellite_info {
75         uint8_t         svid;
76         uint8_t         c_n_1;
77 };
78
79 #define AO_TELEMETRY_SATELLITE_MAX_SAT  12
80
81 struct ao_telemetry_satellite {
82         uint8_t                                 channels;
83         struct ao_telemetry_satellite_info      sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
84 };
85
86 #define ao_gps_orig ao_telemetry_location
87 #define ao_gps_tracking_orig ao_telemetry_satellite
88 #define ao_gps_sat_orig ao_telemetry_satellite_info
89
90 extern __xdata struct ao_telemetry_location     ao_gps_data;
91 extern __xdata struct ao_telemetry_satellite    ao_gps_tracking_data;
92
93 uint8_t ao_gps_mutex;
94
95 void
96 ao_mutex_get(uint8_t *mutex)
97 {
98 }
99
100 void
101 ao_mutex_put(uint8_t *mutex)
102 {
103 }
104
105 static int ao_gps_fd;
106 static FILE *ao_gps_file;
107
108 #if 0
109 static void
110 ao_dbg_char(char c)
111 {
112         char    line[128];
113         line[0] = '\0';
114         if (c < ' ') {
115                 if (c == '\n')
116                         sprintf (line, "\n");
117                 else
118                         sprintf (line, "\\%02x", ((int) c) & 0xff);
119         } else {
120                 sprintf (line, "%c", c);
121         }
122         write(1, line, strlen(line));
123 }
124 #endif
125
126 #include <sys/time.h>
127
128 int
129 get_millis(void)
130 {
131         struct timeval  tv;
132         gettimeofday(&tv, NULL);
133         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
134 }
135
136 static uint8_t  in_message[4096];
137 static int      in_len;
138 static uint16_t recv_len;
139
140 static void check_ublox_message(char *which, uint8_t *msg);
141
142 char
143 ao_gps_getchar(void)
144 {
145         char    c;
146         uint8_t uc;
147         int     i;
148
149         i = getc(ao_gps_file);
150         if (i == EOF) {
151                 perror("getchar");
152                 exit(1);
153         }
154         c = i;
155         uc = (uint8_t) c;
156         if (in_len || uc == 0xb5) {
157                 in_message[in_len++] = c;
158                 if (in_len == 6) {
159                         recv_len = in_message[4] | (in_message[5] << 8);
160                 } else if (in_len > 6 && in_len == recv_len + 8) {
161                         check_ublox_message("recv", in_message + 2);
162                         in_len = 0;
163                 }
164                 
165         }
166         return c;
167 }
168
169 #define MESSAGE_LEN     4096
170
171 static uint8_t  message[MESSAGE_LEN];
172 static int      message_len;
173 static uint16_t send_len;
174
175 void
176 ao_gps_putchar(char c)
177 {
178         int     i;
179         uint8_t uc = (uint8_t) c;
180
181         if (message_len || uc == 0xb5) {
182                 if (message_len < MESSAGE_LEN)
183                         message[message_len++] = uc;
184                 if (message_len == 6) {
185                         send_len = message[4] | (message[5] << 8);
186                 } else if (message_len > 6 && message_len == send_len + 8) {
187                         check_ublox_message("send", message + 2);
188                         message_len = 0;
189                 }
190         }
191
192         for (;;) {
193                 i = write(ao_gps_fd, &c, 1);
194                 if (i == 1)
195                         break;
196                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
197                         continue;
198                 perror("putchar");
199                 exit(1);
200         }
201 }
202
203 #define AO_SERIAL_SPEED_4800    0
204 #define AO_SERIAL_SPEED_9600    1
205 #define AO_SERIAL_SPEED_57600   2
206 #define AO_SERIAL_SPEED_115200  3
207
208 static void
209 ao_gps_set_speed(uint8_t speed)
210 {
211         int     fd = ao_gps_fd;
212         struct termios  termios;
213
214         printf ("\t\tset speed %d\n", speed);
215         tcdrain(fd);
216         tcgetattr(fd, &termios);
217         switch (speed) {
218         case AO_SERIAL_SPEED_4800:
219                 cfsetspeed(&termios, B4800);
220                 break;
221         case AO_SERIAL_SPEED_9600:
222                 cfsetspeed(&termios, B9600);
223                 break;
224         case AO_SERIAL_SPEED_57600:
225                 cfsetspeed(&termios, B57600);
226                 break;
227         case AO_SERIAL_SPEED_115200:
228                 cfsetspeed(&termios, B115200);
229                 break;
230         }
231         tcsetattr(fd, TCSAFLUSH, &termios);
232         tcflush(fd, TCIFLUSH);
233 }
234
235 #define ao_time() 0
236
237 uint8_t ao_task_minimize_latency;
238
239 #define ao_usb_getchar()        0
240
241 #include "ao_gps_print.c"
242 #include "ao_gps_show.c"
243 #include "ao_gps_ublox.c"
244
245 static void
246 check_ublox_message(char *which, uint8_t *msg)
247 {
248         uint8_t class = msg[0];
249         uint8_t id = msg[1];
250         uint16_t len = msg[2] | (msg[3] << 8);
251         uint16_t i;
252         struct ao_ublox_cksum   cksum_msg = { .a = msg[4 + len],
253                                               .b = msg[4 + len + 1] };
254         struct ao_ublox_cksum   cksum= { 0, 0 };
255
256         for (i = 0; i < 4 + len; i++) {
257                 add_cksum(&cksum, msg[i]);
258         }
259         if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
260                 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
261                         which,
262                         cksum_msg.a & 0xff,
263                         cksum_msg.b & 0xff,
264                         cksum.a & 0xff,
265                         cksum.b & 0xff);
266                 return;
267         }
268         switch (class) {
269         case UBLOX_NAV:
270                 switch (id) {
271                 case UBLOX_NAV_DOP: ;
272                         struct ublox_nav_dop    *nav_dop = (void *) msg;
273                         printf ("\tnav-dop    iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
274                                 nav_dop->itow,
275                                 nav_dop->gdop,
276                                 nav_dop->ddop,
277                                 nav_dop->tdop,
278                                 nav_dop->vdop,
279                                 nav_dop->hdop,
280                                 nav_dop->ndop,
281                                 nav_dop->edop);
282                         return;
283                 case UBLOX_NAV_POSLLH: ;
284                         struct ublox_nav_posllh *nav_posllh = (void *) msg;
285                         printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
286                                 nav_posllh->itow,
287                                 nav_posllh->lon / 1e7,
288                                 nav_posllh->lat / 1e7,
289                                 nav_posllh->height / 1e3,
290                                 nav_posllh->hmsl / 1e3,
291                                 nav_posllh->hacc / 1e3,
292                                 nav_posllh->vacc / 1e3);
293                         return;
294                 case UBLOX_NAV_SOL: ;
295                         struct ublox_nav_sol    *nav_sol = (struct ublox_nav_sol *) msg;
296                         printf ("\tnav-sol    iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
297                                 nav_sol->itow, nav_sol->ftow, nav_sol->week,
298                                 nav_sol->gpsfix, nav_sol->flags);
299                         return;
300                 case UBLOX_NAV_SVINFO: ;
301                         struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
302                         printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
303                                 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
304                         int i;
305                         for (i = 0; i < nav_svinfo->numch; i++) {
306                                 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
307                                 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
308                                         nav_svinfo_block->chn,
309                                         nav_svinfo_block->svid,
310                                         nav_svinfo_block->flags,
311                                         nav_svinfo_block->quality,
312                                         nav_svinfo_block->cno,
313                                         nav_svinfo_block->elev,
314                                         nav_svinfo_block->azim,
315                                         nav_svinfo_block->prres);
316                         }
317                         return;
318                 case UBLOX_NAV_VELNED: ;
319                         struct ublox_nav_velned *nav_velned = (void *) msg;
320                         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",
321                                 nav_velned->itow,
322                                 nav_velned->veln / 1e2,
323                                 nav_velned->vele / 1e2,
324                                 nav_velned->veld / 1e2,
325                                 nav_velned->speed / 1e2,
326                                 nav_velned->gspeed / 1e2,
327                                 nav_velned->heading / 1e5,
328                                 nav_velned->sacc / 1e5,
329                                 nav_velned->cacc / 1e6);
330                         return;
331                 case UBLOX_NAV_TIMEUTC:;
332                         struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
333                         printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
334                                 nav_timeutc->itow,
335                                 nav_timeutc->tacc,
336                                 nav_timeutc->nano,
337                                 nav_timeutc->year,
338                                 nav_timeutc->month,
339                                 nav_timeutc->day,
340                                 nav_timeutc->hour,
341                                 nav_timeutc->min,
342                                 nav_timeutc->sec,
343                                 nav_timeutc->valid);
344                         return;
345                 }
346                 break;
347         }
348 #if 1
349         printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
350         for (i = 0; i < len; i++)
351                 printf (" %02x", msg[4 + i]);
352         printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
353 #endif
354         printf ("\n");
355 }
356
357 void
358 ao_dump_state(void *wchan)
359 {
360         if (wchan == &ao_gps_new)
361                 ao_gps_show();
362         return;
363 }
364
365 int
366 ao_gps_open(const char *tty)
367 {
368         struct termios  termios;
369         int fd;
370
371         fd = open (tty, O_RDWR);
372         if (fd < 0)
373                 return -1;
374
375         tcgetattr(fd, &termios);
376         cfmakeraw(&termios);
377         cfsetspeed(&termios, B4800);
378         tcsetattr(fd, TCSAFLUSH, &termios);
379
380         tcdrain(fd);
381         tcflush(fd, TCIFLUSH);
382         return fd;
383 }
384
385 #include <getopt.h>
386
387 static const struct option options[] = {
388         { .name = "tty", .has_arg = 1, .val = 'T' },
389         { 0, 0, 0, 0},
390 };
391
392 static void usage(char *program)
393 {
394         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
395         exit(1);
396 }
397
398 int
399 main (int argc, char **argv)
400 {
401         char    *tty = "/dev/ttyUSB0";
402         int     c;
403
404         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
405                 switch (c) {
406                 case 'T':
407                         tty = optarg;
408                         break;
409                 default:
410                         usage(argv[0]);
411                         break;
412                 }
413         }
414         ao_gps_fd = ao_gps_open(tty);
415         if (ao_gps_fd < 0) {
416                 perror (tty);
417                 exit (1);
418         }
419         ao_gps_file = fdopen(ao_gps_fd, "r");
420         ao_gps();
421         return 0;
422 }