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