Update usage and man page for ao-postflight
[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 #include <plplot/plplot.h>
28
29 static const char *state_names[] = {
30         "startup",
31         "idle",
32         "pad",
33         "boost",
34         "fast",
35         "coast",
36         "drogue",
37         "main",
38         "landed",
39         "invalid"
40 };
41
42 static void
43 plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
44                 double min_time, double max_time)
45 {
46         double  *times;
47         double  ymin, ymax;
48         int     ymin_i, ymax_i;
49         int     i;
50         int     start, stop;
51
52         if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
53                 return;
54
55         times = calloc(stop - start + 1, sizeof (double));
56         for (i = start; i <= stop; i++)
57                 times[i-start] = i * d->step / 100.0;
58
59         ymin_i = cc_perioddata_min(d, min_time, max_time);
60         ymax_i = cc_perioddata_max(d, min_time, max_time);
61         ymin = d->data[ymin_i];
62         ymax = d->data[ymax_i];
63         plenv(times[0], times[stop-start],
64               ymin, ymax, 0, 2);
65         plcol0(1);
66         pllab("Time", axis_label, plot_label);
67         plline(stop - start + 1, times, d->data + start);
68 }
69
70 static struct cc_perioddata *
71 merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
72 {
73         int                     i;
74         struct cc_perioddata    *pd;
75         int                     num;
76         double                  start_time, stop_time;
77         double                  t;
78
79         pd = calloc(1, sizeof (struct cc_perioddata));
80         start_time = first->start;
81         stop_time = last->start + last->step * last->num;
82         num = (stop_time - start_time) / first->step;
83         pd->num = num;
84         pd->data = calloc(num, sizeof (double));
85         pd->start = first->start;
86         pd->step = first->step;
87         for (i = 0; i < num; i++) {
88                 t = pd->start + i * pd->step;
89                 if (t <= split_time) {
90                         pd->data[i] = first->data[i];
91                 } else {
92                         int     j;
93
94                         j = (t - last->start) / last->step;
95                         if (j < 0 || j >= last->num)
96                                 pd->data[i] = 0;
97                         else
98                                 pd->data[i] = last->data[j];
99                 }
100         }
101         return pd;
102 }
103
104 static void
105 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file, char *plot_name)
106 {
107         double  height;
108         double  accel;
109         double  speed;
110         double  avg_speed;
111         double  boost_start, boost_stop;
112         double  min_pres;
113         int     i;
114         int     pres_i, accel_i, speed_i;
115         int     boost_start_set = 0;
116         int     boost_stop_set = 0;
117         enum ao_flight_state    state;
118         double  state_start, state_stop;
119         struct cc_flightcooked *cooked;
120         double  apogee;
121
122         fprintf(summary_file, "Flight:  %9d\nSerial:  %9d\n",
123                 f->flight, f->serial);
124         boost_start = f->accel.data[0].time;
125         boost_stop = f->accel.data[f->accel.num-1].time;
126         for (i = 0; i < f->state.num; i++) {
127                 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
128                         boost_start = f->state.data[i].time;
129                         boost_start_set = 1;
130                 }
131                 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
132                         boost_stop = f->state.data[i].time;
133                         boost_stop_set = 1;
134                 }
135         }
136
137         pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
138                                  f->pres.data[f->pres.num-1].time);
139         if (pres_i >= 0)
140         {
141                 min_pres = f->pres.data[pres_i].value;
142                 height = cc_barometer_to_altitude(min_pres) -
143                         cc_barometer_to_altitude(f->ground_pres);
144                 fprintf(summary_file, "Max height: %9.2fm    %9.2fft   %9.2fs\n",
145                         height, height * 100 / 2.54 / 12,
146                         (f->pres.data[pres_i].time - boost_start) / 100.0);
147                 apogee = f->pres.data[pres_i].time;
148         }
149
150         cooked = cc_flight_cook(f);
151         if (cooked) {
152                 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
153                 if (speed_i >= 0) {
154                         speed = cooked->accel_speed.data[speed_i];
155                         fprintf(summary_file, "Max speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
156                                speed, speed * 100 / 2.4 / 12.0,
157                                (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
158                 }
159         }
160         accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
161         if (accel_i >= 0)
162         {
163                 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
164                                                          f->ground_accel);
165                 fprintf(summary_file, "Max accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
166                         accel, accel /  9.80665,
167                         (f->accel.data[accel_i].time - boost_start) / 100.0);
168         }
169
170         for (i = 0; i < f->state.num; i++) {
171                 state = f->state.data[i].value;
172                 state_start = f->state.data[i].time;
173                 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
174                         i++;
175                 if (i < f->state.num - 1)
176                         state_stop = f->state.data[i + 1].time;
177                 else
178                         state_stop = f->accel.data[f->accel.num-1].time;
179                 fprintf(summary_file, "State: %s\n", state_names[state]);
180                 fprintf(summary_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
181                 fprintf(summary_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
182                 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
183                 if (accel_i >= 0)
184                 {
185                         accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
186                                                                  f->ground_accel);
187                         fprintf(summary_file, "\tMax accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
188                                accel, accel / 9.80665,
189                                (f->accel.data[accel_i].time - boost_start) / 100.0);
190                 }
191
192                 if (cooked) {
193                         if (state < ao_flight_drogue) {
194                                 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
195                                 if (speed_i >= 0)
196                                         speed = cooked->accel_speed.data[speed_i];
197                                 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
198                         } else {
199                                 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
200                                 if (speed_i >= 0)
201                                         speed = cooked->pres_speed.data[speed_i];
202                                 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
203                         }
204                         if (speed_i >= 0)
205                         {
206                                 fprintf(summary_file, "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
207                                        speed, speed * 100 / 2.4 / 12.0,
208                                        (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
209                                 fprintf(summary_file, "\tAvg speed:  %9.2fm/s  %9.2fft/s\n",
210                                         avg_speed, avg_speed * 100 / 2.4 / 12.0);
211                         }
212                 }
213                 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
214                 if (pres_i >= 0)
215                 {
216                         min_pres = f->pres.data[pres_i].value;
217                         height = cc_barometer_to_altitude(min_pres) -
218                                 cc_barometer_to_altitude(f->ground_pres);
219                         fprintf(summary_file, "\tMax height: %9.2fm    %9.2fft   %9.2fs\n",
220                                 height, height * 100 / 2.54 / 12,
221                                 (f->pres.data[pres_i].time - boost_start) / 100.0);
222                 }
223         }
224         if (cooked && detail_file) {
225                 double  max_height = 0;
226                 int     i;
227                 double  *times;
228
229                 fprintf(detail_file, "%9s %9s %9s %9s\n",
230                        "time", "height", "speed", "accel");
231                 for (i = 0; i < cooked->pres_pos.num; i++) {
232                         double  time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
233                         double  accel = cooked->accel_accel.data[i];
234                         double  pos = cooked->pres_pos.data[i];
235                         double  speed;
236                         if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
237                                 speed = cooked->accel_speed.data[i];
238                         else
239                                 speed = cooked->pres_speed.data[i];
240                         fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
241                                time, pos, speed, accel);
242                 }
243         }
244         if (cooked && plot_name) {
245                 struct cc_perioddata    *speed;
246                 plsdev("svgcairo");
247                 plsfnam(plot_name);
248 #define PLOT_DPI        96
249                 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
250                 plscolbg(0xff, 0xff, 0xff);
251                 plscol0(1,0,0,0);
252                 plstar(2, 3);
253                 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
254
255                 plot_perioddata(&cooked->pres_pos, "meters", "Height", -1e10, 1e10);
256                 plot_perioddata(&cooked->pres_pos, "meters", "Height", boost_start, apogee);
257                 plot_perioddata(speed, "meters/second", "Speed", -1e10, 1e10);
258                 plot_perioddata(speed, "meters/second", "Speed", boost_start, apogee);
259                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration", -1e10, 1e10);
260                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration", boost_start, apogee);
261                 free(speed->data);
262                 free(speed);
263                 plend();
264         }
265         if (cooked)
266                 cc_flightcooked_free(cooked);
267 }
268
269 static const struct option options[] = {
270         { .name = "summary", .has_arg = 1, .val = 's' },
271         { .name = "detail", .has_arg = 1, .val = 'd' },
272         { .name = "plot", .has_arg = 1, .val = 'p' },
273         { 0, 0, 0, 0},
274 };
275
276 static void usage(char *program)
277 {
278         fprintf(stderr, "usage: %s [--summary=<summary-file>] [-s <summary-file>] [--detail=<detail-file] [-d <detail-file>] [--plot=<plot-file> -p <plot-file>] {flight-log} ...\n", program);
279         exit(1);
280 }
281
282 int
283 main (int argc, char **argv)
284 {
285         FILE                    *file;
286         FILE                    *summary_file;
287         FILE                    *detail_file;
288         int                     i;
289         int                     ret = 0;
290         struct cc_flightraw     *raw;
291         int                     c;
292         int                     serial;
293         char                    *s;
294         char                    *summary_name = NULL, *detail_name = NULL;
295         char                    *plot_name = NULL;
296
297         while ((c = getopt_long(argc, argv, "s:d:p:", options, NULL)) != -1) {
298                 switch (c) {
299                 case 's':
300                         summary_name = optarg;
301                         break;
302                 case 'd':
303                         detail_name = optarg;
304                         break;
305                 case 'p':
306                         plot_name = optarg;
307                         break;
308                 default:
309                         usage(argv[0]);
310                         break;
311                 }
312         }
313         summary_file = stdout;
314         detail_file = NULL;
315         if (summary_name) {
316                 summary_file = fopen(summary_name, "w");
317                 if (!summary_file) {
318                         perror (summary_name);
319                         exit(1);
320                 }
321         }
322         if (detail_name) {
323                 if (summary_name && !strcmp (summary_name, detail_name))
324                         detail_file = summary_file;
325                 else {
326                         detail_file = fopen(detail_name, "w");
327                         if (!detail_file) {
328                                 perror(detail_name);
329                                 exit(1);
330                         }
331                 }
332         }
333         for (i = optind; i < argc; i++) {
334                 file = fopen(argv[i], "r");
335                 if (!file) {
336                         perror(argv[i]);
337                         ret++;
338                         continue;
339                 }
340                 s = strstr(argv[i], "-serial-");
341                 if (s)
342                         serial = atoi(s + 8);
343                 else
344                         serial = 0;
345                 raw = cc_log_read(file);
346                 if (!raw) {
347                         perror(argv[i]);
348                         ret++;
349                         continue;
350                 }
351                 if (!raw->serial)
352                         raw->serial = serial;
353                 analyse_flight(raw, summary_file, detail_file, plot_name);
354                 cc_flightraw_free(raw);
355         }
356         return ret;
357 }