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 gps_daytime(struct cc_gpselt *gps)
191 return ((gps->hour * 60 +
197 daytime_hour(unsigned daytime)
199 return daytime / 1000 / 60 / 60;
203 daytime_minute(unsigned daytime)
205 return (daytime / 1000 / 60) % 60;
209 daytime_second(unsigned daytime)
211 return (daytime / 1000) % 60;
215 daytime_millisecond(unsigned daytime)
217 return daytime % 1000;
221 compute_daytime_ms(double time, struct cc_gpsdata *gps)
224 unsigned gps_start_daytime, gps_stop_daytime;
226 if (time <= gps->data[0].time) {
227 gps_stop_daytime = gps_daytime(&gps->data[0]);
228 return gps_stop_daytime - (gps->data[0].time - time) * 10;
230 for (i = 0; i < gps->num - 1; i++)
231 if (time > gps->data[i].time)
233 gps_start_daytime = gps_daytime(&gps->data[i]);
234 if (i == gps->num - 1) {
235 return gps_start_daytime + (time - gps->data[i].time) * 10;
237 unsigned gps_period_daytime;
238 double gps_period_time;
239 double time_since_start;
241 gps_stop_daytime = gps_daytime(&gps->data[i + 1]);
243 /* range of gps daytime values */
244 gps_period_daytime = gps_stop_daytime - gps_start_daytime;
246 /* range of gps time values */
247 gps_period_time = gps->data[i+1].time - gps->data[i].time;
249 /* sample time after first gps time */
250 time_since_start = time - gps->data[i].time;
252 return gps_start_daytime +
253 gps_period_daytime * time_since_start / gps_period_time;
258 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file,
259 FILE *raw_file, char *plot_name, FILE *gps_file, FILE *kml_file)
265 double boost_start, boost_stop;
268 int pres_i, accel_i, speed_i;
269 int boost_start_set = 0;
270 int boost_stop_set = 0;
271 enum ao_flight_state state;
272 double state_start, state_stop;
273 struct cc_flightcooked *cooked;
276 fprintf(summary_file,
279 f->serial, f->flight);
281 fprintf(summary_file,
282 "Date: %04d-%02d-%02d\n",
283 f->year, f->month, f->day);
286 fprintf(summary_file,
287 "Time: %2d:%02d:%02d\n",
289 f->gps.data[0].minute,
290 f->gps.data[0].second);
292 boost_start = f->accel.data[0].time;
293 boost_stop = f->accel.data[f->accel.num-1].time;
294 for (i = 0; i < f->state.num; i++) {
295 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
296 boost_start = f->state.data[i].time;
299 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
300 boost_stop = f->state.data[i].time;
305 pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
306 f->pres.data[f->pres.num-1].time);
309 min_pres = f->pres.data[pres_i].value;
310 height = cc_barometer_to_altitude(min_pres) -
311 cc_barometer_to_altitude(f->ground_pres);
312 fprintf(summary_file, "Max height: %9.2fm %9.2fft %9.2fs\n",
313 height, height * 100 / 2.54 / 12,
314 (f->pres.data[pres_i].time - boost_start) / 100.0);
315 apogee = f->pres.data[pres_i].time;
318 cooked = cc_flight_cook(f);
320 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
322 speed = cooked->accel_speed.data[speed_i];
323 fprintf(summary_file, "Max speed: %9.2fm/s %9.2fft/s %9.2fs\n",
324 speed, speed * 100 / 2.4 / 12.0,
325 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
328 accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
331 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
333 fprintf(summary_file, "Max accel: %9.2fm/s² %9.2fg %9.2fs\n",
334 accel, accel / 9.80665,
335 (f->accel.data[accel_i].time - boost_start) / 100.0);
338 for (i = 0; i < f->state.num; i++) {
339 state = f->state.data[i].value;
340 state_start = f->state.data[i].time;
341 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
343 if (i < f->state.num - 1)
344 state_stop = f->state.data[i + 1].time;
346 state_stop = f->accel.data[f->accel.num-1].time;
347 fprintf(summary_file, "State: %s\n", state_names[state]);
348 fprintf(summary_file, "\tStart: %9.2fs\n", (state_start - boost_start) / 100.0);
349 fprintf(summary_file, "\tDuration: %9.2fs\n", (state_stop - state_start) / 100.0);
350 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
353 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
355 fprintf(summary_file, "\tMax accel: %9.2fm/s² %9.2fg %9.2fs\n",
356 accel, accel / 9.80665,
357 (f->accel.data[accel_i].time - boost_start) / 100.0);
361 if (state < ao_flight_drogue) {
362 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
364 speed = cooked->accel_speed.data[speed_i];
365 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
367 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
369 speed = cooked->pres_speed.data[speed_i];
370 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
374 fprintf(summary_file, "\tMax speed: %9.2fm/s %9.2fft/s %9.2fs\n",
375 speed, speed * 100 / 2.4 / 12.0,
376 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
377 fprintf(summary_file, "\tAvg speed: %9.2fm/s %9.2fft/s\n",
378 avg_speed, avg_speed * 100 / 2.4 / 12.0);
381 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
384 min_pres = f->pres.data[pres_i].value;
385 height = cc_barometer_to_altitude(min_pres) -
386 cc_barometer_to_altitude(f->ground_pres);
387 fprintf(summary_file, "\tMax height: %9.2fm %9.2fft %9.2fs\n",
388 height, height * 100 / 2.54 / 12,
389 (f->pres.data[pres_i].time - boost_start) / 100.0);
392 if (cooked && detail_file) {
393 double max_height = 0;
397 fprintf(detail_file, "%9s %9s %9s %9s %9s\n",
398 "time", "height", "speed", "accel", "daytime");
399 for (i = 0; i < cooked->pres_pos.num; i++) {
400 double clock_time = cooked->accel_accel.start + i * cooked->accel_accel.step;
401 double time = (clock_time - boost_start) / 100.0;
402 double accel = cooked->accel_accel.data[i];
403 double pos = cooked->pres_pos.data[i];
406 if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
407 speed = cooked->accel_speed.data[i];
409 speed = cooked->pres_speed.data[i];
411 daytime = compute_daytime_ms(clock_time, &f->gps);
414 fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f %02d:%02d:%02d.%03d\n",
415 time, pos, speed, accel,
416 daytime_hour(daytime),
417 daytime_minute(daytime),
418 daytime_second(daytime),
419 daytime_millisecond(daytime));
423 fprintf(raw_file, "%9s %9s %9s %9s\n",
424 "time", "height", "accel", "daytime");
425 for (i = 0; i < cooked->pres.num; i++) {
426 double time = cooked->pres.data[i].time;
427 double pres = cooked->pres.data[i].value;
428 double accel = cooked->accel.data[i].value;
431 daytime = compute_daytime_ms(time, &f->gps);
434 fprintf(raw_file, "%9.2f %9.2f %9.2f %9.2f %02d:%02d:%02d.%03d\n",
436 daytime_hour(daytime),
437 daytime_minute(daytime),
438 daytime_second(daytime),
439 daytime_millisecond(daytime));
442 if (gps_file || kml_file) {
448 fprintf(gps_file, "%2s %2s %2s %9s %12s %12s %9s %8s %5s\n",
450 "time", "lat", "lon", "alt", "baro", "nsat");
452 fprintf(kml_file, "%s", kml_header);
454 baro_offset = f->gps.data[0].alt;
458 for (i = 0; i < f->gps.num; i++) {
461 while (j < f->gps.numsats - 1) {
462 if (f->gps.sats[j].sat[0].time <= f->gps.data[i].time &&
463 f->gps.data[i].time < f->gps.sats[j+1].sat[0].time)
468 while (baro_pos < cooked->pres_pos.num) {
469 double baro_time = cooked->accel_accel.start + baro_pos * cooked->accel_accel.step;
470 if (baro_time >= f->gps.data[i].time)
474 if (baro_pos < cooked->pres_pos.num)
475 baro = cooked->pres_pos.data[baro_pos];
478 fprintf(gps_file, "%2d %2d %2d %12.7f %12.7f %12.7f %7.1f %7.1f",
480 f->gps.data[i].minute,
481 f->gps.data[i].second,
482 (f->gps.data[i].time - boost_start) / 100.0,
488 fprintf(kml_file, "%12.7f, %12.7f, %12.7f <!-- alt %12.7f time %12.7f sats %d -->",
493 (f->gps.data[i].time - boost_start) / 100.0,
495 if (i < f->gps.num - 1)
496 fprintf(kml_file, ",\n");
498 fprintf(kml_file, "\n");
503 for (k = 0; k < f->gps.sats[j].nsat; k++) {
504 if (f->gps.sats[j].sat[k].svid != 0)
508 fprintf(gps_file, " %4d", nsat);
509 for (k = 0; k < f->gps.sats[j].nsat; k++) {
510 if (f->gps.sats[j].sat[k].svid != 0) {
511 fprintf (gps_file, " %3d(%4.1f)",
512 f->gps.sats[j].sat[k].svid,
513 (double) f->gps.sats[j].sat[k].c_n);
516 fprintf(gps_file, "\n");
521 fprintf(kml_file, "%s", kml_footer);
523 if (cooked && plot_name) {
524 struct cc_perioddata *speed;
528 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
529 plscolbg(0xff, 0xff, 0xff);
532 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
534 plot_perioddata(&cooked->pres_pos, "meters", "Height",
535 -1e10, 1e10, PLOT_HEIGHT);
536 plot_perioddata(&cooked->pres_pos, "meters", "Height to Apogee",
537 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_HEIGHT);
538 plot_perioddata(speed, "meters/second", "Speed",
539 -1e10, 1e10, PLOT_SPEED);
540 plot_perioddata(speed, "meters/second", "Speed to Apogee",
541 boost_start, apogee + (apogee - boost_start) / 10.0, PLOT_SPEED);
542 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration",
543 -1e10, 1e10, PLOT_ACCEL);
544 /* plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration during Boost",
545 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL); */
546 plot_timedata(&cooked->accel, "meters/second²", "Acceleration during Boost",
547 boost_start, boost_stop + (boost_stop - boost_start) / 2.0, PLOT_ACCEL);
553 cc_flightcooked_free(cooked);
556 static const struct option options[] = {
557 { .name = "summary", .has_arg = 2, .val = 's' },
558 { .name = "detail", .has_arg = 2, .val = 'd' },
559 { .name = "plot", .has_arg = 2, .val = 'p' },
560 { .name = "raw", .has_arg = 2, .val = 'r' },
561 { .name = "gps", .has_arg = 2, .val = 'g' },
562 { .name = "kml", .has_arg = 2, .val = 'k' },
563 { .name = "all", .has_arg = 0, .val = 'a' },
567 static void usage(char *program)
569 fprintf(stderr, "usage: %s\n"
571 "\t[--summary=<summary-file>] [-s <summary-file>]\n"
572 "\t[--detail=<detail-file] [-d <detail-file>]\n"
573 "\t[--raw=<raw-file> -r <raw-file]\n"
574 "\t[--plot=<plot-file> -p <plot-file>]\n"
575 "\t[--gps=<gps-file> -g <gps-file>]\n"
576 "\t[--kml=<kml-file> -k <kml-file>]\n"
577 "\t{flight-log} ...\n", program);
582 replace_extension(char *file, char *extension)
589 slash = strrchr(file, '/');
590 dot = strrchr(file, '.');
591 if (!dot || (slash && dot < slash))
592 dot = file + strlen(file);
593 newlen = (dot - file) + strlen (extension) + 1;
594 new = malloc (newlen);
595 strncpy (new, file, dot - file);
596 new[dot-file] = '\0';
597 strcat (new, extension);
602 open_output(char *outname, char *inname, char *extension)
610 o = replace_extension(inname, extension);
622 main (int argc, char **argv)
625 FILE *summary_file = NULL;
626 FILE *detail_file = NULL;
627 FILE *raw_file = NULL;
628 FILE *gps_file = NULL;
629 FILE *kml_file = NULL;
632 struct cc_flightraw *raw;
636 char *summary_name = NULL;
637 char *detail_name = NULL;
638 char *raw_name = NULL;
639 char *plot_name = NULL;
640 char *gps_name = NULL;
641 char *kml_name = NULL;
648 char *this_plot_name = NULL;;
650 while ((c = getopt_long(argc, argv, "s:d:p:r:g:k:a", options, NULL)) != -1) {
653 summary_name = optarg;
657 detail_name = optarg;
677 has_summary = has_detail = has_plot = has_raw = has_gps = has_kml = 1;
685 summary_file = stdout;
686 for (i = optind; i < argc; i++) {
687 file = fopen(argv[i], "r");
693 if (has_summary && !summary_file)
694 summary_file = open_output(summary_name, argv[i], ".summary");
695 if (has_detail && !detail_file)
696 detail_file = open_output(detail_name, argv[i], ".detail");
699 this_plot_name = plot_name;
701 this_plot_name = replace_extension(argv[i], ".plot");
703 if (has_raw && !raw_file)
704 raw_file = open_output(raw_name, argv[i], ".raw");
705 if (has_gps && !gps_file)
706 gps_file = open_output(gps_name, argv[i], ".gps");
707 if (has_kml && !kml_file)
708 kml_file = open_output(gps_name, argv[i], ".kml");
709 s = strstr(argv[i], "-serial-");
711 serial = atoi(s + 8);
714 raw = cc_log_read(file);
721 raw->serial = serial;
722 analyse_flight(raw, summary_file, detail_file, raw_file, this_plot_name, gps_file, kml_file);
723 cc_flightraw_free(raw);
724 if (has_summary && !summary_name) {
725 fclose(summary_file); summary_file = NULL;
727 if (has_detail && !detail_name) {
728 fclose(detail_file); detail_file = NULL;
730 if (this_plot_name && this_plot_name != plot_name) {
731 free (this_plot_name); this_plot_name = NULL;
733 if (has_raw && !raw_name) {
734 fclose(raw_file); raw_file = NULL;
736 if (has_gps && !gps_name) {
737 fclose(gps_file); gps_file = NULL;
739 if (has_kml && !kml_name) {
740 fclose(kml_file); kml_file = NULL;