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[] = {
43 plot_perioddata(struct cc_perioddata *d, char *axis_label, char *plot_label,
44 double min_time, double max_time)
52 if (!cc_perioddata_limits(d, min_time, max_time, &start, &stop))
55 times = calloc(stop - start + 1, sizeof (double));
56 for (i = start; i <= stop; i++)
57 times[i-start] = i * d->step / 100.0;
59 ymin_i = cc_perioddata_min(d, min_time, max_time);
60 ymax_i = cc_perioddata_max(d, min_time, max_time);
61 ymin = d->data[ymin_i];
62 ymax = d->data[ymax_i];
63 plenv(times[0], times[stop-start],
66 pllab("Time", axis_label, plot_label);
67 plline(stop - start + 1, times, d->data + start);
70 static struct cc_perioddata *
71 merge_data(struct cc_perioddata *first, struct cc_perioddata *last, double split_time)
74 struct cc_perioddata *pd;
76 double start_time, stop_time;
79 pd = calloc(1, sizeof (struct cc_perioddata));
80 start_time = first->start;
81 stop_time = last->start + last->step * last->num;
82 num = (stop_time - start_time) / first->step;
84 pd->data = calloc(num, sizeof (double));
85 pd->start = first->start;
86 pd->step = first->step;
87 for (i = 0; i < num; i++) {
88 t = pd->start + i * pd->step;
89 if (t <= split_time) {
90 pd->data[i] = first->data[i];
94 j = (t - last->start) / last->step;
95 if (j < 0 || j >= last->num)
98 pd->data[i] = last->data[j];
105 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file, char *plot_name)
111 double boost_start, boost_stop;
114 int pres_i, accel_i, speed_i;
115 int boost_start_set = 0;
116 int boost_stop_set = 0;
117 enum ao_flight_state state;
118 double state_start, state_stop;
119 struct cc_flightcooked *cooked;
122 fprintf(summary_file, "Flight: %9d\nSerial: %9d\n",
123 f->flight, f->serial);
124 boost_start = f->accel.data[0].time;
125 boost_stop = f->accel.data[f->accel.num-1].time;
126 for (i = 0; i < f->state.num; i++) {
127 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
128 boost_start = f->state.data[i].time;
131 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
132 boost_stop = f->state.data[i].time;
137 pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
138 f->pres.data[f->pres.num-1].time);
141 min_pres = f->pres.data[pres_i].value;
142 height = cc_barometer_to_altitude(min_pres) -
143 cc_barometer_to_altitude(f->ground_pres);
144 fprintf(summary_file, "Max height: %9.2fm %9.2fft %9.2fs\n",
145 height, height * 100 / 2.54 / 12,
146 (f->pres.data[pres_i].time - boost_start) / 100.0);
147 apogee = f->pres.data[pres_i].time;
150 cooked = cc_flight_cook(f);
152 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
154 speed = cooked->accel_speed.data[speed_i];
155 fprintf(summary_file, "Max speed: %9.2fm/s %9.2fft/s %9.2fs\n",
156 speed, speed * 100 / 2.4 / 12.0,
157 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
160 accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
163 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
165 fprintf(summary_file, "Max accel: %9.2fm/s² %9.2fg %9.2fs\n",
166 accel, accel / 9.80665,
167 (f->accel.data[accel_i].time - boost_start) / 100.0);
170 for (i = 0; i < f->state.num; i++) {
171 state = f->state.data[i].value;
172 state_start = f->state.data[i].time;
173 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
175 if (i < f->state.num - 1)
176 state_stop = f->state.data[i + 1].time;
178 state_stop = f->accel.data[f->accel.num-1].time;
179 fprintf(summary_file, "State: %s\n", state_names[state]);
180 fprintf(summary_file, "\tStart: %9.2fs\n", (state_start - boost_start) / 100.0);
181 fprintf(summary_file, "\tDuration: %9.2fs\n", (state_stop - state_start) / 100.0);
182 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
185 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
187 fprintf(summary_file, "\tMax accel: %9.2fm/s² %9.2fg %9.2fs\n",
188 accel, accel / 9.80665,
189 (f->accel.data[accel_i].time - boost_start) / 100.0);
193 if (state < ao_flight_drogue) {
194 speed_i = cc_perioddata_max_mag(&cooked->accel_speed, state_start, state_stop);
196 speed = cooked->accel_speed.data[speed_i];
197 avg_speed = cc_perioddata_average(&cooked->accel_speed, state_start, state_stop);
199 speed_i = cc_perioddata_max_mag(&cooked->pres_speed, state_start, state_stop);
201 speed = cooked->pres_speed.data[speed_i];
202 avg_speed = cc_perioddata_average(&cooked->pres_speed, state_start, state_stop);
206 fprintf(summary_file, "\tMax speed: %9.2fm/s %9.2fft/s %9.2fs\n",
207 speed, speed * 100 / 2.4 / 12.0,
208 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
209 fprintf(summary_file, "\tAvg speed: %9.2fm/s %9.2fft/s\n",
210 avg_speed, avg_speed * 100 / 2.4 / 12.0);
213 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
216 min_pres = f->pres.data[pres_i].value;
217 height = cc_barometer_to_altitude(min_pres) -
218 cc_barometer_to_altitude(f->ground_pres);
219 fprintf(summary_file, "\tMax height: %9.2fm %9.2fft %9.2fs\n",
220 height, height * 100 / 2.54 / 12,
221 (f->pres.data[pres_i].time - boost_start) / 100.0);
224 if (cooked && detail_file) {
225 double max_height = 0;
229 fprintf(detail_file, "%9s %9s %9s %9s\n",
230 "time", "height", "speed", "accel");
231 for (i = 0; i < cooked->pres_pos.num; i++) {
232 double time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
233 double accel = cooked->accel_accel.data[i];
234 double pos = cooked->pres_pos.data[i];
236 if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee)
237 speed = cooked->accel_speed.data[i];
239 speed = cooked->pres_speed.data[i];
240 fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
241 time, pos, speed, accel);
244 if (cooked && plot_name) {
245 struct cc_perioddata *speed;
249 plspage(PLOT_DPI, PLOT_DPI, 8 * PLOT_DPI, 8 * PLOT_DPI, 0, 0);
250 plscolbg(0xff, 0xff, 0xff);
253 speed = merge_data(&cooked->accel_speed, &cooked->pres_speed, apogee);
255 plot_perioddata(&cooked->pres_pos, "meters", "Height", -1e10, 1e10);
256 plot_perioddata(&cooked->pres_pos, "meters", "Height", boost_start, apogee);
257 plot_perioddata(speed, "meters/second", "Speed", -1e10, 1e10);
258 plot_perioddata(speed, "meters/second", "Speed", boost_start, apogee);
259 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration", -1e10, 1e10);
260 plot_perioddata(&cooked->accel_accel, "meters/second²", "Acceleration", boost_start, apogee);
266 cc_flightcooked_free(cooked);
269 static const struct option options[] = {
270 { .name = "summary", .has_arg = 1, .val = 's' },
271 { .name = "detail", .has_arg = 1, .val = 'd' },
272 { .name = "plot", .has_arg = 1, .val = 'p' },
276 static void usage(char *program)
278 fprintf(stderr, "usage: %s [--summary=<summary-file>] [--detail=<detail-file] [--plot=<plot-file>] {flight-log} ...\n", program);
283 main (int argc, char **argv)
290 struct cc_flightraw *raw;
294 char *summary_name = NULL, *detail_name = NULL;
295 char *plot_name = NULL;
297 while ((c = getopt_long(argc, argv, "s:d:p:", options, NULL)) != -1) {
300 summary_name = optarg;
303 detail_name = optarg;
313 summary_file = stdout;
316 summary_file = fopen(summary_name, "w");
318 perror (summary_name);
323 if (summary_name && !strcmp (summary_name, detail_name))
324 detail_file = summary_file;
326 detail_file = fopen(detail_name, "w");
333 for (i = optind; i < argc; i++) {
334 file = fopen(argv[i], "r");
340 s = strstr(argv[i], "-serial-");
342 serial = atoi(s + 8);
345 raw = cc_log_read(file);
352 raw->serial = serial;
353 analyse_flight(raw, summary_file, detail_file, plot_name);
354 cc_flightraw_free(raw);