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