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 const char *kml_state_colours[] = {
55 static int plot_colors[3][3] = {
56 { 0, 0x90, 0 }, /* height */
57 { 0xa0, 0, 0 }, /* speed */
58 { 0, 0, 0xc0 }, /* accel */
66 plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
67 double min_time, double max_time, int plot_type)
75 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
78 times = calloc(stop - start + 1, sizeof (double));
79 for (i = start; i <= stop; i++)
80 times[i-start] = i * d->step / 100.0;
82 ymin_i = cc_perioddata_min(d, min_time, max_time);
83 ymax_i = cc_perioddata_max(d, min_time, max_time);
84 ymin = d->data[ymin_i];
85 ymax = d->data[ymax_i];
87 plscol0(2, plot_colors[plot_type][0], plot_colors[plot_type][1], plot_colors[plot_type][2]);
89 plenv(times[0], times[stop-start],
91 pllab("Time", axis_label, plot_label);
93 plline(stop - start + 1, times, d->data + start);
98 plot_timedata(struct cc_timedata *d, char *axis_label, char *plot_label,
99 double min_time, double max_time, int plot_type)
106 int start = -1, stop = -1;
107 double start_time = 0, stop_time = 0;
110 for (i = 0; i < d->num; i++) {
111 if (start < 0 && d->data[i].time >= min_time) {
112 start_time = d->data[i].time;
115 if (d->data[i].time <= max_time) {
116 stop_time = d->data[i].time;
121 times = calloc(stop - start + 1, sizeof (double));
122 values = calloc(stop - start + 1, sizeof (double));
124 ymin_i = cc_timedata_min(d, min_time, max_time);
125 ymax_i = cc_timedata_max(d, min_time, max_time);
126 ymin = d->data[ymin_i].value;
127 ymax = d->data[ymax_i].value;
128 for (i = start; i <= stop; i++) {
129 times[i-start] = (d->data[i].time - start_time)/100.0;
130 values[i-start] = d->data[i].value;
133 plscol0(2, plot_colors[plot_type][0], plot_colors[plot_type][1], plot_colors[plot_type][2]);
135 plenv(times[0], times[stop-start], ymin, ymax, 0, 2);
136 pllab("Time", axis_label, plot_label);
138 plline(stop - start + 1, times, values);
143 static struct cc_perioddata *
144 merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
147 struct cc_perioddata *pd;
149 double start_time, stop_time;
152 pd = calloc(1, sizeof (struct cc_perioddata));
153 start_time = first->start;
154 stop_time = last->start + last->step * last->num;
155 num = (stop_time - start_time) / first->step;
157 pd->data = calloc(num, sizeof (double));
158 pd->start = first->start;
159 pd->step = first->step;
160 for (i = 0; i < num; i++) {
161 t = pd->start + i * pd->step;
162 if (t <= split_time) {
163 pd->data[i] = first->data[i];
167 j = (t - last->start) / last->step;
168 if (j < 0 || j >= last->num)
171 pd->data[i] = last->data[j];
177 static const char kml_header_start[] =
178 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
179 "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n"
183 static const char kml_header_end[] =
187 static const char kml_style_start[] =
188 " <Style id=\"ao-flightstate-%s\">\n"
189 " <LineStyle><color>%s</color><width>4</width></LineStyle>\n"
192 static const char kml_style_end[] =
197 static const char kml_placemark_start[] =
200 " <styleUrl>#ao-flightstate-%s</styleUrl>\n"
202 " <tessellate>1</tessellate>\n"
203 " <altitudeMode>absolute</altitudeMode>\n"
206 static const char kml_coord_fmt[] =
207 " %12.7f, %12.7f, %12.7f <!-- alt %12.7f time %12.7f sats %d -->\n";
209 static const char kml_placemark_end[] =
214 static const char kml_footer[] =
222 gps_daytime(struct cc_gpselt *gps)
224 return ((gps->hour * 60 +
230 daytime_hour(unsigned daytime)
232 return daytime / 1000 / 60 / 60;
236 daytime_minute(unsigned daytime)
238 return (daytime / 1000 / 60) % 60;
242 daytime_second(unsigned daytime)
244 return (daytime / 1000) % 60;
248 daytime_millisecond(unsigned daytime)
250 return daytime % 1000;
254 compute_daytime_ms(double time, struct cc_gpsdata *gps)
257 unsigned gps_start_daytime, gps_stop_daytime;
259 if (time <= gps->data[0].time) {
260 gps_stop_daytime = gps_daytime(&gps->data[0]);
261 return gps_stop_daytime - (gps->data[0].time - time) * 10;
263 for (i = 0; i < gps->num - 1; i++)
264 if (time > gps->data[i].time)
266 gps_start_daytime = gps_daytime(&gps->data[i]);
267 if (i == gps->num - 1) {
268 return gps_start_daytime + (time - gps->data[i].time) * 10;
270 unsigned gps_period_daytime;
271 double gps_period_time;
272 double time_since_start;
274 gps_stop_daytime = gps_daytime(&gps->data[i + 1]);
276 /* range of gps daytime values */
277 gps_period_daytime = gps_stop_daytime - gps_start_daytime;
279 /* range of gps time values */
280 gps_period_time = gps->data[i+1].time - gps->data[i].time;
282 /* sample time after first gps time */
283 time_since_start = time - gps->data[i].time;
285 return gps_start_daytime +
286 gps_period_daytime * time_since_start / gps_period_time;
291 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file,
292 FILE *raw_file, char *plot_name, FILE *gps_file, FILE *kml_file)
298 double boost_start, boost_stop;
301 int pres_i, accel_i, speed_i;
302 int boost_start_set = 0;
303 int boost_stop_set = 0;
304 enum ao_flight_state state;
305 double state_start, state_stop;
306 struct cc_flightcooked *cooked;
311 snprintf(buf, sizeof (buf), "AO Flight#%d S/N: %03d", f->flight, f->serial);
312 fprintf(kml_file, kml_header_start, buf);
315 fprintf(summary_file,
318 f->serial, f->flight);
321 snprintf(buf, sizeof (buf),
322 "Date: %04d-%02d-%02d\n",
323 f->year, f->month, f->day);
324 fprintf(summary_file, "%s", buf);
325 if (kml_file) fprintf(kml_file, "%s", buf);
328 snprintf(buf, sizeof (buf),
329 "Time: %2d:%02d:%02d\n",
331 f->gps.data[0].minute,
332 f->gps.data[0].second);
333 fprintf(summary_file, "%s", buf);
334 if (kml_file) fprintf(kml_file, "%s", buf);
336 boost_start = f->accel.data[0].time;
337 boost_stop = f->accel.data[f->accel.num-1].time;
338 for (i = 0; i < f->state.num; i++) {
339 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
340 boost_start = f->state.data[i].time;
343 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
344 boost_stop = f->state.data[i].time;
349 pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
350 f->pres.data[f->pres.num-1].time);
353 min_pres = f->pres.data[pres_i].value;
354 height = cc_barometer_to_altitude(min_pres) -
355 cc_barometer_to_altitude(f->ground_pres);
356 apogee = f->pres.data[pres_i].time;
357 snprintf(buf, sizeof (buf), "Max height: %9.2fm %9.2fft %9.2fs\n",
358 height, height * 100 / 2.54 / 12,
359 (f->pres.data[pres_i].time - boost_start) / 100.0);
360 fprintf(summary_file, "%s", buf);
361 if (kml_file) fprintf(kml_file, "%s", buf);
364 cooked = cc_flight_cook(f);
366 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
368 speed = cooked->accel_speed.data[speed_i];
369 snprintf(buf, sizeof (buf), "Max speed: %9.2fm/s %9.2fft/s %9.2fs\n",
370 speed, speed * 100 / 2.4 / 12.0,
371 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
372 fprintf(summary_file, "%s", buf);
373 if (kml_file) fprintf(kml_file, "%s", buf);
376 accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
379 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
381 snprintf(buf, sizeof (buf), "Max accel: %9.2fm/s² %9.2fg %9.2fs\n",
382 accel, accel / 9.80665,
383 (f->accel.data[accel_i].time - boost_start) / 100.0);
384 fprintf(summary_file, "%s", buf);
385 if (kml_file) fprintf(kml_file, "%s", buf);
389 fprintf(kml_file, "%s", kml_header_end);
391 for (i = 0; i < f->state.num; i++) {
392 state = f->state.data[i].value;
393 state_start = f->state.data[i].time;
394 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
396 if (i < f->state.num - 1)
397 state_stop = f->state.data[i + 1].time;
399 state_stop = f->accel.data[f->accel.num-1].time;
400 fprintf(summary_file, "State: %s\n", state_names[state]);
401 fprintf(summary_file, "\tStart: %9.2fs\n", (state_start - boost_start) / 100.0);
402 fprintf(summary_file, "\tDuration: %9.2fs\n", (state_stop - state_start) / 100.0);
404 fprintf(kml_file, kml_style_start, state_names[state], kml_state_colours[state]);
405 fprintf(kml_file, "\tState: %s\n", state_names[state]);
406 fprintf(kml_file, "\tStart: %9.2fs\n", (state_start - boost_start) / 100.0);
407 fprintf(kml_file, "\tDuration: %9.2fs\n", (state_stop - state_start) / 100.0);
410 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
413 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
415 snprintf(buf, sizeof (buf), "\tMax accel: %9.2fm/s² %9.2fg %9.2fs\n",
416 accel, accel / 9.80665,
417 (f->accel.data[accel_i].time - boost_start) / 100.0);
418 fprintf(summary_file, "%s", buf);
419 if (kml_file) fprintf(kml_file, "%s", buf);
423 if (state < ao_flight_drogue) {
424 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
426 speed = cooked->accel_speed.data[speed_i];
427 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
429 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
431 speed = cooked->pres_speed.data[speed_i];
432 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
436 snprintf(buf, sizeof (buf), "\tMax speed: %9.2fm/s %9.2fft/s %9.2fs\n",
437 speed, speed * 100 / 2.4 / 12.0,
438 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
439 fprintf(summary_file, "%s", buf);
440 if (kml_file) fprintf(kml_file, "%s", buf);
442 snprintf(buf, sizeof (buf), "\tAvg speed: %9.2fm/s %9.2fft/s\n",
443 avg_speed, avg_speed * 100 / 2.4 / 12.0);
444 fprintf(summary_file, "%s", buf);
445 if (kml_file) fprintf(kml_file, "%s", buf);
448 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
451 min_pres = f->pres.data[pres_i].value;
452 height = cc_barometer_to_altitude(min_pres) -
453 cc_barometer_to_altitude(f->ground_pres);
454 snprintf(buf, sizeof (buf), "\tMax height: %9.2fm %9.2fft %9.2fs\n",
455 height, height * 100 / 2.54 / 12,
456 (f->pres.data[pres_i].time - boost_start) / 100.0);
457 fprintf(summary_file, "%s", buf);
458 if (kml_file) fprintf(kml_file, "%s", buf);
460 if (kml_file) fprintf(kml_file, "%s", kml_style_end);
462 if (cooked && detail_file) {
463 double max_height = 0;
467 fprintf(detail_file, "%9s %9s %9s %9s %9s\n",
468 "time", "height", "speed", "accel", "daytime");
469 for (i = 0; i < cooked->pres_pos.num; i++) {
470 double clock_time = cooked->accel_accel.start + i * cooked->accel_accel.step;
471 double time = (clock_time - boost_start) / 100.0;
472 double accel = cooked->accel_accel.data[i];
473 double pos = cooked->pres_pos.data[i];
476 if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
477 speed = cooked->accel_speed.data[i];
479 speed = cooked->pres_speed.data[i];
481 daytime = compute_daytime_ms(clock_time, &f->gps);
484 fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f %02d:%02d:%02d.%03d\n",
485 time, pos, speed, accel,
486 daytime_hour(daytime),
487 daytime_minute(daytime),
488 daytime_second(daytime),
489 daytime_millisecond(daytime));
493 fprintf(raw_file, "%9s %9s %9s %9s\n",
494 "time", "height", "accel", "daytime");
495 for (i = 0; i < cooked->pres.num; i++) {
496 double time = cooked->pres.data[i].time;
497 double pres = cooked->pres.data[i].value;
498 double accel = cooked->accel.data[i].value;
501 daytime = compute_daytime_ms(time, &f->gps);
504 fprintf(raw_file, "%9.2f %9.2f %9.2f %02d:%02d:%02d.%03d\n",
506 daytime_hour(daytime),
507 daytime_minute(daytime),
508 daytime_second(daytime),
509 daytime_millisecond(daytime));
512 if (gps_file || kml_file) {
519 fprintf(gps_file, "%2s %2s %2s %9s %12s %12s %9s %8s %5s\n",
521 "time", "lat", "lon", "alt", "baro", "nsat");
523 fprintf(kml_file, kml_placemark_start,
524 state_names[(int)f->state.data[state_idx].value],
525 state_names[(int)f->state.data[state_idx].value]);
528 baro_offset = f->gps.data[0].alt;
532 for (i = 0; i < f->gps.num; i++) {
535 while (j < f->gps.numsats - 1) {
536 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
537 f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
542 while (baro_pos < cooked->pres_pos.num) {
543 double baro_time = cooked->accel_accel.start + baro_pos * cooked->accel_accel.step;
544 if (baro_time >= f->gps.data[i].time)
548 if (baro_pos < cooked->pres_pos.num)
549 baro = cooked->pres_pos.data[baro_pos];
552 fprintf(gps_file, "%2d %2d %2d %12.7f %12.7f %12.7f %7.1f %7.1f",
554 f->gps.data[i].minute,
555 f->gps.data[i].second,
556 (f->gps.data[i].time - boost_start) / 100.0,
564 for (k = 0; k < f->gps.sats[j].nsat; k++) {
565 if (f->gps.sats[j].sat[k].svid != 0)
569 fprintf(gps_file, " %4d", nsat);
570 for (k = 0; k < f->gps.sats[j].nsat; k++) {
571 if (f->gps.sats[j].sat[k].svid != 0) {
572 fprintf (gps_file, " %3d(%4.1f)",
573 f->gps.sats[j].sat[k].svid,
574 (double) f->gps.sats[j].sat[k].c_n);
577 fprintf(gps_file, "\n");
582 snprintf(buf, sizeof (buf), kml_coord_fmt,
587 (f->gps.data[i].time - boost_start) / 100.0,
589 fprintf(kml_file, "%s", buf);
590 if (state_idx + 1 < f->state.num && f->state.data[state_idx + 1].time <= f->gps.data[i].time) {
592 if (f->state.data[state_idx - 1].value != f->state.data[state_idx].value) {
593 fprintf(kml_file, "%s", kml_placemark_end);
594 fprintf(kml_file, kml_placemark_start,
595 state_names[(int)f->state.data[state_idx].value],
596 state_names[(int)f->state.data[state_idx].value]);
597 fprintf(kml_file, "%s", buf);
604 fprintf(kml_file, "%s", kml_footer);
606 if (cooked && plot_name) {
607 struct cc_perioddata *speed;
611 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
612 plscolbg(0xff, 0xff, 0xff);
615 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
617 plot_perioddata(&cooked->pres_pos, "meters", "Height",
618 -1e10, 1e10, PLOT_HEIGHT);
619 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
620 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
621 plot_perioddata(speed, "meters/second", "Speed",
622 -1e10, 1e10, PLOT_SPEED);
623 plot_perioddata(speed, "meters/second", "Speed to Apogee",
624 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
625 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
626 -1e10, 1e10, PLOT_ACCEL);
627 /* plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
628 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
629 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
630 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
636 cc_flightcooked_free(cooked);
639 static const struct option options[] = {
640 { .name = "summary", .has_arg = 2, .val = 's' },
641 { .name = "detail", .has_arg = 2, .val = 'd' },
642 { .name = "plot", .has_arg = 2, .val = 'p' },
643 { .name = "raw", .has_arg = 2, .val = 'r' },
644 { .name = "gps", .has_arg = 2, .val = 'g' },
645 { .name = "kml", .has_arg = 2, .val = 'k' },
646 { .name = "all", .has_arg = 0, .val = 'a' },
650 static void usage(char *program)
652 fprintf(stderr, "usage: %s\n"
654 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
655 "\t[--detail=<detail-file] [-d <detail-file>]\n"
656 "\t[--raw=<raw-file> -r <raw-file]\n"
657 "\t[--plot=<plot-file> -p <plot-file>]\n"
658 "\t[--gps=<gps-file> -g <gps-file>]\n"
659 "\t[--kml=<kml-file> -k <kml-file>]\n"
660 "\t{flight-log} ...\n", program);
665 replace_extension(char *file, char *extension)
672 slash = strrchr(file, '/');
673 dot = strrchr(file, '.');
674 if (!dot || (slash && dot < slash))
675 dot = file + strlen(file);
676 newlen = (dot - file) + strlen (extension) + 1;
677 new = malloc (newlen);
678 strncpy (new, file, dot - file);
679 new[dot-file] = '\0';
680 strcat (new, extension);
685 open_output(char *outname, char *inname, char *extension)
693 o = replace_extension(inname, extension);
705 main (int argc, char **argv)
708 FILE *summary_file = NULL;
709 FILE *detail_file = NULL;
710 FILE *raw_file = NULL;
711 FILE *gps_file = NULL;
712 FILE *kml_file = NULL;
715 struct cc_flightraw *raw;
719 char *summary_name = NULL;
720 char *detail_name = NULL;
721 char *raw_name = NULL;
722 char *plot_name = NULL;
723 char *gps_name = NULL;
724 char *kml_name = NULL;
731 char *this_plot_name = NULL;;
733 while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:a", options, NULL)) != -1) {
736 summary_name = optarg;
740 detail_name = optarg;
760 has_summary = has_detail = has_plot = has_raw = has_gps = has_kml = 1;
768 summary_file = stdout;
769 for (i = optind; i < argc; i++) {
770 file = fopen(argv[i], "r");
776 if (has_summary && !summary_file)
777 summary_file = open_output(summary_name, argv[i], ".summary");
778 if (has_detail && !detail_file)
779 detail_file = open_output(detail_name, argv[i], ".detail");
782 this_plot_name = plot_name;
784 this_plot_name = replace_extension(argv[i], ".plot");
786 if (has_raw && !raw_file)
787 raw_file = open_output(raw_name, argv[i], ".raw");
788 if (has_gps && !gps_file)
789 gps_file = open_output(gps_name, argv[i], ".gps");
790 if (has_kml && !kml_file)
791 kml_file = open_output(kml_name, argv[i], ".kml");
792 s = strstr(argv[i], "-serial-");
794 serial = atoi(s + 8);
797 raw = cc_log_read(file);
804 raw->serial = serial;
805 analyse_flight(raw, summary_file, detail_file, raw_file, this_plot_name, gps_file, kml_file);
806 cc_flightraw_free(raw);
807 if (has_summary && !summary_name) {
808 fclose(summary_file); summary_file = NULL;
810 if (has_detail && !detail_name) {
811 fclose(detail_file); detail_file = NULL;
813 if (this_plot_name && this_plot_name != plot_name) {
814 free (this_plot_name); this_plot_name = NULL;
816 if (has_raw && !raw_name) {
817 fclose(raw_file); raw_file = NULL;
819 if (has_gps && !gps_name) {
820 fclose(gps_file); gps_file = NULL;
822 if (has_kml && !kml_name) {
823 fclose(kml_file); kml_file = NULL;