31c7af60159867f1274d2f07b0b9c0bf3675921a
[fw/altos] / src / test / ao_gps_test_ublox.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 #include <unistd.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_telemetry_location {
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                 pdop;           /* * 5 */
48         uint8_t                 hdop;           /* * 5 */
49         uint8_t                 vdop;           /* * 5 */
50         int16_t                 climb_rate;     /* cm/s */
51         uint16_t                h_error;        /* m */
52         uint16_t                v_error;        /* m */
53 };
54
55 #define UBLOX_SAT_STATE_ACQUIRED                (1 << 0)
56 #define UBLOX_SAT_STATE_CARRIER_PHASE_VALID     (1 << 1)
57 #define UBLOX_SAT_BIT_SYNC_COMPLETE             (1 << 2)
58 #define UBLOX_SAT_SUBFRAME_SYNC_COMPLETE        (1 << 3)
59 #define UBLOX_SAT_CARRIER_PULLIN_COMPLETE       (1 << 4)
60 #define UBLOX_SAT_CODE_LOCKED                   (1 << 5)
61 #define UBLOX_SAT_ACQUISITION_FAILED            (1 << 6)
62 #define UBLOX_SAT_EPHEMERIS_AVAILABLE           (1 << 7)
63
64 struct ao_telemetry_satellite_info {
65         uint8_t         svid;
66         uint8_t         c_n_1;
67 };
68
69 #define AO_TELEMETRY_SATELLITE_MAX_SAT  12
70
71 struct ao_telemetry_satellite {
72         uint8_t                                 channels;
73         struct ao_telemetry_satellite_info      sats[AO_TELEMETRY_SATELLITE_MAX_SAT];
74 };
75
76 #define ao_gps_orig ao_telemetry_location
77 #define ao_gps_tracking_orig ao_telemetry_satellite
78 #define ao_gps_sat_orig ao_telemetry_satellite_info
79
80 extern __xdata struct ao_telemetry_location     ao_gps_data;
81 extern __xdata struct ao_telemetry_satellite    ao_gps_tracking_data;
82
83 uint8_t ao_gps_mutex;
84
85 void
86 ao_mutex_get(uint8_t *mutex)
87 {
88 }
89
90 void
91 ao_mutex_put(uint8_t *mutex)
92 {
93 }
94
95 static int ao_gps_fd;
96 static FILE *ao_gps_file;
97
98 #if 0
99 static void
100 ao_dbg_char(char c)
101 {
102         char    line[128];
103         line[0] = '\0';
104         if (c < ' ') {
105                 if (c == '\n')
106                         sprintf (line, "\n");
107                 else
108                         sprintf (line, "\\%02x", ((int) c) & 0xff);
109         } else {
110                 sprintf (line, "%c", c);
111         }
112         write(1, line, strlen(line));
113 }
114 #endif
115
116 #include <sys/time.h>
117
118 int
119 get_millis(void)
120 {
121         struct timeval  tv;
122         gettimeofday(&tv, NULL);
123         return tv.tv_sec * 1000 + tv.tv_usec / 1000;
124 }
125
126 static uint8_t  in_message[4096];
127 static int      in_len;
128 static uint16_t recv_len;
129
130 static void check_ublox_message(char *which, uint8_t *msg);
131
132 char
133 ao_gps_getchar(void)
134 {
135         char    c;
136         uint8_t uc;
137         int     i;
138
139         i = getc(ao_gps_file);
140         if (i == EOF) {
141                 perror("getchar");
142                 exit(1);
143         }
144         c = i;
145         uc = (uint8_t) c;
146         if (in_len || uc == 0xb5) {
147                 in_message[in_len++] = c;
148                 if (in_len == 6) {
149                         recv_len = in_message[4] | (in_message[5] << 8);
150                 } else if (in_len > 6 && in_len == recv_len + 8) {
151                         check_ublox_message("recv", in_message + 2);
152                         in_len = 0;
153                 }
154                 
155         }
156         return c;
157 }
158
159 #define MESSAGE_LEN     4096
160
161 static uint8_t  message[MESSAGE_LEN];
162 static int      message_len;
163 static uint16_t send_len;
164
165 void
166 ao_gps_putchar(char c)
167 {
168         int     i;
169         uint8_t uc = (uint8_t) c;
170
171         if (message_len || uc == 0xb5) {
172                 if (message_len < MESSAGE_LEN)
173                         message[message_len++] = uc;
174                 if (message_len == 6) {
175                         send_len = message[4] | (message[5] << 8);
176                 } else if (message_len > 6 && message_len == send_len + 8) {
177                         check_ublox_message("send", message + 2);
178                         message_len = 0;
179                 }
180         }
181
182         for (;;) {
183                 i = write(ao_gps_fd, &c, 1);
184                 if (i == 1)
185                         break;
186                 if (i < 0 && (errno == EINTR || errno == EAGAIN))
187                         continue;
188                 perror("putchar");
189                 exit(1);
190         }
191 }
192
193 #define AO_SERIAL_SPEED_4800    0
194 #define AO_SERIAL_SPEED_9600    1
195 #define AO_SERIAL_SPEED_57600   2
196 #define AO_SERIAL_SPEED_115200  3
197
198 static void
199 ao_gps_set_speed(uint8_t speed)
200 {
201         int     fd = ao_gps_fd;
202         struct termios  termios;
203
204         printf ("\t\tset speed %d\n", speed);
205         tcdrain(fd);
206         tcgetattr(fd, &termios);
207         switch (speed) {
208         case AO_SERIAL_SPEED_4800:
209                 cfsetspeed(&termios, B4800);
210                 break;
211         case AO_SERIAL_SPEED_9600:
212                 cfsetspeed(&termios, B9600);
213                 break;
214         case AO_SERIAL_SPEED_57600:
215                 cfsetspeed(&termios, B57600);
216                 break;
217         case AO_SERIAL_SPEED_115200:
218                 cfsetspeed(&termios, B115200);
219                 break;
220         }
221         tcsetattr(fd, TCSAFLUSH, &termios);
222         tcflush(fd, TCIFLUSH);
223 }
224
225 #define ao_time() 0
226
227 uint8_t ao_task_minimize_latency;
228
229 #define ao_usb_getchar()        0
230
231 #include "ao_gps_print.c"
232 #include "ao_gps_show.c"
233 #include "ao_gps_ublox.c"
234
235 static void
236 check_ublox_message(char *which, uint8_t *msg)
237 {
238         uint8_t class = msg[0];
239         uint8_t id = msg[1];
240         uint16_t len = msg[2] | (msg[3] << 8);
241         uint16_t i;
242         struct ao_ublox_cksum   cksum_msg = { .a = msg[4 + len],
243                                               .b = msg[4 + len + 1] };
244         struct ao_ublox_cksum   cksum= { 0, 0 };
245
246         for (i = 0; i < 4 + len; i++) {
247                 add_cksum(&cksum, msg[i]);
248         }
249         if (cksum.a != cksum_msg.a || cksum.b != cksum_msg.b) {
250                 printf ("\t%s: cksum mismatch %02x,%02x != %02x,%02x\n",
251                         which,
252                         cksum_msg.a & 0xff,
253                         cksum_msg.b & 0xff,
254                         cksum.a & 0xff,
255                         cksum.b & 0xff);
256                 return;
257         }
258         switch (class) {
259         case UBLOX_NAV:
260                 switch (id) {
261                 case UBLOX_NAV_DOP: ;
262                         struct ublox_nav_dop    *nav_dop = (void *) msg;
263                         printf ("\tnav-dop    iTOW %9u gDOP %5u dDOP %5u tDOP %5u vDOP %5u hDOP %5u nDOP %5u eDOP %5u\n",
264                                 nav_dop->itow,
265                                 nav_dop->gdop,
266                                 nav_dop->ddop,
267                                 nav_dop->tdop,
268                                 nav_dop->vdop,
269                                 nav_dop->hdop,
270                                 nav_dop->ndop,
271                                 nav_dop->edop);
272                         return;
273                 case UBLOX_NAV_POSLLH: ;
274                         struct ublox_nav_posllh *nav_posllh = (void *) msg;
275                         printf ("\tnav-posllh iTOW %9u lon %12.7f lat %12.7f height %10.3f hMSL %10.3f hAcc %10.3f vAcc %10.3f\n",
276                                 nav_posllh->itow,
277                                 nav_posllh->lon / 1e7,
278                                 nav_posllh->lat / 1e7,
279                                 nav_posllh->height / 1e3,
280                                 nav_posllh->hmsl / 1e3,
281                                 nav_posllh->hacc / 1e3,
282                                 nav_posllh->vacc / 1e3);
283                         return;
284                 case UBLOX_NAV_SOL: ;
285                         struct ublox_nav_sol    *nav_sol = (struct ublox_nav_sol *) msg;
286                         printf ("\tnav-sol    iTOW %9u fTOW %9d week %5d gpsFix %2d flags %02x\n",
287                                 nav_sol->itow, nav_sol->ftow, nav_sol->week,
288                                 nav_sol->gpsfix, nav_sol->flags);
289                         return;
290                 case UBLOX_NAV_SVINFO: ;
291                         struct ublox_nav_svinfo *nav_svinfo = (struct ublox_nav_svinfo *) msg;
292                         printf ("\tnav-svinfo iTOW %9u numCH %3d globalFlags %02x\n",
293                                 nav_svinfo->itow, nav_svinfo->numch, nav_svinfo->globalflags);
294                         int i;
295                         for (i = 0; i < nav_svinfo->numch; i++) {
296                                 struct ublox_nav_svinfo_block *nav_svinfo_block = (void *) (msg + 12 + 12 * i);
297                                 printf ("\t\tchn %3u svid %3u flags %02x quality %3u cno %3u elev %3d azim %6d prRes %9d\n",
298                                         nav_svinfo_block->chn,
299                                         nav_svinfo_block->svid,
300                                         nav_svinfo_block->flags,
301                                         nav_svinfo_block->quality,
302                                         nav_svinfo_block->cno,
303                                         nav_svinfo_block->elev,
304                                         nav_svinfo_block->azim,
305                                         nav_svinfo_block->prres);
306                         }
307                         return;
308                 case UBLOX_NAV_VELNED: ;
309                         struct ublox_nav_velned *nav_velned = (void *) msg;
310                         printf ("\tnav-velned iTOW %9u velN %10.2f velE %10.2f velD %10.2f speed %10.2f gSpeed %10.2f heading %10.5f sAcc %10.2f cAcc %10.5f\n",
311                                 nav_velned->itow,
312                                 nav_velned->veln / 1e2,
313                                 nav_velned->vele / 1e2,
314                                 nav_velned->veld / 1e2,
315                                 nav_velned->speed / 1e2,
316                                 nav_velned->gspeed / 1e2,
317                                 nav_velned->heading / 1e5,
318                                 nav_velned->sacc / 1e5,
319                                 nav_velned->cacc / 1e6);
320                         return;
321                 case UBLOX_NAV_TIMEUTC:;
322                         struct ublox_nav_timeutc *nav_timeutc = (void *) msg;
323                         printf ("\tnav-timeutc iTOW %9u tAcc %5u nano %5d %4u-%2d-%2d %2d:%02d:%02d flags %02x\n",
324                                 nav_timeutc->itow,
325                                 nav_timeutc->tacc,
326                                 nav_timeutc->nano,
327                                 nav_timeutc->year,
328                                 nav_timeutc->month,
329                                 nav_timeutc->day,
330                                 nav_timeutc->hour,
331                                 nav_timeutc->min,
332                                 nav_timeutc->sec,
333                                 nav_timeutc->valid);
334                         return;
335                 }
336                 break;
337         }
338 #if 1
339         printf ("\t%s: class %02x id %02x len %d:", which, class & 0xff, id & 0xff, len & 0xffff);
340         for (i = 0; i < len; i++)
341                 printf (" %02x", msg[4 + i]);
342         printf (" cksum %02x %02x", cksum_msg.a & 0xff, cksum_msg.b & 0xff);
343 #endif
344         printf ("\n");
345 }
346
347 void
348 ao_dump_state(void *wchan)
349 {
350         if (wchan == &ao_gps_new)
351                 ao_gps_show();
352         return;
353 }
354
355 int
356 ao_gps_open(const char *tty)
357 {
358         struct termios  termios;
359         int fd;
360
361         fd = open (tty, O_RDWR);
362         if (fd < 0)
363                 return -1;
364
365         tcgetattr(fd, &termios);
366         cfmakeraw(&termios);
367         cfsetspeed(&termios, B4800);
368         tcsetattr(fd, TCSAFLUSH, &termios);
369
370         tcdrain(fd);
371         tcflush(fd, TCIFLUSH);
372         return fd;
373 }
374
375 #include <getopt.h>
376
377 static const struct option options[] = {
378         { .name = "tty", .has_arg = 1, .val = 'T' },
379         { 0, 0, 0, 0},
380 };
381
382 static void usage(char *program)
383 {
384         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
385         exit(1);
386 }
387
388 int
389 main (int argc, char **argv)
390 {
391         char    *tty = "/dev/ttyUSB0";
392         int     c;
393
394         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
395                 switch (c) {
396                 case 'T':
397                         tty = optarg;
398                         break;
399                 default:
400                         usage(argv[0]);
401                         break;
402                 }
403         }
404         ao_gps_fd = ao_gps_open(tty);
405         if (ao_gps_fd < 0) {
406                 perror (tty);
407                 exit (1);
408         }
409         ao_gps_file = fdopen(ao_gps_fd, "r");
410         ao_gps();
411         return 0;
412 }