altos: Add logging and telem to telegps
[fw/altos] / ao-tools / ao-telem / ao-telem.c
1 /*
2  * Copyright © 2011 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 _GNU_SOURCE
19 #include <string.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include "cc.h"
25
26 static const struct option options[] = {
27         { .name = "crc", .has_arg = 0, .val = 'c' },
28         { 0, 0, 0, 0},
29 };
30
31 static void usage(char *program)
32 {
33         fprintf(stderr, "usage: %s\n"
34                 "\t{flight-log} ...\n", program);
35         exit(1);
36 }
37
38 #define bool(b) ((b) ? "true" : "false")
39
40 int
41 main (int argc, char **argv)
42 {
43         char    line[80];
44         int c, i, ret;
45         char *s;
46         FILE *file;
47         int serial;
48         int ignore_crc = 0;
49         while ((c = getopt_long(argc, argv, "c", options, NULL)) != -1) {
50                 switch (c) {
51                 case 'c':
52                         ignore_crc = 1;
53                         break;
54                 default:
55                         usage(argv[0]);
56                         break;
57                 }
58         }
59         for (i = optind; i < argc; i++) {
60                 file = fopen(argv[i], "r");
61                 if (!file) {
62                         perror(argv[i]);
63                         ret++;
64                         continue;
65                 }
66                 s = strstr(argv[i], "-serial-");
67                 if (s)
68                         serial = atoi(s + 8);
69                 else
70                         serial = 0;
71                 while (fgets(line, sizeof (line), file)) {
72                         union ao_telemetry_all telem;
73                         char call[AO_MAX_CALLSIGN+1];
74                         char version[AO_MAX_VERSION+1];
75
76                         if (cc_telemetry_parse(line, &telem)) {
77                                 int rssi = (int8_t) telem.generic.rssi / 2 - 74;
78
79                                 printf ("serial %5d rssi %d status %02x tick %5d type %3d ",
80                                         telem.generic.serial, rssi, telem.generic.status,
81                                         telem.generic.tick, telem.generic.type);
82                                 if (!ignore_crc && (telem.generic.status & (1 << 7)) == 0) {
83                                         printf ("CRC error\n");
84                                         continue;
85                                 }
86                                 switch (telem.generic.type) {
87                                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
88                                 case AO_TELEMETRY_SENSOR_TELEMINI:
89                                 case AO_TELEMETRY_SENSOR_TELENANO:
90                                         printf ("state %1d accel %5d pres %5d ",
91                                                 telem.sensor.state, telem.sensor.accel, telem.sensor.pres);
92                                         printf ("accel %6.2f speed %6.2f height %5d ",
93                                                 telem.sensor.acceleration / 16.0,
94                                                 telem.sensor.speed / 16.0,
95                                                 telem.sensor.height);
96                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d\n",
97                                                 telem.sensor.ground_pres,
98                                                 telem.sensor.ground_accel,
99                                                 telem.sensor.accel_plus_g,
100                                                 telem.sensor.accel_minus_g);
101                                         break;
102                                 case AO_TELEMETRY_CONFIGURATION:
103                                         memcpy(call, telem.configuration.callsign, AO_MAX_CALLSIGN);
104                                         memcpy(version, telem.configuration.version, AO_MAX_VERSION);
105                                         call[AO_MAX_CALLSIGN] = '\0';
106                                         version[AO_MAX_CALLSIGN] = '\0';
107                                         printf ("device %3d flight %5d config %3d.%03d delay %2d main %4d",
108                                                 telem.configuration.device,
109                                                 telem.configuration.flight,
110                                                 telem.configuration.config_major,
111                                                 telem.configuration.config_minor,
112                                                 telem.configuration.apogee_delay,
113                                                 telem.configuration.main_deploy,
114                                                 telem.configuration.flight_log_max);
115                                         printf (" call %8s version %8s\n", call, version);
116                                         break;
117                                 case AO_TELEMETRY_LOCATION:
118                                         printf ("sats %d flags %s%s%s%s",
119                                                 telem.location.flags & 0xf,
120                                                 (telem.location.flags & (1 << 4)) ? "valid" : "invalid",
121                                                 (telem.location.flags & (1 << 5)) ? ",running" : "",
122                                                 (telem.location.flags & (1 << 6)) ? ",date" : "",
123                                                 (telem.location.flags & (1 << 7)) ? ",course" : "");
124                                         printf (" alt %5d lat %12.7f lon %12.7f",
125                                                 telem.location.altitude,
126                                                 telem.location.latitude / 1e7,
127                                                 telem.location.longitude / 1e7);
128                                         if ((telem.location.flags & (1 << 6)) != 0) {
129                                                 printf (" year %2d month %2d day %2d",
130                                                         telem.location.year,
131                                                         telem.location.month,
132                                                         telem.location.day);
133                                                 printf (" hour %2d minute %2d second %2d",
134                                                         telem.location.hour,
135                                                         telem.location.minute,
136                                                         telem.location.second);
137                                         }
138                                         printf (" pdop %3.1f hdop %3.1f vdop %3.1f mode %d",
139                                                 telem.location.pdop / 5.0,
140                                                 telem.location.hdop / 5.0,
141                                                 telem.location.vdop / 5.0,
142                                                 telem.location.mode);
143                                         if ((telem.location.flags & (1 << 7)) != 0)
144                                                 printf (" ground_speed %6.2f climb_rate %6.2f course %d",
145                                                         telem.location.ground_speed / 100.0,
146                                                         telem.location.climb_rate / 100.0,
147                                                         telem.location.course * 2);
148                                         printf ("\n");
149                                         break;
150                                 case AO_TELEMETRY_SATELLITE:
151                                         printf ("sats %d", telem.satellite.channels);
152                                         for (c = 0; c < 12 && c < telem.satellite.channels; c++) {
153                                                 printf (" sat %d svid %d c_n_1 %d",
154                                                         c,
155                                                         telem.satellite.sats[c].svid,
156                                                         telem.satellite.sats[c].c_n_1);
157                                         }
158                                         printf ("\n");
159                                         break;
160                                 case AO_TELEMETRY_MEGA_SENSOR:
161                                         printf ("accel %5d pres %9d temp %5d accel_x %5d accel_y %5d accel_z %5d gyro_x %5d gyro_y %5d gyro_z %5d mag_x %5d mag_y %5d mag_z %5d\n",
162                                                 telem.mega_sensor.accel,
163                                                 telem.mega_sensor.pres,
164                                                 telem.mega_sensor.temp,
165                                                 telem.mega_sensor.accel_x,
166                                                 telem.mega_sensor.accel_y,
167                                                 telem.mega_sensor.accel_z,
168                                                 telem.mega_sensor.gyro_x,
169                                                 telem.mega_sensor.gyro_y,
170                                                 telem.mega_sensor.gyro_z,
171                                                 telem.mega_sensor.mag_x,
172                                                 telem.mega_sensor.mag_y,
173                                                 telem.mega_sensor.mag_z);
174                                         break;
175                                 case AO_TELEMETRY_MEGA_DATA:
176                                         printf ("state %1d v_batt %5d v_pyro %5d ",
177                                                 telem.mega_data.state,
178                                                 telem.mega_data.v_batt,
179                                                 telem.mega_data.v_pyro);
180                                         for (c = 0; c < 6; c++)
181                                                 printf ("s%1d %5d ", c,
182                                                         telem.mega_data.sense[c] |
183                                                         (telem.mega_data.sense[c] << 8));
184                                         
185                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d ",
186                                                 telem.mega_data.ground_pres,
187                                                 telem.mega_data.ground_accel,
188                                                 telem.mega_data.accel_plus_g,
189                                                 telem.mega_data.accel_minus_g);
190
191                                         printf ("accel %6.2f speed %6.2f height %5d\n",
192                                                 telem.mega_data.acceleration / 16.0,
193                                                 telem.mega_data.speed / 16.0,
194                                                 telem.mega_data.height);
195
196                                         break;
197                                 default:
198                                         printf("\n");
199                                 }
200                         }
201                 }
202                 fclose (file);
203
204         }
205         return ret;
206 }