aa54a1b9a4ffe5b0a1906ff211e659197118d463
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define _GNU_SOURCE
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include "cc.h"
26
27 static const struct option options[] = {
28         { .name = "crc", .has_arg = 0, .val = 'c' },
29         { 0, 0, 0, 0},
30 };
31
32 static void usage(char *program)
33 {
34         fprintf(stderr, "usage: %s\n"
35                 "\t{flight-log} ...\n", program);
36         exit(1);
37 }
38
39 #define bool(b) ((b) ? "true" : "false")
40
41 int
42 main (int argc, char **argv)
43 {
44         char    line[80];
45         int c, i, ret;
46         char *s;
47         FILE *file;
48         int serial;
49         int ignore_crc = 0;
50         while ((c = getopt_long(argc, argv, "c", options, NULL)) != -1) {
51                 switch (c) {
52                 case 'c':
53                         ignore_crc = 1;
54                         break;
55                 default:
56                         usage(argv[0]);
57                         break;
58                 }
59         }
60         for (i = optind; i < argc; i++) {
61                 file = fopen(argv[i], "r");
62                 if (!file) {
63                         perror(argv[i]);
64                         ret++;
65                         continue;
66                 }
67                 s = strstr(argv[i], "-serial-");
68                 if (s)
69                         serial = atoi(s + 8);
70                 else
71                         serial = 0;
72                 while (fgets(line, sizeof (line), file)) {
73                         union ao_telemetry_all telem;
74                         char call[AO_MAX_CALLSIGN+1];
75                         char version[AO_MAX_VERSION+1];
76
77                         if (cc_telemetry_parse(line, &telem)) {
78                                 int rssi = (int8_t) telem.generic.rssi / 2 - 74;
79
80                                 printf ("serial %5d rssi %d status %02x tick %5d type %3d ",
81                                         telem.generic.serial, rssi, telem.generic.status,
82                                         telem.generic.tick, telem.generic.type);
83                                 if (!ignore_crc && (telem.generic.status & (1 << 7)) == 0) {
84                                         printf ("CRC error\n");
85                                         continue;
86                                 }
87                                 switch (telem.generic.type) {
88                                 case AO_TELEMETRY_SENSOR_TELEMETRUM:
89                                 case AO_TELEMETRY_SENSOR_TELEMINI:
90                                 case AO_TELEMETRY_SENSOR_TELENANO:
91                                         printf ("state %1d accel %5d pres %5d ",
92                                                 telem.sensor.state, telem.sensor.accel, telem.sensor.pres);
93                                         printf ("accel %6.2f speed %6.2f height %5d ",
94                                                 telem.sensor.acceleration / 16.0,
95                                                 telem.sensor.speed / 16.0,
96                                                 telem.sensor.height);
97                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d\n",
98                                                 telem.sensor.ground_pres,
99                                                 telem.sensor.ground_accel,
100                                                 telem.sensor.accel_plus_g,
101                                                 telem.sensor.accel_minus_g);
102                                         break;
103                                 case AO_TELEMETRY_CONFIGURATION:
104                                         memcpy(call, telem.configuration.callsign, AO_MAX_CALLSIGN);
105                                         memcpy(version, telem.configuration.version, AO_MAX_VERSION);
106                                         call[AO_MAX_CALLSIGN] = '\0';
107                                         version[AO_MAX_CALLSIGN] = '\0';
108                                         printf ("device %3d flight %5d config %3d.%03d delay %2d main %4d",
109                                                 telem.configuration.device,
110                                                 telem.configuration.flight,
111                                                 telem.configuration.config_major,
112                                                 telem.configuration.config_minor,
113                                                 telem.configuration.apogee_delay,
114                                                 telem.configuration.main_deploy,
115                                                 telem.configuration.flight_log_max);
116                                         printf (" call %8s version %8s\n", call, version);
117                                         break;
118                                 case AO_TELEMETRY_LOCATION:
119                                         printf ("sats %d flags %s%s%s%s",
120                                                 telem.location.flags & 0xf,
121                                                 (telem.location.flags & (1 << 4)) ? "valid" : "invalid",
122                                                 (telem.location.flags & (1 << 5)) ? ",running" : "",
123                                                 (telem.location.flags & (1 << 6)) ? ",date" : "",
124                                                 (telem.location.flags & (1 << 7)) ? ",course" : "");
125                                         printf (" alt %5d lat %12.7f lon %12.7f",
126                                                 telem.location.altitude,
127                                                 telem.location.latitude / 1e7,
128                                                 telem.location.longitude / 1e7);
129                                         if ((telem.location.flags & (1 << 6)) != 0) {
130                                                 printf (" year %2d month %2d day %2d",
131                                                         telem.location.year,
132                                                         telem.location.month,
133                                                         telem.location.day);
134                                                 printf (" hour %2d minute %2d second %2d",
135                                                         telem.location.hour,
136                                                         telem.location.minute,
137                                                         telem.location.second);
138                                         }
139                                         printf (" pdop %3.1f hdop %3.1f vdop %3.1f mode %d",
140                                                 telem.location.pdop / 5.0,
141                                                 telem.location.hdop / 5.0,
142                                                 telem.location.vdop / 5.0,
143                                                 telem.location.mode);
144                                         if ((telem.location.flags & (1 << 7)) != 0)
145                                                 printf (" ground_speed %6.2f climb_rate %6.2f course %d",
146                                                         telem.location.ground_speed / 100.0,
147                                                         telem.location.climb_rate / 100.0,
148                                                         telem.location.course * 2);
149                                         printf ("\n");
150                                         break;
151                                 case AO_TELEMETRY_SATELLITE:
152                                         printf ("sats %d", telem.satellite.channels);
153                                         for (c = 0; c < 12 && c < telem.satellite.channels; c++) {
154                                                 printf (" sat %d svid %d c_n_1 %d",
155                                                         c,
156                                                         telem.satellite.sats[c].svid,
157                                                         telem.satellite.sats[c].c_n_1);
158                                         }
159                                         printf ("\n");
160                                         break;
161                                 case AO_TELEMETRY_MEGA_SENSOR:
162                                         printf ("orient %3d 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",
163                                                 telem.mega_sensor.orient,
164                                                 telem.mega_sensor.accel,
165                                                 telem.mega_sensor.pres,
166                                                 telem.mega_sensor.temp,
167                                                 telem.mega_sensor.accel_x,
168                                                 telem.mega_sensor.accel_y,
169                                                 telem.mega_sensor.accel_z,
170                                                 telem.mega_sensor.gyro_x,
171                                                 telem.mega_sensor.gyro_y,
172                                                 telem.mega_sensor.gyro_z,
173                                                 telem.mega_sensor.mag_x,
174                                                 telem.mega_sensor.mag_y,
175                                                 telem.mega_sensor.mag_z);
176                                         break;
177                                 case AO_TELEMETRY_COMPANION:
178                                         printf("board_id %3d update_period %3d channels %2d",
179                                                telem.companion.board_id,
180                                                telem.companion.update_period,
181                                                telem.companion.channels);
182                                         for (c = 0; c < telem.companion.channels; c++)
183                                                 printf(" %6d", telem.companion.companion_data[c]);
184                                         printf("\n");
185                                         break;
186                                 case AO_TELEMETRY_MEGA_DATA:
187                                         printf ("state %1d v_batt %5d v_pyro %5d ",
188                                                 telem.mega_data.state,
189                                                 telem.mega_data.v_batt,
190                                                 telem.mega_data.v_pyro);
191                                         for (c = 0; c < 6; c++)
192                                                 printf ("s%1d %5d ", c,
193                                                         telem.mega_data.sense[c] |
194                                                         (telem.mega_data.sense[c] << 8));
195
196                                         printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d ",
197                                                 telem.mega_data.ground_pres,
198                                                 telem.mega_data.ground_accel,
199                                                 telem.mega_data.accel_plus_g,
200                                                 telem.mega_data.accel_minus_g);
201
202                                         printf ("accel %6.2f speed %6.2f height %5d\n",
203                                                 telem.mega_data.acceleration / 16.0,
204                                                 telem.mega_data.speed / 16.0,
205                                                 telem.mega_data.height);
206
207                                         break;
208                                 case AO_TELEMETRY_METRUM_SENSOR:
209                                         printf ("state %1d accel %5d pres %9d temp %6.2f acceleration %6.2f speed %6.2f height %5d v_batt %5d sense_a %5d sense_m %5d\n",
210                                                 telem.metrum_sensor.state,
211                                                 telem.metrum_sensor.accel,
212                                                 telem.metrum_sensor.pres,
213                                                 telem.metrum_sensor.temp / 100.0,
214                                                 telem.metrum_sensor.acceleration / 16.0,
215                                                 telem.metrum_sensor.speed / 16.0,
216                                                 telem.metrum_sensor.height,
217                                                 telem.metrum_sensor.v_batt,
218                                                 telem.metrum_sensor.sense_a,
219                                                 telem.metrum_sensor.sense_m);
220                                         break;
221                                 case AO_TELEMETRY_METRUM_DATA:
222                                         printf ("ground_pres %9d ground_accel %5d accel_plus %5d accel_minus %5d\n",
223                                                 telem.metrum_data.ground_pres,
224                                                 telem.metrum_data.ground_accel,
225                                                 telem.metrum_data.accel_plus_g,
226                                                 telem.metrum_data.accel_minus_g);
227                                         break;
228                                 case AO_TELEMETRY_MINI:
229                                         printf ("state %1d v_batt %5d sense_a %5d sense_m %5d pres %9d temp %6.2f acceleration %6.2f speed %6.2f height %5d ground_pres %9d\n",
230                                                 telem.mini.state,
231                                                 telem.mini.v_batt,
232                                                 telem.mini.sense_a,
233                                                 telem.mini.sense_m,
234                                                 telem.mini.pres,
235                                                 telem.mini.temp / 100.0,
236                                                 telem.mini.acceleration / 16.0,
237                                                 telem.mini.speed / 16.0,
238                                                 telem.mini.height,
239                                                 telem.mini.ground_pres);
240                                         break;
241                                 default:
242                                         printf("\n");
243                                 }
244                         }
245                 }
246                 fclose (file);
247
248         }
249         return ret;
250 }