]> git.gag.com Git - fw/altos/blob - src/test/ao_flight_test.c
altosdroid: Keep speaking even when screen is off
[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 #if 0
38 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
39 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
40 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
41 #endif
42
43 #define AO_GPS_NEW_DATA         1
44 #define AO_GPS_NEW_TRACKING     2
45
46 int ao_gps_new;
47
48 #if !defined(TELEMEGA) && !defined(TELEMETRUM_V2)
49 #define TELEMETRUM_V1 1
50 #endif
51
52 #if TELEMEGA
53 #define AO_ADC_NUM_SENSE        6
54 #define HAS_MS5607              1
55 #define HAS_MPU6000             1
56 #define HAS_MMA655X             1
57 #define HAS_HMC5883             1
58 #define HAS_BEEP                1
59 #define AO_CONFIG_MAX_SIZE      1024
60
61 struct ao_adc {
62         int16_t                 sense[AO_ADC_NUM_SENSE];
63         int16_t                 v_batt;
64         int16_t                 v_pbatt;
65         int16_t                 temp;
66 };
67 #endif
68
69 #if TELEMETRUM_V2
70 #define AO_ADC_NUM_SENSE        2
71 #define HAS_MS5607              1
72 #define HAS_MMA655X             1
73 #define HAS_BEEP                1
74 #define AO_CONFIG_MAX_SIZE      1024
75
76 struct ao_adc {
77         int16_t                 sense_a;
78         int16_t                 sense_m;
79         int16_t                 v_batt;
80         int16_t                 temp;
81 };
82 #endif
83
84
85 #if TELEMETRUM_V1
86 /*
87  * One set of samples read from the A/D converter
88  */
89 struct ao_adc {
90         int16_t         accel;          /* accelerometer */
91         int16_t         pres;           /* pressure sensor */
92         int16_t         pres_real;      /* unclipped */
93         int16_t         temp;           /* temperature sensor */
94         int16_t         v_batt;         /* battery voltage */
95         int16_t         sense_d;        /* drogue continuity sense */
96         int16_t         sense_m;        /* main continuity sense */
97 };
98
99 #ifndef HAS_ACCEL
100 #define HAS_ACCEL 1
101 #define HAS_ACCEL_REF 0
102 #endif
103
104 #endif
105
106 #define __pdata
107 #define __data
108 #define __xdata
109 #define __code
110 #define __reentrant
111
112 #define HAS_FLIGHT 1
113 #define HAS_IGNITE 1
114 #define HAS_USB 1
115 #define HAS_GPS 1
116
117 #include <ao_data.h>
118 #include <ao_log.h>
119 #include <ao_telemetry.h>
120 #include <ao_sample.h>
121
122 #if TELEMEGA
123 int ao_gps_count;
124 struct ao_telemetry_location ao_gps_first;
125 struct ao_telemetry_location ao_gps_prev;
126 struct ao_telemetry_location ao_gps_static;
127
128 struct ao_telemetry_satellite ao_gps_tracking;
129
130 static inline double sqr(double a) { return a * a; }
131
132 void
133 cc_great_circle (double start_lat, double start_lon,
134                  double end_lat, double end_lon,
135                  double *dist, double *bearing)
136 {
137         const double rad = M_PI / 180;
138         const double earth_radius = 6371.2 * 1000;      /* in meters */
139         double lat1 = rad * start_lat;
140         double lon1 = rad * -start_lon;
141         double lat2 = rad * end_lat;
142         double lon2 = rad * -end_lon;
143
144 //      double d_lat = lat2 - lat1;
145         double d_lon = lon2 - lon1;
146
147         /* From http://en.wikipedia.org/wiki/Great-circle_distance */
148         double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
149                           sqr(cos(lat1) * sin(lat2) -
150                               sin(lat1) * cos(lat2) * cos(d_lon)));
151         double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
152         double d = atan2(vdn,vdd);
153         double course;
154
155         if (cos(lat1) < 1e-20) {
156                 if (lat1 > 0)
157                         course = M_PI;
158                 else
159                         course = -M_PI;
160         } else {
161                 if (d < 1e-10)
162                         course = 0;
163                 else
164                         course = acos((sin(lat2)-sin(lat1)*cos(d)) /
165                                       (sin(d)*cos(lat1)));
166                 if (sin(lon2-lon1) > 0)
167                         course = 2 * M_PI-course;
168         }
169         *dist = d * earth_radius;
170         *bearing = course * 180/M_PI;
171 }
172
173 double
174 ao_distance_from_pad(void)
175 {
176         double  dist, bearing;
177         if (!ao_gps_count)
178                 return 0;
179         
180         cc_great_circle(ao_gps_first.latitude / 1e7,
181                         ao_gps_first.longitude / 1e7,
182                         ao_gps_static.latitude / 1e7,
183                         ao_gps_static.longitude / 1e7,
184                         &dist, &bearing);
185         return dist;
186 }
187
188 double
189 ao_gps_angle(void)
190 {
191         double  dist, bearing;
192         double  height;
193         double  angle;
194
195         if (ao_gps_count < 2)
196                 return 0;
197
198         cc_great_circle(ao_gps_prev.latitude / 1e7,
199                         ao_gps_prev.longitude / 1e7,
200                         ao_gps_static.latitude / 1e7,
201                         ao_gps_static.longitude / 1e7,
202                         &dist, &bearing);
203         height = AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_static) - AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_prev);
204
205         angle = atan2(dist, height);
206         return angle * 180/M_PI;
207 }
208 #endif
209
210 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
211 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
212 #define from_fix(x)     ((x) >> 16)
213
214 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
215
216 extern enum ao_flight_state ao_flight_state;
217
218 #define FALSE 0
219 #define TRUE 1
220
221 volatile struct ao_data ao_data_ring[AO_DATA_RING];
222 volatile uint8_t ao_data_head;
223 int     ao_summary = 0;
224
225 #define ao_led_on(l)
226 #define ao_led_off(l)
227 #define ao_timer_set_adc_interval(i)
228 #define ao_wakeup(wchan) ao_dump_state()
229 #define ao_cmd_register(c)
230 #define ao_usb_disable()
231 #define ao_telemetry_set_interval(x)
232 #define ao_rdf_set(rdf)
233 #define ao_packet_slave_start()
234 #define ao_packet_slave_stop()
235 #define flush()
236
237 enum ao_igniter {
238         ao_igniter_drogue = 0,
239         ao_igniter_main = 1
240 };
241
242 struct ao_data ao_data_static;
243
244 int     drogue_height;
245 double  drogue_time;
246 int     main_height;
247 double  main_time;
248
249 int     tick_offset;
250
251 static ao_k_t   ao_k_height;
252
253 int16_t
254 ao_time(void)
255 {
256         return ao_data_static.tick;
257 }
258
259 void
260 ao_delay(int16_t interval)
261 {
262         return;
263 }
264
265 void
266 ao_ignite(enum ao_igniter igniter)
267 {
268         double time = (double) (ao_data_static.tick + tick_offset) / 100;
269
270         if (igniter == ao_igniter_drogue) {
271                 drogue_time = time;
272                 drogue_height = ao_k_height >> 16;
273         } else {
274                 main_time = time;
275                 main_height = ao_k_height >> 16;
276         }
277 }
278
279 struct ao_task {
280         int dummy;
281 };
282
283 #define ao_add_task(t,f,n) ((void) (t))
284
285 #define ao_log_start()
286 #define ao_log_stop()
287
288 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
289 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
290
291 #define AO_FLIGHT_TEST
292
293 int     ao_flight_debug;
294
295 FILE *emulator_in;
296 char *emulator_app;
297 char *emulator_name;
298 char *emulator_info;
299 double emulator_error_max = 4;
300 double emulator_height_error_max = 20;  /* noise in the baro sensor */
301
302 void
303 ao_dump_state(void);
304
305 void
306 ao_sleep(void *wchan);
307
308 const char const * const ao_state_names[] = {
309         "startup", "idle", "pad", "boost", "fast",
310         "coast", "drogue", "main", "landed", "invalid"
311 };
312
313 struct ao_cmds {
314         void            (*func)(void);
315         const char      *help;
316 };
317
318 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
319 #define ao_xmemset(d,v,c) memset(d,v,c)
320 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
321
322 #define AO_NEED_ALTITUDE_TO_PRES 1
323 #if TELEMEGA || TELEMETRUM_V2
324 #include "ao_convert_pa.c"
325 #include <ao_ms5607.h>
326 struct ao_ms5607_prom   ao_ms5607_prom;
327 #include "ao_ms5607_convert.c"
328 #define AO_PYRO_NUM     4
329 #include <ao_pyro.h>
330 #else
331 #include "ao_convert.c"
332 #endif
333
334 #include <ao_config.h>
335 #include <ao_fake_flight.h>
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 || TELEMETRUM_V2
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 #if 0
558                         int     out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
559
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 #if 0
569                         printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
570                                 time,
571                                 ao_state_names[ao_flight_state],
572                                 ao_k_height / 65536.0,
573                                 ao_sample_orient, out,
574                                 ao_distance_from_pad(),
575                                 (int) floor (ao_gps_angle() + 0.5),
576                                 (ao_gps_static.flags & 0xf) * 10);
577
578 #endif
579 #if 0
580                         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",
581                                 ao_state_names[ao_flight_state],
582                                 ground_azel.az, ground_azel.el,
583                                 mag_azel.az, mag_azel.el,
584                                 rot_azel.az, rot_azel.el,
585                                 ground_azel.el - rot_azel.el,
586                                 ground_azel.az - rot_azel.az,
587                                 ao_mag_angle,
588                                 ao_sample_orient,
589                                 ao_ground_mag.x,
590                                 ao_ground_mag.y,
591                                 ao_ground_mag.z,
592                                 ao_mag.x,
593                                 ao_mag.y,
594                                 ao_mag.z,
595                                 ao_mag_rot.x,
596                                 ao_mag_rot.y,
597                                 ao_mag_rot.z);
598 #endif
599 #endif
600
601 #if 1
602                         printf("%7.2f height %8.2f accel %8.3f "
603 #if TELEMEGA && 1
604                                "angle %5d "
605                                "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 "
606 #endif
607                                "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",
608                                time,
609                                height,
610                                accel,
611 #if TELEMEGA && 1
612                                ao_sample_orient,
613
614                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
615                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
616                                ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
617                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
618                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
619                                ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
620                                ao_data_static.hmc5883.x,
621                                ao_data_static.hmc5883.y,
622                                ao_data_static.hmc5883.z,
623                                ao_mag_angle,
624 #endif
625                                ao_state_names[ao_flight_state],
626                                ao_k_height / 65536.0,
627                                ao_k_speed / 65536.0 / 16.0,
628                                ao_k_accel / 65536.0 / 16.0,
629                                ao_avg_height,
630                                drogue_height,
631                                main_height,
632                                ao_error_h_sq_avg);
633 #endif
634                         
635 //                      if (ao_flight_state == ao_flight_landed)
636 //                              ao_test_exit();
637                 }
638         }
639 }
640
641
642 uint16_t
643 uint16(uint8_t *bytes, int off)
644 {
645         return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
646 }
647
648 int16_t
649 int16(uint8_t *bytes, int off)
650 {
651         return (int16_t) uint16(bytes, off);
652 }
653
654 uint32_t
655 uint32(uint8_t *bytes, int off)
656 {
657         return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
658                 (((uint32_t) bytes[off+2]) << 16) |
659                 (((uint32_t) bytes[off+3]) << 24);
660 }
661
662 int32_t
663 int32(uint8_t *bytes, int off)
664 {
665         return (int32_t) uint32(bytes, off);
666 }
667
668 static int log_format;
669
670 void
671 ao_sleep(void *wchan)
672 {
673         if (wchan == &ao_data_head) {
674                 char            type = 0;
675                 uint16_t        tick = 0;
676                 uint16_t        a = 0, b = 0;
677                 uint8_t         bytes[1024];
678                 union ao_telemetry_all  telem;
679                 char            line[1024];
680                 char            *saveptr;
681                 char            *l;
682                 char            *words[64];
683                 int             nword;
684
685 #if TELEMEGA
686                 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
687                         ao_pyro_check();
688 #endif
689                 for (;;) {
690                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
691                         {
692 #if TELEMEGA
693                                 ao_data_static.mpu6000 = ao_ground_mpu6000;
694 #endif
695 #if TELEMETRUM_V1
696                                 ao_data_static.adc.accel = ao_flight_ground_accel;
697 #endif
698                                 ao_insert();
699                                 return;
700                         }
701
702                         if (!fgets(line, sizeof (line), emulator_in)) {
703                                 if (++ao_eof_read >= 1000) {
704                                         if (!ao_summary)
705                                                 printf ("no more data, exiting simulation\n");
706                                         ao_test_exit();
707                                 }
708                                 ao_data_static.tick += 10;
709                                 ao_insert();
710                                 return;
711                         }
712                         l = line;
713                         for (nword = 0; nword < 64; nword++) {
714                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
715                                 l = NULL;
716                                 if (words[nword] == NULL)
717                                         break;
718                         }
719 #if TELEMEGA
720                         if ((log_format == AO_LOG_FORMAT_TELEMEGA_OLD || log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) {
721                                 int     i;
722                                 struct ao_ms5607_value  value;
723
724                                 type = words[0][0];
725                                 tick = strtoul(words[1], NULL, 16);
726 //                              printf ("%c %04x", type, tick);
727                                 for (i = 2; i < nword; i++) {
728                                         bytes[i - 2] = strtoul(words[i], NULL, 16);
729 //                                      printf(" %02x", bytes[i-2]);
730                                 }
731 //                              printf ("\n");
732                                 switch (type) {
733                                 case 'F':
734                                         ao_flight_ground_accel = int16(bytes, 2);
735                                         ao_flight_started = 1;
736                                         ao_ground_pres = int32(bytes, 4);
737                                         ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
738                                         ao_ground_accel_along = int16(bytes, 8);
739                                         ao_ground_accel_across = int16(bytes, 10);
740                                         ao_ground_accel_through = int16(bytes, 12);
741                                         ao_ground_roll = int16(bytes, 14);
742                                         ao_ground_pitch = int16(bytes, 16);
743                                         ao_ground_yaw = int16(bytes, 18);
744                                         ao_ground_mpu6000.accel_x = ao_ground_accel_across;
745                                         ao_ground_mpu6000.accel_y = ao_ground_accel_along;
746                                         ao_ground_mpu6000.accel_z = ao_ground_accel_through;
747                                         ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
748                                         ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
749                                         ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
750                                         break;
751                                 case 'A':
752                                         ao_data_static.tick = tick;
753                                         ao_data_static.ms5607_raw.pres = int32(bytes, 0);
754                                         ao_data_static.ms5607_raw.temp = int32(bytes, 4);
755                                         ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
756                                         ao_data_static.mpu6000.accel_x = int16(bytes, 8);
757                                         ao_data_static.mpu6000.accel_y = int16(bytes, 10);
758                                         ao_data_static.mpu6000.accel_z = int16(bytes, 12);
759                                         ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
760                                         ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
761                                         ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
762                                         ao_data_static.hmc5883.x = int16(bytes, 20);
763                                         ao_data_static.hmc5883.y = int16(bytes, 22);
764                                         ao_data_static.hmc5883.z = int16(bytes, 24);
765 #if HAS_MMA655X
766                                         ao_data_static.mma655x = int16(bytes, 26);
767                                         if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
768                                                 ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
769 #endif
770                                         ao_records_read++;
771                                         ao_insert();
772                                         return;
773                                 case 'G':
774                                         ao_gps_prev = ao_gps_static;
775                                         ao_gps_static.tick = tick;
776                                         ao_gps_static.latitude = int32(bytes, 0);
777                                         ao_gps_static.longitude = int32(bytes, 4);
778                                         {
779                                                 int32_t altitude = int32(bytes, 8);
780                                                 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
781                                         }
782                                         ao_gps_static.flags = bytes[13];
783                                         if (!ao_gps_count)
784                                                 ao_gps_first = ao_gps_static;
785                                         ao_gps_count++;
786                                         break;
787                                 }
788                                 continue;
789                         } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
790                                 if (strcmp(words[1], "reserved:") == 0)
791                                         ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
792                                 else if (strcmp(words[1], "sens:") == 0)
793                                         ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
794                                 else if (strcmp(words[1], "off:") == 0)
795                                         ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
796                                 else if (strcmp(words[1], "tcs:") == 0)
797                                         ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
798                                 else if (strcmp(words[1], "tco:") == 0)
799                                         ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
800                                 else if (strcmp(words[1], "tref:") == 0)
801                                         ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
802                                 else if (strcmp(words[1], "tempsens:") == 0)
803                                         ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
804                                 else if (strcmp(words[1], "crc:") == 0)
805                                         ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
806                                 continue;
807                         } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
808                                 int     p = strtoul(words[1], NULL, 10);
809                                 int     i, j;
810                                 struct ao_pyro  *pyro = &ao_config.pyro[p];
811
812                                 for (i = 2; i < nword; i++) {
813                                         for (j = 0; j < NUM_PYRO_VALUES; j++)
814                                                 if (!strcmp (words[i], ao_pyro_values[j].name))
815                                                         break;
816                                         if (j == NUM_PYRO_VALUES)
817                                                 continue;
818                                         pyro->flags |= ao_pyro_values[j].flag;
819                                         if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
820                                                 int16_t val = strtoul(words[++i], NULL, 10);
821                                                 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
822                                                 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
823                                         }
824                                 }
825                         }
826 #endif
827 #if TELEMETRUM_V2
828                         if (log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1) {
829                                 int     i;
830                                 struct ao_ms5607_value  value;
831
832                                 type = words[0][0];
833                                 tick = strtoul(words[1], NULL, 16);
834 //                              printf ("%c %04x", type, tick);
835                                 for (i = 2; i < nword; i++) {
836                                         bytes[i - 2] = strtoul(words[i], NULL, 16);
837 //                                      printf(" %02x", bytes[i-2]);
838                                 }
839 //                              printf ("\n");
840                                 switch (type) {
841                                 case 'F':
842                                         ao_flight_ground_accel = int16(bytes, 2);
843                                         ao_flight_started = 1;
844                                         ao_ground_pres = int32(bytes, 4);
845                                         ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
846                                         break;
847                                 case 'A':
848                                         ao_data_static.tick = tick;
849                                         ao_data_static.ms5607_raw.pres = int32(bytes, 0);
850                                         ao_data_static.ms5607_raw.temp = int32(bytes, 4);
851                                         ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
852                                         ao_data_static.mma655x = int16(bytes, 8);
853                                         ao_records_read++;
854                                         ao_insert();
855                                         return;
856                                 }
857                                 continue;
858                         } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
859                                 if (strcmp(words[1], "reserved:") == 0)
860                                         ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
861                                 else if (strcmp(words[1], "sens:") == 0)
862                                         ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
863                                 else if (strcmp(words[1], "off:") == 0)
864                                         ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
865                                 else if (strcmp(words[1], "tcs:") == 0)
866                                         ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
867                                 else if (strcmp(words[1], "tco:") == 0)
868                                         ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
869                                 else if (strcmp(words[1], "tref:") == 0)
870                                         ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
871                                 else if (strcmp(words[1], "tempsens:") == 0)
872                                         ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
873                                 else if (strcmp(words[1], "crc:") == 0)
874                                         ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
875                                 continue;
876                         }
877 #endif
878 #if TELEMETRUM_V1
879                         if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
880                                 type = words[0][0];
881                                 tick = strtoul(words[1], NULL, 16);
882                                 a = strtoul(words[2], NULL, 16);
883                                 b = strtoul(words[3], NULL, 16);
884                                 if (type == 'P')
885                                         type = 'A';
886                         }
887 #endif
888                         else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
889                                 log_format = strtoul(words[1], NULL, 10);
890                         } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
891                                 ao_config.accel_plus_g = atoi(words[3]);
892                                 ao_config.accel_minus_g = atoi(words[5]);
893 #ifdef TELEMEGA
894                         } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
895                                 ao_config.accel_zero_along = atoi(words[3]);
896                                 ao_config.accel_zero_across = atoi(words[5]);
897                                 ao_config.accel_zero_through = atoi(words[7]);
898 #endif
899                         } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
900                                 ao_config.main_deploy = atoi(words[2]);
901                         } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
902                                    strcmp(words[1], "lockout:") == 0) {
903                                 ao_config.apogee_lockout = atoi(words[2]);
904                         } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
905                                    strcmp(words[1], "orientation:") == 0) {
906                                 ao_config.pad_orientation = atoi(words[2]);
907                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
908                                 tick = atoi(words[10]);
909                                 if (!ao_flight_started) {
910                                         type = 'F';
911                                         a = atoi(words[26]);
912                                         ao_flight_started = 1;
913                                 } else {
914                                         type = 'A';
915                                         a = atoi(words[12]);
916                                         b = atoi(words[14]);
917                                 }
918                         } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
919                                 tick = strtol(words[1], NULL, 16);
920                                 a = 16384 - 328;
921                                 b = strtol(words[2], NULL, 10);
922                                 type = 'A';
923                                 if (!ao_flight_started) {
924                                         ao_flight_ground_accel = 16384 - 328;
925                                         ao_config.accel_plus_g = 16384 - 328;
926                                         ao_config.accel_minus_g = 16384 + 328;
927                                         ao_flight_started = 1;
928                                 }
929                         } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
930                                 __xdata char    *hex = words[1];
931                                 char    elt[3];
932                                 int     i, len;
933                                 uint8_t sum;
934
935                                 len = strlen(hex);
936                                 if (len > sizeof (bytes) * 2) {
937                                         len = sizeof (bytes)*2;
938                                         hex[len] = '\0';
939                                 }
940                                 for (i = 0; i < len; i += 2) {
941                                         elt[0] = hex[i];
942                                         elt[1] = hex[i+1];
943                                         elt[2] = '\0';
944                                         bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
945                                 }
946                                 len = i/2;
947                                 if (bytes[0] != len - 2) {
948                                         printf ("bad length %d != %d\n", bytes[0], len - 2);
949                                         continue;
950                                 }
951                                 sum = 0x5a;
952                                 for (i = 1; i < len-1; i++)
953                                         sum += bytes[i];
954                                 if (sum != bytes[len-1]) {
955                                         printf ("bad checksum\n");
956                                         continue;
957                                 }
958                                 if ((bytes[len-2] & 0x80) == 0) {
959                                         continue;
960                                 }
961                                 if (len == 36) {
962                                         ao_xmemcpy(&telem, bytes + 1, 32);
963                                         tick = telem.generic.tick;
964                                         switch (telem.generic.type) {
965                                         case AO_TELEMETRY_SENSOR_TELEMETRUM:
966                                         case AO_TELEMETRY_SENSOR_TELEMINI:
967                                         case AO_TELEMETRY_SENSOR_TELENANO:
968                                                 if (!ao_flight_started) {
969                                                         ao_flight_ground_accel = telem.sensor.ground_accel;
970                                                         ao_config.accel_plus_g = telem.sensor.accel_plus_g;
971                                                         ao_config.accel_minus_g = telem.sensor.accel_minus_g;
972                                                         ao_flight_started = 1;
973                                                 }
974                                                 type = 'A';
975                                                 a = telem.sensor.accel;
976                                                 b = telem.sensor.pres;
977                                                 break;
978                                         }
979                                 } else if (len == 99) {
980                                         ao_flight_started = 1;
981                                         tick = uint16(bytes+1, 21);
982                                         ao_flight_ground_accel = int16(bytes+1, 7);
983                                         ao_config.accel_plus_g = int16(bytes+1, 17);
984                                         ao_config.accel_minus_g = int16(bytes+1, 19);
985                                         type = 'A';
986                                         a = int16(bytes+1, 23);
987                                         b = int16(bytes+1, 25);
988                                 } else if (len == 98) {
989                                         ao_flight_started = 1;
990                                         tick = uint16(bytes+1, 20);
991                                         ao_flight_ground_accel = int16(bytes+1, 6);
992                                         ao_config.accel_plus_g = int16(bytes+1, 16);
993                                         ao_config.accel_minus_g = int16(bytes+1, 18);
994                                         type = 'A';
995                                         a = int16(bytes+1, 22);
996                                         b = int16(bytes+1, 24);
997                                 } else {
998                                         printf("unknown len %d\n", len);
999                                         continue;
1000                                 }
1001                         }
1002                         if (type != 'F' && !ao_flight_started)
1003                                 continue;
1004
1005 #if TELEMEGA || TELEMETRUM_V2
1006                         (void) a;
1007                         (void) b;
1008 #else
1009                         switch (type) {
1010                         case 'F':
1011                                 ao_flight_ground_accel = a;
1012                                 if (ao_config.accel_plus_g == 0) {
1013                                         ao_config.accel_plus_g = a;
1014                                         ao_config.accel_minus_g = a + 530;
1015                                 }
1016                                 if (ao_config.main_deploy == 0)
1017                                         ao_config.main_deploy = 250;
1018                                 ao_flight_started = 1;
1019                                 break;
1020                         case 'S':
1021                                 break;
1022                         case 'A':
1023                                 ao_data_static.tick = tick;
1024                                 ao_data_static.adc.accel = a;
1025                                 ao_data_static.adc.pres_real = b;
1026                                 ao_data_static.adc.pres = b;
1027                                 ao_records_read++;
1028                                 ao_insert();
1029                                 return;
1030                         case 'T':
1031                                 ao_data_static.tick = tick;
1032                                 ao_data_static.adc.temp = a;
1033                                 ao_data_static.adc.v_batt = b;
1034                                 break;
1035                         case 'D':
1036                         case 'G':
1037                         case 'N':
1038                         case 'W':
1039                         case 'H':
1040                                 break;
1041                         }
1042 #endif
1043                 }
1044
1045         }
1046 }
1047 #define COUNTS_PER_G 264.8
1048
1049 void
1050 ao_dump_state(void)
1051 {
1052 }
1053
1054 static const struct option options[] = {
1055         { .name = "summary", .has_arg = 0, .val = 's' },
1056         { .name = "debug", .has_arg = 0, .val = 'd' },
1057         { .name = "info", .has_arg = 1, .val = 'i' },
1058         { 0, 0, 0, 0},
1059 };
1060
1061 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1062 {
1063         emulator_name = name;
1064         emulator_in = f;
1065         emulator_info = info;
1066         ao_summary = summary;
1067
1068         ao_flight_init();
1069         ao_flight();
1070 }
1071
1072 int
1073 main (int argc, char **argv)
1074 {
1075         int     summary = 0;
1076         int     c;
1077         int     i;
1078         char    *info = NULL;
1079
1080 #if HAS_ACCEL
1081         emulator_app="full";
1082 #else
1083         emulator_app="baro";
1084 #endif
1085         while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1086                 switch (c) {
1087                 case 's':
1088                         summary = 1;
1089                         break;
1090                 case 'd':
1091                         ao_flight_debug = 1;
1092                         break;
1093                 case 'i':
1094                         info = optarg;
1095                         break;
1096                 }
1097         }
1098
1099         if (optind == argc)
1100                 run_flight_fixed("<stdin>", stdin, summary, info);
1101         else
1102                 for (i = optind; i < argc; i++) {
1103                         FILE    *f = fopen(argv[i], "r");
1104                         if (!f) {
1105                                 perror(argv[i]);
1106                                 continue;
1107                         }
1108                         run_flight_fixed(argv[i], f, summary, info);
1109                         fclose(f);
1110                 }
1111         exit(0);
1112 }