altos/test: Adapt flight test to int16_t flight number type
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stddef.h>
25 #include <string.h>
26 #include <getopt.h>
27 #include <math.h>
28 #define log ao_log_data
29
30 #define GRAVITY 9.80665
31
32 #define AO_HERTZ        100
33
34 #define HAS_ADC 1
35 #define AO_DATA_RING    64
36 #define ao_data_ring_next(n)    (((n) + 1) & (AO_DATA_RING - 1))
37 #define ao_data_ring_prev(n)    (((n) - 1) & (AO_DATA_RING - 1))
38
39 #if 0
40 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
41 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
42 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
43 #endif
44
45 #define AO_GPS_NEW_DATA         1
46 #define AO_GPS_NEW_TRACKING     2
47
48 int ao_gps_new;
49
50 #if !defined(TELEMEGA) && !defined(TELEMETRUM_V2) && !defined(EASYMINI)
51 #define TELEMETRUM_V1 1
52 #endif
53
54 #if TELEMEGA
55 #define AO_ADC_NUM_SENSE        6
56 #define HAS_MS5607              1
57 #define HAS_MPU6000             1
58 #define HAS_MMA655X             1
59 #define HAS_HMC5883             1
60 #define HAS_BEEP                1
61 #define AO_CONFIG_MAX_SIZE      1024
62 #define AO_MMA655X_INVERT       0
63
64 struct ao_adc {
65         int16_t                 sense[AO_ADC_NUM_SENSE];
66         int16_t                 v_batt;
67         int16_t                 v_pbatt;
68         int16_t                 temp;
69 };
70 #endif
71
72 #if TELEMETRUM_V2
73 #define AO_ADC_NUM_SENSE        2
74 #define HAS_MS5607              1
75 #define HAS_MMA655X             1
76 #define AO_MMA655X_INVERT       0
77 #define HAS_BEEP                1
78 #define AO_CONFIG_MAX_SIZE      1024
79
80 struct ao_adc {
81         int16_t                 sense_a;
82         int16_t                 sense_m;
83         int16_t                 v_batt;
84         int16_t                 temp;
85 };
86 #endif
87
88 #if EASYMINI
89 #define AO_ADC_NUM_SENSE        2
90 #define HAS_MS5607              1
91 #define HAS_BEEP                1
92 #define AO_CONFIG_MAX_SIZE      1024
93
94 struct ao_adc {
95         int16_t                 sense_a;
96         int16_t                 sense_m;
97         int16_t                 v_batt;
98 };
99 #endif
100
101 #if TELEMETRUM_V1
102 /*
103  * One set of samples read from the A/D converter
104  */
105 struct ao_adc {
106         int16_t         accel;          /* accelerometer */
107         int16_t         pres;           /* pressure sensor */
108         int16_t         pres_real;      /* unclipped */
109         int16_t         temp;           /* temperature sensor */
110         int16_t         v_batt;         /* battery voltage */
111         int16_t         sense_d;        /* drogue continuity sense */
112         int16_t         sense_m;        /* main continuity sense */
113 };
114
115 #ifndef HAS_ACCEL
116 #define HAS_ACCEL 1
117 #define HAS_ACCEL_REF 0
118 #endif
119
120 #endif
121
122 #define __pdata
123 #define __data
124 #define __xdata
125 #define __code
126 #define __reentrant
127
128 #define HAS_FLIGHT 1
129 #define HAS_IGNITE 1
130 #define HAS_USB 1
131 #define HAS_GPS 1
132
133 #include <ao_data.h>
134 #include <ao_log.h>
135 #include <ao_telemetry.h>
136 #include <ao_sample.h>
137
138 #if TELEMEGA
139 int ao_gps_count;
140 struct ao_telemetry_location ao_gps_first;
141 struct ao_telemetry_location ao_gps_prev;
142 struct ao_telemetry_location ao_gps_static;
143
144 struct ao_telemetry_satellite ao_gps_tracking;
145
146 static inline double sqr(double a) { return a * a; }
147
148 void
149 cc_great_circle (double start_lat, double start_lon,
150                  double end_lat, double end_lon,
151                  double *dist, double *bearing)
152 {
153         const double rad = M_PI / 180;
154         const double earth_radius = 6371.2 * 1000;      /* in meters */
155         double lat1 = rad * start_lat;
156         double lon1 = rad * -start_lon;
157         double lat2 = rad * end_lat;
158         double lon2 = rad * -end_lon;
159
160 //      double d_lat = lat2 - lat1;
161         double d_lon = lon2 - lon1;
162
163         /* From http://en.wikipedia.org/wiki/Great-circle_distance */
164         double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
165                           sqr(cos(lat1) * sin(lat2) -
166                               sin(lat1) * cos(lat2) * cos(d_lon)));
167         double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
168         double d = atan2(vdn,vdd);
169         double course;
170
171         if (cos(lat1) < 1e-20) {
172                 if (lat1 > 0)
173                         course = M_PI;
174                 else
175                         course = -M_PI;
176         } else {
177                 if (d < 1e-10)
178                         course = 0;
179                 else
180                         course = acos((sin(lat2)-sin(lat1)*cos(d)) /
181                                       (sin(d)*cos(lat1)));
182                 if (sin(lon2-lon1) > 0)
183                         course = 2 * M_PI-course;
184         }
185         *dist = d * earth_radius;
186         *bearing = course * 180/M_PI;
187 }
188
189 double
190 ao_distance_from_pad(void)
191 {
192         double  dist, bearing;
193         if (!ao_gps_count)
194                 return 0;
195
196         cc_great_circle(ao_gps_first.latitude / 1e7,
197                         ao_gps_first.longitude / 1e7,
198                         ao_gps_static.latitude / 1e7,
199                         ao_gps_static.longitude / 1e7,
200                         &dist, &bearing);
201         return dist;
202 }
203
204 double
205 ao_gps_angle(void)
206 {
207         double  dist, bearing;
208         double  height;
209         double  angle;
210
211         if (ao_gps_count < 2)
212                 return 0;
213
214         cc_great_circle(ao_gps_prev.latitude / 1e7,
215                         ao_gps_prev.longitude / 1e7,
216                         ao_gps_static.latitude / 1e7,
217                         ao_gps_static.longitude / 1e7,
218                         &dist, &bearing);
219         height = AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_static) - AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_prev);
220
221         angle = atan2(dist, height);
222         return angle * 180/M_PI;
223 }
224 #endif
225
226 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
227 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
228 #define from_fix(x)     ((x) >> 16)
229
230 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
231
232 extern enum ao_flight_state ao_flight_state;
233
234 #define FALSE 0
235 #define TRUE 1
236
237 volatile struct ao_data ao_data_ring[AO_DATA_RING];
238 volatile uint8_t ao_data_head;
239 int     ao_summary = 0;
240
241 #define ao_led_on(l)
242 #define ao_led_off(l)
243 #define ao_timer_set_adc_interval(i)
244 #define ao_wakeup(wchan) ao_dump_state()
245 #define ao_cmd_register(c)
246 #define ao_usb_disable()
247 #define ao_telemetry_set_interval(x)
248 #define ao_rdf_set(rdf)
249 #define ao_packet_slave_start()
250 #define ao_packet_slave_stop()
251 #define flush()
252
253 enum ao_igniter {
254         ao_igniter_drogue = 0,
255         ao_igniter_main = 1
256 };
257
258 struct ao_data ao_data_static;
259
260 int     drogue_height;
261 double  drogue_time;
262 int     main_height;
263 double  main_time;
264
265 int     tick_offset;
266
267 static ao_k_t   ao_k_height;
268 static double   simple_speed;
269
270 int16_t
271 ao_time(void)
272 {
273         return ao_data_static.tick;
274 }
275
276 void
277 ao_delay(int16_t interval)
278 {
279         return;
280 }
281
282 void
283 ao_ignite(enum ao_igniter igniter)
284 {
285         double time = (double) (ao_data_static.tick + tick_offset) / 100;
286
287         if (igniter == ao_igniter_drogue) {
288                 drogue_time = time;
289                 drogue_height = ao_k_height >> 16;
290         } else {
291                 main_time = time;
292                 main_height = ao_k_height >> 16;
293         }
294 }
295
296 struct ao_task {
297         int dummy;
298 };
299
300 #define ao_add_task(t,f,n) ((void) (t))
301
302 #define ao_log_start()
303 #define ao_log_stop()
304
305 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
306 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
307
308 #define AO_FLIGHT_TEST
309
310 int     ao_flight_debug;
311
312 struct ao_eeprom        *eeprom;
313 uint32_t                eeprom_offset;
314
315 FILE *emulator_in;
316 char *emulator_app;
317 char *emulator_name;
318 char *emulator_info;
319 double emulator_error_max = 4;
320 double emulator_height_error_max = 20;  /* noise in the baro sensor */
321
322 void
323 ao_dump_state(void);
324
325 void
326 ao_sleep(void *wchan);
327
328 const char * const ao_state_names[] = {
329         "startup", "idle", "pad", "boost", "fast",
330         "coast", "drogue", "main", "landed", "invalid"
331 };
332
333 struct ao_cmds {
334         void            (*func)(void);
335         const char      *help;
336 };
337
338 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
339 #define ao_xmemset(d,v,c) memset(d,v,c)
340 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
341
342 #define AO_NEED_ALTITUDE_TO_PRES 1
343 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
344 #include "ao_convert_pa.c"
345 #include <ao_ms5607.h>
346 struct ao_ms5607_prom   ao_ms5607_prom;
347 #include "ao_ms5607_convert.c"
348 #if TELEMEGA
349 #define AO_PYRO_NUM     4
350 #include <ao_pyro.h>
351 #endif
352 #else
353 #include "ao_convert.c"
354 #endif
355
356 #include <ao_config.h>
357 #include <ao_fake_flight.h>
358 #include <ao_eeprom_read.h>
359 #include <ao_log.h>
360
361 #define ao_config_get()
362
363 struct ao_config ao_config;
364
365 #define DATA_TO_XDATA(x) (x)
366
367
368 extern int16_t ao_ground_accel, ao_flight_accel;
369 extern int16_t ao_accel_2g;
370
371 typedef int16_t accel_t;
372
373 uint16_t        ao_serial_number;
374 int16_t         ao_flight_number;
375
376 extern uint16_t ao_sample_tick;
377
378 extern alt_t    ao_sample_height;
379 extern accel_t  ao_sample_accel;
380 extern int32_t  ao_accel_scale;
381 extern alt_t    ao_ground_height;
382 extern alt_t    ao_sample_alt;
383
384 double ao_sample_qangle;
385
386 int ao_sample_prev_tick;
387 uint16_t        prev_tick;
388
389
390 #include "ao_kalman.c"
391 #include "ao_sqrt.c"
392 #include "ao_sample.c"
393 #include "ao_flight.c"
394 #if TELEMEGA
395 #define AO_PYRO_NUM     4
396
397 #define AO_PYRO_0       0
398 #define AO_PYRO_1       1
399 #define AO_PYRO_2       2
400 #define AO_PYRO_3       3
401
402 #define PYRO_DBG        1
403
404 static void
405 ao_pyro_pin_set(uint8_t pin, uint8_t value)
406 {
407         printf ("set pyro %d %d\n", pin, value);
408 }
409
410 #include "ao_pyro.c"
411 #endif
412 #include "ao_eeprom_read.c"
413 #include "ao_eeprom_read_old.c"
414
415 #define to_double(f)    ((f) / 65536.0)
416
417 static int      ao_records_read = 0;
418 static int      ao_eof_read = 0;
419 #if !EASYMINI
420 static int      ao_flight_ground_accel;
421 #endif
422 static int      ao_flight_started = 0;
423 static int      ao_test_max_height;
424 static double   ao_test_max_height_time;
425 static int      ao_test_main_height;
426 static double   ao_test_main_height_time;
427 static double   ao_test_landed_time;
428 static double   ao_test_landed_height;
429 static double   ao_test_landed_time;
430 static int      landed_set;
431 static double   landed_time;
432 static double   landed_height;
433 #if AO_PYRO_NUM
434 static uint16_t pyros_fired;
435 #endif
436
437 #if HAS_MPU6000
438 static struct ao_mpu6000_sample ao_ground_mpu6000;
439 #endif
440
441 #if HAS_ACCEL
442 int ao_error_h_sq_avg;
443 #endif
444
445 void
446 ao_test_exit(void)
447 {
448         double  drogue_error;
449         double  main_error;
450         double  landed_error;
451         double  landed_time_error;
452
453         if (!ao_test_main_height_time) {
454                 ao_test_main_height_time = ao_test_max_height_time;
455                 ao_test_main_height = ao_test_max_height;
456         }
457         drogue_error = fabs(ao_test_max_height_time - drogue_time);
458         main_error = fabs(ao_test_main_height_time - main_time);
459         landed_error = fabs(ao_test_landed_height - landed_height);
460         landed_time_error = ao_test_landed_time - landed_time;
461         if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
462                 printf ("%s %s\n",
463                         emulator_app, emulator_name);
464                 if (emulator_info)
465                         printf ("\t%s\n", emulator_info);
466                 printf ("\tApogee error %g\n", drogue_error);
467                 printf ("\tMain error %g\n", main_error);
468                 printf ("\tLanded height error %g\n", landed_error);
469                 printf ("\tLanded time error %g\n", landed_time_error);
470                 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
471                         ao_test_max_height, ao_test_max_height_time,
472                         ao_test_main_height, ao_test_main_height_time,
473                         ao_test_landed_height, ao_test_landed_time);
474                 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
475                         drogue_height, drogue_time, main_height, main_time,
476                         landed_height, landed_time);
477                 exit (1);
478         }
479         exit(0);
480 }
481
482 #ifdef TELEMEGA
483 struct ao_azel {
484         int     az;
485         int     el;
486 };
487
488 static void
489 azel (struct ao_azel *r, struct ao_quaternion *q)
490 {
491         double  v;
492
493         r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
494         v = sqrt (q->x*q->x + q->y*q->y);
495         r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
496 }
497 #endif
498
499 void
500 ao_insert(void)
501 {
502         double  time;
503
504         ao_data_ring[ao_data_head] = ao_data_static;
505         if (ao_flight_state != ao_flight_startup) {
506 #if HAS_ACCEL
507                 double  accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
508                         (ao_config.accel_minus_g - ao_config.accel_plus_g);
509 #else
510                 double  accel = 0.0;
511 #endif
512
513                 (void) accel;
514                 if (!tick_offset)
515                         tick_offset = -ao_data_static.tick;
516                 if ((prev_tick - ao_data_static.tick) > 0x400)
517                         tick_offset += 65536;
518                 simple_speed += accel * (ao_data_static.tick - prev_tick) / 100.0;
519                 prev_tick = ao_data_static.tick;
520                 time = (double) (ao_data_static.tick + tick_offset) / 100;
521
522 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
523                 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
524                 double height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
525
526                 /* Hack to skip baro spike at accidental drogue charge
527                  * firing in 2015-09-26-serial-2093-flight-0012.eeprom
528                  * so we can test the kalman filter with this data. Just
529                  * keep reporting the same baro value across the pressure spike
530                  */
531                 {
532                         static struct ao_ms5607_sample save;
533                         if (ao_serial_number == 2093 && ao_flight_number == 12 && 32.5 < time && time < 33.7) {
534                                 ao_data_ring[ao_data_head].ms5607_raw = save;
535                         } else {
536                                 save = ao_data_static.ms5607_raw;
537                         }
538                 }
539 #else
540                 double  height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
541 #endif
542
543                 if (ao_test_max_height < height) {
544                         ao_test_max_height = height;
545                         ao_test_max_height_time = time;
546                         ao_test_landed_height = height;
547                         ao_test_landed_time = time;
548                 }
549                 if (height > ao_config.main_deploy) {
550                         ao_test_main_height_time = time;
551                         ao_test_main_height = height;
552                 }
553
554                 if (ao_test_landed_height > height) {
555                         ao_test_landed_height = height;
556                         ao_test_landed_time = time;
557                 }
558
559                 if (ao_flight_state == ao_flight_landed && !landed_set) {
560                         landed_set = 1;
561                         landed_time = time;
562                         landed_height = height;
563                 }
564
565                 if (!ao_summary) {
566 #if TELEMEGA
567                         static struct ao_quaternion     ao_ground_mag;
568                         static int                      ao_ground_mag_set;
569
570                         if (!ao_ground_mag_set) {
571                                 ao_quaternion_init_vector (&ao_ground_mag,
572                                                            ao_data_mag_across(&ao_data_static),
573                                                            ao_data_mag_through(&ao_data_static),
574                                                            ao_data_mag_along(&ao_data_static));
575                                 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
576                                 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
577                                 ao_ground_mag_set = 1;
578                         }
579
580                         struct ao_quaternion            ao_mag, ao_mag_rot;
581
582                         ao_quaternion_init_vector(&ao_mag,
583                                                   ao_data_mag_across(&ao_data_static),
584                                                   ao_data_mag_through(&ao_data_static),
585                                                   ao_data_mag_along(&ao_data_static));
586
587                         ao_quaternion_normalize(&ao_mag, &ao_mag);
588                         ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
589
590                         float                           ao_dot;
591                         int                             ao_mag_angle;
592
593                         ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
594
595                         struct ao_azel                  ground_azel, mag_azel, rot_azel;
596
597                         azel(&ground_azel, &ao_ground_mag);
598                         azel(&mag_azel, &ao_mag);
599                         azel(&rot_azel, &ao_mag_rot);
600
601                         ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
602
603                         (void) ao_mag_angle;
604
605                         static struct ao_quaternion     ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
606                         struct ao_quaternion            ao_out;
607
608                         ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
609
610 #if 0
611                         int     out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
612
613                         printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
614                                 time,
615                                 ao_state_names[ao_flight_state],
616                                 ao_k_height / 65536.0,
617                                 ao_sample_orient, out,
618                                 mag_azel.el,
619                                 mag_azel.az);
620 #endif
621 #if 0
622                         printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
623                                 time,
624                                 ao_state_names[ao_flight_state],
625                                 ao_k_height / 65536.0,
626                                 ao_sample_orient, out,
627                                 ao_distance_from_pad(),
628                                 (int) floor (ao_gps_angle() + 0.5),
629                                 (ao_gps_static.flags & 0xf) * 10);
630
631 #endif
632 #if 0
633                         printf ("\t\tstate %-8.8s ground az: %4d el %4d mag az %4d el %4d rot az %4d el %4d el_diff %4d az_diff %4d angle %4d tilt %4d ground %8.5f %8.5f %8.5f cur %8.5f %8.5f %8.5f rot %8.5f %8.5f %8.5f\n",
634                                 ao_state_names[ao_flight_state],
635                                 ground_azel.az, ground_azel.el,
636                                 mag_azel.az, mag_azel.el,
637                                 rot_azel.az, rot_azel.el,
638                                 ground_azel.el - rot_azel.el,
639                                 ground_azel.az - rot_azel.az,
640                                 ao_mag_angle,
641                                 ao_sample_orient,
642                                 ao_ground_mag.x,
643                                 ao_ground_mag.y,
644                                 ao_ground_mag.z,
645                                 ao_mag.x,
646                                 ao_mag.y,
647                                 ao_mag.z,
648                                 ao_mag_rot.x,
649                                 ao_mag_rot.y,
650                                 ao_mag_rot.z);
651 #endif
652 #endif
653
654 #if 1
655                         printf("%7.2f height %8.2f accel %8.3f accel_speed %8.3f "
656                                "state %-8.8s k_height %8.2f k_speed %8.3f k_accel %8.3f avg_height %5d drogue %4d main %4d error %5d"
657 #if TELEMEGA
658                                " angle %5d "
659                                "accel_x %8.3f accel_y %8.3f accel_z %8.3f gyro_x %8.3f gyro_y %8.3f gyro_z %8.3f mag_x %8d mag_y %8d, mag_z %8d mag_angle %4d "
660 #endif
661                                "\n",
662                                time,
663                                height,
664                                accel,
665                                simple_speed > -100.0 ? simple_speed : -100.0,
666                                ao_state_names[ao_flight_state],
667                                ao_k_height / 65536.0,
668                                ao_k_speed / 65536.0 / 16.0,
669                                ao_k_accel / 65536.0 / 16.0,
670                                ao_avg_height,
671                                drogue_height,
672                                main_height,
673                                ao_error_h_sq_avg
674 #if TELEMEGA
675                                , ao_sample_orient,
676
677                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
678                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
679                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
680                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
681                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
682                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
683                                ao_data_static.hmc5883.x,
684                                ao_data_static.hmc5883.y,
685                                ao_data_static.hmc5883.z,
686                                ao_mag_angle
687 #endif
688                                 );
689 #endif
690
691 //                      if (ao_flight_state == ao_flight_landed)
692 //                              ao_test_exit();
693                 }
694         }
695         ao_data_head = ao_data_ring_next(ao_data_head);
696 }
697
698
699 uint16_t
700 uint16(uint8_t *bytes, int off)
701 {
702         return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
703 }
704
705 int16_t
706 int16(uint8_t *bytes, int off)
707 {
708         return (int16_t) uint16(bytes, off);
709 }
710
711 uint32_t
712 uint32(uint8_t *bytes, int off)
713 {
714         return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
715                 (((uint32_t) bytes[off+2]) << 16) |
716                 (((uint32_t) bytes[off+3]) << 24);
717 }
718
719 int32_t
720 int32(uint8_t *bytes, int off)
721 {
722         return (int32_t) uint32(bytes, off);
723 }
724
725 uint32_t
726 uint24(uint8_t *bytes, int off)
727 {
728         return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
729                 (((uint32_t) bytes[off+2]) << 16);
730 }
731
732 int32_t
733 int24(uint8_t *bytes, int off)
734 {
735         return (int32_t) uint24(bytes, off);
736 }
737
738 static int log_format;
739
740 void
741 ao_sleep(void *wchan)
742 {
743         if (wchan == &ao_data_head) {
744 #if TELEMEGA
745                 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
746                         ao_pyro_check();
747 #endif
748                 for (;;) {
749                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
750                         {
751
752 #if TELEMEGA
753                                 ao_data_static.mpu6000 = ao_ground_mpu6000;
754 #endif
755 #if TELEMETRUM_V1
756                                 ao_data_static.adc.accel = ao_flight_ground_accel;
757 #endif
758
759                                 ao_insert();
760                                 return;
761                         }
762
763                         if (eeprom) {
764 #if TELEMEGA
765                                 struct ao_log_mega      *log_mega;
766 #endif
767 #if TELEMETRUM_V2
768                                 struct ao_log_metrum    *log_metrum;
769 #endif
770 #if EASYMINI
771                                 struct ao_log_mini      *log_mini;
772 #endif
773 #if TELEMETRUM_V1
774                                 struct ao_log_record    *log_record;
775 #endif
776
777                                 if (eeprom_offset >= eeprom->len) {
778                                         if (++ao_eof_read >= 1000)
779                                                 if (!ao_summary)
780                                                         printf ("no more data, exiting simulation\n");
781                                         ao_test_exit();
782                                         ao_data_static.tick += 10;
783                                         ao_insert();
784                                         return;
785                                 }
786                                 switch (eeprom->log_format) {
787 #if TELEMEGA
788                                 case AO_LOG_FORMAT_TELEMEGA_OLD:
789                                 case AO_LOG_FORMAT_TELEMEGA:
790                                         log_mega = (struct ao_log_mega *) &eeprom->data[eeprom_offset];
791                                         eeprom_offset += sizeof (*log_mega);
792                                         switch (log_mega->type) {
793                                         case AO_LOG_FLIGHT:
794                                                 ao_flight_number = log_mega->u.flight.flight;
795                                                 ao_flight_ground_accel = log_mega->u.flight.ground_accel;
796                                                 ao_flight_started = 1;
797                                                 ao_ground_pres = log_mega->u.flight.ground_pres;
798                                                 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
799                                                 ao_ground_accel_along = log_mega->u.flight.ground_accel_along;
800                                                 ao_ground_accel_across = log_mega->u.flight.ground_accel_across;
801                                                 ao_ground_accel_through = log_mega->u.flight.ground_accel_through;
802                                                 ao_ground_roll = log_mega->u.flight.ground_roll;
803                                                 ao_ground_pitch = log_mega->u.flight.ground_pitch;
804                                                 ao_ground_yaw = log_mega->u.flight.ground_yaw;
805                                                 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
806                                                 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
807                                                 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
808                                                 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
809                                                 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
810                                                 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
811                                                 break;
812                                         case AO_LOG_STATE:
813                                                 break;
814                                         case AO_LOG_SENSOR:
815                                                 ao_data_static.tick = log_mega->tick;
816                                                 ao_data_static.ms5607_raw.pres = log_mega->u.sensor.pres;
817                                                 ao_data_static.ms5607_raw.temp = log_mega->u.sensor.temp;
818                                                 ao_data_static.mpu6000.accel_x = log_mega->u.sensor.accel_x;
819                                                 ao_data_static.mpu6000.accel_y = log_mega->u.sensor.accel_y;
820                                                 ao_data_static.mpu6000.accel_z = log_mega->u.sensor.accel_z;
821                                                 ao_data_static.mpu6000.gyro_x = log_mega->u.sensor.gyro_x;
822                                                 ao_data_static.mpu6000.gyro_y = log_mega->u.sensor.gyro_y;
823                                                 ao_data_static.mpu6000.gyro_z = log_mega->u.sensor.gyro_z;
824                                                 ao_data_static.hmc5883.x = log_mega->u.sensor.mag_x;
825                                                 ao_data_static.hmc5883.y = log_mega->u.sensor.mag_y;
826                                                 ao_data_static.hmc5883.z = log_mega->u.sensor.mag_z;
827                                                 ao_data_static.mma655x = log_mega->u.sensor.accel;
828                                                 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
829                                                         ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
830                                                 ao_records_read++;
831                                                 ao_insert();
832                                                 return;
833                                         case AO_LOG_TEMP_VOLT:
834                                                 if (pyros_fired != log_mega->u.volt.pyro) {
835                                                         printf("pyro changed %x -> %x\n", pyros_fired, log_mega->u.volt.pyro);
836                                                         pyros_fired = log_mega->u.volt.pyro;
837                                                 }
838                                                 break;
839                                         case AO_LOG_GPS_TIME:
840                                                 ao_gps_prev = ao_gps_static;
841                                                 ao_gps_static.tick = log_mega->tick;
842                                                 ao_gps_static.latitude = log_mega->u.gps.latitude;
843                                                 ao_gps_static.longitude = log_mega->u.gps.longitude;
844                                                 {
845                                                         int16_t altitude_low = log_mega->u.gps.altitude_low;
846                                                         int16_t altitude_high = log_mega->u.gps.altitude_high;
847                                                         int32_t altitude = altitude_low | ((int32_t) altitude_high << 16);
848
849                                                         AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
850                                                 }
851                                                 ao_gps_static.flags = log_mega->u.gps.flags;
852                                                 if (!ao_gps_count)
853                                                         ao_gps_first = ao_gps_static;
854                                                 ao_gps_count++;
855                                                 break;
856                                         case AO_LOG_GPS_SAT:
857                                                 break;
858                                         }
859                                         break;
860 #endif
861 #if TELEMETRUM_V2
862                                 case AO_LOG_FORMAT_TELEMETRUM:
863                                         log_metrum = (struct ao_log_metrum *) &eeprom->data[eeprom_offset];
864                                         eeprom_offset += sizeof (*log_metrum);
865                                         switch (log_metrum->type) {
866                                         case AO_LOG_FLIGHT:
867                                                 ao_flight_started = 1;
868                                                 ao_flight_number = log_metrum->u.flight.flight;
869                                                 ao_flight_ground_accel = log_metrum->u.flight.ground_accel;
870                                                 ao_ground_pres = log_metrum->u.flight.ground_pres;
871                                                 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
872                                                 break;
873                                         case AO_LOG_SENSOR:
874                                                 ao_data_static.tick = log_metrum->tick;
875                                                 ao_data_static.ms5607_raw.pres = log_metrum->u.sensor.pres;
876                                                 ao_data_static.ms5607_raw.temp = log_metrum->u.sensor.temp;
877                                                 ao_data_static.mma655x = log_metrum->u.sensor.accel;
878                                                 ao_records_read++;
879                                                 ao_insert();
880                                                 return;
881                                         }
882                                         break;
883 #endif
884 #if EASYMINI
885                                 case AO_LOG_FORMAT_EASYMINI1:
886                                 case AO_LOG_FORMAT_EASYMINI2:
887                                 case AO_LOG_FORMAT_TELEMINI3:
888                                         log_mini = (struct ao_log_mini *) &eeprom->data[eeprom_offset];
889                                         eeprom_offset += sizeof (*log_mini);
890                                         switch (log_mini->type) {
891                                         case AO_LOG_FLIGHT:
892                                                 ao_flight_started = 1;
893                                                 ao_flight_number = log_mini->u.flight.flight;
894                                                 ao_ground_pres = log_mini->u.flight.ground_pres;
895                                                 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
896                                                 break;
897                                         case AO_LOG_SENSOR:
898                                                 ao_data_static.tick = log_mini->tick;
899                                                 ao_data_static.ms5607_raw.pres = int24(log_mini->u.sensor.pres, 0);
900                                                 ao_data_static.ms5607_raw.temp = int24(log_mini->u.sensor.temp, 0);
901                                                 ao_records_read++;
902                                                 ao_insert();
903                                                 return;
904                                         }
905                                         break;
906 #endif
907 #if TELEMETRUM_V1
908                                 case AO_LOG_FORMAT_FULL:
909                                 case AO_LOG_FORMAT_TINY:
910                                         log_record = (struct ao_log_record *) &eeprom->data[eeprom_offset];
911                                         eeprom_offset += sizeof (*log_record);
912                                         switch (log_record->type) {
913                                         case AO_LOG_FLIGHT:
914                                                 ao_flight_started = 1;
915                                                 ao_flight_ground_accel = log_record->u.flight.ground_accel;
916                                                 ao_flight_number = log_record->u.flight.flight;
917                                                 break;
918                                         case AO_LOG_SENSOR:
919                                         case 'P':       /* ancient telemini */
920                                                 ao_data_static.tick = log_record->tick;
921                                                 ao_data_static.adc.accel = log_record->u.sensor.accel;
922                                                 ao_data_static.adc.pres_real = log_record->u.sensor.pres;
923                                                 ao_data_static.adc.pres = log_record->u.sensor.pres;
924                                                 ao_records_read++;
925                                                 ao_insert();
926                                                 return;
927                                         case AO_LOG_TEMP_VOLT:
928                                                 ao_data_static.tick = log_record->tick;;
929                                                 ao_data_static.adc.temp = log_record->u.temp_volt.temp;
930                                                 ao_data_static.adc.v_batt = log_record->u.temp_volt.v_batt;
931                                                 break;
932                                         }
933                                         break;
934 #endif
935                                 default:
936                                         printf ("invalid log format %d\n", log_format);
937                                         ao_test_exit();
938                                 }
939                         }
940                 }
941
942         }
943 }
944 #define COUNTS_PER_G 264.8
945
946 void
947 ao_dump_state(void)
948 {
949 }
950
951 static const struct option options[] = {
952         { .name = "summary", .has_arg = 0, .val = 's' },
953         { .name = "debug", .has_arg = 0, .val = 'd' },
954         { .name = "info", .has_arg = 1, .val = 'i' },
955         { 0, 0, 0, 0},
956 };
957
958 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
959 {
960         emulator_name = name;
961         emulator_in = f;
962         emulator_info = info;
963         ao_summary = summary;
964
965         if (strstr(name, ".eeprom") != NULL) {
966                 char    c;
967
968                 c = getc(f);
969                 ungetc(c, f);
970                 if (c == '{')
971                         eeprom = ao_eeprom_read(f);
972                 else
973                         eeprom = ao_eeprom_read_old(f);
974
975                 if (eeprom) {
976 #if HAS_MS5607
977                         ao_ms5607_prom = eeprom->ms5607_prom;
978 #endif
979                         ao_config = eeprom->config;
980                         ao_serial_number = eeprom->serial_number;
981                         log_format = eeprom->log_format;
982                 }
983         }
984
985         ao_flight_init();
986         ao_flight();
987 }
988
989 int
990 main (int argc, char **argv)
991 {
992         int     summary = 0;
993         int     c;
994         int     i;
995         char    *info = NULL;
996
997 #if HAS_ACCEL
998         emulator_app="full";
999 #else
1000         emulator_app="baro";
1001 #endif
1002         while ((c = getopt_long(argc, argv, "sdpi:", options, NULL)) != -1) {
1003                 switch (c) {
1004                 case 's':
1005                         summary = 1;
1006                         break;
1007                 case 'd':
1008                         ao_flight_debug = 1;
1009                         break;
1010                 case 'p':
1011 #if PYRO_DBG
1012                         pyro_dbg = 1;
1013 #endif
1014                         break;
1015                 case 'i':
1016                         info = optarg;
1017                         break;
1018                 }
1019         }
1020
1021         if (optind == argc)
1022                 run_flight_fixed("<stdin>", stdin, summary, info);
1023         else
1024                 for (i = optind; i < argc; i++) {
1025                         FILE    *f = fopen(argv[i], "r");
1026                         if (!f) {
1027                                 perror(argv[i]);
1028                                 continue;
1029                         }
1030                         run_flight_fixed(argv[i], f, summary, info);
1031                         fclose(f);
1032                 }
1033         exit(0);
1034 }