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