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