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