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