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