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