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