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