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