altos: Require sequencing through 'main' state before landing
[fw/altos] / src / ao_flight_test.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #define _GNU_SOURCE
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <getopt.h>
25 #include <math.h>
26
27 #define AO_HERTZ        100
28
29 #define AO_ADC_RING     64
30 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
31 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
32
33 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
34 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
35 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
36
37 /*
38  * One set of samples read from the A/D converter
39  */
40 struct ao_adc {
41         uint16_t        tick;           /* tick when the sample was read */
42         int16_t         accel;          /* accelerometer */
43         int16_t         pres;           /* pressure sensor */
44         int16_t         pres_real;      /* unclipped */
45         int16_t         temp;           /* temperature sensor */
46         int16_t         v_batt;         /* battery voltage */
47         int16_t         sense_d;        /* drogue continuity sense */
48         int16_t         sense_m;        /* main continuity sense */
49 };
50
51 #define __pdata
52 #define __data
53 #define __xdata
54 #define __code
55 #define __reentrant
56
57 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
58 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
59 #define from_fix(x)     ((x) >> 16)
60
61 /*
62  * Above this height, the baro sensor doesn't work
63  */
64 #define AO_MAX_BARO_HEIGHT      12000
65 #define AO_BARO_SATURATE        13000
66 #define AO_MIN_BARO_VALUE       ao_altitude_to_pres(AO_BARO_SATURATE)
67
68 /*
69  * Above this speed, baro measurements are unreliable
70  */
71 #define AO_MAX_BARO_SPEED       200
72
73 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
74
75 enum ao_flight_state {
76         ao_flight_startup = 0,
77         ao_flight_idle = 1,
78         ao_flight_pad = 2,
79         ao_flight_boost = 3,
80         ao_flight_fast = 4,
81         ao_flight_coast = 5,
82         ao_flight_drogue = 6,
83         ao_flight_main = 7,
84         ao_flight_landed = 8,
85         ao_flight_invalid = 9
86 };
87
88 extern enum ao_flight_state ao_flight_state;
89
90 #define FALSE 0
91 #define TRUE 1
92
93 struct ao_adc ao_adc_ring[AO_ADC_RING];
94 uint8_t ao_adc_head;
95 int     ao_summary = 0;
96
97 #define ao_led_on(l)
98 #define ao_led_off(l)
99 #define ao_timer_set_adc_interval(i)
100 #define ao_wakeup(wchan) ao_dump_state()
101 #define ao_cmd_register(c)
102 #define ao_usb_disable()
103 #define ao_telemetry_set_interval(x)
104 #define ao_rdf_set(rdf)
105 #define ao_packet_slave_start()
106 #define ao_packet_slave_stop()
107
108 enum ao_igniter {
109         ao_igniter_drogue = 0,
110         ao_igniter_main = 1
111 };
112
113 struct ao_adc ao_adc_static;
114
115 int     drogue_height;
116 double  drogue_time;
117 int     main_height;
118 double  main_time;
119
120 int     tick_offset;
121
122 static int32_t  ao_k_height;
123
124 void
125 ao_ignite(enum ao_igniter igniter)
126 {
127         double time = (double) (ao_adc_static.tick + tick_offset) / 100;
128
129         if (igniter == ao_igniter_drogue) {
130                 drogue_time = time;
131                 drogue_height = ao_k_height >> 16;
132         } else {
133                 main_time = time;
134                 main_height = ao_k_height >> 16;
135         }
136 }
137
138 struct ao_task {
139         int dummy;
140 };
141
142 #define ao_add_task(t,f,n)
143
144 #define ao_log_start()
145 #define ao_log_stop()
146
147 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
148 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
149
150 #define AO_FLIGHT_TEST
151
152 int     ao_flight_debug;
153
154 FILE *emulator_in;
155 char *emulator_app;
156 char *emulator_name;
157 double emulator_error_max = 4;
158 double emulator_height_error_max = 20;  /* noise in the baro sensor */
159
160 void
161 ao_dump_state(void);
162
163 void
164 ao_sleep(void *wchan);
165
166 const char const * const ao_state_names[] = {
167         "startup", "idle", "pad", "boost", "fast",
168         "coast", "drogue", "main", "landed", "invalid"
169 };
170
171 struct ao_cmds {
172         void            (*func)(void);
173         const char      *help;
174 };
175
176 #include "ao_convert.c"
177
178 struct ao_config {
179         uint16_t        main_deploy;
180         int16_t         accel_plus_g;
181         int16_t         accel_minus_g;
182 };
183
184 #define ao_config_get()
185
186 struct ao_config ao_config;
187
188 #define DATA_TO_XDATA(x) (x)
189
190 #define HAS_FLIGHT 1
191 #define HAS_ADC 1
192 #define HAS_USB 1
193 #define HAS_GPS 1
194 #ifndef HAS_ACCEL
195 #define HAS_ACCEL 1
196 #define HAS_ACCEL_REF 0
197 #endif
198
199 #define GRAVITY 9.80665
200 extern int16_t ao_ground_accel, ao_flight_accel;
201 extern int16_t ao_accel_2g;
202
203 extern uint16_t ao_sample_tick;
204
205 extern int16_t  ao_sample_height;
206 extern int16_t  ao_sample_accel;
207 extern int32_t  ao_accel_scale;
208 extern int16_t  ao_ground_height;
209 extern int16_t  ao_sample_alt;
210
211 int ao_sample_prev_tick;
212 uint16_t        prev_tick;
213
214 #include "ao_kalman.c"
215 #include "ao_sample.c"
216 #include "ao_flight.c"
217
218 #define to_double(f)    ((f) / 65536.0)
219
220 static int      ao_records_read = 0;
221 static int      ao_eof_read = 0;
222 static int      ao_flight_ground_accel;
223 static int      ao_flight_started = 0;
224 static int      ao_test_max_height;
225 static double   ao_test_max_height_time;
226 static int      ao_test_main_height;
227 static double   ao_test_main_height_time;
228 static double   ao_test_landed_time;
229 static double   ao_test_landed_height;
230 static double   ao_test_landed_time;
231 static int      landed_set;
232 static double   landed_time;
233 static double   landed_height;
234
235 void
236 ao_test_exit(void)
237 {
238         double  drogue_error;
239         double  main_error;
240         double  landed_error;
241         double  landed_time_error;
242
243         if (!ao_test_main_height_time) {
244                 ao_test_main_height_time = ao_test_max_height_time;
245                 ao_test_main_height = ao_test_max_height;
246         }
247         drogue_error = fabs(ao_test_max_height_time - drogue_time);
248         main_error = fabs(ao_test_main_height_time - main_time);
249         landed_error = fabs(ao_test_landed_height - landed_height);
250         landed_time_error = ao_test_landed_time - landed_time;
251         if (drogue_error > emulator_error_max || main_error > emulator_error_max ||
252             landed_time_error > emulator_error_max || landed_error > emulator_height_error_max) {
253                 printf ("%s %s\n",
254                         emulator_app, emulator_name);
255                 printf ("\tApogee error %g\n", drogue_error);
256                 printf ("\tMain error %g\n", main_error);
257                 printf ("\tLanded height error %g\n", landed_error);
258                 printf ("\tLanded time error %g\n", landed_time_error);
259                 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
260                         ao_test_max_height, ao_test_max_height_time,
261                         ao_test_main_height, ao_test_main_height_time,
262                         ao_test_landed_height, ao_test_landed_time);
263                 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
264                         drogue_height, drogue_time, main_height, main_time,
265                         landed_height, landed_time);
266                 exit (1);
267         }
268         exit(0);
269 }
270
271 void
272 ao_insert(void)
273 {
274         double  time;
275
276         ao_adc_ring[ao_adc_head] = ao_adc_static;
277         ao_adc_head = ao_adc_ring_next(ao_adc_head);
278         if (ao_flight_state != ao_flight_startup) {
279                 double  height = ao_pres_to_altitude(ao_adc_static.pres_real) - ao_ground_height;
280                 double  accel = ((ao_flight_ground_accel - ao_adc_static.accel) * GRAVITY * 2.0) /
281                         (ao_config.accel_minus_g - ao_config.accel_plus_g);
282
283                 if (!tick_offset)
284                         tick_offset = -ao_adc_static.tick;
285                 if ((prev_tick - ao_adc_static.tick) > 0x400)
286                         tick_offset += 65536;
287                 prev_tick = ao_adc_static.tick;
288                 time = (double) (ao_adc_static.tick + tick_offset) / 100;
289
290                 if (ao_test_max_height < height) {
291                         ao_test_max_height = height;
292                         ao_test_max_height_time = time;
293                         ao_test_landed_height = height;
294                         ao_test_landed_time = time;
295                 }
296                 if (height > ao_config.main_deploy) {
297                         ao_test_main_height_time = time;
298                         ao_test_main_height = height;
299                 }
300
301                 if (ao_test_landed_height > height) {
302                         ao_test_landed_height = height;
303                         ao_test_landed_time = time;
304                 }
305
306                 if (ao_flight_state == ao_flight_landed && !landed_set) {
307                         landed_set = 1;
308                         landed_time = time;
309                         landed_height = height;
310                 }
311
312                 if (!ao_summary) {
313                         printf("%7.2f height %8.2f accel %8.3f state %-8.8s k_height %8.2f k_speed %8.3f k_accel %8.3f avg_height %5d drogue %4d main %4d error %5d\n",
314                                time,
315                                height,
316                                accel,
317                                ao_state_names[ao_flight_state],
318                                ao_k_height / 65536.0,
319                                ao_k_speed / 65536.0 / 16.0,
320                                ao_k_accel / 65536.0 / 16.0,
321                                ao_avg_height,
322                                drogue_height,
323                                main_height,
324                                ao_error_h_sq_avg);
325                         
326 //                      if (ao_flight_state == ao_flight_landed)
327 //                              ao_test_exit();
328                 }
329         }
330 }
331
332 #define AO_MAX_CALLSIGN                 8
333 #define AO_MAX_VERSION                  8
334 #define AO_MAX_TELEMETRY                128
335
336 struct ao_telemetry_generic {
337         uint16_t        serial;         /* 0 */
338         uint16_t        tick;           /* 2 */
339         uint8_t         type;           /* 4 */
340         uint8_t         payload[27];    /* 5 */
341         /* 32 */
342 };
343
344 #define AO_TELEMETRY_SENSOR_TELEMETRUM  0x01
345 #define AO_TELEMETRY_SENSOR_TELEMINI    0x02
346 #define AO_TELEMETRY_SENSOR_TELENANO    0x03
347
348 struct ao_telemetry_sensor {
349         uint16_t        serial;         /*  0 */
350         uint16_t        tick;           /*  2 */
351         uint8_t         type;           /*  4 */
352
353         uint8_t         state;          /*  5 flight state */
354         int16_t         accel;          /*  6 accelerometer (TM only) */
355         int16_t         pres;           /*  8 pressure sensor */
356         int16_t         temp;           /* 10 temperature sensor */
357         int16_t         v_batt;         /* 12 battery voltage */
358         int16_t         sense_d;        /* 14 drogue continuity sense (TM/Tm) */
359         int16_t         sense_m;        /* 16 main continuity sense (TM/Tm) */
360
361         int16_t         acceleration;   /* 18 m/s² * 16 */
362         int16_t         speed;          /* 20 m/s * 16 */
363         int16_t         height;         /* 22 m */
364
365         int16_t         ground_pres;    /* 24 average pres on pad */
366         int16_t         ground_accel;   /* 26 average accel on pad */
367         int16_t         accel_plus_g;   /* 28 accel calibration at +1g */
368         int16_t         accel_minus_g;  /* 30 accel calibration at -1g */
369         /* 32 */
370 };
371
372 #define AO_TELEMETRY_CONFIGURATION      0x04
373
374 struct ao_telemetry_configuration {
375         uint16_t        serial;                         /*  0 */
376         uint16_t        tick;                           /*  2 */
377         uint8_t         type;                           /*  4 */
378
379         uint8_t         device;                         /*  5 device type */
380         uint16_t        flight;                         /*  6 flight number */
381         uint8_t         config_major;                   /*  8 Config major version */
382         uint8_t         config_minor;                   /*  9 Config minor version */
383         uint16_t        apogee_delay;                   /* 10 Apogee deploy delay in seconds */
384         uint16_t        main_deploy;                    /* 12 Main deploy alt in meters */
385         uint16_t        flight_log_max;                 /* 14 Maximum flight log size in kB */
386         char            callsign[AO_MAX_CALLSIGN];      /* 16 Radio operator identity */
387         char            version[AO_MAX_VERSION];        /* 24 Software version */
388         /* 32 */
389 };
390
391 #define AO_TELEMETRY_LOCATION           0x05
392
393 #define AO_GPS_MODE_NOT_VALID           'N'
394 #define AO_GPS_MODE_AUTONOMOUS          'A'
395 #define AO_GPS_MODE_DIFFERENTIAL        'D'
396 #define AO_GPS_MODE_ESTIMATED           'E'
397 #define AO_GPS_MODE_MANUAL              'M'
398 #define AO_GPS_MODE_SIMULATED           'S'
399
400 struct ao_telemetry_location {
401         uint16_t        serial;         /*  0 */
402         uint16_t        tick;           /*  2 */
403         uint8_t         type;           /*  4 */
404
405         uint8_t         flags;          /*  5 Number of sats and other flags */
406         int16_t         altitude;       /*  6 GPS reported altitude (m) */
407         int32_t         latitude;       /*  8 latitude (degrees * 10⁷) */
408         int32_t         longitude;      /* 12 longitude (degrees * 10⁷) */
409         uint8_t         year;           /* 16 (- 2000) */
410         uint8_t         month;          /* 17 (1-12) */
411         uint8_t         day;            /* 18 (1-31) */
412         uint8_t         hour;           /* 19 (0-23) */
413         uint8_t         minute;         /* 20 (0-59) */
414         uint8_t         second;         /* 21 (0-59) */
415         uint8_t         pdop;           /* 22 (m * 5) */
416         uint8_t         hdop;           /* 23 (m * 5) */
417         uint8_t         vdop;           /* 24 (m * 5) */
418         uint8_t         mode;           /* 25 */
419         uint16_t        ground_speed;   /* 26 cm/s */
420         int16_t         climb_rate;     /* 28 cm/s */
421         uint8_t         course;         /* 30 degrees / 2 */
422         uint8_t         unused[1];      /* 31 */
423         /* 32 */
424 };
425
426 #define AO_TELEMETRY_SATELLITE          0x06
427
428 struct ao_telemetry_satellite_info {
429         uint8_t         svid;
430         uint8_t         c_n_1;
431 };
432
433 struct ao_telemetry_satellite {
434         uint16_t                                serial;         /*  0 */
435         uint16_t                                tick;           /*  2 */
436         uint8_t                                 type;           /*  4 */
437         uint8_t                                 channels;       /*  5 number of reported sats */
438
439         struct ao_telemetry_satellite_info      sats[12];       /* 6 */
440         uint8_t                                 unused[2];      /* 30 */
441         /* 32 */
442 };
443
444 union ao_telemetry_all {
445         struct ao_telemetry_generic             generic;
446         struct ao_telemetry_sensor              sensor;
447         struct ao_telemetry_configuration       configuration;
448         struct ao_telemetry_location            location;
449         struct ao_telemetry_satellite           satellite;
450 };
451
452 uint16_t
453 uint16(uint8_t *bytes, int off)
454 {
455         off++;
456         return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
457 }
458
459 int16_t
460 int16(uint8_t *bytes, int off)
461 {
462         return (int16_t) uint16(bytes, off);
463 }
464
465 void
466 ao_sleep(void *wchan)
467 {
468         if (wchan == &ao_adc_head) {
469                 char            type;
470                 uint16_t        tick;
471                 uint16_t        a, b;
472                 int             ret;
473                 uint8_t         bytes[1024];
474                 union ao_telemetry_all  telem;
475                 char            line[1024];
476                 char            *saveptr;
477                 char            *l;
478                 char            *words[64];
479                 int             nword;
480
481                 for (;;) {
482                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
483                         {
484                                 ao_adc_static.accel = ao_flight_ground_accel;
485                                 ao_insert();
486                                 return;
487                         }
488
489                         if (!fgets(line, sizeof (line), emulator_in)) {
490                                 if (++ao_eof_read >= 1000) {
491                                         if (!ao_summary)
492                                                 printf ("no more data, exiting simulation\n");
493                                         ao_test_exit();
494                                 }
495                                 ao_adc_static.tick += 10;
496                                 ao_insert();
497                                 return;
498                         }
499                         l = line;
500                         for (nword = 0; nword < 64; nword++) {
501                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
502                                 l = NULL;
503                                 if (words[nword] == NULL)
504                                         break;
505                         }
506                         if (nword == 4) {
507                                 type = words[0][0];
508                                 tick = strtoul(words[1], NULL, 16);
509                                 a = strtoul(words[2], NULL, 16);
510                                 b = strtoul(words[3], NULL, 16);
511                                 if (type == 'P')
512                                         type = 'A';
513                         } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
514                                 ao_config.accel_plus_g = atoi(words[3]);
515                                 ao_config.accel_minus_g = atoi(words[5]);
516                         } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
517                                 ao_config.main_deploy = atoi(words[2]);
518                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
519                                 tick = atoi(words[10]);
520                                 if (!ao_flight_started) {
521                                         type = 'F';
522                                         a = atoi(words[26]);
523                                         ao_flight_started = 1;
524                                 } else {
525                                         type = 'A';
526                                         a = atoi(words[12]);
527                                         b = atoi(words[14]);
528                                 }
529                         } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
530                                 tick = strtol(words[1], NULL, 16);
531                                 a = 16384 - 328;
532                                 b = strtol(words[2], NULL, 10);
533                                 type = 'A';
534                                 if (!ao_flight_started) {
535                                         ao_flight_ground_accel = 16384 - 328;
536                                         ao_config.accel_plus_g = 16384 - 328;
537                                         ao_config.accel_minus_g = 16384 + 328;
538                                         ao_flight_started = 1;
539                                 }
540                         } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
541                                 char    *hex = words[1];
542                                 char    elt[3];
543                                 int     i, len;
544                                 uint8_t sum;
545
546                                 len = strlen(hex);
547                                 if (len > sizeof (bytes) * 2) {
548                                         len = sizeof (bytes)*2;
549                                         hex[len] = '\0';
550                                 }
551                                 for (i = 0; i < len; i += 2) {
552                                         elt[0] = hex[i];
553                                         elt[1] = hex[i+1];
554                                         elt[2] = '\0';
555                                         bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
556                                 }
557                                 len = i/2;
558                                 if (bytes[0] != len - 2) {
559                                         printf ("bad length %d != %d\n", bytes[0], len - 2);
560                                         continue;
561                                 }
562                                 sum = 0x5a;
563                                 for (i = 1; i < len-1; i++)
564                                         sum += bytes[i];
565                                 if (sum != bytes[len-1]) {
566                                         printf ("bad checksum\n");
567                                         continue;
568                                 }
569                                 if ((bytes[len-2] & 0x80) == 0) {
570                                         continue;
571                                 }
572                                 if (len == 36) {
573                                         memcpy(&telem, bytes + 1, 32);
574                                         tick = telem.generic.tick;
575                                         switch (telem.generic.type) {
576                                         case AO_TELEMETRY_SENSOR_TELEMETRUM:
577                                         case AO_TELEMETRY_SENSOR_TELEMINI:
578                                         case AO_TELEMETRY_SENSOR_TELENANO:
579                                                 if (!ao_flight_started) {
580                                                         ao_flight_ground_accel = telem.sensor.ground_accel;
581                                                         ao_config.accel_plus_g = telem.sensor.accel_plus_g;
582                                                         ao_config.accel_minus_g = telem.sensor.accel_minus_g;
583                                                         ao_flight_started = 1;
584                                                 }
585                                                 type = 'A';
586                                                 a = telem.sensor.accel;
587                                                 b = telem.sensor.pres;
588                                                 break;
589                                         }
590                                 } else if (len == 99) {
591                                         ao_flight_started = 1;
592                                         tick = uint16(bytes, 21);
593                                         ao_flight_ground_accel = int16(bytes, 7);
594                                         ao_config.accel_plus_g = int16(bytes, 17);
595                                         ao_config.accel_minus_g = int16(bytes, 19);
596                                         type = 'A';
597                                         a = int16(bytes, 23);
598                                         b = int16(bytes, 25);
599                                 } else if (len == 98) {
600                                         ao_flight_started = 1;
601                                         tick = uint16(bytes, 20);
602                                         ao_flight_ground_accel = int16(bytes, 6);
603                                         ao_config.accel_plus_g = int16(bytes, 16);
604                                         ao_config.accel_minus_g = int16(bytes, 18);
605                                         type = 'A';
606                                         a = int16(bytes, 22);
607                                         b = int16(bytes, 24);
608                                 } else {
609                                         printf("unknown len %d\n", len);
610                                         continue;
611                                 }
612                         }
613                         if (type != 'F' && !ao_flight_started)
614                                 continue;
615
616                         switch (type) {
617                         case 'F':
618                                 ao_flight_ground_accel = a;
619                                 if (ao_config.accel_plus_g == 0) {
620                                         ao_config.accel_plus_g = a;
621                                         ao_config.accel_minus_g = a + 530;
622                                 }
623                                 if (ao_config.main_deploy == 0)
624                                         ao_config.main_deploy = 250;
625                                 ao_flight_started = 1;
626                                 break;
627                         case 'S':
628                                 break;
629                         case 'A':
630                                 ao_adc_static.tick = tick;
631                                 ao_adc_static.accel = a;
632                                 ao_adc_static.pres_real = b;
633                                 if (b < AO_MIN_BARO_VALUE)
634                                         b = AO_MIN_BARO_VALUE;
635                                 ao_adc_static.pres = b;
636                                 ao_records_read++;
637                                 ao_insert();
638                                 return;
639                         case 'T':
640                                 ao_adc_static.tick = tick;
641                                 ao_adc_static.temp = a;
642                                 ao_adc_static.v_batt = b;
643                                 break;
644                         case 'D':
645                         case 'G':
646                         case 'N':
647                         case 'W':
648                         case 'H':
649                                 break;
650                         }
651                 }
652
653         }
654 }
655 #define COUNTS_PER_G 264.8
656
657 void
658 ao_dump_state(void)
659 {
660 }
661
662 static const struct option options[] = {
663         { .name = "summary", .has_arg = 0, .val = 's' },
664         { .name = "debug", .has_arg = 0, .val = 'd' },
665         { 0, 0, 0, 0},
666 };
667
668 void run_flight_fixed(char *name, FILE *f, int summary)
669 {
670         emulator_name = name;
671         emulator_in = f;
672         ao_summary = summary;
673         ao_flight_init();
674         ao_flight();
675 }
676
677 int
678 main (int argc, char **argv)
679 {
680         int     summary = 0;
681         int     c;
682         int     i;
683
684 #if HAS_ACCEL
685         emulator_app="full";
686 #else
687         emulator_app="baro";
688 #endif
689         while ((c = getopt_long(argc, argv, "sd", options, NULL)) != -1) {
690                 switch (c) {
691                 case 's':
692                         summary = 1;
693                         break;
694                 case 'd':
695                         ao_flight_debug = 1;
696                         break;
697                 }
698         }
699
700         if (optind == argc)
701                 run_flight_fixed("<stdin>", stdin, summary);
702         else
703                 for (i = optind; i < argc; i++) {
704                         FILE    *f = fopen(argv[i], "r");
705                         if (!f) {
706                                 perror(argv[i]);
707                                 continue;
708                         }
709                         run_flight_fixed(argv[i], f, summary);
710                         fclose(f);
711                 }
712 }