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