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