3995d98d199b721b8f1f1cf7be2737e40d320bb5
[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
53 struct ao_adc {
54         int16_t                 sense[AO_ADC_NUM_SENSE];
55         int16_t                 v_batt;
56         int16_t                 v_pbatt;
57         int16_t                 accel_ref;
58         int16_t                 accel;
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_gps_static.altitude - ao_gps_prev.altitude;
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   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 struct ao_config {
321         uint16_t        main_deploy;
322         int16_t         accel_plus_g;
323         int16_t         accel_minus_g;
324         uint8_t         pad_orientation;
325         uint16_t        apogee_lockout;
326 #if TELEMEGA
327         struct ao_pyro  pyro[AO_PYRO_NUM];      /* minor version 12 */
328         int16_t         accel_zero_along;
329         int16_t         accel_zero_across;
330         int16_t         accel_zero_through;
331 #endif
332 };
333
334 #define AO_PAD_ORIENTATION_ANTENNA_UP   0
335 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
336
337 #define ao_config_get()
338
339 struct ao_config ao_config;
340
341 #define DATA_TO_XDATA(x) (x)
342
343
344 extern int16_t ao_ground_accel, ao_flight_accel;
345 extern int16_t ao_accel_2g;
346
347 typedef int16_t accel_t;
348
349 extern uint16_t ao_sample_tick;
350
351 extern alt_t    ao_sample_height;
352 extern accel_t  ao_sample_accel;
353 extern int32_t  ao_accel_scale;
354 extern alt_t    ao_ground_height;
355 extern alt_t    ao_sample_alt;
356
357 double ao_sample_qangle;
358
359 int ao_sample_prev_tick;
360 uint16_t        prev_tick;
361
362
363 #include "ao_kalman.c"
364 #include "ao_sqrt.c"
365 #include "ao_sample.c"
366 #include "ao_flight.c"
367 #if TELEMEGA
368 #define AO_PYRO_NUM     4
369
370 #define AO_PYRO_0       0
371 #define AO_PYRO_1       1
372 #define AO_PYRO_2       2
373 #define AO_PYRO_3       3
374
375 static void
376 ao_pyro_pin_set(uint8_t pin, uint8_t value)
377 {
378         printf ("set pyro %d %d\n", pin, value);
379 }
380
381 #include "ao_pyro.c"
382 #endif
383
384 #define to_double(f)    ((f) / 65536.0)
385
386 static int      ao_records_read = 0;
387 static int      ao_eof_read = 0;
388 static int      ao_flight_ground_accel;
389 static int      ao_flight_started = 0;
390 static int      ao_test_max_height;
391 static double   ao_test_max_height_time;
392 static int      ao_test_main_height;
393 static double   ao_test_main_height_time;
394 static double   ao_test_landed_time;
395 static double   ao_test_landed_height;
396 static double   ao_test_landed_time;
397 static int      landed_set;
398 static double   landed_time;
399 static double   landed_height;
400
401 #if HAS_MPU6000
402 static struct ao_mpu6000_sample ao_ground_mpu6000;
403 #endif
404
405 void
406 ao_test_exit(void)
407 {
408         double  drogue_error;
409         double  main_error;
410         double  landed_error;
411         double  landed_time_error;
412
413         if (!ao_test_main_height_time) {
414                 ao_test_main_height_time = ao_test_max_height_time;
415                 ao_test_main_height = ao_test_max_height;
416         }
417         drogue_error = fabs(ao_test_max_height_time - drogue_time);
418         main_error = fabs(ao_test_main_height_time - main_time);
419         landed_error = fabs(ao_test_landed_height - landed_height);
420         landed_time_error = ao_test_landed_time - landed_time;
421         if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
422                 printf ("%s %s\n",
423                         emulator_app, emulator_name);
424                 if (emulator_info)
425                         printf ("\t%s\n", emulator_info);
426                 printf ("\tApogee error %g\n", drogue_error);
427                 printf ("\tMain error %g\n", main_error);
428                 printf ("\tLanded height error %g\n", landed_error);
429                 printf ("\tLanded time error %g\n", landed_time_error);
430                 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
431                         ao_test_max_height, ao_test_max_height_time,
432                         ao_test_main_height, ao_test_main_height_time,
433                         ao_test_landed_height, ao_test_landed_time);
434                 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
435                         drogue_height, drogue_time, main_height, main_time,
436                         landed_height, landed_time);
437                 exit (1);
438         }
439         exit(0);
440 }
441
442 #ifdef TELEMEGA
443 struct ao_azel {
444         int     az;
445         int     el;
446 };
447
448 static void
449 azel (struct ao_azel *r, struct ao_quaternion *q)
450 {
451         double  v;
452
453         r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
454         v = sqrt (q->x*q->x + q->y*q->y);
455         r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
456 }
457 #endif
458
459 void
460 ao_insert(void)
461 {
462         double  time;
463
464         ao_data_ring[ao_data_head] = ao_data_static;
465         ao_data_head = ao_data_ring_next(ao_data_head);
466         if (ao_flight_state != ao_flight_startup) {
467 #if HAS_ACCEL
468                 double  accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
469                         (ao_config.accel_minus_g - ao_config.accel_plus_g);
470 #else
471                 double  accel = 0.0;
472 #endif
473 #if TELEMEGA
474                 double  height;
475
476                 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
477                 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
478 #else
479                 double  height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
480 #endif
481
482                 (void) accel;
483                 if (!tick_offset)
484                         tick_offset = -ao_data_static.tick;
485                 if ((prev_tick - ao_data_static.tick) > 0x400)
486                         tick_offset += 65536;
487                 prev_tick = ao_data_static.tick;
488                 time = (double) (ao_data_static.tick + tick_offset) / 100;
489
490                 if (ao_test_max_height < height) {
491                         ao_test_max_height = height;
492                         ao_test_max_height_time = time;
493                         ao_test_landed_height = height;
494                         ao_test_landed_time = time;
495                 }
496                 if (height > ao_config.main_deploy) {
497                         ao_test_main_height_time = time;
498                         ao_test_main_height = height;
499                 }
500
501                 if (ao_test_landed_height > height) {
502                         ao_test_landed_height = height;
503                         ao_test_landed_time = time;
504                 }
505
506                 if (ao_flight_state == ao_flight_landed && !landed_set) {
507                         landed_set = 1;
508                         landed_time = time;
509                         landed_height = height;
510                 }
511
512                 if (!ao_summary) {
513 #if TELEMEGA
514                         static struct ao_quaternion     ao_ground_mag;
515                         static int                      ao_ground_mag_set;
516
517                         if (!ao_ground_mag_set) {
518                                 ao_quaternion_init_vector (&ao_ground_mag,
519                                                            ao_data_mag_across(&ao_data_static),
520                                                            ao_data_mag_through(&ao_data_static),
521                                                            ao_data_mag_along(&ao_data_static));
522                                 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
523                                 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
524                                 ao_ground_mag_set = 1;
525                         }
526
527                         struct ao_quaternion            ao_mag, ao_mag_rot;
528
529                         ao_quaternion_init_vector(&ao_mag,
530                                                   ao_data_mag_across(&ao_data_static),
531                                                   ao_data_mag_through(&ao_data_static),
532                                                   ao_data_mag_along(&ao_data_static));
533
534                         ao_quaternion_normalize(&ao_mag, &ao_mag);
535                         ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
536                         
537                         float                           ao_dot;
538                         int                             ao_mag_angle;
539
540                         ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
541
542                         struct ao_azel                  ground_azel, mag_azel, rot_azel;
543
544                         azel(&ground_azel, &ao_ground_mag);
545                         azel(&mag_azel, &ao_mag);
546                         azel(&rot_azel, &ao_mag_rot);
547
548                         ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
549
550                         (void) ao_mag_angle;
551
552                         static struct ao_quaternion     ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
553                         struct ao_quaternion            ao_out;
554
555                         ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
556
557                         int     out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
558
559 #if 0
560                         printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
561                                 time,
562                                 ao_state_names[ao_flight_state],
563                                 ao_k_height / 65536.0,
564                                 ao_sample_orient, out,
565                                 mag_azel.el,
566                                 mag_azel.az);
567 #endif
568                         printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
569                                 time,
570                                 ao_state_names[ao_flight_state],
571                                 ao_k_height / 65536.0,
572                                 ao_sample_orient, out,
573                                 ao_distance_from_pad(),
574                                 (int) floor (ao_gps_angle() + 0.5),
575                                 (ao_gps_static.flags & 0xf) * 10);
576
577 #if 0
578                         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",
579                                 ao_state_names[ao_flight_state],
580                                 ground_azel.az, ground_azel.el,
581                                 mag_azel.az, mag_azel.el,
582                                 rot_azel.az, rot_azel.el,
583                                 ground_azel.el - rot_azel.el,
584                                 ground_azel.az - rot_azel.az,
585                                 ao_mag_angle,
586                                 ao_sample_orient,
587                                 ao_ground_mag.x,
588                                 ao_ground_mag.y,
589                                 ao_ground_mag.z,
590                                 ao_mag.x,
591                                 ao_mag.y,
592                                 ao_mag.z,
593                                 ao_mag_rot.x,
594                                 ao_mag_rot.y,
595                                 ao_mag_rot.z);
596 #endif
597 #endif
598
599 #if 0
600                         printf("%7.2f height %8.2f accel %8.3f "
601 #if TELEMEGA
602                                "angle %5d "
603                                "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 "
604 #endif
605                                "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",
606                                time,
607                                height,
608                                accel,
609 #if TELEMEGA
610                                ao_sample_orient,
611
612                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
613                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
614                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
615                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
616                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
617                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
618                                ao_data_static.hmc5883.x,
619                                ao_data_static.hmc5883.y,
620                                ao_data_static.hmc5883.z,
621                                ao_mag_angle,
622 #endif
623                                ao_state_names[ao_flight_state],
624                                ao_k_height / 65536.0,
625                                ao_k_speed / 65536.0 / 16.0,
626                                ao_k_accel / 65536.0 / 16.0,
627                                ao_avg_height,
628                                drogue_height,
629                                main_height,
630                                ao_error_h_sq_avg);
631 #endif
632                         
633 //                      if (ao_flight_state == ao_flight_landed)
634 //                              ao_test_exit();
635                 }
636         }
637 }
638
639
640 uint16_t
641 uint16(uint8_t *bytes, int off)
642 {
643         return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
644 }
645
646 int16_t
647 int16(uint8_t *bytes, int off)
648 {
649         return (int16_t) uint16(bytes, off);
650 }
651
652 uint32_t
653 uint32(uint8_t *bytes, int off)
654 {
655         return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
656                 (((uint32_t) bytes[off+2]) << 16) |
657                 (((uint32_t) bytes[off+3]) << 24);
658 }
659
660 int32_t
661 int32(uint8_t *bytes, int off)
662 {
663         return (int32_t) uint32(bytes, off);
664 }
665
666 static int log_format;
667
668 void
669 ao_sleep(void *wchan)
670 {
671         if (wchan == &ao_data_head) {
672                 char            type = 0;
673                 uint16_t        tick = 0;
674                 uint16_t        a = 0, b = 0;
675                 uint8_t         bytes[1024];
676                 union ao_telemetry_all  telem;
677                 char            line[1024];
678                 char            *saveptr;
679                 char            *l;
680                 char            *words[64];
681                 int             nword;
682
683 #if TELEMEGA
684                 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
685                         ao_pyro_check();
686 #endif
687                 for (;;) {
688                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
689                         {
690 #if TELEMEGA
691                                 ao_data_static.mpu6000 = ao_ground_mpu6000;
692 #else
693                                 ao_data_static.adc.accel = ao_flight_ground_accel;
694 #endif
695                                 ao_insert();
696                                 return;
697                         }
698
699                         if (!fgets(line, sizeof (line), emulator_in)) {
700                                 if (++ao_eof_read >= 1000) {
701                                         if (!ao_summary)
702                                                 printf ("no more data, exiting simulation\n");
703                                         ao_test_exit();
704                                 }
705                                 ao_data_static.tick += 10;
706                                 ao_insert();
707                                 return;
708                         }
709                         l = line;
710                         for (nword = 0; nword < 64; nword++) {
711                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
712                                 l = NULL;
713                                 if (words[nword] == NULL)
714                                         break;
715                         }
716 #if TELEMEGA
717                         if (log_format == AO_LOG_FORMAT_TELEMEGA && nword == 30 && strlen(words[0]) == 1) {
718                                 int     i;
719                                 struct ao_ms5607_value  value;
720
721                                 type = words[0][0];
722                                 tick = strtoul(words[1], NULL, 16);
723 //                              printf ("%c %04x", type, tick);
724                                 for (i = 2; i < nword; i++) {
725                                         bytes[i - 2] = strtoul(words[i], NULL, 16);
726 //                                      printf(" %02x", bytes[i-2]);
727                                 }
728 //                              printf ("\n");
729                                 switch (type) {
730                                 case 'F':
731                                         ao_flight_ground_accel = int16(bytes, 2);
732                                         ao_flight_started = 1;
733                                         ao_ground_pres = int32(bytes, 4);
734                                         ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
735                                         ao_ground_accel_along = int16(bytes, 8);
736                                         ao_ground_accel_across = int16(bytes, 10);
737                                         ao_ground_accel_through = int16(bytes, 12);
738                                         ao_ground_roll = int16(bytes, 14);
739                                         ao_ground_pitch = int16(bytes, 16);
740                                         ao_ground_yaw = int16(bytes, 18);
741                                         ao_ground_mpu6000.accel_x = ao_ground_accel_across;
742                                         ao_ground_mpu6000.accel_y = ao_ground_accel_along;
743                                         ao_ground_mpu6000.accel_z = ao_ground_accel_through;
744                                         ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
745                                         ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
746                                         ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
747                                         break;
748                                 case 'A':
749                                         ao_data_static.tick = tick;
750                                         ao_data_static.ms5607_raw.pres = int32(bytes, 0);
751                                         ao_data_static.ms5607_raw.temp = int32(bytes, 4);
752                                         ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
753                                         ao_data_static.mpu6000.accel_x = int16(bytes, 8);
754                                         ao_data_static.mpu6000.accel_y = int16(bytes, 10);
755                                         ao_data_static.mpu6000.accel_z = int16(bytes, 12);
756                                         ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
757                                         ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
758                                         ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
759                                         ao_data_static.hmc5883.x = int16(bytes, 20);
760                                         ao_data_static.hmc5883.y = int16(bytes, 22);
761                                         ao_data_static.hmc5883.z = int16(bytes, 24);
762 #if HAS_MMA655X
763                                         ao_data_static.mma655x = int16(bytes, 26);
764 #endif
765                                         ao_records_read++;
766                                         ao_insert();
767                                         return;
768                                 case 'G':
769                                         ao_gps_prev = ao_gps_static;
770                                         ao_gps_static.tick = tick;
771                                         ao_gps_static.latitude = int32(bytes, 0);
772                                         ao_gps_static.longitude = int32(bytes, 4);
773                                         ao_gps_static.altitude = int32(bytes, 8);
774                                         ao_gps_static.flags = bytes[13];
775                                         if (!ao_gps_count)
776                                                 ao_gps_first = ao_gps_static;
777                                         ao_gps_count++;
778                                         break;
779                                 }
780                                 continue;
781                         } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
782                                 if (strcmp(words[1], "reserved:") == 0)
783                                         ms5607_prom.reserved = strtoul(words[2], NULL, 10);
784                                 else if (strcmp(words[1], "sens:") == 0)
785                                         ms5607_prom.sens = strtoul(words[2], NULL, 10);
786                                 else if (strcmp(words[1], "off:") == 0)
787                                         ms5607_prom.off = strtoul(words[2], NULL, 10);
788                                 else if (strcmp(words[1], "tcs:") == 0)
789                                         ms5607_prom.tcs = strtoul(words[2], NULL, 10);
790                                 else if (strcmp(words[1], "tco:") == 0)
791                                         ms5607_prom.tco = strtoul(words[2], NULL, 10);
792                                 else if (strcmp(words[1], "tref:") == 0)
793                                         ms5607_prom.tref = strtoul(words[2], NULL, 10);
794                                 else if (strcmp(words[1], "tempsens:") == 0)
795                                         ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
796                                 else if (strcmp(words[1], "crc:") == 0)
797                                         ms5607_prom.crc = strtoul(words[2], NULL, 10);
798                                 continue;
799                         } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
800                                 int     p = strtoul(words[1], NULL, 10);
801                                 int     i, j;
802                                 struct ao_pyro  *pyro = &ao_config.pyro[p];
803
804                                 for (i = 2; i < nword; i++) {
805                                         for (j = 0; j < NUM_PYRO_VALUES; j++)
806                                                 if (!strcmp (words[i], ao_pyro_values[j].name))
807                                                         break;
808                                         if (j == NUM_PYRO_VALUES)
809                                                 continue;
810                                         pyro->flags |= ao_pyro_values[j].flag;
811                                         if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
812                                                 int16_t val = strtoul(words[++i], NULL, 10);
813                                                 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
814                                         }
815                                 }
816                         }
817 #else
818                         if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
819                                 type = words[0][0];
820                                 tick = strtoul(words[1], NULL, 16);
821                                 a = strtoul(words[2], NULL, 16);
822                                 b = strtoul(words[3], NULL, 16);
823                                 if (type == 'P')
824                                         type = 'A';
825                         }
826 #endif
827                         else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
828                                 log_format = strtoul(words[1], NULL, 10);
829                         } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
830                                 ao_config.accel_plus_g = atoi(words[3]);
831                                 ao_config.accel_minus_g = atoi(words[5]);
832 #ifdef TELEMEGA
833                         } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
834                                 ao_config.accel_zero_along = atoi(words[3]);
835                                 ao_config.accel_zero_across = atoi(words[5]);
836                                 ao_config.accel_zero_through = atoi(words[7]);
837                                 printf ("%d %d %d\n", ao_config.accel_zero_along, ao_config.accel_zero_across, ao_config.accel_zero_through);
838 #endif
839                         } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
840                                 ao_config.main_deploy = atoi(words[2]);
841                         } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
842                                    strcmp(words[1], "lockout:") == 0) {
843                                 ao_config.apogee_lockout = atoi(words[2]);
844                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
845                                 tick = atoi(words[10]);
846                                 if (!ao_flight_started) {
847                                         type = 'F';
848                                         a = atoi(words[26]);
849                                         ao_flight_started = 1;
850                                 } else {
851                                         type = 'A';
852                                         a = atoi(words[12]);
853                                         b = atoi(words[14]);
854                                 }
855                         } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
856                                 tick = strtol(words[1], NULL, 16);
857                                 a = 16384 - 328;
858                                 b = strtol(words[2], NULL, 10);
859                                 type = 'A';
860                                 if (!ao_flight_started) {
861                                         ao_flight_ground_accel = 16384 - 328;
862                                         ao_config.accel_plus_g = 16384 - 328;
863                                         ao_config.accel_minus_g = 16384 + 328;
864                                         ao_flight_started = 1;
865                                 }
866                         } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
867                                 __xdata char    *hex = words[1];
868                                 char    elt[3];
869                                 int     i, len;
870                                 uint8_t sum;
871
872                                 len = strlen(hex);
873                                 if (len > sizeof (bytes) * 2) {
874                                         len = sizeof (bytes)*2;
875                                         hex[len] = '\0';
876                                 }
877                                 for (i = 0; i < len; i += 2) {
878                                         elt[0] = hex[i];
879                                         elt[1] = hex[i+1];
880                                         elt[2] = '\0';
881                                         bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
882                                 }
883                                 len = i/2;
884                                 if (bytes[0] != len - 2) {
885                                         printf ("bad length %d != %d\n", bytes[0], len - 2);
886                                         continue;
887                                 }
888                                 sum = 0x5a;
889                                 for (i = 1; i < len-1; i++)
890                                         sum += bytes[i];
891                                 if (sum != bytes[len-1]) {
892                                         printf ("bad checksum\n");
893                                         continue;
894                                 }
895                                 if ((bytes[len-2] & 0x80) == 0) {
896                                         continue;
897                                 }
898                                 if (len == 36) {
899                                         ao_xmemcpy(&telem, bytes + 1, 32);
900                                         tick = telem.generic.tick;
901                                         switch (telem.generic.type) {
902                                         case AO_TELEMETRY_SENSOR_TELEMETRUM:
903                                         case AO_TELEMETRY_SENSOR_TELEMINI:
904                                         case AO_TELEMETRY_SENSOR_TELENANO:
905                                                 if (!ao_flight_started) {
906                                                         ao_flight_ground_accel = telem.sensor.ground_accel;
907                                                         ao_config.accel_plus_g = telem.sensor.accel_plus_g;
908                                                         ao_config.accel_minus_g = telem.sensor.accel_minus_g;
909                                                         ao_flight_started = 1;
910                                                 }
911                                                 type = 'A';
912                                                 a = telem.sensor.accel;
913                                                 b = telem.sensor.pres;
914                                                 break;
915                                         }
916                                 } else if (len == 99) {
917                                         ao_flight_started = 1;
918                                         tick = uint16(bytes+1, 21);
919                                         ao_flight_ground_accel = int16(bytes+1, 7);
920                                         ao_config.accel_plus_g = int16(bytes+1, 17);
921                                         ao_config.accel_minus_g = int16(bytes+1, 19);
922                                         type = 'A';
923                                         a = int16(bytes+1, 23);
924                                         b = int16(bytes+1, 25);
925                                 } else if (len == 98) {
926                                         ao_flight_started = 1;
927                                         tick = uint16(bytes+1, 20);
928                                         ao_flight_ground_accel = int16(bytes+1, 6);
929                                         ao_config.accel_plus_g = int16(bytes+1, 16);
930                                         ao_config.accel_minus_g = int16(bytes+1, 18);
931                                         type = 'A';
932                                         a = int16(bytes+1, 22);
933                                         b = int16(bytes+1, 24);
934                                 } else {
935                                         printf("unknown len %d\n", len);
936                                         continue;
937                                 }
938                         }
939                         if (type != 'F' && !ao_flight_started)
940                                 continue;
941
942 #if TELEMEGA
943                         (void) a;
944                         (void) b;
945 #else
946                         switch (type) {
947                         case 'F':
948                                 ao_flight_ground_accel = a;
949                                 if (ao_config.accel_plus_g == 0) {
950                                         ao_config.accel_plus_g = a;
951                                         ao_config.accel_minus_g = a + 530;
952                                 }
953                                 if (ao_config.main_deploy == 0)
954                                         ao_config.main_deploy = 250;
955                                 ao_flight_started = 1;
956                                 break;
957                         case 'S':
958                                 break;
959                         case 'A':
960                                 ao_data_static.tick = tick;
961                                 ao_data_static.adc.accel = a;
962                                 ao_data_static.adc.pres_real = b;
963                                 if (b < AO_MIN_BARO_VALUE)
964                                         b = AO_MIN_BARO_VALUE;
965                                 ao_data_static.adc.pres = b;
966                                 ao_records_read++;
967                                 ao_insert();
968                                 return;
969                         case 'T':
970                                 ao_data_static.tick = tick;
971                                 ao_data_static.adc.temp = a;
972                                 ao_data_static.adc.v_batt = b;
973                                 break;
974                         case 'D':
975                         case 'G':
976                         case 'N':
977                         case 'W':
978                         case 'H':
979                                 break;
980                         }
981 #endif
982                 }
983
984         }
985 }
986 #define COUNTS_PER_G 264.8
987
988 void
989 ao_dump_state(void)
990 {
991 }
992
993 static const struct option options[] = {
994         { .name = "summary", .has_arg = 0, .val = 's' },
995         { .name = "debug", .has_arg = 0, .val = 'd' },
996         { .name = "info", .has_arg = 1, .val = 'i' },
997         { 0, 0, 0, 0},
998 };
999
1000 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1001 {
1002         emulator_name = name;
1003         emulator_in = f;
1004         emulator_info = info;
1005         ao_summary = summary;
1006         ao_flight_init();
1007         ao_flight();
1008 }
1009
1010 int
1011 main (int argc, char **argv)
1012 {
1013         int     summary = 0;
1014         int     c;
1015         int     i;
1016         char    *info = NULL;
1017
1018 #if HAS_ACCEL
1019         emulator_app="full";
1020 #else
1021         emulator_app="baro";
1022 #endif
1023         while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1024                 switch (c) {
1025                 case 's':
1026                         summary = 1;
1027                         break;
1028                 case 'd':
1029                         ao_flight_debug = 1;
1030                         break;
1031                 case 'i':
1032                         info = optarg;
1033                         break;
1034                 }
1035         }
1036
1037         if (optind == argc)
1038                 run_flight_fixed("<stdin>", stdin, summary, info);
1039         else
1040                 for (i = optind; i < argc; i++) {
1041                         FILE    *f = fopen(argv[i], "r");
1042                         if (!f) {
1043                                 perror(argv[i]);
1044                                 continue;
1045                         }
1046                         run_flight_fixed(argv[i], f, summary, info);
1047                         fclose(f);
1048                 }
1049         exit(0);
1050 }