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))
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))
43 #define AO_GPS_NEW_DATA 1
44 #define AO_GPS_NEW_TRACKING 2
48 #if !defined(TELEMEGA) && !defined(TELEMETRUM_V2)
49 #define TELEMETRUM_V1 1
53 #define AO_ADC_NUM_SENSE 6
59 #define AO_CONFIG_MAX_SIZE 1024
62 int16_t sense[AO_ADC_NUM_SENSE];
70 #define AO_ADC_NUM_SENSE 2
74 #define AO_CONFIG_MAX_SIZE 1024
87 * One set of samples read from the A/D converter
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 */
101 #define HAS_ACCEL_REF 0
119 #include <ao_telemetry.h>
120 #include <ao_sample.h>
124 struct ao_telemetry_location ao_gps_first;
125 struct ao_telemetry_location ao_gps_prev;
126 struct ao_telemetry_location ao_gps_static;
128 struct ao_telemetry_satellite ao_gps_tracking;
130 static inline double sqr(double a) { return a * a; }
133 cc_great_circle (double start_lat, double start_lon,
134 double end_lat, double end_lon,
135 double *dist, double *bearing)
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;
144 // double d_lat = lat2 - lat1;
145 double d_lon = lon2 - lon1;
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);
155 if (cos(lat1) < 1e-20) {
164 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
166 if (sin(lon2-lon1) > 0)
167 course = 2 * M_PI-course;
169 *dist = d * earth_radius;
170 *bearing = course * 180/M_PI;
174 ao_distance_from_pad(void)
176 double dist, bearing;
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,
191 double dist, bearing;
195 if (ao_gps_count < 2)
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,
203 height = AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_static) - AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_prev);
205 angle = atan2(dist, height);
206 return angle * 180/M_PI;
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)
214 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
216 extern enum ao_flight_state ao_flight_state;
221 volatile struct ao_data ao_data_ring[AO_DATA_RING];
222 volatile uint8_t ao_data_head;
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()
238 ao_igniter_drogue = 0,
242 struct ao_data ao_data_static;
251 static ao_k_t ao_k_height;
256 return ao_data_static.tick;
260 ao_delay(int16_t interval)
266 ao_ignite(enum ao_igniter igniter)
268 double time = (double) (ao_data_static.tick + tick_offset) / 100;
270 if (igniter == ao_igniter_drogue) {
272 drogue_height = ao_k_height >> 16;
275 main_height = ao_k_height >> 16;
283 #define ao_add_task(t,f,n) ((void) (t))
285 #define ao_log_start()
286 #define ao_log_stop()
288 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
289 #define AO_SEC_TO_TICKS(s) ((s) * 100)
291 #define AO_FLIGHT_TEST
299 double emulator_error_max = 4;
300 double emulator_height_error_max = 20; /* noise in the baro sensor */
306 ao_sleep(void *wchan);
308 const char const * const ao_state_names[] = {
309 "startup", "idle", "pad", "boost", "fast",
310 "coast", "drogue", "main", "landed", "invalid"
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)
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
331 #include "ao_convert.c"
334 #include <ao_config.h>
335 #include <ao_fake_flight.h>
337 #define ao_config_get()
339 struct ao_config ao_config;
341 #define DATA_TO_XDATA(x) (x)
344 extern int16_t ao_ground_accel, ao_flight_accel;
345 extern int16_t ao_accel_2g;
347 typedef int16_t accel_t;
349 extern uint16_t ao_sample_tick;
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;
357 double ao_sample_qangle;
359 int ao_sample_prev_tick;
363 #include "ao_kalman.c"
365 #include "ao_sample.c"
366 #include "ao_flight.c"
368 #define AO_PYRO_NUM 4
376 ao_pyro_pin_set(uint8_t pin, uint8_t value)
378 printf ("set pyro %d %d\n", pin, value);
384 #define to_double(f) ((f) / 65536.0)
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;
402 static struct ao_mpu6000_sample ao_ground_mpu6000;
411 double landed_time_error;
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;
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) {
423 emulator_app, emulator_name);
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);
449 azel (struct ao_azel *r, struct ao_quaternion *q)
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);
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) {
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);
473 #if TELEMEGA || TELEMETRUM_V2
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;
479 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
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;
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;
496 if (height > ao_config.main_deploy) {
497 ao_test_main_height_time = time;
498 ao_test_main_height = height;
501 if (ao_test_landed_height > height) {
502 ao_test_landed_height = height;
503 ao_test_landed_time = time;
506 if (ao_flight_state == ao_flight_landed && !landed_set) {
509 landed_height = height;
514 static struct ao_quaternion ao_ground_mag;
515 static int ao_ground_mag_set;
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;
527 struct ao_quaternion ao_mag, ao_mag_rot;
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));
534 ao_quaternion_normalize(&ao_mag, &ao_mag);
535 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
540 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
542 struct ao_azel ground_azel, mag_azel, rot_azel;
544 azel(&ground_azel, &ao_ground_mag);
545 azel(&mag_azel, &ao_mag);
546 azel(&rot_azel, &ao_mag_rot);
548 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
552 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
553 struct ao_quaternion ao_out;
555 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
558 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
560 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
562 ao_state_names[ao_flight_state],
563 ao_k_height / 65536.0,
564 ao_sample_orient, out,
569 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
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);
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,
602 printf("%7.2f height %8.2f accel %8.3f "
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 "
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",
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,
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,
635 // if (ao_flight_state == ao_flight_landed)
643 uint16(uint8_t *bytes, int off)
645 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
649 int16(uint8_t *bytes, int off)
651 return (int16_t) uint16(bytes, off);
655 uint32(uint8_t *bytes, int off)
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);
663 int32(uint8_t *bytes, int off)
665 return (int32_t) uint32(bytes, off);
668 static int log_format;
671 ao_sleep(void *wchan)
673 if (wchan == &ao_data_head) {
676 uint16_t a = 0, b = 0;
678 union ao_telemetry_all telem;
686 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
690 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
693 ao_data_static.mpu6000 = ao_ground_mpu6000;
696 ao_data_static.adc.accel = ao_flight_ground_accel;
702 if (!fgets(line, sizeof (line), emulator_in)) {
703 if (++ao_eof_read >= 1000) {
705 printf ("no more data, exiting simulation\n");
708 ao_data_static.tick += 10;
713 for (nword = 0; nword < 64; nword++) {
714 words[nword] = strtok_r(l, " \t\n", &saveptr);
716 if (words[nword] == NULL)
720 if ((log_format == AO_LOG_FORMAT_TELEMEGA_OLD || log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) {
722 struct ao_ms5607_value value;
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]);
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;
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);
766 ao_data_static.mma655x = int16(bytes, 26);
767 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
768 ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
774 ao_gps_prev = ao_gps_static;
775 ao_gps_static.tick = tick;
776 ao_gps_static.latitude = int32(bytes, 0);
777 ao_gps_static.longitude = int32(bytes, 4);
779 int32_t altitude = int32(bytes, 8);
780 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
782 ao_gps_static.flags = bytes[13];
784 ao_gps_first = ao_gps_static;
789 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
790 if (strcmp(words[1], "reserved:") == 0)
791 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
792 else if (strcmp(words[1], "sens:") == 0)
793 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
794 else if (strcmp(words[1], "off:") == 0)
795 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
796 else if (strcmp(words[1], "tcs:") == 0)
797 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
798 else if (strcmp(words[1], "tco:") == 0)
799 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
800 else if (strcmp(words[1], "tref:") == 0)
801 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
802 else if (strcmp(words[1], "tempsens:") == 0)
803 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
804 else if (strcmp(words[1], "crc:") == 0)
805 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
807 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
808 int p = strtoul(words[1], NULL, 10);
810 struct ao_pyro *pyro = &ao_config.pyro[p];
812 for (i = 2; i < nword; i++) {
813 for (j = 0; j < NUM_PYRO_VALUES; j++)
814 if (!strcmp (words[i], ao_pyro_values[j].name))
816 if (j == NUM_PYRO_VALUES)
818 pyro->flags |= ao_pyro_values[j].flag;
819 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
820 int16_t val = strtoul(words[++i], NULL, 10);
821 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
822 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
828 if (log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1) {
830 struct ao_ms5607_value value;
833 tick = strtoul(words[1], NULL, 16);
834 // printf ("%c %04x", type, tick);
835 for (i = 2; i < nword; i++) {
836 bytes[i - 2] = strtoul(words[i], NULL, 16);
837 // printf(" %02x", bytes[i-2]);
842 ao_flight_ground_accel = int16(bytes, 2);
843 ao_flight_started = 1;
844 ao_ground_pres = int32(bytes, 4);
845 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
848 ao_data_static.tick = tick;
849 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
850 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
851 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
852 ao_data_static.mma655x = int16(bytes, 8);
858 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
859 if (strcmp(words[1], "reserved:") == 0)
860 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
861 else if (strcmp(words[1], "sens:") == 0)
862 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
863 else if (strcmp(words[1], "off:") == 0)
864 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
865 else if (strcmp(words[1], "tcs:") == 0)
866 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
867 else if (strcmp(words[1], "tco:") == 0)
868 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
869 else if (strcmp(words[1], "tref:") == 0)
870 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
871 else if (strcmp(words[1], "tempsens:") == 0)
872 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
873 else if (strcmp(words[1], "crc:") == 0)
874 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
879 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
881 tick = strtoul(words[1], NULL, 16);
882 a = strtoul(words[2], NULL, 16);
883 b = strtoul(words[3], NULL, 16);
888 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
889 log_format = strtoul(words[1], NULL, 10);
890 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
891 ao_config.accel_plus_g = atoi(words[3]);
892 ao_config.accel_minus_g = atoi(words[5]);
894 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
895 ao_config.accel_zero_along = atoi(words[3]);
896 ao_config.accel_zero_across = atoi(words[5]);
897 ao_config.accel_zero_through = atoi(words[7]);
899 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
900 ao_config.main_deploy = atoi(words[2]);
901 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
902 strcmp(words[1], "lockout:") == 0) {
903 ao_config.apogee_lockout = atoi(words[2]);
904 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
905 strcmp(words[1], "orientation:") == 0) {
906 ao_config.pad_orientation = atoi(words[2]);
907 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
908 tick = atoi(words[10]);
909 if (!ao_flight_started) {
912 ao_flight_started = 1;
918 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
919 tick = strtol(words[1], NULL, 16);
921 b = strtol(words[2], NULL, 10);
923 if (!ao_flight_started) {
924 ao_flight_ground_accel = 16384 - 328;
925 ao_config.accel_plus_g = 16384 - 328;
926 ao_config.accel_minus_g = 16384 + 328;
927 ao_flight_started = 1;
929 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
930 __xdata char *hex = words[1];
936 if (len > sizeof (bytes) * 2) {
937 len = sizeof (bytes)*2;
940 for (i = 0; i < len; i += 2) {
944 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
947 if (bytes[0] != len - 2) {
948 printf ("bad length %d != %d\n", bytes[0], len - 2);
952 for (i = 1; i < len-1; i++)
954 if (sum != bytes[len-1]) {
955 printf ("bad checksum\n");
958 if ((bytes[len-2] & 0x80) == 0) {
962 ao_xmemcpy(&telem, bytes + 1, 32);
963 tick = telem.generic.tick;
964 switch (telem.generic.type) {
965 case AO_TELEMETRY_SENSOR_TELEMETRUM:
966 case AO_TELEMETRY_SENSOR_TELEMINI:
967 case AO_TELEMETRY_SENSOR_TELENANO:
968 if (!ao_flight_started) {
969 ao_flight_ground_accel = telem.sensor.ground_accel;
970 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
971 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
972 ao_flight_started = 1;
975 a = telem.sensor.accel;
976 b = telem.sensor.pres;
979 } else if (len == 99) {
980 ao_flight_started = 1;
981 tick = uint16(bytes+1, 21);
982 ao_flight_ground_accel = int16(bytes+1, 7);
983 ao_config.accel_plus_g = int16(bytes+1, 17);
984 ao_config.accel_minus_g = int16(bytes+1, 19);
986 a = int16(bytes+1, 23);
987 b = int16(bytes+1, 25);
988 } else if (len == 98) {
989 ao_flight_started = 1;
990 tick = uint16(bytes+1, 20);
991 ao_flight_ground_accel = int16(bytes+1, 6);
992 ao_config.accel_plus_g = int16(bytes+1, 16);
993 ao_config.accel_minus_g = int16(bytes+1, 18);
995 a = int16(bytes+1, 22);
996 b = int16(bytes+1, 24);
998 printf("unknown len %d\n", len);
1002 if (type != 'F' && !ao_flight_started)
1005 #if TELEMEGA || TELEMETRUM_V2
1011 ao_flight_ground_accel = a;
1012 if (ao_config.accel_plus_g == 0) {
1013 ao_config.accel_plus_g = a;
1014 ao_config.accel_minus_g = a + 530;
1016 if (ao_config.main_deploy == 0)
1017 ao_config.main_deploy = 250;
1018 ao_flight_started = 1;
1023 ao_data_static.tick = tick;
1024 ao_data_static.adc.accel = a;
1025 ao_data_static.adc.pres_real = b;
1026 ao_data_static.adc.pres = b;
1031 ao_data_static.tick = tick;
1032 ao_data_static.adc.temp = a;
1033 ao_data_static.adc.v_batt = b;
1047 #define COUNTS_PER_G 264.8
1054 static const struct option options[] = {
1055 { .name = "summary", .has_arg = 0, .val = 's' },
1056 { .name = "debug", .has_arg = 0, .val = 'd' },
1057 { .name = "info", .has_arg = 1, .val = 'i' },
1061 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1063 emulator_name = name;
1065 emulator_info = info;
1066 ao_summary = summary;
1073 main (int argc, char **argv)
1081 emulator_app="full";
1083 emulator_app="baro";
1085 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1091 ao_flight_debug = 1;
1100 run_flight_fixed("<stdin>", stdin, summary, info);
1102 for (i = optind; i < argc; i++) {
1103 FILE *f = fopen(argv[i], "r");
1108 run_flight_fixed(argv[i], f, summary, info);