2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
27 #include <plplot/plplot.h>
29 static const char *state_names[] = {
42 static int plot_colors[3][3] = {
43 { 0, 0x90, 0 }, /* height */
44 { 0xa0, 0, 0 }, /* speed */
45 { 0, 0, 0xc0 }, /* accel */
53 plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
54 double min_time, double max_time, int plot_type)
62 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
65 times = calloc(stop - start + 1, sizeof (double));
66 for (i = start; i <= stop; i++)
67 times[i-start] = i * d->step / 100.0;
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];
74 plscol0(2, plot_colors[plot_type][0], plot_colors[plot_type][1], plot_colors[plot_type][2]);
76 plenv(times[0], times[stop-start],
78 pllab("Time", axis_label, plot_label);
80 plline(stop - start + 1, times, d->data + start);
85 plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
86 double min_time, double max_time, int plot_type)
93 int start = -1, stop = -1;
94 double start_time = 0, stop_time = 0;
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;
102 if (d->data[i].time <= max_time) {
103 stop_time = d->data[i].time;
108 times = calloc(stop - start + 1, sizeof (double));
109 values = calloc(stop - start + 1, sizeof (double));
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;
120 plscol0(2, plot_colors[plot_type][0], plot_colors[plot_type][1], plot_colors[plot_type][2]);
122 plenv(times[0], times[stop-start], ymin, ymax, 0, 2);
123 pllab("Time", axis_label, plot_label);
125 plline(stop - start + 1, times, values);
130 static struct cc_perioddata *
131 merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
134 struct cc_perioddata *pd;
136 double start_time, stop_time;
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;
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];
154 j = (t - last->start) / last->step;
155 if (j < 0 || j >= last->num)
158 pd->data[i] = last->data[j];
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"
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"
175 " <MultiGeometry id=\"khMultiGeometry697\">\n"
176 " <LineString id=\"khLineString698\">\n"
177 " <tessellate>1</tessellate>\n"
178 " <altitudeMode>absolute</altitudeMode>\n"
181 static const char kml_footer[] =
184 " </MultiGeometry>\n"
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)
196 double boost_start, boost_stop;
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;
207 fprintf(summary_file,
210 f->serial, f->flight);
212 fprintf(summary_file,
213 "Date: %04d-%02d-%02d\n",
214 f->year, f->month, f->day);
217 fprintf(summary_file,
218 "Time: %2d:%02d:%02d\n",
220 f->gps.data[0].minute,
221 f->gps.data[0].second);
223 boost_start = f->accel.data[0].time;
224 boost_stop = f->accel.data[f->accel.num-1].time;
225 for (i = 0; i < f->state.num; i++) {
226 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
227 boost_start = f->state.data[i].time;
230 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
231 boost_stop = f->state.data[i].time;
236 pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
237 f->pres.data[f->pres.num-1].time);
240 min_pres = f->pres.data[pres_i].value;
241 height = cc_barometer_to_altitude(min_pres) -
242 cc_barometer_to_altitude(f->ground_pres);
243 fprintf(summary_file, "Max height: %9.2fm %9.2fft %9.2fs\n",
244 height, height * 100 / 2.54 / 12,
245 (f->pres.data[pres_i].time - boost_start) / 100.0);
246 apogee = f->pres.data[pres_i].time;
249 cooked = cc_flight_cook(f);
251 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
253 speed = cooked->accel_speed.data[speed_i];
254 fprintf(summary_file, "Max speed: %9.2fm/s %9.2fft/s %9.2fs\n",
255 speed, speed * 100 / 2.4 / 12.0,
256 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
259 accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
262 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
264 fprintf(summary_file, "Max accel: %9.2fm/s² %9.2fg %9.2fs\n",
265 accel, accel / 9.80665,
266 (f->accel.data[accel_i].time - boost_start) / 100.0);
269 for (i = 0; i < f->state.num; i++) {
270 state = f->state.data[i].value;
271 state_start = f->state.data[i].time;
272 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
274 if (i < f->state.num - 1)
275 state_stop = f->state.data[i + 1].time;
277 state_stop = f->accel.data[f->accel.num-1].time;
278 fprintf(summary_file, "State: %s\n", state_names[state]);
279 fprintf(summary_file, "\tStart: %9.2fs\n", (state_start - boost_start) / 100.0);
280 fprintf(summary_file, "\tDuration: %9.2fs\n", (state_stop - state_start) / 100.0);
281 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
284 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
286 fprintf(summary_file, "\tMax accel: %9.2fm/s² %9.2fg %9.2fs\n",
287 accel, accel / 9.80665,
288 (f->accel.data[accel_i].time - boost_start) / 100.0);
292 if (state < ao_flight_drogue) {
293 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
295 speed = cooked->accel_speed.data[speed_i];
296 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
298 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
300 speed = cooked->pres_speed.data[speed_i];
301 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
305 fprintf(summary_file, "\tMax speed: %9.2fm/s %9.2fft/s %9.2fs\n",
306 speed, speed * 100 / 2.4 / 12.0,
307 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
308 fprintf(summary_file, "\tAvg speed: %9.2fm/s %9.2fft/s\n",
309 avg_speed, avg_speed * 100 / 2.4 / 12.0);
312 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
315 min_pres = f->pres.data[pres_i].value;
316 height = cc_barometer_to_altitude(min_pres) -
317 cc_barometer_to_altitude(f->ground_pres);
318 fprintf(summary_file, "\tMax height: %9.2fm %9.2fft %9.2fs\n",
319 height, height * 100 / 2.54 / 12,
320 (f->pres.data[pres_i].time - boost_start) / 100.0);
323 if (cooked && detail_file) {
324 double max_height = 0;
328 fprintf(detail_file, "%9s %9s %9s %9s\n",
329 "time", "height", "speed", "accel");
330 for (i = 0; i < cooked->pres_pos.num; i++) {
331 double time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
332 double accel = cooked->accel_accel.data[i];
333 double pos = cooked->pres_pos.data[i];
335 if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
336 speed = cooked->accel_speed.data[i];
338 speed = cooked->pres_speed.data[i];
339 fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
340 time, pos, speed, accel);
344 fprintf(raw_file, "%9s %9s %9s\n",
345 "time", "height", "accel");
346 for (i = 0; i < cooked->pres.num; i++) {
347 double time = cooked->pres.data[i].time;
348 double pres = cooked->pres.data[i].value;
349 double accel = cooked->accel.data[i].value;
350 fprintf(raw_file, "%9.2f %9.2f %9.2f %9.2f\n",
356 fprintf(gps_file, "%9s %12s %12s %12s\n",
357 "time", "lat", "lon", "alt");
358 for (i = 0; i < f->gps.num; i++) {
361 while (j < f->gps.numsats - 1) {
362 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
363 f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
367 fprintf(gps_file, "%12.7f %12.7f %12.7f %12.7f",
368 (f->gps.data[i].time - boost_start) / 100.0,
373 for (k = 0; k < f->gps.sats[j].nsat; k++) {
374 fprintf (gps_file, " %12.7f", (double) f->gps.sats[j].sat[k].c_n);
375 if (f->gps.sats[j].sat[k].svid != 0)
378 fprintf(gps_file, " %d\n", nsat);
384 fprintf(kml_file, "%s", kml_header);
385 for (i = 0; i < f->gps.num; i++) {
388 while (j < f->gps.numsats - 1) {
389 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
390 f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
395 for (k = 0; k < f->gps.sats[j].nsat; k++)
396 if (f->gps.sats[j].sat[k].svid != 0)
399 fprintf(kml_file, "%12.7f, %12.7f, %12.7f <!-- time %12.7f sats %d -->",
403 (f->gps.data[i].time - boost_start) / 100.0,
405 if (i < f->gps.num - 1)
406 fprintf(kml_file, ",\n");
408 fprintf(kml_file, "\n");
410 fprintf(kml_file, "%s", kml_footer);
412 if (cooked && plot_name) {
413 struct cc_perioddata *speed;
417 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
418 plscolbg(0xff, 0xff, 0xff);
421 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
423 plot_perioddata(&cooked->pres_pos, "meters", "Height",
424 -1e10, 1e10, PLOT_HEIGHT);
425 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
426 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
427 plot_perioddata(speed, "meters/second", "Speed",
428 -1e10, 1e10, PLOT_SPEED);
429 plot_perioddata(speed, "meters/second", "Speed to Apogee",
430 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
431 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
432 -1e10, 1e10, PLOT_ACCEL);
433 /* plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
434 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
435 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
436 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
442 cc_flightcooked_free(cooked);
445 static const struct option options[] = {
446 { .name = "summary", .has_arg = 1, .val = 's' },
447 { .name = "detail", .has_arg = 1, .val = 'd' },
448 { .name = "plot", .has_arg = 1, .val = 'p' },
449 { .name = "raw", .has_arg = 1, .val = 'r' },
450 { .name = "gps", .has_arg = 1, .val = 'g' },
451 { .name = "kml", .has_arg = 1, .val = 'k' },
455 static void usage(char *program)
457 fprintf(stderr, "usage: %s\n"
458 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
459 "\t[--detail=<detail-file] [-d <detail-file>]\n"
460 "\t[--raw=<raw-file> -r <raw-file]\n"
461 "\t[--plot=<plot-file> -p <plot-file>]\n"
462 "\t[--gps=<gps-file> -g <gps-file>]\n"
463 "\t[--kml=<kml-file> -k <kml-file>]\n"
464 "\t{flight-log} ...\n", program);
469 main (int argc, char **argv)
472 FILE *summary_file = NULL;
473 FILE *detail_file = NULL;
474 FILE *raw_file = NULL;
475 FILE *gps_file = NULL;
476 FILE *kml_file = NULL;
479 struct cc_flightraw *raw;
483 char *summary_name = NULL;
484 char *detail_name = NULL;
485 char *raw_name = NULL;
486 char *plot_name = NULL;
487 char *gps_name = NULL;
488 char *kml_name = NULL;
490 while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:", options, NULL)) != -1) {
493 summary_name = optarg;
496 detail_name = optarg;
515 summary_file = stdout;
517 summary_file = fopen(summary_name, "w");
519 perror (summary_name);
524 if (summary_name && !strcmp (summary_name, detail_name))
525 detail_file = summary_file;
527 detail_file = fopen(detail_name, "w");
535 raw_file = fopen (raw_name, "w");
542 gps_file = fopen(gps_name, "w");
549 kml_file = fopen(kml_name, "w");
555 for (i = optind; i < argc; i++) {
556 file = fopen(argv[i], "r");
562 s = strstr(argv[i], "-serial-");
564 serial = atoi(s + 8);
567 raw = cc_log_read(file);
574 raw->serial = serial;
575 analyse_flight(raw, summary_file, detail_file, raw_file, plot_name, gps_file, kml_file);
576 cc_flightraw_free(raw);