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