altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / test / ao_gps_test_skytraq.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_gps_orig {
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;       /* m */
50         uint16_t                ground_speed;   /* cm/s */
51         uint8_t                 course;         /* degrees / 2 */
52         uint8_t                 hdop;           /* * 5 */
53         int16_t                 climb_rate;     /* cm/s */
54         uint16_t                h_error;        /* m */
55         uint16_t                v_error;        /* m */
56 };
57
58 #define AO_TELEMETRY_LOCATION_ALTITUDE(l)       ((l)->altitude)
59 #define AO_TELEMETRY_LOCATION_SET_ALTITUDE(l,a) ((l)->altitude = (a))
60
61 #define SIRF_SAT_STATE_ACQUIRED                 (1 << 0)
62 #define SIRF_SAT_STATE_CARRIER_PHASE_VALID      (1 << 1)
63 #define SIRF_SAT_BIT_SYNC_COMPLETE              (1 << 2)
64 #define SIRF_SAT_SUBFRAME_SYNC_COMPLETE         (1 << 3)
65 #define SIRF_SAT_CARRIER_PULLIN_COMPLETE        (1 << 4)
66 #define SIRF_SAT_CODE_LOCKED                    (1 << 5)
67 #define SIRF_SAT_ACQUISITION_FAILED             (1 << 6)
68 #define SIRF_SAT_EPHEMERIS_AVAILABLE            (1 << 7)
69
70 struct ao_gps_sat_orig {
71         uint8_t         svid;
72         uint8_t         c_n_1;
73 };
74
75 #define AO_MAX_GPS_TRACKING     12
76
77 struct ao_gps_tracking_orig {
78         uint8_t                 channels;
79         struct ao_gps_sat_orig  sats[AO_MAX_GPS_TRACKING];
80 };
81
82 #define ao_telemetry_location ao_gps_orig
83 #define ao_telemetry_satellite ao_gps_tracking_orig
84 #define ao_telemetry_satellite_info ao_gps_sat_orig
85
86 extern struct ao_telemetry_location     ao_gps_data;
87 extern struct ao_telemetry_satellite    ao_gps_tracking_data;
88
89 uint8_t ao_gps_mutex;
90
91 void
92 ao_mutex_get(uint8_t *mutex)
93 {
94 }
95
96 void
97 ao_mutex_put(uint8_t *mutex)
98 {
99 }
100
101 static int
102 ao_gps_fd;
103
104 #if 0
105 static void
106 ao_dbg_char(char c)
107 {
108         char    line[128];
109         line[0] = '\0';
110         if (c < ' ') {
111                 if (c == '\n')
112                         sprintf (line, "\n");
113                 else
114                         sprintf (line, "\\%02x", ((int) c) & 0xff);
115         } else {
116                 sprintf (line, "%c", c);
117         }
118         write(1, line, strlen(line));
119 }
120 #endif
121
122 #define QUEUE_LEN       4096
123
124 static char     input_queue[QUEUE_LEN];
125 int             input_head, input_tail;
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 void
138 check_skytraq_message(char *from, uint8_t *msg, int len)
139 {
140         uint16_t        encoded_len, encoded_cksum;
141         uint16_t        cksum;
142         uint8_t         id;
143         int             i;
144
145 //      fwrite(msg, 1, len, stdout);
146         return;
147         if (msg[0] != 0xa0 || msg[1] != 0xa2) {
148                 printf ("bad header\n");
149                 return;
150         }
151         if (len < 7) {
152                 printf("short\n");
153                 return;
154         }
155         if (msg[len-1] != 0xb3 || msg[len-2] != 0xb0) {
156                 printf ("bad trailer\n");
157                 return;
158         }
159         encoded_len = (msg[2] << 8) | msg[3];
160         id = msg[4];
161 /*      printf ("%9d: %3d\n", get_millis(), id); */
162         if (encoded_len != len - 8) {
163                 if (id != 52)
164                         printf ("length mismatch (got %d, wanted %d)\n",
165                                 len - 8, encoded_len);
166                 return;
167         }
168         encoded_cksum = (msg[len - 4] << 8) | msg[len-3];
169         cksum = 0;
170         for (i = 4; i < len - 4; i++)
171                 cksum = (cksum + msg[i]) & 0x7fff;
172         if (encoded_cksum != cksum) {
173                 printf ("cksum mismatch (got %04x wanted %04x)\n",
174                         cksum, encoded_cksum);
175                 return;
176         }
177         id = msg[4];
178         switch (id) {
179         case 41:{
180                 int     off = 4;
181
182                 uint8_t         id;
183                 uint16_t        nav_valid;
184                 uint16_t        nav_type;
185                 uint16_t        week;
186                 uint32_t        tow;
187                 uint16_t        year;
188                 uint8_t         month;
189                 uint8_t         day;
190                 uint8_t         hour;
191                 uint8_t         minute;
192                 uint16_t        second;
193                 uint32_t        sat_list;
194                 int32_t         lat;
195                 int32_t         lon;
196                 int32_t         alt_ell;
197                 int32_t         alt_msl;
198                 int8_t          datum;
199                 uint16_t        sog;
200                 uint16_t        cog;
201                 int16_t         mag_var;
202                 int16_t         climb_rate;
203                 int16_t         heading_rate;
204                 uint32_t        h_error;
205                 uint32_t        v_error;
206                 uint32_t        t_error;
207                 uint16_t        h_v_error;
208
209 #define get_u8(u)       u = (msg[off]); off+= 1
210 #define get_u16(u)      u = (msg[off] << 8) | (msg[off + 1]); off+= 2
211 #define get_u32(u)      u = (msg[off] << 24) | (msg[off + 1] << 16) | (msg[off+2] << 8) | (msg[off+3]); off+= 4
212
213                 get_u8(id);
214                 get_u16(nav_valid);
215                 get_u16(nav_type);
216                 get_u16(week);
217                 get_u32(tow);
218                 get_u16(year);
219                 get_u8(month);
220                 get_u8(day);
221                 get_u8(hour);
222                 get_u8(minute);
223                 get_u16(second);
224                 get_u32(sat_list);
225                 get_u32(lat);
226                 get_u32(lon);
227                 get_u32(alt_ell);
228                 get_u32(alt_msl);
229                 get_u8(datum);
230                 get_u16(sog);
231                 get_u16(cog);
232                 get_u16(mag_var);
233                 get_u16(climb_rate);
234                 get_u16(heading_rate);
235                 get_u32(h_error);
236                 get_u32(v_error);
237                 get_u32(t_error);
238                 get_u16(h_v_error);
239
240
241                 (void) mag_var;
242                 (void) id;
243                 printf ("Geodetic Navigation Data (41):\n");
244                 printf ("\tNav valid %04x\n", nav_valid);
245                 printf ("\tNav type %04x\n", nav_type);
246                 printf ("\tWeek %5d", week);
247                 printf (" TOW %9d", tow);
248                 printf (" %4d-%2d-%2d %02d:%02d:%07.4f\n",
249                         year, month, day,
250                         hour, minute, second / 1000.0);
251                 printf ("\tsats: %08x\n", sat_list);
252                 printf ("\tlat: %g", lat / 1.0e7);
253                 printf (" lon: %g", lon / 1.0e7);
254                 printf (" alt_ell: %g", alt_ell / 100.0);
255                 printf (" alt_msll: %g", alt_msl / 100.0);
256                 printf (" datum: %d\n", datum);
257                 printf ("\tground speed: %g", sog / 100.0);
258                 printf (" course: %g", cog / 100.0);
259                 printf (" climb: %g", climb_rate / 100.0);
260                 printf (" heading rate: %g\n", heading_rate / 100.0);
261                 printf ("\th error: %g", h_error / 100.0);
262                 printf (" v error: %g", v_error / 100.0);
263                 printf (" t error: %g", t_error / 100.0);
264                 printf (" h vel error: %g\n", h_v_error / 100.0);
265                 break;
266         }
267         case 4: {
268                 int off = 4;
269                 uint8_t         id;
270                 int16_t         gps_week;
271                 uint32_t        gps_tow;
272                 uint8_t         channels;
273                 int             j, k;
274
275                 get_u8(id);
276                 get_u16(gps_week);
277                 get_u32(gps_tow);
278                 get_u8(channels);
279
280                 (void) id;
281                 printf ("Measured Tracker Data (4):\n");
282                 printf ("GPS week: %d\n", gps_week);
283                 printf ("GPS time of week: %d\n", gps_tow);
284                 printf ("channels: %d\n", channels);
285                 for (j = 0; j < 12; j++) {
286                         uint8_t svid, azimuth, elevation;
287                         uint16_t state;
288                         uint8_t c_n[10];
289                         get_u8(svid);
290                         get_u8(azimuth);
291                         get_u8(elevation);
292                         get_u16(state);
293                         for (k = 0; k < 10; k++) {
294                                 get_u8(c_n[k]);
295                         }
296                         printf ("Sat %3d:", svid);
297                         printf (" aziumuth: %6.1f", azimuth * 1.5);
298                         printf (" elevation: %6.1f", elevation * 0.5);
299                         printf (" state: 0x%02x", state);
300                         printf (" c_n:");
301                         for (k = 0; k < 10; k++)
302                                 printf(" %3d", c_n[k]);
303                         if (state & SIRF_SAT_STATE_ACQUIRED)
304                                 printf(" acq,");
305                         if (state & SIRF_SAT_STATE_CARRIER_PHASE_VALID)
306                                 printf(" car,");
307                         if (state & SIRF_SAT_BIT_SYNC_COMPLETE)
308                                 printf(" bit,");
309                         if (state & SIRF_SAT_SUBFRAME_SYNC_COMPLETE)
310                                 printf(" sub,");
311                         if (state & SIRF_SAT_CARRIER_PULLIN_COMPLETE)
312                                 printf(" pullin,");
313                         if (state & SIRF_SAT_CODE_LOCKED)
314                                 printf(" code,");
315                         if (state & SIRF_SAT_ACQUISITION_FAILED)
316                                 printf(" fail,");
317                         if (state & SIRF_SAT_EPHEMERIS_AVAILABLE)
318                                 printf(" ephem,");
319                         printf ("\n");
320                 }
321                 break;
322         }
323         default:
324                 return;
325                 printf ("%s %4d:", from, encoded_len);
326                 for (i = 4; i < len - 4; i++) {
327                         if (((i - 4) & 0xf) == 0)
328                                 printf("\n   ");
329                         printf (" %3d", msg[i]);
330                 }
331                 printf ("\n");
332         }
333 }
334
335 static uint8_t  skytraq_message[4096];
336 static int      skytraq_message_len;
337 static uint8_t  skytraq_in_message[4096];
338 static int      skytraq_in_len;
339
340 char
341 ao_serial1_getchar(void)
342 {
343         char    c;
344         uint8_t uc;
345
346         while (input_head == input_tail) {
347                 for (;;) {
348                         input_tail = read(ao_gps_fd, input_queue, QUEUE_LEN);
349                         if (input_tail < 0) {
350                                 if (errno == EINTR || errno == EAGAIN)
351                                         continue;
352                                 perror ("getchar");
353                                 exit (1);
354                         }
355                         input_head = 0;
356                         break;
357                 }
358         }
359         c = input_queue[input_head];
360         input_head = (input_head + 1) % QUEUE_LEN;
361         uc = c;
362 //      printf ("c: %02x %c\n", uc, uc);
363         if (skytraq_in_len || uc == '$') {
364                 if (skytraq_in_len < 4096)
365                         skytraq_in_message[skytraq_in_len++] = uc;
366                 if (uc == 0x0a) {
367                         check_skytraq_message("recv", skytraq_in_message, skytraq_in_len);
368                         skytraq_in_len = 0;
369                 }
370         }
371         return c;
372 }
373
374
375 void
376 ao_serial1_putchar(char c)
377 {
378         int     i;
379         uint8_t uc = (uint8_t) c;
380
381         if (skytraq_message_len || uc == 0xa0) {
382                 if (skytraq_message_len < 4096)
383                         skytraq_message[skytraq_message_len++] = uc;
384                 if (uc == 0x0a) {
385                         check_skytraq_message("send", skytraq_message, skytraq_message_len);
386                         skytraq_message_len = 0;
387                 }
388         }
389         for (;;) {
390                 i = write(ao_gps_fd, &c, 1);
391                 if (i == 1) {
392                         if ((uint8_t) c == 0xb3 || c == '\r') {
393 /*                              static const struct timespec delay = {
394                                         .tv_sec = 0,
395                                         .tv_nsec = 100 * 1000 * 1000
396                                 };
397 */
398                                 tcdrain(ao_gps_fd);
399 //                              nanosleep(&delay, NULL);
400                         }
401                         break;
402                 }
403                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
404                         continue;
405                 perror("putchar");
406                 exit(1);
407         }
408 }
409
410 #define AO_SERIAL_SPEED_4800    0
411 #define AO_SERIAL_SPEED_9600    1
412 #define AO_SERIAL_SPEED_57600   2
413 #define AO_SERIAL_SPEED_115200  3
414
415 static void
416 ao_serial1_set_speed(uint8_t speed)
417 {
418         int     fd = ao_gps_fd;
419         struct termios  termios;
420
421         tcdrain(fd);
422         tcgetattr(fd, &termios);
423         switch (speed) {
424         case AO_SERIAL_SPEED_4800:
425                 cfsetspeed(&termios, B4800);
426                 break;
427         case AO_SERIAL_SPEED_9600:
428                 cfsetspeed(&termios, B9600);
429                 break;
430         case AO_SERIAL_SPEED_57600:
431                 cfsetspeed(&termios, B57600);
432                 break;
433         case AO_SERIAL_SPEED_115200:
434                 cfsetspeed(&termios, B115200);
435                 break;
436         }
437         tcsetattr(fd, TCSAFLUSH, &termios);
438         tcflush(fd, TCIFLUSH);
439 }
440
441 #define ao_time() 0
442
443 uint8_t ao_task_minimize_latency;
444
445 #define ao_usb_getchar()        0
446
447 #include "ao_gps_print.c"
448 #include "ao_gps_show.c"
449 #include "ao_gps_skytraq.c"
450
451 void
452 ao_dump_state(void *wchan)
453 {
454         if (wchan == &ao_gps_new)
455                 ao_gps_show();
456 }
457
458 int
459 ao_gps_open(const char *tty)
460 {
461         struct termios  termios;
462         int fd;
463
464         fd = open (tty, O_RDWR);
465         if (fd < 0)
466                 return -1;
467
468         tcgetattr(fd, &termios);
469         cfmakeraw(&termios);
470         cfsetspeed(&termios, B4800);
471         tcsetattr(fd, TCSAFLUSH, &termios);
472
473         tcdrain(fd);
474         tcflush(fd, TCIFLUSH);
475         return fd;
476 }
477
478 #include <getopt.h>
479
480 static const struct option options[] = {
481         { .name = "tty", .has_arg = 1, .val = 'T' },
482         { 0, 0, 0, 0},
483 };
484
485 static void usage(char *program)
486 {
487         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
488         exit(1);
489 }
490
491 int
492 main (int argc, char **argv)
493 {
494         char    *tty = "/dev/ttyUSB0";
495         int     c;
496
497         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
498                 switch (c) {
499                 case 'T':
500                         tty = optarg;
501                         break;
502                 default:
503                         usage(argv[0]);
504                         break;
505                 }
506         }
507         ao_gps_fd = ao_gps_open(tty);
508         if (ao_gps_fd < 0) {
509                 perror (tty);
510                 exit (1);
511         }
512         ao_gps();
513         return 0;
514 }