Switch order of serial/flight in ao-postflight summary
[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 const char kml_header[] =
165         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
166         "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"
167         "  <Placemark>\n"
168         "    <name>gps</name>\n"
169         "    <Style id=\"khStyle690\">\n"
170         "      <LineStyle id=\"khLineStyle694\">\n"
171         "        <color>ff00ffff</color>\n"
172         "        <width>4</width>\n"
173         "      </LineStyle>\n"
174         "      </Style>\n"
175         "    <MultiGeometry id=\"khMultiGeometry697\">\n"
176         "      <LineString id=\"khLineString698\">\n"
177         "        <tessellate>1</tessellate>\n"
178         "        <altitudeMode>absolute</altitudeMode>\n"
179         "        <coordinates>\n";
180
181 static const char kml_footer[] =
182         "</coordinates>\n"
183         "    </LineString>\n"
184         "  </MultiGeometry>\n"
185         "</Placemark>\n"
186         "</kml>\n";
187
188 static void
189 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file,
190                FILE *raw_file, char *plot_name, FILE *gps_file, FILE *kml_file)
191 {
192         double  height;
193         double  accel;
194         double  speed;
195         double  avg_speed;
196         double  boost_start, boost_stop;
197         double  min_pres;
198         int     i;
199         int     pres_i, accel_i, speed_i;
200         int     boost_start_set = 0;
201         int     boost_stop_set = 0;
202         enum ao_flight_state    state;
203         double  state_start, state_stop;
204         struct cc_flightcooked *cooked;
205         double  apogee;
206
207         fprintf(summary_file,
208                 "Serial:  %9d\n"
209                 "Flight:  %9d\n",
210                 f->serial, f->flight);
211         boost_start = f->accel.data[0].time;
212         boost_stop = f->accel.data[f->accel.num-1].time;
213         for (i = 0; i < f->state.num; i++) {
214                 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
215                         boost_start = f->state.data[i].time;
216                         boost_start_set = 1;
217                 }
218                 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
219                         boost_stop = f->state.data[i].time;
220                         boost_stop_set = 1;
221                 }
222         }
223
224         pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
225                                  f->pres.data[f->pres.num-1].time);
226         if (pres_i >= 0)
227         {
228                 min_pres = f->pres.data[pres_i].value;
229                 height = cc_barometer_to_altitude(min_pres) -
230                         cc_barometer_to_altitude(f->ground_pres);
231                 fprintf(summary_file, "Max height: %9.2fm    %9.2fft   %9.2fs\n",
232                         height, height * 100 / 2.54 / 12,
233                         (f->pres.data[pres_i].time - boost_start) / 100.0);
234                 apogee = f->pres.data[pres_i].time;
235         }
236
237         cooked = cc_flight_cook(f);
238         if (cooked) {
239                 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
240                 if (speed_i >= 0) {
241                         speed = cooked->accel_speed.data[speed_i];
242                         fprintf(summary_file, "Max speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
243                                speed, speed * 100 / 2.4 / 12.0,
244                                (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
245                 }
246         }
247         accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
248         if (accel_i >= 0)
249         {
250                 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
251                                                          f->ground_accel);
252                 fprintf(summary_file, "Max accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
253                         accel, accel /  9.80665,
254                         (f->accel.data[accel_i].time - boost_start) / 100.0);
255         }
256
257         for (i = 0; i < f->state.num; i++) {
258                 state = f->state.data[i].value;
259                 state_start = f->state.data[i].time;
260                 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
261                         i++;
262                 if (i < f->state.num - 1)
263                         state_stop = f->state.data[i + 1].time;
264                 else
265                         state_stop = f->accel.data[f->accel.num-1].time;
266                 fprintf(summary_file, "State: %s\n", state_names[state]);
267                 fprintf(summary_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
268                 fprintf(summary_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
269                 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
270                 if (accel_i >= 0)
271                 {
272                         accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
273                                                                  f->ground_accel);
274                         fprintf(summary_file, "\tMax accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
275                                accel, accel / 9.80665,
276                                (f->accel.data[accel_i].time - boost_start) / 100.0);
277                 }
278
279                 if (cooked) {
280                         if (state < ao_flight_drogue) {
281                                 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
282                                 if (speed_i >= 0)
283                                         speed = cooked->accel_speed.data[speed_i];
284                                 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
285                         } else {
286                                 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
287                                 if (speed_i >= 0)
288                                         speed = cooked->pres_speed.data[speed_i];
289                                 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
290                         }
291                         if (speed_i >= 0)
292                         {
293                                 fprintf(summary_file, "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
294                                        speed, speed * 100 / 2.4 / 12.0,
295                                        (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
296                                 fprintf(summary_file, "\tAvg speed:  %9.2fm/s  %9.2fft/s\n",
297                                         avg_speed, avg_speed * 100 / 2.4 / 12.0);
298                         }
299                 }
300                 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
301                 if (pres_i >= 0)
302                 {
303                         min_pres = f->pres.data[pres_i].value;
304                         height = cc_barometer_to_altitude(min_pres) -
305                                 cc_barometer_to_altitude(f->ground_pres);
306                         fprintf(summary_file, "\tMax height: %9.2fm    %9.2fft   %9.2fs\n",
307                                 height, height * 100 / 2.54 / 12,
308                                 (f->pres.data[pres_i].time - boost_start) / 100.0);
309                 }
310         }
311         if (cooked && detail_file) {
312                 double  max_height = 0;
313                 int     i;
314                 double  *times;
315
316                 fprintf(detail_file, "%9s %9s %9s %9s\n",
317                        "time", "height", "speed", "accel");
318                 for (i = 0; i < cooked->pres_pos.num; i++) {
319                         double  time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
320                         double  accel = cooked->accel_accel.data[i];
321                         double  pos = cooked->pres_pos.data[i];
322                         double  speed;
323                         if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
324                                 speed = cooked->accel_speed.data[i];
325                         else
326                                 speed = cooked->pres_speed.data[i];
327                         fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
328                                time, pos, speed, accel);
329                 }
330         }
331         if (raw_file) {
332                 fprintf(raw_file, "%9s %9s %9s\n",
333                        "time", "height", "accel");
334                 for (i = 0; i < cooked->pres.num; i++) {
335                         double time = cooked->pres.data[i].time;
336                         double pres = cooked->pres.data[i].value;
337                         double accel = cooked->accel.data[i].value;
338                         fprintf(raw_file, "%9.2f %9.2f %9.2f %9.2f\n",
339                                 time, pres, accel);
340                 }
341         }
342         if (gps_file) {
343                 int     j = 0;
344                 fprintf(gps_file, "%9s %12s %12s %12s\n",
345                         "time", "lat", "lon", "alt");
346                 for (i = 0; i < f->gps.num; i++) {
347                         int     nsat = 0;
348                         int     k;
349                         while (j < f->gps.numsats - 1) {
350                                 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
351                                     f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
352                                         break;
353                                 j++;
354                         }
355                         fprintf(gps_file, "%12.7f %12.7f %12.7f %12.7f",
356                                 (f->gps.data[i].time - boost_start) / 100.0,
357                                 f->gps.data[i].lat,
358                                 f->gps.data[i].lon,
359                                 f->gps.data[i].alt);
360                         nsat = 0;
361                         for (k = 0; k < f->gps.sats[j].nsat; k++) {
362                                 fprintf (gps_file, " %12.7f", (double) f->gps.sats[j].sat[k].c_n);
363                                 if (f->gps.sats[j].sat[k].state == 0xbf)
364                                         nsat++;
365                         }
366                         fprintf(gps_file, " %d\n", nsat);
367                 }
368         }
369         if (kml_file) {
370                 int     j = 0;
371
372                 fprintf(kml_file, "%s", kml_header);
373                 for (i = 0; i < f->gps.num; i++) {
374                         int     nsat = 0;
375                         int     k;
376                         while (j < f->gps.numsats - 1) {
377                                 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
378                                     f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
379                                         break;
380                                 j++;
381                         }
382                         nsat = 0;
383                         for (k = 0; k < f->gps.sats[j].nsat; k++)
384                                 if (f->gps.sats[j].sat[k].state == 0xbf)
385                                         nsat++;
386
387                         fprintf(kml_file, "%12.7f, %12.7f, %12.7f <!-- time %12.7f sats %d -->",
388                                 f->gps.data[i].lon,
389                                 f->gps.data[i].lat,
390                                 f->gps.data[i].alt,
391                                 (f->gps.data[i].time - boost_start) / 100.0,
392                                 nsat);
393                         if (i < f->gps.num - 1)
394                                 fprintf(kml_file, ",\n");
395                         else
396                                 fprintf(kml_file, "\n");
397                 }
398                 fprintf(kml_file, "%s", kml_footer);
399         }
400         if (cooked && plot_name) {
401                 struct cc_perioddata    *speed;
402                 plsdev("svgcairo");
403                 plsfnam(plot_name);
404 #define PLOT_DPI        96
405                 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
406                 plscolbg(0xff, 0xff, 0xff);
407                 plscol0(1,0,0,0);
408                 plstar(2, 3);
409                 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
410
411                 plot_perioddata(&cooked->pres_pos, "meters", "Height",
412                                 -1e10, 1e10, PLOT_HEIGHT);
413                 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
414                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
415                 plot_perioddata(speed, "meters/second", "Speed",
416                                 -1e10, 1e10, PLOT_SPEED);
417                 plot_perioddata(speed, "meters/second", "Speed to Apogee",
418                                 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
419                 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
420                                 -1e10, 1e10, PLOT_ACCEL);
421 /*              plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
422                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
423                 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
424                                 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
425                 free(speed->data);
426                 free(speed);
427                 plend();
428         }
429         if (cooked)
430                 cc_flightcooked_free(cooked);
431 }
432
433 static const struct option options[] = {
434         { .name = "summary", .has_arg = 1, .val = 's' },
435         { .name = "detail", .has_arg = 1, .val = 'd' },
436         { .name = "plot", .has_arg = 1, .val = 'p' },
437         { .name = "raw", .has_arg = 1, .val = 'r' },
438         { .name = "gps", .has_arg = 1, .val = 'g' },
439         { .name = "kml", .has_arg = 1, .val = 'k' },
440         { 0, 0, 0, 0},
441 };
442
443 static void usage(char *program)
444 {
445         fprintf(stderr, "usage: %s\n"
446                 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
447                 "\t[--detail=<detail-file] [-d <detail-file>]\n"
448                 "\t[--raw=<raw-file> -r <raw-file]\n"
449                 "\t[--plot=<plot-file> -p <plot-file>]\n"
450                 "\t[--gps=<gps-file> -g <gps-file>]\n"
451                 "\t[--kml=<kml-file> -k <kml-file>]\n"
452                 "\t{flight-log} ...\n", program);
453         exit(1);
454 }
455
456 int
457 main (int argc, char **argv)
458 {
459         FILE                    *file;
460         FILE                    *summary_file = NULL;
461         FILE                    *detail_file = NULL;
462         FILE                    *raw_file = NULL;
463         FILE                    *gps_file = NULL;
464         FILE                    *kml_file = NULL;
465         int                     i;
466         int                     ret = 0;
467         struct cc_flightraw     *raw;
468         int                     c;
469         int                     serial;
470         char                    *s;
471         char                    *summary_name = NULL;
472         char                    *detail_name = NULL;
473         char                    *raw_name = NULL;
474         char                    *plot_name = NULL;
475         char                    *gps_name = NULL;
476         char                    *kml_name = NULL;
477
478         while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:", options, NULL)) != -1) {
479                 switch (c) {
480                 case 's':
481                         summary_name = optarg;
482                         break;
483                 case 'd':
484                         detail_name = optarg;
485                         break;
486                 case 'p':
487                         plot_name = optarg;
488                         break;
489                 case 'r':
490                         raw_name = optarg;
491                         break;
492                 case 'g':
493                         gps_name = optarg;
494                         break;
495                 case 'k':
496                         kml_name = optarg;
497                         break;
498                 default:
499                         usage(argv[0]);
500                         break;
501                 }
502         }
503         summary_file = stdout;
504         if (summary_name) {
505                 summary_file = fopen(summary_name, "w");
506                 if (!summary_file) {
507                         perror (summary_name);
508                         exit(1);
509                 }
510         }
511         if (detail_name) {
512                 if (summary_name && !strcmp (summary_name, detail_name))
513                         detail_file = summary_file;
514                 else {
515                         detail_file = fopen(detail_name, "w");
516                         if (!detail_file) {
517                                 perror(detail_name);
518                                 exit(1);
519                         }
520                 }
521         }
522         if (raw_name) {
523                 raw_file = fopen (raw_name, "w");
524                 if (!raw_file) {
525                         perror(raw_name);
526                         exit(1);
527                 }
528         }
529         if (gps_name) {
530                 gps_file = fopen(gps_name, "w");
531                 if (!gps_file) {
532                         perror(gps_name);
533                         exit(1);
534                 }
535         }
536         if (kml_name) {
537                 kml_file = fopen(kml_name, "w");
538                 if (!kml_file) {
539                         perror(kml_name);
540                         exit(1);
541                 }
542         }
543         for (i = optind; i < argc; i++) {
544                 file = fopen(argv[i], "r");
545                 if (!file) {
546                         perror(argv[i]);
547                         ret++;
548                         continue;
549                 }
550                 s = strstr(argv[i], "-serial-");
551                 if (s)
552                         serial = atoi(s + 8);
553                 else
554                         serial = 0;
555                 raw = cc_log_read(file);
556                 if (!raw) {
557                         perror(argv[i]);
558                         ret++;
559                         continue;
560                 }
561                 if (!raw->serial)
562                         raw->serial = serial;
563                 analyse_flight(raw, summary_file, detail_file, raw_file, plot_name, gps_file, kml_file);
564                 cc_flightraw_free(raw);
565         }
566         return ret;
567 }