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