altos/test: Adjust CRC error rate after FEC fix
[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 [--crc] {flight.telem} ...\n",program);
35         exit(1);
36 }
37
38 #define bool(b) ((b) ? "true" : "false")
39
40 static int ignore_crc;
41
42 static void
43 process(FILE *file)
44 {
45         char    line[1024];
46         int c;
47
48         while (fgets(line, sizeof (line), file)) {
49                 union ao_telemetry_all telem;
50                 char call[AO_MAX_CALLSIGN+1];
51                 char version[AO_MAX_VERSION+1];
52
53                 if (cc_telemetry_parse(line, &telem)) {
54                         int rssi = (int8_t) telem.generic.rssi / 2 - 74;
55
56                         printf ("serial %5d rssi %d status %02x tick %5d type %3d ",
57                                 telem.generic.serial, rssi, telem.generic.status,
58                                 telem.generic.tick, telem.generic.type);
59                         if (!ignore_crc && (telem.generic.status & (1 << 7)) == 0) {
60                                 printf ("CRC error\n");
61                                 continue;
62                         }
63                         switch (telem.generic.type) {
64                         case AO_TELEMETRY_SENSOR_TELEMETRUM:
65                         case AO_TELEMETRY_SENSOR_TELEMINI:
66                         case AO_TELEMETRY_SENSOR_TELENANO:
67                                 printf ("state %1d accel %5d pres %5d ",
68                                         telem.sensor.state, telem.sensor.accel, telem.sensor.pres);
69                                 printf ("accel %6.2f speed %6.2f height %5d ",
70                                         telem.sensor.acceleration / 16.0,
71                                         telem.sensor.speed / 16.0,
72                                         telem.sensor.height);
73                                 printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d\n",
74                                         telem.sensor.ground_pres,
75                                         telem.sensor.ground_accel,
76                                         telem.sensor.accel_plus_g,
77                                         telem.sensor.accel_minus_g);
78                                 break;
79                         case AO_TELEMETRY_CONFIGURATION:
80                                 memcpy(call, telem.configuration.callsign, AO_MAX_CALLSIGN);
81                                 memcpy(version, telem.configuration.version, AO_MAX_VERSION);
82                                 call[AO_MAX_CALLSIGN] = '\0';
83                                 version[AO_MAX_CALLSIGN] = '\0';
84                                 printf ("device %3d flight %5d config %3d.%03d delay %2d main %4d log_max %5d",
85                                         telem.configuration.device,
86                                         telem.configuration.flight,
87                                         telem.configuration.config_major,
88                                         telem.configuration.config_minor,
89                                         telem.configuration.apogee_delay,
90                                         telem.configuration.main_deploy,
91                                         telem.configuration.flight_log_max);
92                                 printf (" call %8s version %8s\n", call, version);
93                                 break;
94                         case AO_TELEMETRY_LOCATION:
95                                 printf ("sats %d flags %s%s%s%s",
96                                         telem.location.flags & 0xf,
97                                         (telem.location.flags & (1 << 4)) ? "valid" : "invalid",
98                                         (telem.location.flags & (1 << 5)) ? ",running" : "",
99                                         (telem.location.flags & (1 << 6)) ? ",date" : "",
100                                         (telem.location.flags & (1 << 7)) ? ",course" : "");
101                                 printf (" alt %5d lat %12.7f lon %12.7f",
102                                         AO_TELEMETRY_LOCATION_ALTITUDE(&telem.location),
103                                         telem.location.latitude / 1e7,
104                                         telem.location.longitude / 1e7);
105                                 if ((telem.location.flags & (1 << 6)) != 0) {
106                                         printf (" year %2d month %2d day %2d",
107                                                 telem.location.year,
108                                                 telem.location.month,
109                                                 telem.location.day);
110                                         printf (" hour %2d minute %2d second %2d",
111                                                 telem.location.hour,
112                                                 telem.location.minute,
113                                                 telem.location.second);
114                                 }
115                                 printf (" pdop %3.1f hdop %3.1f vdop %3.1f mode %d",
116                                         telem.location.pdop / 5.0,
117                                         telem.location.hdop / 5.0,
118                                         telem.location.vdop / 5.0,
119                                         telem.location.mode);
120                                 if ((telem.location.flags & (1 << 7)) != 0)
121                                         printf (" ground_speed %6.2f climb_rate %6.2f course %d",
122                                                 telem.location.ground_speed / 100.0,
123                                                 telem.location.climb_rate / 100.0,
124                                                 telem.location.course * 2);
125                                 printf ("\n");
126                                 break;
127                         case AO_TELEMETRY_SATELLITE:
128                                 printf ("sats %d", telem.satellite.channels);
129                                 for (c = 0; c < 12 && c < telem.satellite.channels; c++) {
130                                         printf (" sat %d svid %d c_n_1 %d",
131                                                 c,
132                                                 telem.satellite.sats[c].svid,
133                                                 telem.satellite.sats[c].c_n_1);
134                                 }
135                                 printf ("\n");
136                                 break;
137                         case AO_TELEMETRY_MEGA_SENSOR_MPU:
138                         case AO_TELEMETRY_MEGA_SENSOR_BMX160:
139                                 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",
140                                         telem.mega_sensor.orient,
141                                         telem.mega_sensor.accel,
142                                         telem.mega_sensor.pres,
143                                         telem.mega_sensor.temp,
144                                         telem.mega_sensor.accel_x,
145                                         telem.mega_sensor.accel_y,
146                                         telem.mega_sensor.accel_z,
147                                         telem.mega_sensor.gyro_x,
148                                         telem.mega_sensor.gyro_y,
149                                         telem.mega_sensor.gyro_z,
150                                         telem.mega_sensor.mag_x,
151                                         telem.mega_sensor.mag_y,
152                                         telem.mega_sensor.mag_z);
153                                 break;
154                         case AO_TELEMETRY_COMPANION:
155                                 printf("board_id %3d update_period %3d channels %2d",
156                                        telem.companion.board_id,
157                                        telem.companion.update_period,
158                                        telem.companion.channels);
159                                 for (c = 0; c < telem.companion.channels; c++)
160                                         printf(" %6d", telem.companion.companion_data[c]);
161                                 printf("\n");
162                                 break;
163                         case AO_TELEMETRY_MEGA_DATA:
164                                 printf ("state %1d v_batt %5d v_pyro %5d ",
165                                         telem.mega_data.state,
166                                         telem.mega_data.v_batt,
167                                         telem.mega_data.v_pyro);
168                                 for (c = 0; c < 6; c++)
169                                         printf ("s%1d %5d ", c,
170                                                 telem.mega_data.sense[c] |
171                                                 (telem.mega_data.sense[c] << 8));
172
173                                 printf ("ground_pres %5d ground_accel %5d accel_plus %5d accel_minus %5d ",
174                                         telem.mega_data.ground_pres,
175                                         telem.mega_data.ground_accel,
176                                         telem.mega_data.accel_plus_g,
177                                         telem.mega_data.accel_minus_g);
178
179                                 printf ("accel %6.2f speed %6.2f height %5d\n",
180                                         telem.mega_data.acceleration / 16.0,
181                                         telem.mega_data.speed / 16.0,
182                                         telem.mega_data.height);
183
184                                 break;
185                         case AO_TELEMETRY_METRUM_SENSOR:
186                                 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",
187                                         telem.metrum_sensor.state,
188                                         telem.metrum_sensor.accel,
189                                         telem.metrum_sensor.pres,
190                                         telem.metrum_sensor.temp / 100.0,
191                                         telem.metrum_sensor.acceleration / 16.0,
192                                         telem.metrum_sensor.speed / 16.0,
193                                         telem.metrum_sensor.height,
194                                         telem.metrum_sensor.v_batt,
195                                         telem.metrum_sensor.sense_a,
196                                         telem.metrum_sensor.sense_m);
197                                 break;
198                         case AO_TELEMETRY_METRUM_DATA:
199                                 printf ("ground_pres %9d ground_accel %5d accel_plus %5d accel_minus %5d\n",
200                                         telem.metrum_data.ground_pres,
201                                         telem.metrum_data.ground_accel,
202                                         telem.metrum_data.accel_plus_g,
203                                         telem.metrum_data.accel_minus_g);
204                                 break;
205                         case AO_TELEMETRY_MINI:
206                                 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",
207                                         telem.mini.state,
208                                         telem.mini.v_batt,
209                                         telem.mini.sense_a,
210                                         telem.mini.sense_m,
211                                         telem.mini.pres,
212                                         telem.mini.temp / 100.0,
213                                         telem.mini.acceleration / 16.0,
214                                         telem.mini.speed / 16.0,
215                                         telem.mini.height,
216                                         telem.mini.ground_pres);
217                                 break;
218                         default:
219                                 printf("\n");
220                         }
221                 }
222         }
223 }
224
225 int
226 main (int argc, char **argv)
227 {
228         int c, i, ret = 0;
229         FILE *file;
230         while ((c = getopt_long(argc, argv, "c", options, NULL)) != -1) {
231                 switch (c) {
232                 case 'c':
233                         ignore_crc = 1;
234                         break;
235                 default:
236                         usage(argv[0]);
237                         break;
238                 }
239         }
240         if (optind < argc) {
241                 for (i = optind; i < argc; i++) {
242                         file = fopen(argv[i], "r");
243                         if (!file) {
244                                 perror(argv[i]);
245                                 ret++;
246                                 continue;
247                         }
248                         process(file);
249                         fclose (file);
250
251                 }
252         } else
253                 process(stdin);
254         return ret;
255 }