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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 #define GRAVITY 9.80665
34 #define AO_DATA_RING 64
35 #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1))
36 #define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1))
39 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
40 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
41 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
44 #define AO_GPS_NEW_DATA 1
45 #define AO_GPS_NEW_TRACKING 2
49 #if !defined(TELEMEGA) && !defined(TELEMETRUM_V2)
50 #define TELEMETRUM_V1 1
54 #define AO_ADC_NUM_SENSE 6
60 #define AO_CONFIG_MAX_SIZE 1024
63 int16_t sense[AO_ADC_NUM_SENSE];
71 #define AO_ADC_NUM_SENSE 2
75 #define AO_CONFIG_MAX_SIZE 1024
88 * One set of samples read from the A/D converter
91 int16_t accel; /* accelerometer */
92 int16_t pres; /* pressure sensor */
93 int16_t pres_real; /* unclipped */
94 int16_t temp; /* temperature sensor */
95 int16_t v_batt; /* battery voltage */
96 int16_t sense_d; /* drogue continuity sense */
97 int16_t sense_m; /* main continuity sense */
102 #define HAS_ACCEL_REF 0
120 #include <ao_telemetry.h>
121 #include <ao_sample.h>
125 struct ao_telemetry_location ao_gps_first;
126 struct ao_telemetry_location ao_gps_prev;
127 struct ao_telemetry_location ao_gps_static;
129 struct ao_telemetry_satellite ao_gps_tracking;
131 static inline double sqr(double a) { return a * a; }
134 cc_great_circle (double start_lat, double start_lon,
135 double end_lat, double end_lon,
136 double *dist, double *bearing)
138 const double rad = M_PI / 180;
139 const double earth_radius = 6371.2 * 1000; /* in meters */
140 double lat1 = rad * start_lat;
141 double lon1 = rad * -start_lon;
142 double lat2 = rad * end_lat;
143 double lon2 = rad * -end_lon;
145 // double d_lat = lat2 - lat1;
146 double d_lon = lon2 - lon1;
148 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
149 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
150 sqr(cos(lat1) * sin(lat2) -
151 sin(lat1) * cos(lat2) * cos(d_lon)));
152 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
153 double d = atan2(vdn,vdd);
156 if (cos(lat1) < 1e-20) {
165 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
167 if (sin(lon2-lon1) > 0)
168 course = 2 * M_PI-course;
170 *dist = d * earth_radius;
171 *bearing = course * 180/M_PI;
175 ao_distance_from_pad(void)
177 double dist, bearing;
181 cc_great_circle(ao_gps_first.latitude / 1e7,
182 ao_gps_first.longitude / 1e7,
183 ao_gps_static.latitude / 1e7,
184 ao_gps_static.longitude / 1e7,
192 double dist, bearing;
196 if (ao_gps_count < 2)
199 cc_great_circle(ao_gps_prev.latitude / 1e7,
200 ao_gps_prev.longitude / 1e7,
201 ao_gps_static.latitude / 1e7,
202 ao_gps_static.longitude / 1e7,
204 height = AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_static) - AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_prev);
206 angle = atan2(dist, height);
207 return angle * 180/M_PI;
211 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
212 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
213 #define from_fix(x) ((x) >> 16)
215 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
217 extern enum ao_flight_state ao_flight_state;
222 volatile struct ao_data ao_data_ring[AO_DATA_RING];
223 volatile uint8_t ao_data_head;
227 #define ao_led_off(l)
228 #define ao_timer_set_adc_interval(i)
229 #define ao_wakeup(wchan) ao_dump_state()
230 #define ao_cmd_register(c)
231 #define ao_usb_disable()
232 #define ao_telemetry_set_interval(x)
233 #define ao_rdf_set(rdf)
234 #define ao_packet_slave_start()
235 #define ao_packet_slave_stop()
239 ao_igniter_drogue = 0,
243 struct ao_data ao_data_static;
252 static ao_k_t ao_k_height;
257 return ao_data_static.tick;
261 ao_delay(int16_t interval)
267 ao_ignite(enum ao_igniter igniter)
269 double time = (double) (ao_data_static.tick + tick_offset) / 100;
271 if (igniter == ao_igniter_drogue) {
273 drogue_height = ao_k_height >> 16;
276 main_height = ao_k_height >> 16;
284 #define ao_add_task(t,f,n) ((void) (t))
286 #define ao_log_start()
287 #define ao_log_stop()
289 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
290 #define AO_SEC_TO_TICKS(s) ((s) * 100)
292 #define AO_FLIGHT_TEST
300 double emulator_error_max = 4;
301 double emulator_height_error_max = 20; /* noise in the baro sensor */
307 ao_sleep(void *wchan);
309 const char const * const ao_state_names[] = {
310 "startup", "idle", "pad", "boost", "fast",
311 "coast", "drogue", "main", "landed", "invalid"
319 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
320 #define ao_xmemset(d,v,c) memset(d,v,c)
321 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
323 #define AO_NEED_ALTITUDE_TO_PRES 1
324 #if TELEMEGA || TELEMETRUM_V2
325 #include "ao_convert_pa.c"
326 #include <ao_ms5607.h>
327 struct ao_ms5607_prom ao_ms5607_prom;
328 #include "ao_ms5607_convert.c"
329 #define AO_PYRO_NUM 4
332 #include "ao_convert.c"
335 #include <ao_config.h>
336 #include <ao_fake_flight.h>
338 #define ao_config_get()
340 struct ao_config ao_config;
342 #define DATA_TO_XDATA(x) (x)
345 extern int16_t ao_ground_accel, ao_flight_accel;
346 extern int16_t ao_accel_2g;
348 typedef int16_t accel_t;
350 extern uint16_t ao_sample_tick;
352 extern alt_t ao_sample_height;
353 extern accel_t ao_sample_accel;
354 extern int32_t ao_accel_scale;
355 extern alt_t ao_ground_height;
356 extern alt_t ao_sample_alt;
358 double ao_sample_qangle;
360 int ao_sample_prev_tick;
364 #include "ao_kalman.c"
366 #include "ao_sample.c"
367 #include "ao_flight.c"
369 #define AO_PYRO_NUM 4
377 ao_pyro_pin_set(uint8_t pin, uint8_t value)
379 printf ("set pyro %d %d\n", pin, value);
385 #define to_double(f) ((f) / 65536.0)
387 static int ao_records_read = 0;
388 static int ao_eof_read = 0;
389 static int ao_flight_ground_accel;
390 static int ao_flight_started = 0;
391 static int ao_test_max_height;
392 static double ao_test_max_height_time;
393 static int ao_test_main_height;
394 static double ao_test_main_height_time;
395 static double ao_test_landed_time;
396 static double ao_test_landed_height;
397 static double ao_test_landed_time;
398 static int landed_set;
399 static double landed_time;
400 static double landed_height;
403 static struct ao_mpu6000_sample ao_ground_mpu6000;
412 double landed_time_error;
414 if (!ao_test_main_height_time) {
415 ao_test_main_height_time = ao_test_max_height_time;
416 ao_test_main_height = ao_test_max_height;
418 drogue_error = fabs(ao_test_max_height_time - drogue_time);
419 main_error = fabs(ao_test_main_height_time - main_time);
420 landed_error = fabs(ao_test_landed_height - landed_height);
421 landed_time_error = ao_test_landed_time - landed_time;
422 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
424 emulator_app, emulator_name);
426 printf ("\t%s\n", emulator_info);
427 printf ("\tApogee error %g\n", drogue_error);
428 printf ("\tMain error %g\n", main_error);
429 printf ("\tLanded height error %g\n", landed_error);
430 printf ("\tLanded time error %g\n", landed_time_error);
431 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
432 ao_test_max_height, ao_test_max_height_time,
433 ao_test_main_height, ao_test_main_height_time,
434 ao_test_landed_height, ao_test_landed_time);
435 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
436 drogue_height, drogue_time, main_height, main_time,
437 landed_height, landed_time);
450 azel (struct ao_azel *r, struct ao_quaternion *q)
454 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
455 v = sqrt (q->x*q->x + q->y*q->y);
456 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
465 ao_data_ring[ao_data_head] = ao_data_static;
466 ao_data_head = ao_data_ring_next(ao_data_head);
467 if (ao_flight_state != ao_flight_startup) {
469 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
470 (ao_config.accel_minus_g - ao_config.accel_plus_g);
474 #if TELEMEGA || TELEMETRUM_V2
477 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
478 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
480 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
485 tick_offset = -ao_data_static.tick;
486 if ((prev_tick - ao_data_static.tick) > 0x400)
487 tick_offset += 65536;
488 prev_tick = ao_data_static.tick;
489 time = (double) (ao_data_static.tick + tick_offset) / 100;
491 if (ao_test_max_height < height) {
492 ao_test_max_height = height;
493 ao_test_max_height_time = time;
494 ao_test_landed_height = height;
495 ao_test_landed_time = time;
497 if (height > ao_config.main_deploy) {
498 ao_test_main_height_time = time;
499 ao_test_main_height = height;
502 if (ao_test_landed_height > height) {
503 ao_test_landed_height = height;
504 ao_test_landed_time = time;
507 if (ao_flight_state == ao_flight_landed && !landed_set) {
510 landed_height = height;
515 static struct ao_quaternion ao_ground_mag;
516 static int ao_ground_mag_set;
518 if (!ao_ground_mag_set) {
519 ao_quaternion_init_vector (&ao_ground_mag,
520 ao_data_mag_across(&ao_data_static),
521 ao_data_mag_through(&ao_data_static),
522 ao_data_mag_along(&ao_data_static));
523 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
524 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
525 ao_ground_mag_set = 1;
528 struct ao_quaternion ao_mag, ao_mag_rot;
530 ao_quaternion_init_vector(&ao_mag,
531 ao_data_mag_across(&ao_data_static),
532 ao_data_mag_through(&ao_data_static),
533 ao_data_mag_along(&ao_data_static));
535 ao_quaternion_normalize(&ao_mag, &ao_mag);
536 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
541 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
543 struct ao_azel ground_azel, mag_azel, rot_azel;
545 azel(&ground_azel, &ao_ground_mag);
546 azel(&mag_azel, &ao_mag);
547 azel(&rot_azel, &ao_mag_rot);
549 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
553 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
554 struct ao_quaternion ao_out;
556 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
559 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
561 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
563 ao_state_names[ao_flight_state],
564 ao_k_height / 65536.0,
565 ao_sample_orient, out,
570 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
572 ao_state_names[ao_flight_state],
573 ao_k_height / 65536.0,
574 ao_sample_orient, out,
575 ao_distance_from_pad(),
576 (int) floor (ao_gps_angle() + 0.5),
577 (ao_gps_static.flags & 0xf) * 10);
581 printf ("\t\tstate %-8.8s ground az: %4d el %4d mag az %4d el %4d rot az %4d el %4d el_diff %4d az_diff %4d angle %4d tilt %4d ground %8.5f %8.5f %8.5f cur %8.5f %8.5f %8.5f rot %8.5f %8.5f %8.5f\n",
582 ao_state_names[ao_flight_state],
583 ground_azel.az, ground_azel.el,
584 mag_azel.az, mag_azel.el,
585 rot_azel.az, rot_azel.el,
586 ground_azel.el - rot_azel.el,
587 ground_azel.az - rot_azel.az,
603 printf("%7.2f height %8.2f accel %8.3f "
606 "accel_x %8.3f accel_y %8.3f accel_z %8.3f gyro_x %8.3f gyro_y %8.3f gyro_z %8.3f mag_x %8d mag_y %8d, mag_z %8d mag_angle %4d "
608 "state %-8.8s k_height %8.2f k_speed %8.3f k_accel %8.3f avg_height %5d drogue %4d main %4d error %5d\n",
615 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
616 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
617 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
618 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
619 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
620 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
621 ao_data_static.hmc5883.x,
622 ao_data_static.hmc5883.y,
623 ao_data_static.hmc5883.z,
626 ao_state_names[ao_flight_state],
627 ao_k_height / 65536.0,
628 ao_k_speed / 65536.0 / 16.0,
629 ao_k_accel / 65536.0 / 16.0,
636 // if (ao_flight_state == ao_flight_landed)
644 uint16(uint8_t *bytes, int off)
646 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
650 int16(uint8_t *bytes, int off)
652 return (int16_t) uint16(bytes, off);
656 uint32(uint8_t *bytes, int off)
658 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
659 (((uint32_t) bytes[off+2]) << 16) |
660 (((uint32_t) bytes[off+3]) << 24);
664 int32(uint8_t *bytes, int off)
666 return (int32_t) uint32(bytes, off);
669 static int log_format;
672 ao_sleep(void *wchan)
674 if (wchan == &ao_data_head) {
677 uint16_t a = 0, b = 0;
679 union ao_telemetry_all telem;
687 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
691 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
694 ao_data_static.mpu6000 = ao_ground_mpu6000;
697 ao_data_static.adc.accel = ao_flight_ground_accel;
703 if (!fgets(line, sizeof (line), emulator_in)) {
704 if (++ao_eof_read >= 1000) {
706 printf ("no more data, exiting simulation\n");
709 ao_data_static.tick += 10;
714 for (nword = 0; nword < 64; nword++) {
715 words[nword] = strtok_r(l, " \t\n", &saveptr);
717 if (words[nword] == NULL)
721 if ((log_format == AO_LOG_FORMAT_TELEMEGA_OLD || log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) {
723 struct ao_ms5607_value value;
726 tick = strtoul(words[1], NULL, 16);
727 // printf ("%c %04x", type, tick);
728 for (i = 2; i < nword; i++) {
729 bytes[i - 2] = strtoul(words[i], NULL, 16);
730 // printf(" %02x", bytes[i-2]);
735 ao_flight_ground_accel = int16(bytes, 2);
736 ao_flight_started = 1;
737 ao_ground_pres = int32(bytes, 4);
738 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
739 ao_ground_accel_along = int16(bytes, 8);
740 ao_ground_accel_across = int16(bytes, 10);
741 ao_ground_accel_through = int16(bytes, 12);
742 ao_ground_roll = int16(bytes, 14);
743 ao_ground_pitch = int16(bytes, 16);
744 ao_ground_yaw = int16(bytes, 18);
745 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
746 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
747 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
748 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
749 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
750 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
753 ao_data_static.tick = tick;
754 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
755 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
756 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
757 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
758 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
759 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
760 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
761 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
762 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
763 ao_data_static.hmc5883.x = int16(bytes, 20);
764 ao_data_static.hmc5883.y = int16(bytes, 22);
765 ao_data_static.hmc5883.z = int16(bytes, 24);
767 ao_data_static.mma655x = int16(bytes, 26);
768 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
769 ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
775 ao_gps_prev = ao_gps_static;
776 ao_gps_static.tick = tick;
777 ao_gps_static.latitude = int32(bytes, 0);
778 ao_gps_static.longitude = int32(bytes, 4);
780 int32_t altitude = int32(bytes, 8);
781 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
783 ao_gps_static.flags = bytes[13];
785 ao_gps_first = ao_gps_static;
790 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
791 if (strcmp(words[1], "reserved:") == 0)
792 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
793 else if (strcmp(words[1], "sens:") == 0)
794 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
795 else if (strcmp(words[1], "off:") == 0)
796 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
797 else if (strcmp(words[1], "tcs:") == 0)
798 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
799 else if (strcmp(words[1], "tco:") == 0)
800 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
801 else if (strcmp(words[1], "tref:") == 0)
802 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
803 else if (strcmp(words[1], "tempsens:") == 0)
804 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
805 else if (strcmp(words[1], "crc:") == 0)
806 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
808 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
809 int p = strtoul(words[1], NULL, 10);
811 struct ao_pyro *pyro = &ao_config.pyro[p];
813 for (i = 2; i < nword; i++) {
814 for (j = 0; j < NUM_PYRO_VALUES; j++)
815 if (!strcmp (words[i], ao_pyro_values[j].name))
817 if (j == NUM_PYRO_VALUES)
819 pyro->flags |= ao_pyro_values[j].flag;
820 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
821 int16_t val = strtoul(words[++i], NULL, 10);
822 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
823 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
829 if (log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1) {
831 struct ao_ms5607_value value;
834 tick = strtoul(words[1], NULL, 16);
835 // printf ("%c %04x", type, tick);
836 for (i = 2; i < nword; i++) {
837 bytes[i - 2] = strtoul(words[i], NULL, 16);
838 // printf(" %02x", bytes[i-2]);
843 ao_flight_ground_accel = int16(bytes, 2);
844 ao_flight_started = 1;
845 ao_ground_pres = int32(bytes, 4);
846 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
849 ao_data_static.tick = tick;
850 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
851 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
852 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
853 ao_data_static.mma655x = int16(bytes, 8);
859 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
860 if (strcmp(words[1], "reserved:") == 0)
861 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
862 else if (strcmp(words[1], "sens:") == 0)
863 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
864 else if (strcmp(words[1], "off:") == 0)
865 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
866 else if (strcmp(words[1], "tcs:") == 0)
867 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
868 else if (strcmp(words[1], "tco:") == 0)
869 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
870 else if (strcmp(words[1], "tref:") == 0)
871 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
872 else if (strcmp(words[1], "tempsens:") == 0)
873 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
874 else if (strcmp(words[1], "crc:") == 0)
875 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
880 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
882 tick = strtoul(words[1], NULL, 16);
883 a = strtoul(words[2], NULL, 16);
884 b = strtoul(words[3], NULL, 16);
889 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
890 log_format = strtoul(words[1], NULL, 10);
891 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
892 ao_config.accel_plus_g = atoi(words[3]);
893 ao_config.accel_minus_g = atoi(words[5]);
895 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
896 ao_config.accel_zero_along = atoi(words[3]);
897 ao_config.accel_zero_across = atoi(words[5]);
898 ao_config.accel_zero_through = atoi(words[7]);
900 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
901 ao_config.main_deploy = atoi(words[2]);
902 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
903 strcmp(words[1], "lockout:") == 0) {
904 ao_config.apogee_lockout = atoi(words[2]);
905 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
906 strcmp(words[1], "orientation:") == 0) {
907 ao_config.pad_orientation = atoi(words[2]);
908 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
909 tick = atoi(words[10]);
910 if (!ao_flight_started) {
913 ao_flight_started = 1;
919 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
920 tick = strtol(words[1], NULL, 16);
922 b = strtol(words[2], NULL, 10);
924 if (!ao_flight_started) {
925 ao_flight_ground_accel = 16384 - 328;
926 ao_config.accel_plus_g = 16384 - 328;
927 ao_config.accel_minus_g = 16384 + 328;
928 ao_flight_started = 1;
930 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
931 __xdata char *hex = words[1];
937 if (len > sizeof (bytes) * 2) {
938 len = sizeof (bytes)*2;
941 for (i = 0; i < len; i += 2) {
945 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
948 if (bytes[0] != len - 2) {
949 printf ("bad length %d != %d\n", bytes[0], len - 2);
953 for (i = 1; i < len-1; i++)
955 if (sum != bytes[len-1]) {
956 printf ("bad checksum\n");
959 if ((bytes[len-2] & 0x80) == 0) {
963 ao_xmemcpy(&telem, bytes + 1, 32);
964 tick = telem.generic.tick;
965 switch (telem.generic.type) {
966 case AO_TELEMETRY_SENSOR_TELEMETRUM:
967 case AO_TELEMETRY_SENSOR_TELEMINI:
968 case AO_TELEMETRY_SENSOR_TELENANO:
969 if (!ao_flight_started) {
970 ao_flight_ground_accel = telem.sensor.ground_accel;
971 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
972 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
973 ao_flight_started = 1;
976 a = telem.sensor.accel;
977 b = telem.sensor.pres;
980 } else if (len == 99) {
981 ao_flight_started = 1;
982 tick = uint16(bytes+1, 21);
983 ao_flight_ground_accel = int16(bytes+1, 7);
984 ao_config.accel_plus_g = int16(bytes+1, 17);
985 ao_config.accel_minus_g = int16(bytes+1, 19);
987 a = int16(bytes+1, 23);
988 b = int16(bytes+1, 25);
989 } else if (len == 98) {
990 ao_flight_started = 1;
991 tick = uint16(bytes+1, 20);
992 ao_flight_ground_accel = int16(bytes+1, 6);
993 ao_config.accel_plus_g = int16(bytes+1, 16);
994 ao_config.accel_minus_g = int16(bytes+1, 18);
996 a = int16(bytes+1, 22);
997 b = int16(bytes+1, 24);
999 printf("unknown len %d\n", len);
1003 if (type != 'F' && !ao_flight_started)
1006 #if TELEMEGA || TELEMETRUM_V2
1012 ao_flight_ground_accel = a;
1013 if (ao_config.accel_plus_g == 0) {
1014 ao_config.accel_plus_g = a;
1015 ao_config.accel_minus_g = a + 530;
1017 if (ao_config.main_deploy == 0)
1018 ao_config.main_deploy = 250;
1019 ao_flight_started = 1;
1024 ao_data_static.tick = tick;
1025 ao_data_static.adc.accel = a;
1026 ao_data_static.adc.pres_real = b;
1027 ao_data_static.adc.pres = b;
1032 ao_data_static.tick = tick;
1033 ao_data_static.adc.temp = a;
1034 ao_data_static.adc.v_batt = b;
1048 #define COUNTS_PER_G 264.8
1055 static const struct option options[] = {
1056 { .name = "summary", .has_arg = 0, .val = 's' },
1057 { .name = "debug", .has_arg = 0, .val = 'd' },
1058 { .name = "info", .has_arg = 1, .val = 'i' },
1062 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1064 emulator_name = name;
1066 emulator_info = info;
1067 ao_summary = summary;
1074 main (int argc, char **argv)
1082 emulator_app="full";
1084 emulator_app="baro";
1086 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1092 ao_flight_debug = 1;
1101 run_flight_fixed("<stdin>", stdin, summary, info);
1103 for (i = optind; i < argc; i++) {
1104 FILE *f = fopen(argv[i], "r");
1109 run_flight_fixed(argv[i], f, summary, info);