6418521e39c8a47117a88b920b6f6fca493e469e
[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 int plot_colors[3][3] = {
43         { 0, 0x90, 0 }, /* height */
44         { 0xa0, 0, 0 }, /* speed */
45         { 0, 0, 0xc0 }, /* accel */
46 };
47
48 #define PLOT_HEIGHT     0
49 #define PLOT_SPEED      1
50 #define PLOT_ACCEL      2
51
52 static void
53 plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
54                 double min_time, double max_time, int plot_type)
55 {
56         double  *times;
57         double  ymin, ymax;
58         int     ymin_i, ymax_i;
59         int     i;
60         int     start, stop;
61
62         if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
63                 return;
64
65         times = calloc(stop - start + 1, sizeof (double));
66         for (i = start; i <= stop; i++)
67                 times[i-start] = i * d->step / 100.0;
68
69         ymin_i = cc_perioddata_min(d, min_time, max_time);
70         ymax_i = cc_perioddata_max(d, min_time, max_time);
71         ymin = d->data[ymin_i];
72         ymax = d->data[ymax_i];
73         plscol0(1, 0, 0, 0);
74         plscol0(2, plot_colors[plot_type][0],  plot_colors[plot_type][1],  plot_colors[plot_type][2]);
75         plcol0(1);
76         plenv(times[0], times[stop-start],
77               ymin, ymax, 0, 2);
78         pllab("Time", axis_label, plot_label);
79         plcol0(2);
80         plline(stop - start + 1, times, d->data + start);
81         free(times);
82 }
83
84 static void
85 plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
86               double min_time, double max_time, int plot_type)
87 {
88         double  *times;
89         double  *values;
90         double  ymin, ymax;
91         int     ymin_i, ymax_i;
92         int     i;
93         int     start = -1, stop = -1;
94         double  start_time = 0, stop_time = 0;
95         int     num;
96
97         for (i = 0; i < d->num; i++) {
98                 if (start < 0 && d->data[i].time >= min_time) {
99                         start_time = d->data[i].time;
100                         start = i;
101                 }
102                 if (d->data[i].time <= max_time) {
103                         stop_time = d->data[i].time;
104                         stop = i;
105                 }
106         }
107
108         times = calloc(stop - start + 1, sizeof (double));
109         values = calloc(stop - start + 1, sizeof (double));
110
111         ymin_i = cc_timedata_min(d, min_time, max_time);
112         ymax_i = cc_timedata_max(d, min_time, max_time);
113         ymin = d->data[ymin_i].value;
114         ymax = d->data[ymax_i].value;
115         for (i = start; i <= stop; i++) {
116                 times[i-start] = (d->data[i].time - start_time)/100.0;
117                 values[i-start] = d->data[i].value;
118         }
119         plscol0(1, 0, 0, 0);
120         plscol0(2, plot_colors[plot_type][0],  plot_colors[plot_type][1],  plot_colors[plot_type][2]);
121         plcol0(1);
122         plenv(times[0], times[stop-start], ymin, ymax, 0, 2);
123         pllab("Time", axis_label, plot_label);
124         plcol0(2);
125         plline(stop - start + 1, times, values);
126         free(times);
127         free(values);
128 }
129
130 static struct cc_perioddata *
131 merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
132 {
133         int                     i;
134         struct cc_perioddata    *pd;
135         int                     num;
136         double                  start_time, stop_time;
137         double                  t;
138
139         pd = calloc(1, sizeof (struct cc_perioddata));
140         start_time = first->start;
141         stop_time = last->start + last->step * last->num;
142         num = (stop_time - start_time) / first->step;
143         pd->num = num;
144         pd->data = calloc(num, sizeof (double));
145         pd->start = first->start;
146         pd->step = first->step;
147         for (i = 0; i < num; i++) {
148                 t = pd->start + i * pd->step;
149                 if (t <= split_time) {
150                         pd->data[i] = first->data[i];
151                 } else {
152                         int     j;
153
154                         j = (t - last->start) / last->step;
155                         if (j < 0 || j >= last->num)
156                                 pd->data[i] = 0;
157                         else
158                                 pd->data[i] = last->data[j];
159                 }
160         }
161         return pd;
162 }
163
164 static void
165 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file, FILE *raw_file, char *plot_name, FILE *gps_file)
166 {
167         double  height;
168         double  accel;
169         double  speed;
170         double  avg_speed;
171         double  boost_start, boost_stop;
172         double  min_pres;
173         int     i;
174         int     pres_i, accel_i, speed_i;
175         int     boost_start_set = 0;
176         int     boost_stop_set = 0;
177         enum ao_flight_state    state;
178         double  state_start, state_stop;
179         struct cc_flightcooked *cooked;
180         double  apogee;
181
182         fprintf(summary_file, "Flight:  %9d\nSerial:  %9d\n",
183                 f->flight, f->serial);
184         boost_start = f->accel.data[0].time;
185         boost_stop = f->accel.data[f->accel.num-1].time;
186         for (i = 0; i < f->state.num; i++) {
187                 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
188                         boost_start = f->state.data[i].time;
189                         boost_start_set = 1;
190                 }
191                 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
192                         boost_stop = f->state.data[i].time;
193                         boost_stop_set = 1;
194                 }
195         }
196
197         pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
198                                  f->pres.data[f->pres.num-1].time);
199         if (pres_i >= 0)
200         {
201                 min_pres = f->pres.data[pres_i].value;
202                 height = cc_barometer_to_altitude(min_pres) -
203                         cc_barometer_to_altitude(f->ground_pres);
204                 fprintf(summary_file, "Max height: %9.2fm    %9.2fft   %9.2fs\n",
205                         height, height * 100 / 2.54 / 12,
206                         (f->pres.data[pres_i].time - boost_start) / 100.0);
207                 apogee = f->pres.data[pres_i].time;
208         }
209
210         cooked = cc_flight_cook(f);
211         if (cooked) {
212                 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
213                 if (speed_i >= 0) {
214                         speed = cooked->accel_speed.data[speed_i];
215                         fprintf(summary_file, "Max speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
216                                speed, speed * 100 / 2.4 / 12.0,
217                                (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
218                 }
219         }
220         accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
221         if (accel_i >= 0)
222         {
223                 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
224                                                          f->ground_accel);
225                 fprintf(summary_file, "Max accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
226                         accel, accel /  9.80665,
227                         (f->accel.data[accel_i].time - boost_start) / 100.0);
228         }
229
230         for (i = 0; i < f->state.num; i++) {
231                 state = f->state.data[i].value;
232                 state_start = f->state.data[i].time;
233                 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
234                         i++;
235                 if (i < f->state.num - 1)
236                         state_stop = f->state.data[i + 1].time;
237                 else
238                         state_stop = f->accel.data[f->accel.num-1].time;
239                 fprintf(summary_file, "State: %s\n", state_names[state]);
240                 fprintf(summary_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
241                 fprintf(summary_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
242                 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
243                 if (accel_i >= 0)
244                 {
245                         accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
246                                                                  f->ground_accel);
247                         fprintf(summary_file, "\tMax accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
248                                accel, accel / 9.80665,
249                                (f->accel.data[accel_i].time - boost_start) / 100.0);
250                 }
251
252                 if (cooked) {
253                         if (state < ao_flight_drogue) {
254                                 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
255                                 if (speed_i >= 0)
256                                         speed = cooked->accel_speed.data[speed_i];
257                                 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
258                         } else {
259                                 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
260                                 if (speed_i >= 0)
261                                         speed = cooked->pres_speed.data[speed_i];
262                                 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
263                         }
264                         if (speed_i >= 0)
265                         {
266                                 fprintf(summary_file, "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
267                                        speed, speed * 100 / 2.4 / 12.0,
268                                        (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
269                                 fprintf(summary_file, "\tAvg speed:  %9.2fm/s  %9.2fft/s\n",
270                                         avg_speed, avg_speed * 100 / 2.4 / 12.0);
271                         }
272                 }
273                 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
274                 if (pres_i >= 0)
275                 {
276                         min_pres = f->pres.data[pres_i].value;
277                         height = cc_barometer_to_altitude(min_pres) -
278                                 cc_barometer_to_altitude(f->ground_pres);
279                         fprintf(summary_file, "\tMax height: %9.2fm    %9.2fft   %9.2fs\n",
280                                 height, height * 100 / 2.54 / 12,
281                                 (f->pres.data[pres_i].time - boost_start) / 100.0);
282                 }
283         }
284         if (cooked && detail_file) {
285                 double  max_height = 0;
286                 int     i;
287                 double  *times;
288
289                 fprintf(detail_file, "%9s %9s %9s %9s\n",
290                        "time", "height", "speed", "accel");
291                 for (i = 0; i < cooked->pres_pos.num; i++) {
292                         double  time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
293                         double  accel = cooked->accel_accel.data[i];
294                         double  pos = cooked->pres_pos.data[i];
295                         double  speed;
296                         if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
297                                 speed = cooked->accel_speed.data[i];
298                         else
299                                 speed = cooked->pres_speed.data[i];
300                         fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
301                                time, pos, speed, accel);
302                 }
303         }
304         if (raw_file) {
305                 fprintf(raw_file, "%9s %9s %9s\n",
306                        "time", "height", "accel");
307                 for (i = 0; i < cooked->pres.num; i++) {
308                         double time = cooked->pres.data[i].time;
309                         double pres = cooked->pres.data[i].value;
310                         double accel = cooked->accel.data[i].value;
311                         fprintf(raw_file, "%9.2f %9.2f %9.2f %9.2f\n",
312                                 time, pres, accel);
313                 }
314         }
315         if (gps_file) {
316                 fprintf(gps_file, "%9s %12s %12s %12s\n",
317                         "time", "lat", "lon", "alt");
318                 for (i = 0; i < f->gps.num; i++) {
319                         fprintf(gps_file, "%12.7f %12.7f %12.7f %12.7f\n",
320                                 (f->gps.data[i].time - boost_start) / 100.0,
321                                 f->gps.data[i].lat,
322                                 f->gps.data[i].lon,
323                                 f->gps.data[i].alt);
324                 }
325         }
326         if (cooked && plot_name) {
327                 struct cc_perioddata    *speed;
328                 plsdev("svgcairo");
329                 plsfnam(plot_name);
330 #define PLOT_DPI        96
331                 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
332                 plscolbg(0xff, 0xff, 0xff);
333                 plscol0(1,0,0,0);
334                 plstar(2, 3);
335                 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
336
337                 plot_perioddata(&cooked->pres_pos, "meters", "Height",
338                                 -1e10, 1e10, PLOT_HEIGHT);
339                 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
340                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
341                 plot_perioddata(speed, "meters/second", "Speed",
342                                 -1e10, 1e10, PLOT_SPEED);
343                 plot_perioddata(speed, "meters/second", "Speed to Apogee",
344                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
345                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
346                                 -1e10, 1e10, PLOT_ACCEL);
347 /*              plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
348                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
349                 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
350                                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
351                 free(speed->data);
352                 free(speed);
353                 plend();
354         }
355         if (cooked)
356                 cc_flightcooked_free(cooked);
357 }
358
359 static const struct option options[] = {
360         { .name = "summary", .has_arg = 1, .val = 's' },
361         { .name = "detail", .has_arg = 1, .val = 'd' },
362         { .name = "plot", .has_arg = 1, .val = 'p' },
363         { .name = "raw", .has_arg = 1, .val = 'r' },
364         { .name = "gps", .has_arg = 1, .val = 'g' },
365         { 0, 0, 0, 0},
366 };
367
368 static void usage(char *program)
369 {
370         fprintf(stderr, "usage: %s\n"
371                 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
372                 "\t[--detail=<detail-file] [-d <detail-file>]\n"
373                 "\t[--raw=<raw-file> -r <raw-file]\n"
374                 "\t[--plot=<plot-file> -p <plot-file>]\n"
375                 "\t[--gps=<gps-file> -g <gps-file>]\n"
376                 "\t{flight-log} ...\n", program);
377         exit(1);
378 }
379
380 int
381 main (int argc, char **argv)
382 {
383         FILE                    *file;
384         FILE                    *summary_file = NULL;
385         FILE                    *detail_file = NULL;
386         FILE                    *raw_file = NULL;
387         FILE                    *gps_file = NULL;
388         int                     i;
389         int                     ret = 0;
390         struct cc_flightraw     *raw;
391         int                     c;
392         int                     serial;
393         char                    *s;
394         char                    *summary_name = NULL;
395         char                    *detail_name = NULL;
396         char                    *raw_name = NULL;
397         char                    *plot_name = NULL;
398         char                    *gps_name = NULL;
399
400         while ((c = getopt_long(argc, argv, "s:d:p:r:g:", options, NULL)) != -1) {
401                 switch (c) {
402                 case 's':
403                         summary_name = optarg;
404                         break;
405                 case 'd':
406                         detail_name = optarg;
407                         break;
408                 case 'p':
409                         plot_name = optarg;
410                         break;
411                 case 'r':
412                         raw_name = optarg;
413                         break;
414                 case 'g':
415                         gps_name = optarg;
416                         break;
417                 default:
418                         usage(argv[0]);
419                         break;
420                 }
421         }
422         summary_file = stdout;
423         if (summary_name) {
424                 summary_file = fopen(summary_name, "w");
425                 if (!summary_file) {
426                         perror (summary_name);
427                         exit(1);
428                 }
429         }
430         if (detail_name) {
431                 if (summary_name && !strcmp (summary_name, detail_name))
432                         detail_file = summary_file;
433                 else {
434                         detail_file = fopen(detail_name, "w");
435                         if (!detail_file) {
436                                 perror(detail_name);
437                                 exit(1);
438                         }
439                 }
440         }
441         if (raw_name) {
442                 raw_file = fopen (raw_name, "w");
443                 if (!raw_file) {
444                         perror(raw_name);
445                         exit(1);
446                 }
447         }
448         if (gps_name) {
449                 gps_file = fopen(gps_name, "w");
450                 if (!gps_file) {
451                         perror(gps_name);
452                         exit(1);
453                 }
454         }
455         for (i = optind; i < argc; i++) {
456                 file = fopen(argv[i], "r");
457                 if (!file) {
458                         perror(argv[i]);
459                         ret++;
460                         continue;
461                 }
462                 s = strstr(argv[i], "-serial-");
463                 if (s)
464                         serial = atoi(s + 8);
465                 else
466                         serial = 0;
467                 raw = cc_log_read(file);
468                 if (!raw) {
469                         perror(argv[i]);
470                         ret++;
471                         continue;
472                 }
473                 if (!raw->serial)
474                         raw->serial = serial;
475                 analyse_flight(raw, summary_file, detail_file, raw_file, plot_name, gps_file);
476                 cc_flightraw_free(raw);
477         }
478         return ret;
479 }