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