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.
30 static const char *state_names[] = {
44 analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
49 double boost_start, boost_stop;
52 int pres_i, accel_i, speed_i;
53 int boost_start_set = 0;
54 int boost_stop_set = 0;
55 enum ao_flight_state state;
56 double state_start, state_stop;
57 struct cc_flightcooked *cooked;
60 fprintf(summary_file, "Flight: %9d\nSerial: %9d\n",
61 f->flight, f->serial);
62 boost_start = f->accel.data[0].time;
63 boost_stop = f->accel.data[f->accel.num-1].time;
64 for (i = 0; i < f->state.num; i++) {
65 if (f->state.data[i].value == ao_flight_boost && !boost_start_set) {
66 boost_start = f->state.data[i].time;
69 if (f->state.data[i].value > ao_flight_boost && !boost_stop_set) {
70 boost_stop = f->state.data[i].time;
75 pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
76 f->pres.data[f->pres.num-1].time);
79 min_pres = f->pres.data[pres_i].value;
80 height = cc_barometer_to_altitude(min_pres) -
81 cc_barometer_to_altitude(f->ground_pres);
82 fprintf(summary_file, "Max height: %9.2fm %9.2fft %9.2fs\n",
83 height, height * 100 / 2.54 / 12,
84 (f->pres.data[pres_i].time - boost_start) / 100.0);
85 apogee = f->pres.data[pres_i].time;
88 cooked = cc_flight_cook(f);
90 speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
92 speed = cooked->accel_speed.data[speed_i];
93 fprintf(summary_file, "Max speed: %9.2fm/s %9.2fft/s %9.2fs\n",
94 speed, speed * 100 / 2.4 / 12.0,
95 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
98 accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
101 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
103 fprintf(summary_file, "Max accel: %9.2fm/s² %9.2fg %9.2fs\n",
104 accel, accel / 9.80665,
105 (f->accel.data[accel_i].time - boost_start) / 100.0);
108 for (i = 0; i < f->state.num; i++) {
109 state = f->state.data[i].value;
110 state_start = f->state.data[i].time;
111 while (i < f->state.num - 1 && f->state.data[i+1].value == state)
113 if (i < f->state.num - 1)
114 state_stop = f->state.data[i + 1].time;
116 state_stop = f->accel.data[f->accel.num-1].time;
117 fprintf(summary_file, "State: %s\n", state_names[state]);
118 fprintf(summary_file, "\tStart: %9.2fs\n", (state_start - boost_start) / 100.0);
119 fprintf(summary_file, "\tDuration: %9.2fs\n", (state_stop - state_start) / 100.0);
120 accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
123 accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
125 fprintf(summary_file, "\tMax accel: %9.2fm/s² %9.2fg %9.2fs\n",
126 accel, accel / 9.80665,
127 (f->accel.data[accel_i].time - boost_start) / 100.0);
131 if (state_start < apogee) {
132 speed_i = cc_perioddata_max(&cooked->accel_speed, state_start, state_stop);
134 speed = cooked->accel_speed.data[speed_i];
136 speed_i = cc_perioddata_max(&cooked->pres_speed, state_start, state_stop);
138 speed = cooked->pres_speed.data[speed_i];
141 fprintf(summary_file, "\tMax speed: %9.2fm/s %9.2fft/s %9.2fs\n",
142 speed, speed * 100 / 2.4 / 12.0,
143 (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
145 pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
148 min_pres = f->pres.data[pres_i].value;
149 height = cc_barometer_to_altitude(min_pres) -
150 cc_barometer_to_altitude(f->ground_pres);
151 fprintf(summary_file, "\tMax height: %9.2fm %9.2fft %9.2fs\n",
152 height, height * 100 / 2.54 / 12,
153 (f->pres.data[pres_i].time - boost_start) / 100.0);
156 if (cooked && detail_file) {
158 double max_height = 0;
161 for (i = 0; i < cooked->pres_pos.num; i++) {
162 if (cooked->pres_pos.data[i] > max_height) {
163 max_height = cooked->pres_pos.data[i];
164 apogee_time = cooked->pres_pos.start + cooked->pres_pos.step * i;
167 fprintf(detail_file, "%9s %9s %9s %9s\n",
168 "time", "height", "speed", "accel");
169 for (i = 0; i < cooked->pres_pos.num; i++) {
170 double time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
171 double accel = cooked->accel_accel.data[i];
172 double pos = cooked->pres_pos.data[i];
174 if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee_time)
175 speed = cooked->accel_speed.data[i];
177 speed = cooked->pres_speed.data[i];
178 fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
179 time, pos, speed, accel);
184 static const struct option options[] = {
185 { .name = "summary", .has_arg = 1, .val = 'S' },
186 { .name = "detail", .has_arg = 1, .val = 'D' },
190 static void usage(char *program)
192 fprintf(stderr, "usage: %s [--summary=<summary-file>] [--detail=<detail-file] {flight-log} ...\n", program);
197 main (int argc, char **argv)
204 struct cc_flightraw *raw;
208 char *summary_name = NULL, *detail_name = NULL;
210 while ((c = getopt_long(argc, argv, "S:D:", options, NULL)) != -1) {
213 summary_name = optarg;
216 detail_name = optarg;
223 summary_file = stdout;
226 summary_file = fopen(summary_name, "w");
228 perror (summary_name);
233 if (!strcmp (summary_name, detail_name))
234 detail_file = summary_file;
236 detail_file = fopen(detail_name, "w");
243 for (i = optind; i < argc; i++) {
244 file = fopen(argv[i], "r");
250 s = strstr(argv[i], "-serial-");
252 serial = atoi(s + 8);
255 raw = cc_log_read(file);
262 raw->serial = serial;
263 analyse_flight(raw, summary_file, detail_file);
264 cc_flightraw_free(raw);