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