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