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