Add DSP code to filter data, allowing for integration/differentiation
[fw/altos] / ao-tools / ao-postflight / ao-postflight.c
index 9371f35174ed2c8966ee1dff67439d421246d6a8..c5814c93de4e7fe30fccd3a89c832aeb5693d4b1 100644 (file)
 
 #define NUM_BLOCK      512
 
-static const struct option options[] = {
-       { 0, 0, 0, 0},
-};
-
-static void usage(char *program)
-{
-       fprintf(stderr, "usage: %s {flight-log} ...\n", program);
-       exit(1);
-}
-
 static const char *state_names[] = {
        "startup",
        "idle",
@@ -51,20 +41,23 @@ static const char *state_names[] = {
 };
 
 void
-analyse_flight(struct cc_flightraw *f)
+analyse_flight(struct cc_flightraw *f, FILE *summary_file, FILE *detail_file)
 {
        double  height;
        double  accel;
+       double  speed;
        double  boost_start, boost_stop;
        double  min_pres;
        int     i;
-       int     pres_i, accel_i;
+       int     pres_i, accel_i, speed_i;
        int     boost_start_set = 0;
        int     boost_stop_set = 0;
        enum ao_flight_state    state;
        double  state_start, state_stop;
+       struct cc_flightcooked *cooked;
+       double  apogee;
 
-       printf ("Flight:  %9d\nSerial:  %9d\n",
+       fprintf(summary_file, "Flight:  %9d\nSerial:  %9d\n",
                f->flight, f->serial);
        boost_start = f->accel.data[0].time;
        boost_stop = f->accel.data[f->accel.num-1].time;
@@ -81,25 +74,37 @@ analyse_flight(struct cc_flightraw *f)
 
        pres_i = cc_timedata_min(&f->pres, f->pres.data[0].time,
                                 f->pres.data[f->pres.num-1].time);
-       if (pres_i)
+       if (pres_i >= 0)
        {
                min_pres = f->pres.data[pres_i].value;
                height = cc_barometer_to_altitude(min_pres) -
                        cc_barometer_to_altitude(f->ground_pres);
-               printf ("Max height: %9.2fm    %9.2fft %9.2fs\n",
+               fprintf(summary_file, "Max height: %9.2fm    %9.2fft   %9.2fs\n",
                        height, height * 100 / 2.54 / 12,
                        (f->pres.data[pres_i].time - boost_start) / 100.0);
+               apogee = f->pres.data[pres_i].time;
        }
 
+       cooked = cc_flight_cook(f);
+       if (cooked) {
+               speed_i = cc_perioddata_max(&cooked->accel_speed, boost_start, boost_stop);
+               if (speed_i >= 0) {
+                       speed = cooked->accel_speed.data[speed_i];
+                       fprintf(summary_file, "Max speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
+                              speed, speed * 100 / 2.4 / 12.0,
+                              (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
+               }
+       }
        accel_i = cc_timedata_min(&f->accel, boost_start, boost_stop);
-       if (accel_i)
+       if (accel_i >= 0)
        {
                accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
                                                         f->ground_accel);
-               printf ("Max accel:  %9.2fm/s² %9.2fg  %9.2fs\n",
+               fprintf(summary_file, "Max accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
                        accel, accel /  9.80665,
                        (f->accel.data[accel_i].time - boost_start) / 100.0);
        }
+
        for (i = 0; i < f->state.num; i++) {
                state = f->state.data[i].value;
                state_start = f->state.data[i].time;
@@ -109,50 +114,132 @@ analyse_flight(struct cc_flightraw *f)
                        state_stop = f->state.data[i + 1].time;
                else
                        state_stop = f->accel.data[f->accel.num-1].time;
-               printf("State: %s\n", state_names[state]);
-               printf("\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
-               printf("\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
+               fprintf(summary_file, "State: %s\n", state_names[state]);
+               fprintf(summary_file, "\tStart:      %9.2fs\n", (state_start - boost_start) / 100.0);
+               fprintf(summary_file, "\tDuration:   %9.2fs\n", (state_stop - state_start) / 100.0);
                accel_i = cc_timedata_min(&f->accel, state_start, state_stop);
                if (accel_i >= 0)
                {
                        accel = cc_accelerometer_to_acceleration(f->accel.data[accel_i].value,
                                                                 f->ground_accel);
-                       printf("\tMax accel:  %9.2fm/s² %9.2fg  %9.2fs\n",
+                       fprintf(summary_file, "\tMax accel:  %9.2fm/s² %9.2fg    %9.2fs\n",
                               accel, accel / 9.80665,
                               (f->accel.data[accel_i].time - boost_start) / 100.0);
                }
 
+               if (cooked) {
+                       if (state_start < apogee) {
+                               speed_i = cc_perioddata_max(&cooked->accel_speed, state_start, state_stop);
+                               if (speed_i >= 0)
+                                       speed = cooked->accel_speed.data[speed_i];
+                       } else {
+                               speed_i = cc_perioddata_max(&cooked->pres_speed, state_start, state_stop);
+                               if (speed_i >= 0)
+                                       speed = cooked->pres_speed.data[speed_i];
+                       }
+                       if (speed_i >= 0)
+                               fprintf(summary_file, "\tMax speed:  %9.2fm/s  %9.2fft/s %9.2fs\n",
+                                      speed, speed * 100 / 2.4 / 12.0,
+                                      (cooked->accel_speed.start + speed_i * cooked->accel_speed.step - boost_start) / 100.0);
+               }
                pres_i = cc_timedata_min(&f->pres, state_start, state_stop);
                if (pres_i >= 0)
                {
                        min_pres = f->pres.data[pres_i].value;
                        height = cc_barometer_to_altitude(min_pres) -
                                cc_barometer_to_altitude(f->ground_pres);
-                       printf ("\tMax height: %9.2fm    %9.2fft %9.2fs\n",
+                       fprintf(summary_file, "\tMax height: %9.2fm    %9.2fft   %9.2fs\n",
                                height, height * 100 / 2.54 / 12,
                                (f->pres.data[pres_i].time - boost_start) / 100.0);
                }
        }
+       if (cooked && detail_file) {
+               double  apogee_time;
+               double  max_height = 0;
+               int     i;
+
+               for (i = 0; i < cooked->pres_pos.num; i++) {
+                       if (cooked->pres_pos.data[i] > max_height) {
+                               max_height = cooked->pres_pos.data[i];
+                               apogee_time = cooked->pres_pos.start + cooked->pres_pos.step * i;
+                       }
+               }
+               fprintf(detail_file, "%9s %9s %9s %9s\n",
+                      "time", "height", "speed", "accel");
+               for (i = 0; i < cooked->pres_pos.num; i++) {
+                       double  time = (cooked->accel_accel.start + i * cooked->accel_accel.step - boost_start) / 100.0;
+                       double  accel = cooked->accel_accel.data[i];
+                       double  pos = cooked->pres_pos.data[i];
+                       double  speed;
+                       if (cooked->pres_pos.start + cooked->pres_pos.step * i < apogee_time)
+                               speed = cooked->accel_speed.data[i];
+                       else
+                               speed = cooked->pres_speed.data[i];
+                       fprintf(detail_file, "%9.2f %9.2f %9.2f %9.2f\n",
+                              time, pos, speed, accel);
+               }
+       }
+}
+
+static const struct option options[] = {
+       { .name = "summary", .has_arg = 1, .val = 'S' },
+       { .name = "detail", .has_arg = 1, .val = 'D' },
+       { 0, 0, 0, 0},
+};
+
+static void usage(char *program)
+{
+       fprintf(stderr, "usage: %s [--summary=<summary-file>] [--detail=<detail-file] {flight-log} ...\n", program);
+       exit(1);
 }
 
 int
 main (int argc, char **argv)
 {
        FILE                    *file;
+       FILE                    *summary_file;
+       FILE                    *detail_file;
        int                     i;
        int                     ret = 0;
        struct cc_flightraw     *raw;
        int                     c;
        int                     serial;
        char                    *s;
+       char                    *summary_name, *detail_name;
 
-       while ((c = getopt_long(argc, argv, "", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "S:D:", options, NULL)) != -1) {
                switch (c) {
+               case 'S':
+                       summary_name = optarg;
+                       break;
+               case 'D':
+                       detail_name = optarg;
+                       break;
                default:
                        usage(argv[0]);
                        break;
                }
        }
+       summary_file = stdout;
+       detail_file = NULL;
+       if (summary_name) {
+               summary_file = fopen(summary_name, "w");
+               if (!summary_file) {
+                       perror (summary_name);
+                       exit(1);
+               }
+       }
+       if (detail_name) {
+               if (!strcmp (summary_name, detail_name))
+                       detail_file = summary_file;
+               else {
+                       detail_file = fopen(detail_name, "w");
+                       if (!detail_file) {
+                               perror(detail_name);
+                               exit(1);
+                       }
+               }
+       }
        for (i = optind; i < argc; i++) {
                file = fopen(argv[i], "r");
                if (!file) {
@@ -173,7 +260,7 @@ main (int argc, char **argv)
                }
                if (!raw->serial)
                        raw->serial = serial;
-               analyse_flight(raw);
+               analyse_flight(raw, summary_file, detail_file);
                cc_flightraw_free(raw);
        }
        return ret;