2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
28 #define GRAVITY 9.80665
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))
37 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
38 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
39 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
41 #define AO_GPS_NEW_DATA 1
42 #define AO_GPS_NEW_TRACKING 2
47 #define AO_ADC_NUM_SENSE 6
55 int16_t sense[AO_ADC_NUM_SENSE];
62 * One set of samples read from the A/D converter
65 int16_t accel; /* accelerometer */
66 int16_t pres; /* pressure sensor */
67 int16_t pres_real; /* unclipped */
68 int16_t temp; /* temperature sensor */
69 int16_t v_batt; /* battery voltage */
70 int16_t sense_d; /* drogue continuity sense */
71 int16_t sense_m; /* main continuity sense */
76 #define HAS_ACCEL_REF 0
94 #include <ao_telemetry.h>
98 struct ao_telemetry_location ao_gps_first;
99 struct ao_telemetry_location ao_gps_prev;
100 struct ao_telemetry_location ao_gps_static;
102 struct ao_telemetry_satellite ao_gps_tracking;
104 static inline double sqr(double a) { return a * a; }
107 cc_great_circle (double start_lat, double start_lon,
108 double end_lat, double end_lon,
109 double *dist, double *bearing)
111 const double rad = M_PI / 180;
112 const double earth_radius = 6371.2 * 1000; /* in meters */
113 double lat1 = rad * start_lat;
114 double lon1 = rad * -start_lon;
115 double lat2 = rad * end_lat;
116 double lon2 = rad * -end_lon;
118 // double d_lat = lat2 - lat1;
119 double d_lon = lon2 - lon1;
121 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
122 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
123 sqr(cos(lat1) * sin(lat2) -
124 sin(lat1) * cos(lat2) * cos(d_lon)));
125 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
126 double d = atan2(vdn,vdd);
129 if (cos(lat1) < 1e-20) {
138 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
140 if (sin(lon2-lon1) > 0)
141 course = 2 * M_PI-course;
143 *dist = d * earth_radius;
144 *bearing = course * 180/M_PI;
148 ao_distance_from_pad(void)
150 double dist, bearing;
154 cc_great_circle(ao_gps_first.latitude / 1e7,
155 ao_gps_first.longitude / 1e7,
156 ao_gps_static.latitude / 1e7,
157 ao_gps_static.longitude / 1e7,
165 double dist, bearing;
169 if (ao_gps_count < 2)
172 cc_great_circle(ao_gps_prev.latitude / 1e7,
173 ao_gps_prev.longitude / 1e7,
174 ao_gps_static.latitude / 1e7,
175 ao_gps_static.longitude / 1e7,
177 height = ao_gps_static.altitude - ao_gps_prev.altitude;
179 angle = atan2(dist, height);
180 return angle * 180/M_PI;
184 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
185 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
186 #define from_fix(x) ((x) >> 16)
189 * Above this height, the baro sensor doesn't work
191 #define AO_BARO_SATURATE 13000
192 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
195 * Above this speed, baro measurements are unreliable
197 #define AO_MAX_BARO_SPEED 200
199 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
201 extern enum ao_flight_state ao_flight_state;
206 volatile struct ao_data ao_data_ring[AO_DATA_RING];
207 volatile uint8_t ao_data_head;
211 #define ao_led_off(l)
212 #define ao_timer_set_adc_interval(i)
213 #define ao_wakeup(wchan) ao_dump_state()
214 #define ao_cmd_register(c)
215 #define ao_usb_disable()
216 #define ao_telemetry_set_interval(x)
217 #define ao_rdf_set(rdf)
218 #define ao_packet_slave_start()
219 #define ao_packet_slave_stop()
223 ao_igniter_drogue = 0,
227 struct ao_data ao_data_static;
236 static int32_t ao_k_height;
241 return ao_data_static.tick;
245 ao_delay(int16_t interval)
251 ao_ignite(enum ao_igniter igniter)
253 double time = (double) (ao_data_static.tick + tick_offset) / 100;
255 if (igniter == ao_igniter_drogue) {
257 drogue_height = ao_k_height >> 16;
260 main_height = ao_k_height >> 16;
268 #define ao_add_task(t,f,n) ((void) (t))
270 #define ao_log_start()
271 #define ao_log_stop()
273 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
274 #define AO_SEC_TO_TICKS(s) ((s) * 100)
276 #define AO_FLIGHT_TEST
284 double emulator_error_max = 4;
285 double emulator_height_error_max = 20; /* noise in the baro sensor */
291 ao_sleep(void *wchan);
293 const char const * const ao_state_names[] = {
294 "startup", "idle", "pad", "boost", "fast",
295 "coast", "drogue", "main", "landed", "invalid"
303 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
304 #define ao_xmemset(d,v,c) memset(d,v,c)
305 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
307 #define AO_NEED_ALTITUDE_TO_PRES 1
309 #include "ao_convert_pa.c"
310 #include <ao_ms5607.h>
311 struct ao_ms5607_prom ao_ms5607_prom;
312 #include "ao_ms5607_convert.c"
313 #define AO_PYRO_NUM 4
316 #include "ao_convert.c"
319 #include <ao_config.h>
320 #include <ao_fake_flight.h>
322 #define ao_config_get()
324 struct ao_config ao_config;
326 #define DATA_TO_XDATA(x) (x)
329 extern int16_t ao_ground_accel, ao_flight_accel;
330 extern int16_t ao_accel_2g;
332 typedef int16_t accel_t;
334 extern uint16_t ao_sample_tick;
336 extern alt_t ao_sample_height;
337 extern accel_t ao_sample_accel;
338 extern int32_t ao_accel_scale;
339 extern alt_t ao_ground_height;
340 extern alt_t ao_sample_alt;
342 double ao_sample_qangle;
344 int ao_sample_prev_tick;
348 #include "ao_kalman.c"
350 #include "ao_sample.c"
351 #include "ao_flight.c"
353 #define AO_PYRO_NUM 4
361 ao_pyro_pin_set(uint8_t pin, uint8_t value)
363 printf ("set pyro %d %d\n", pin, value);
369 #define to_double(f) ((f) / 65536.0)
371 static int ao_records_read = 0;
372 static int ao_eof_read = 0;
373 static int ao_flight_ground_accel;
374 static int ao_flight_started = 0;
375 static int ao_test_max_height;
376 static double ao_test_max_height_time;
377 static int ao_test_main_height;
378 static double ao_test_main_height_time;
379 static double ao_test_landed_time;
380 static double ao_test_landed_height;
381 static double ao_test_landed_time;
382 static int landed_set;
383 static double landed_time;
384 static double landed_height;
387 static struct ao_mpu6000_sample ao_ground_mpu6000;
396 double landed_time_error;
398 if (!ao_test_main_height_time) {
399 ao_test_main_height_time = ao_test_max_height_time;
400 ao_test_main_height = ao_test_max_height;
402 drogue_error = fabs(ao_test_max_height_time - drogue_time);
403 main_error = fabs(ao_test_main_height_time - main_time);
404 landed_error = fabs(ao_test_landed_height - landed_height);
405 landed_time_error = ao_test_landed_time - landed_time;
406 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
408 emulator_app, emulator_name);
410 printf ("\t%s\n", emulator_info);
411 printf ("\tApogee error %g\n", drogue_error);
412 printf ("\tMain error %g\n", main_error);
413 printf ("\tLanded height error %g\n", landed_error);
414 printf ("\tLanded time error %g\n", landed_time_error);
415 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
416 ao_test_max_height, ao_test_max_height_time,
417 ao_test_main_height, ao_test_main_height_time,
418 ao_test_landed_height, ao_test_landed_time);
419 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
420 drogue_height, drogue_time, main_height, main_time,
421 landed_height, landed_time);
434 azel (struct ao_azel *r, struct ao_quaternion *q)
438 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
439 v = sqrt (q->x*q->x + q->y*q->y);
440 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
449 ao_data_ring[ao_data_head] = ao_data_static;
450 ao_data_head = ao_data_ring_next(ao_data_head);
451 if (ao_flight_state != ao_flight_startup) {
453 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
454 (ao_config.accel_minus_g - ao_config.accel_plus_g);
461 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
462 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
464 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
469 tick_offset = -ao_data_static.tick;
470 if ((prev_tick - ao_data_static.tick) > 0x400)
471 tick_offset += 65536;
472 prev_tick = ao_data_static.tick;
473 time = (double) (ao_data_static.tick + tick_offset) / 100;
475 if (ao_test_max_height < height) {
476 ao_test_max_height = height;
477 ao_test_max_height_time = time;
478 ao_test_landed_height = height;
479 ao_test_landed_time = time;
481 if (height > ao_config.main_deploy) {
482 ao_test_main_height_time = time;
483 ao_test_main_height = height;
486 if (ao_test_landed_height > height) {
487 ao_test_landed_height = height;
488 ao_test_landed_time = time;
491 if (ao_flight_state == ao_flight_landed && !landed_set) {
494 landed_height = height;
499 static struct ao_quaternion ao_ground_mag;
500 static int ao_ground_mag_set;
502 if (!ao_ground_mag_set) {
503 ao_quaternion_init_vector (&ao_ground_mag,
504 ao_data_mag_across(&ao_data_static),
505 ao_data_mag_through(&ao_data_static),
506 ao_data_mag_along(&ao_data_static));
507 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
508 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
509 ao_ground_mag_set = 1;
512 struct ao_quaternion ao_mag, ao_mag_rot;
514 ao_quaternion_init_vector(&ao_mag,
515 ao_data_mag_across(&ao_data_static),
516 ao_data_mag_through(&ao_data_static),
517 ao_data_mag_along(&ao_data_static));
519 ao_quaternion_normalize(&ao_mag, &ao_mag);
520 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
525 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
527 struct ao_azel ground_azel, mag_azel, rot_azel;
529 azel(&ground_azel, &ao_ground_mag);
530 azel(&mag_azel, &ao_mag);
531 azel(&rot_azel, &ao_mag_rot);
533 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
537 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
538 struct ao_quaternion ao_out;
540 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
542 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
545 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
547 ao_state_names[ao_flight_state],
548 ao_k_height / 65536.0,
549 ao_sample_orient, out,
553 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
555 ao_state_names[ao_flight_state],
556 ao_k_height / 65536.0,
557 ao_sample_orient, out,
558 ao_distance_from_pad(),
559 (int) floor (ao_gps_angle() + 0.5),
560 (ao_gps_static.flags & 0xf) * 10);
563 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",
564 ao_state_names[ao_flight_state],
565 ground_azel.az, ground_azel.el,
566 mag_azel.az, mag_azel.el,
567 rot_azel.az, rot_azel.el,
568 ground_azel.el - rot_azel.el,
569 ground_azel.az - rot_azel.az,
585 printf("%7.2f height %8.2f accel %8.3f "
588 "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 "
590 "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",
597 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
598 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
599 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
600 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
601 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
602 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
603 ao_data_static.hmc5883.x,
604 ao_data_static.hmc5883.y,
605 ao_data_static.hmc5883.z,
608 ao_state_names[ao_flight_state],
609 ao_k_height / 65536.0,
610 ao_k_speed / 65536.0 / 16.0,
611 ao_k_accel / 65536.0 / 16.0,
618 // if (ao_flight_state == ao_flight_landed)
626 uint16(uint8_t *bytes, int off)
628 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
632 int16(uint8_t *bytes, int off)
634 return (int16_t) uint16(bytes, off);
638 uint32(uint8_t *bytes, int off)
640 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
641 (((uint32_t) bytes[off+2]) << 16) |
642 (((uint32_t) bytes[off+3]) << 24);
646 int32(uint8_t *bytes, int off)
648 return (int32_t) uint32(bytes, off);
651 static int log_format;
654 ao_sleep(void *wchan)
656 if (wchan == &ao_data_head) {
659 uint16_t a = 0, b = 0;
661 union ao_telemetry_all telem;
669 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
673 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
676 ao_data_static.mpu6000 = ao_ground_mpu6000;
678 ao_data_static.adc.accel = ao_flight_ground_accel;
684 if (!fgets(line, sizeof (line), emulator_in)) {
685 if (++ao_eof_read >= 1000) {
687 printf ("no more data, exiting simulation\n");
690 ao_data_static.tick += 10;
695 for (nword = 0; nword < 64; nword++) {
696 words[nword] = strtok_r(l, " \t\n", &saveptr);
698 if (words[nword] == NULL)
702 if (log_format == AO_LOG_FORMAT_TELEMEGA && nword == 30 && strlen(words[0]) == 1) {
704 struct ao_ms5607_value value;
707 tick = strtoul(words[1], NULL, 16);
708 // printf ("%c %04x", type, tick);
709 for (i = 2; i < nword; i++) {
710 bytes[i - 2] = strtoul(words[i], NULL, 16);
711 // printf(" %02x", bytes[i-2]);
716 ao_flight_ground_accel = int16(bytes, 2);
717 ao_flight_started = 1;
718 ao_ground_pres = int32(bytes, 4);
719 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
720 ao_ground_accel_along = int16(bytes, 8);
721 ao_ground_accel_across = int16(bytes, 10);
722 ao_ground_accel_through = int16(bytes, 12);
723 ao_ground_roll = int16(bytes, 14);
724 ao_ground_pitch = int16(bytes, 16);
725 ao_ground_yaw = int16(bytes, 18);
726 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
727 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
728 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
729 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
730 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
731 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
734 ao_data_static.tick = tick;
735 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
736 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
737 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
738 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
739 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
740 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
741 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
742 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
743 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
744 ao_data_static.hmc5883.x = int16(bytes, 20);
745 ao_data_static.hmc5883.y = int16(bytes, 22);
746 ao_data_static.hmc5883.z = int16(bytes, 24);
748 ao_data_static.mma655x = int16(bytes, 26);
754 ao_gps_prev = ao_gps_static;
755 ao_gps_static.tick = tick;
756 ao_gps_static.latitude = int32(bytes, 0);
757 ao_gps_static.longitude = int32(bytes, 4);
758 ao_gps_static.altitude = int32(bytes, 8);
759 ao_gps_static.flags = bytes[13];
761 ao_gps_first = ao_gps_static;
766 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
767 if (strcmp(words[1], "reserved:") == 0)
768 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
769 else if (strcmp(words[1], "sens:") == 0)
770 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
771 else if (strcmp(words[1], "off:") == 0)
772 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
773 else if (strcmp(words[1], "tcs:") == 0)
774 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
775 else if (strcmp(words[1], "tco:") == 0)
776 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
777 else if (strcmp(words[1], "tref:") == 0)
778 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
779 else if (strcmp(words[1], "tempsens:") == 0)
780 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
781 else if (strcmp(words[1], "crc:") == 0)
782 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
784 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
785 int p = strtoul(words[1], NULL, 10);
787 struct ao_pyro *pyro = &ao_config.pyro[p];
789 for (i = 2; i < nword; i++) {
790 for (j = 0; j < NUM_PYRO_VALUES; j++)
791 if (!strcmp (words[i], ao_pyro_values[j].name))
793 if (j == NUM_PYRO_VALUES)
795 pyro->flags |= ao_pyro_values[j].flag;
796 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
797 int16_t val = strtoul(words[++i], NULL, 10);
798 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
803 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
805 tick = strtoul(words[1], NULL, 16);
806 a = strtoul(words[2], NULL, 16);
807 b = strtoul(words[3], NULL, 16);
812 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
813 log_format = strtoul(words[1], NULL, 10);
814 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
815 ao_config.accel_plus_g = atoi(words[3]);
816 ao_config.accel_minus_g = atoi(words[5]);
818 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
819 ao_config.accel_zero_along = atoi(words[3]);
820 ao_config.accel_zero_across = atoi(words[5]);
821 ao_config.accel_zero_through = atoi(words[7]);
822 printf ("%d %d %d\n", ao_config.accel_zero_along, ao_config.accel_zero_across, ao_config.accel_zero_through);
824 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
825 ao_config.main_deploy = atoi(words[2]);
826 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
827 strcmp(words[1], "lockout:") == 0) {
828 ao_config.apogee_lockout = atoi(words[2]);
829 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
830 tick = atoi(words[10]);
831 if (!ao_flight_started) {
834 ao_flight_started = 1;
840 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
841 tick = strtol(words[1], NULL, 16);
843 b = strtol(words[2], NULL, 10);
845 if (!ao_flight_started) {
846 ao_flight_ground_accel = 16384 - 328;
847 ao_config.accel_plus_g = 16384 - 328;
848 ao_config.accel_minus_g = 16384 + 328;
849 ao_flight_started = 1;
851 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
852 __xdata char *hex = words[1];
858 if (len > sizeof (bytes) * 2) {
859 len = sizeof (bytes)*2;
862 for (i = 0; i < len; i += 2) {
866 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
869 if (bytes[0] != len - 2) {
870 printf ("bad length %d != %d\n", bytes[0], len - 2);
874 for (i = 1; i < len-1; i++)
876 if (sum != bytes[len-1]) {
877 printf ("bad checksum\n");
880 if ((bytes[len-2] & 0x80) == 0) {
884 ao_xmemcpy(&telem, bytes + 1, 32);
885 tick = telem.generic.tick;
886 switch (telem.generic.type) {
887 case AO_TELEMETRY_SENSOR_TELEMETRUM:
888 case AO_TELEMETRY_SENSOR_TELEMINI:
889 case AO_TELEMETRY_SENSOR_TELENANO:
890 if (!ao_flight_started) {
891 ao_flight_ground_accel = telem.sensor.ground_accel;
892 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
893 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
894 ao_flight_started = 1;
897 a = telem.sensor.accel;
898 b = telem.sensor.pres;
901 } else if (len == 99) {
902 ao_flight_started = 1;
903 tick = uint16(bytes+1, 21);
904 ao_flight_ground_accel = int16(bytes+1, 7);
905 ao_config.accel_plus_g = int16(bytes+1, 17);
906 ao_config.accel_minus_g = int16(bytes+1, 19);
908 a = int16(bytes+1, 23);
909 b = int16(bytes+1, 25);
910 } else if (len == 98) {
911 ao_flight_started = 1;
912 tick = uint16(bytes+1, 20);
913 ao_flight_ground_accel = int16(bytes+1, 6);
914 ao_config.accel_plus_g = int16(bytes+1, 16);
915 ao_config.accel_minus_g = int16(bytes+1, 18);
917 a = int16(bytes+1, 22);
918 b = int16(bytes+1, 24);
920 printf("unknown len %d\n", len);
924 if (type != 'F' && !ao_flight_started)
933 ao_flight_ground_accel = a;
934 if (ao_config.accel_plus_g == 0) {
935 ao_config.accel_plus_g = a;
936 ao_config.accel_minus_g = a + 530;
938 if (ao_config.main_deploy == 0)
939 ao_config.main_deploy = 250;
940 ao_flight_started = 1;
945 ao_data_static.tick = tick;
946 ao_data_static.adc.accel = a;
947 ao_data_static.adc.pres_real = b;
948 if (b < AO_MIN_BARO_VALUE)
949 b = AO_MIN_BARO_VALUE;
950 ao_data_static.adc.pres = b;
955 ao_data_static.tick = tick;
956 ao_data_static.adc.temp = a;
957 ao_data_static.adc.v_batt = b;
971 #define COUNTS_PER_G 264.8
978 static const struct option options[] = {
979 { .name = "summary", .has_arg = 0, .val = 's' },
980 { .name = "debug", .has_arg = 0, .val = 'd' },
981 { .name = "info", .has_arg = 1, .val = 'i' },
985 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
987 emulator_name = name;
989 emulator_info = info;
990 ao_summary = summary;
997 main (int argc, char **argv)
1005 emulator_app="full";
1007 emulator_app="baro";
1009 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1015 ao_flight_debug = 1;
1024 run_flight_fixed("<stdin>", stdin, summary, info);
1026 for (i = optind; i < argc; i++) {
1027 FILE *f = fopen(argv[i], "r");
1032 run_flight_fixed(argv[i], f, summary, info);