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