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