9371f35174ed2c8966ee1dff67439d421246d6a8
[fw/altos] / ao-tools / ao-postflight / ao-postflight.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; 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-usb.h"
26 #include "cc.h"
27
28 #define NUM_BLOCK       512
29
30 static const struct option options[] = {
31         { 0, 0, 0, 0},
32 };
33
34 static void usage(char *program)
35 {
36         fprintf(stderr, "usage: %s {flight-log} ...\n", program);
37         exit(1);
38 }
39
40 static const char *state_names[] = {
41         "startup",
42         "idle",
43         "pad",
44         "boost",
45         "fast",
46         "coast",
47         "drogue",
48         "main",
49         "landed",
50         "invalid"
51 };
52
53 void
54 analyse_flight(struct cc_flightraw *f)
55 {
56         double  height;
57         double  accel;
58         double  boost_start, boost_stop;
59         double  min_pres;
60         int     i;
61         int     pres_i, accel_i;
62         int     boost_start_set = 0;
63         int     boost_stop_set = 0;
64         enum ao_flight_state    state;
65         double  state_start, state_stop;
66
67         printf ("Flight:  %9d\nSerial:  %9d\n",
68                 f->flight, f->serial);
69         boost_start = f->accel.data[0].time;
70         boost_stop = f->accel.data[f->accel.num-1].time;
71         for (i = 0; i < f->state.num; i++) {
72                 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
73                         boost_start = f->state.data[i].time;
74                         boost_start_set = 1;
75                 }
76                 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
77                         boost_stop = f->state.data[i].time;
78                         boost_stop_set = 1;
79                 }
80         }
81
82         pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
83                                  f->pres.data[f->pres.num-1].time);
84         if (pres_i)
85         {
86                 min_pres = f->pres.data[pres_i].value;
87                 height = cc_barometer_to_altitude(min_pres) -
88                         cc_barometer_to_altitude(f->ground_pres);
89                 printf ("Max height: %9.2fm    %9.2fft %9.2fs\n",
90                         height, height * 100 / 2.54 / 12,
91                         (f->pres.data[pres_i].time - boost_start) / 100.0);
92         }
93
94         accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
95         if (accel_i)
96         {
97                 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
98                                                          f->ground_accel);
99                 printf ("Max accel:  %9.2fm/s² %9.2fg  %9.2fs\n",
100                         accel, accel /  9.80665,
101                         (f->accel.data[accel_i].time - boost_start) / 100.0);
102         }
103         for (i = 0; i < f->state.num; i++) {
104                 state = f->state.data[i].value;
105                 state_start = f->state.data[i].time;
106                 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
107                         i++;
108                 if (i < f->state.num - 1)
109                         state_stop = f->state.data[i + 1].time;
110                 else
111                         state_stop = f->accel.data[f->accel.num-1].time;
112                 printf("State: %s\n", state_names[state]);
113                 printf("\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
114                 printf("\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
115                 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
116                 if (accel_i >= 0)
117                 {
118                         accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
119                                                                  f->ground_accel);
120                         printf("\tMax accel:  %9.2fm/s² %9.2fg  %9.2fs\n",
121                                accel, accel / 9.80665,
122                                (f->accel.data[accel_i].time - boost_start) / 100.0);
123                 }
124
125                 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
126                 if (pres_i >= 0)
127                 {
128                         min_pres = f->pres.data[pres_i].value;
129                         height = cc_barometer_to_altitude(min_pres) -
130                                 cc_barometer_to_altitude(f->ground_pres);
131                         printf ("\tMax height: %9.2fm    %9.2fft %9.2fs\n",
132                                 height, height * 100 / 2.54 / 12,
133                                 (f->pres.data[pres_i].time - boost_start) / 100.0);
134                 }
135         }
136 }
137
138 int
139 main (int argc, char **argv)
140 {
141         FILE                    *file;
142         int                     i;
143         int                     ret = 0;
144         struct cc_flightraw     *raw;
145         int                     c;
146         int                     serial;
147         char                    *s;
148
149         while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) {
150                 switch (c) {
151                 default:
152                         usage(argv[0]);
153                         break;
154                 }
155         }
156         for (i = optind; i < argc; i++) {
157                 file = fopen(argv[i], "r");
158                 if (!file) {
159                         perror(argv[i]);
160                         ret++;
161                         continue;
162                 }
163                 s = strstr(argv[i], "-serial-");
164                 if (s)
165                         serial = atoi(s + 8);
166                 else
167                         serial = 0;
168                 raw = cc_log_read(file);
169                 if (!raw) {
170                         perror(argv[i]);
171                         ret++;
172                         continue;
173                 }
174                 if (!raw->serial)
175                         raw->serial = serial;
176                 analyse_flight(raw);
177                 cc_flightraw_free(raw);
178         }
179         return ret;
180 }