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
61 #define AO_MMA655X_INVERT 0
64 int16_t sense[AO_ADC_NUM_SENSE];
72 #define AO_ADC_NUM_SENSE 2
75 #define AO_MMA655X_INVERT 1
77 #define AO_CONFIG_MAX_SIZE 1024
90 * One set of samples read from the A/D converter
93 int16_t accel; /* accelerometer */
94 int16_t pres; /* pressure sensor */
95 int16_t pres_real; /* unclipped */
96 int16_t temp; /* temperature sensor */
97 int16_t v_batt; /* battery voltage */
98 int16_t sense_d; /* drogue continuity sense */
99 int16_t sense_m; /* main continuity sense */
104 #define HAS_ACCEL_REF 0
122 #include <ao_telemetry.h>
123 #include <ao_sample.h>
127 struct ao_telemetry_location ao_gps_first;
128 struct ao_telemetry_location ao_gps_prev;
129 struct ao_telemetry_location ao_gps_static;
131 struct ao_telemetry_satellite ao_gps_tracking;
133 static inline double sqr(double a) { return a * a; }
136 cc_great_circle (double start_lat, double start_lon,
137 double end_lat, double end_lon,
138 double *dist, double *bearing)
140 const double rad = M_PI / 180;
141 const double earth_radius = 6371.2 * 1000; /* in meters */
142 double lat1 = rad * start_lat;
143 double lon1 = rad * -start_lon;
144 double lat2 = rad * end_lat;
145 double lon2 = rad * -end_lon;
147 // double d_lat = lat2 - lat1;
148 double d_lon = lon2 - lon1;
150 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
151 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
152 sqr(cos(lat1) * sin(lat2) -
153 sin(lat1) * cos(lat2) * cos(d_lon)));
154 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
155 double d = atan2(vdn,vdd);
158 if (cos(lat1) < 1e-20) {
167 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
169 if (sin(lon2-lon1) > 0)
170 course = 2 * M_PI-course;
172 *dist = d * earth_radius;
173 *bearing = course * 180/M_PI;
177 ao_distance_from_pad(void)
179 double dist, bearing;
183 cc_great_circle(ao_gps_first.latitude / 1e7,
184 ao_gps_first.longitude / 1e7,
185 ao_gps_static.latitude / 1e7,
186 ao_gps_static.longitude / 1e7,
194 double dist, bearing;
198 if (ao_gps_count < 2)
201 cc_great_circle(ao_gps_prev.latitude / 1e7,
202 ao_gps_prev.longitude / 1e7,
203 ao_gps_static.latitude / 1e7,
204 ao_gps_static.longitude / 1e7,
206 height = AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_static) - AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_prev);
208 angle = atan2(dist, height);
209 return angle * 180/M_PI;
213 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
214 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
215 #define from_fix(x) ((x) >> 16)
217 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
219 extern enum ao_flight_state ao_flight_state;
224 volatile struct ao_data ao_data_ring[AO_DATA_RING];
225 volatile uint8_t ao_data_head;
229 #define ao_led_off(l)
230 #define ao_timer_set_adc_interval(i)
231 #define ao_wakeup(wchan) ao_dump_state()
232 #define ao_cmd_register(c)
233 #define ao_usb_disable()
234 #define ao_telemetry_set_interval(x)
235 #define ao_rdf_set(rdf)
236 #define ao_packet_slave_start()
237 #define ao_packet_slave_stop()
241 ao_igniter_drogue = 0,
245 struct ao_data ao_data_static;
254 static ao_k_t ao_k_height;
259 return ao_data_static.tick;
263 ao_delay(int16_t interval)
269 ao_ignite(enum ao_igniter igniter)
271 double time = (double) (ao_data_static.tick + tick_offset) / 100;
273 if (igniter == ao_igniter_drogue) {
275 drogue_height = ao_k_height >> 16;
278 main_height = ao_k_height >> 16;
286 #define ao_add_task(t,f,n) ((void) (t))
288 #define ao_log_start()
289 #define ao_log_stop()
291 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
292 #define AO_SEC_TO_TICKS(s) ((s) * 100)
294 #define AO_FLIGHT_TEST
302 double emulator_error_max = 4;
303 double emulator_height_error_max = 20; /* noise in the baro sensor */
309 ao_sleep(void *wchan);
311 const char const * const ao_state_names[] = {
312 "startup", "idle", "pad", "boost", "fast",
313 "coast", "drogue", "main", "landed", "invalid"
321 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
322 #define ao_xmemset(d,v,c) memset(d,v,c)
323 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
325 #define AO_NEED_ALTITUDE_TO_PRES 1
326 #if TELEMEGA || TELEMETRUM_V2
327 #include "ao_convert_pa.c"
328 #include <ao_ms5607.h>
329 struct ao_ms5607_prom ao_ms5607_prom;
330 #include "ao_ms5607_convert.c"
331 #define AO_PYRO_NUM 4
334 #include "ao_convert.c"
337 #include <ao_config.h>
338 #include <ao_fake_flight.h>
340 #define ao_config_get()
342 struct ao_config ao_config;
344 #define DATA_TO_XDATA(x) (x)
347 extern int16_t ao_ground_accel, ao_flight_accel;
348 extern int16_t ao_accel_2g;
350 typedef int16_t accel_t;
352 extern uint16_t ao_sample_tick;
354 extern alt_t ao_sample_height;
355 extern accel_t ao_sample_accel;
356 extern int32_t ao_accel_scale;
357 extern alt_t ao_ground_height;
358 extern alt_t ao_sample_alt;
360 double ao_sample_qangle;
362 int ao_sample_prev_tick;
366 #include "ao_kalman.c"
368 #include "ao_sample.c"
369 #include "ao_flight.c"
371 #define AO_PYRO_NUM 4
381 ao_pyro_pin_set(uint8_t pin, uint8_t value)
383 printf ("set pyro %d %d\n", pin, value);
389 #define to_double(f) ((f) / 65536.0)
391 static int ao_records_read = 0;
392 static int ao_eof_read = 0;
393 static int ao_flight_ground_accel;
394 static int ao_flight_started = 0;
395 static int ao_test_max_height;
396 static double ao_test_max_height_time;
397 static int ao_test_main_height;
398 static double ao_test_main_height_time;
399 static double ao_test_landed_time;
400 static double ao_test_landed_height;
401 static double ao_test_landed_time;
402 static int landed_set;
403 static double landed_time;
404 static double landed_height;
407 static struct ao_mpu6000_sample ao_ground_mpu6000;
416 double landed_time_error;
418 if (!ao_test_main_height_time) {
419 ao_test_main_height_time = ao_test_max_height_time;
420 ao_test_main_height = ao_test_max_height;
422 drogue_error = fabs(ao_test_max_height_time - drogue_time);
423 main_error = fabs(ao_test_main_height_time - main_time);
424 landed_error = fabs(ao_test_landed_height - landed_height);
425 landed_time_error = ao_test_landed_time - landed_time;
426 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
428 emulator_app, emulator_name);
430 printf ("\t%s\n", emulator_info);
431 printf ("\tApogee error %g\n", drogue_error);
432 printf ("\tMain error %g\n", main_error);
433 printf ("\tLanded height error %g\n", landed_error);
434 printf ("\tLanded time error %g\n", landed_time_error);
435 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
436 ao_test_max_height, ao_test_max_height_time,
437 ao_test_main_height, ao_test_main_height_time,
438 ao_test_landed_height, ao_test_landed_time);
439 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
440 drogue_height, drogue_time, main_height, main_time,
441 landed_height, landed_time);
454 azel (struct ao_azel *r, struct ao_quaternion *q)
458 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
459 v = sqrt (q->x*q->x + q->y*q->y);
460 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
469 ao_data_ring[ao_data_head] = ao_data_static;
470 ao_data_head = ao_data_ring_next(ao_data_head);
471 if (ao_flight_state != ao_flight_startup) {
473 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
474 (ao_config.accel_minus_g - ao_config.accel_plus_g);
478 #if TELEMEGA || TELEMETRUM_V2
481 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
482 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
484 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
489 tick_offset = -ao_data_static.tick;
490 if ((prev_tick - ao_data_static.tick) > 0x400)
491 tick_offset += 65536;
492 prev_tick = ao_data_static.tick;
493 time = (double) (ao_data_static.tick + tick_offset) / 100;
495 if (ao_test_max_height < height) {
496 ao_test_max_height = height;
497 ao_test_max_height_time = time;
498 ao_test_landed_height = height;
499 ao_test_landed_time = time;
501 if (height > ao_config.main_deploy) {
502 ao_test_main_height_time = time;
503 ao_test_main_height = height;
506 if (ao_test_landed_height > height) {
507 ao_test_landed_height = height;
508 ao_test_landed_time = time;
511 if (ao_flight_state == ao_flight_landed && !landed_set) {
514 landed_height = height;
519 static struct ao_quaternion ao_ground_mag;
520 static int ao_ground_mag_set;
522 if (!ao_ground_mag_set) {
523 ao_quaternion_init_vector (&ao_ground_mag,
524 ao_data_mag_across(&ao_data_static),
525 ao_data_mag_through(&ao_data_static),
526 ao_data_mag_along(&ao_data_static));
527 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
528 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
529 ao_ground_mag_set = 1;
532 struct ao_quaternion ao_mag, ao_mag_rot;
534 ao_quaternion_init_vector(&ao_mag,
535 ao_data_mag_across(&ao_data_static),
536 ao_data_mag_through(&ao_data_static),
537 ao_data_mag_along(&ao_data_static));
539 ao_quaternion_normalize(&ao_mag, &ao_mag);
540 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
545 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
547 struct ao_azel ground_azel, mag_azel, rot_azel;
549 azel(&ground_azel, &ao_ground_mag);
550 azel(&mag_azel, &ao_mag);
551 azel(&rot_azel, &ao_mag_rot);
553 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
557 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
558 struct ao_quaternion ao_out;
560 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
563 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
565 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
567 ao_state_names[ao_flight_state],
568 ao_k_height / 65536.0,
569 ao_sample_orient, out,
574 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
576 ao_state_names[ao_flight_state],
577 ao_k_height / 65536.0,
578 ao_sample_orient, out,
579 ao_distance_from_pad(),
580 (int) floor (ao_gps_angle() + 0.5),
581 (ao_gps_static.flags & 0xf) * 10);
585 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",
586 ao_state_names[ao_flight_state],
587 ground_azel.az, ground_azel.el,
588 mag_azel.az, mag_azel.el,
589 rot_azel.az, rot_azel.el,
590 ground_azel.el - rot_azel.el,
591 ground_azel.az - rot_azel.az,
607 printf("%7.2f height %8.2f accel %8.3f "
610 "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 "
612 "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",
619 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
620 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
621 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
622 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
623 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
624 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
625 ao_data_static.hmc5883.x,
626 ao_data_static.hmc5883.y,
627 ao_data_static.hmc5883.z,
630 ao_state_names[ao_flight_state],
631 ao_k_height / 65536.0,
632 ao_k_speed / 65536.0 / 16.0,
633 ao_k_accel / 65536.0 / 16.0,
640 // if (ao_flight_state == ao_flight_landed)
648 uint16(uint8_t *bytes, int off)
650 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
654 int16(uint8_t *bytes, int off)
656 return (int16_t) uint16(bytes, off);
660 uint32(uint8_t *bytes, int off)
662 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
663 (((uint32_t) bytes[off+2]) << 16) |
664 (((uint32_t) bytes[off+3]) << 24);
668 int32(uint8_t *bytes, int off)
670 return (int32_t) uint32(bytes, off);
673 static int log_format;
676 ao_sleep(void *wchan)
678 if (wchan == &ao_data_head) {
681 uint16_t a = 0, b = 0;
683 union ao_telemetry_all telem;
691 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
695 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
698 ao_data_static.mpu6000 = ao_ground_mpu6000;
701 ao_data_static.adc.accel = ao_flight_ground_accel;
707 if (!fgets(line, sizeof (line), emulator_in)) {
708 if (++ao_eof_read >= 1000) {
710 printf ("no more data, exiting simulation\n");
713 ao_data_static.tick += 10;
718 for (nword = 0; nword < 64; nword++) {
719 words[nword] = strtok_r(l, " \t\n", &saveptr);
721 if (words[nword] == NULL)
725 if ((log_format == AO_LOG_FORMAT_TELEMEGA_OLD || log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) {
727 struct ao_ms5607_value value;
730 tick = strtoul(words[1], NULL, 16);
731 // printf ("%c %04x", type, tick);
732 for (i = 2; i < nword; i++) {
733 bytes[i - 2] = strtoul(words[i], NULL, 16);
734 // printf(" %02x", bytes[i-2]);
739 ao_flight_ground_accel = int16(bytes, 2);
740 ao_flight_started = 1;
741 ao_ground_pres = int32(bytes, 4);
742 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
743 ao_ground_accel_along = int16(bytes, 8);
744 ao_ground_accel_across = int16(bytes, 10);
745 ao_ground_accel_through = int16(bytes, 12);
746 ao_ground_roll = int16(bytes, 14);
747 ao_ground_pitch = int16(bytes, 16);
748 ao_ground_yaw = int16(bytes, 18);
749 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
750 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
751 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
752 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
753 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
754 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
757 ao_data_static.tick = tick;
758 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
759 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
760 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
761 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
762 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
763 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
764 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
765 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
766 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
767 ao_data_static.hmc5883.x = int16(bytes, 20);
768 ao_data_static.hmc5883.y = int16(bytes, 22);
769 ao_data_static.hmc5883.z = int16(bytes, 24);
771 ao_data_static.mma655x = int16(bytes, 26);
772 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
773 ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
779 ao_gps_prev = ao_gps_static;
780 ao_gps_static.tick = tick;
781 ao_gps_static.latitude = int32(bytes, 0);
782 ao_gps_static.longitude = int32(bytes, 4);
784 int32_t altitude = int32(bytes, 8);
785 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
787 ao_gps_static.flags = bytes[13];
789 ao_gps_first = ao_gps_static;
794 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
795 if (strcmp(words[1], "reserved:") == 0)
796 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
797 else if (strcmp(words[1], "sens:") == 0)
798 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
799 else if (strcmp(words[1], "off:") == 0)
800 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
801 else if (strcmp(words[1], "tcs:") == 0)
802 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
803 else if (strcmp(words[1], "tco:") == 0)
804 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
805 else if (strcmp(words[1], "tref:") == 0)
806 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
807 else if (strcmp(words[1], "tempsens:") == 0)
808 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
809 else if (strcmp(words[1], "crc:") == 0)
810 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
812 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
813 int p = strtoul(words[1], NULL, 10);
815 struct ao_pyro *pyro = &ao_config.pyro[p];
817 for (i = 2; i < nword; i++) {
818 for (j = 0; j < NUM_PYRO_VALUES; j++)
819 if (!strcmp (words[i], ao_pyro_values[j].name))
821 if (j == NUM_PYRO_VALUES)
823 pyro->flags |= ao_pyro_values[j].flag;
824 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
825 int16_t val = strtoul(words[++i], NULL, 10);
826 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
827 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
833 if (log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1) {
835 struct ao_ms5607_value value;
838 tick = strtoul(words[1], NULL, 16);
839 // printf ("%c %04x", type, tick);
840 for (i = 2; i < nword; i++) {
841 bytes[i - 2] = strtoul(words[i], NULL, 16);
842 // printf(" %02x", bytes[i-2]);
847 ao_flight_ground_accel = int16(bytes, 2);
848 ao_flight_started = 1;
849 ao_ground_pres = int32(bytes, 4);
850 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
853 ao_data_static.tick = tick;
854 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
855 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
856 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
857 ao_data_static.mma655x = int16(bytes, 8);
863 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
864 if (strcmp(words[1], "reserved:") == 0)
865 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
866 else if (strcmp(words[1], "sens:") == 0)
867 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
868 else if (strcmp(words[1], "off:") == 0)
869 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
870 else if (strcmp(words[1], "tcs:") == 0)
871 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
872 else if (strcmp(words[1], "tco:") == 0)
873 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
874 else if (strcmp(words[1], "tref:") == 0)
875 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
876 else if (strcmp(words[1], "tempsens:") == 0)
877 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
878 else if (strcmp(words[1], "crc:") == 0)
879 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
884 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
886 tick = strtoul(words[1], NULL, 16);
887 a = strtoul(words[2], NULL, 16);
888 b = strtoul(words[3], NULL, 16);
893 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
894 log_format = strtoul(words[1], NULL, 10);
895 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
896 ao_config.accel_plus_g = atoi(words[3]);
897 ao_config.accel_minus_g = atoi(words[5]);
899 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
900 ao_config.accel_zero_along = atoi(words[3]);
901 ao_config.accel_zero_across = atoi(words[5]);
902 ao_config.accel_zero_through = atoi(words[7]);
904 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
905 ao_config.main_deploy = atoi(words[2]);
906 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
907 strcmp(words[1], "lockout:") == 0) {
908 ao_config.apogee_lockout = atoi(words[2]);
909 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
910 strcmp(words[1], "orientation:") == 0) {
911 ao_config.pad_orientation = atoi(words[2]);
912 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
913 tick = atoi(words[10]);
914 if (!ao_flight_started) {
917 ao_flight_started = 1;
923 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
924 tick = strtol(words[1], NULL, 16);
926 b = strtol(words[2], NULL, 10);
928 if (!ao_flight_started) {
929 ao_flight_ground_accel = 16384 - 328;
930 ao_config.accel_plus_g = 16384 - 328;
931 ao_config.accel_minus_g = 16384 + 328;
932 ao_flight_started = 1;
934 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
935 __xdata char *hex = words[1];
941 if (len > sizeof (bytes) * 2) {
942 len = sizeof (bytes)*2;
945 for (i = 0; i < len; i += 2) {
949 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
952 if (bytes[0] != len - 2) {
953 printf ("bad length %d != %d\n", bytes[0], len - 2);
957 for (i = 1; i < len-1; i++)
959 if (sum != bytes[len-1]) {
960 printf ("bad checksum\n");
963 if ((bytes[len-2] & 0x80) == 0) {
967 ao_xmemcpy(&telem, bytes + 1, 32);
968 tick = telem.generic.tick;
969 switch (telem.generic.type) {
970 case AO_TELEMETRY_SENSOR_TELEMETRUM:
971 case AO_TELEMETRY_SENSOR_TELEMINI:
972 case AO_TELEMETRY_SENSOR_TELENANO:
973 if (!ao_flight_started) {
974 ao_flight_ground_accel = telem.sensor.ground_accel;
975 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
976 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
977 ao_flight_started = 1;
980 a = telem.sensor.accel;
981 b = telem.sensor.pres;
984 } else if (len == 99) {
985 ao_flight_started = 1;
986 tick = uint16(bytes+1, 21);
987 ao_flight_ground_accel = int16(bytes+1, 7);
988 ao_config.accel_plus_g = int16(bytes+1, 17);
989 ao_config.accel_minus_g = int16(bytes+1, 19);
991 a = int16(bytes+1, 23);
992 b = int16(bytes+1, 25);
993 } else if (len == 98) {
994 ao_flight_started = 1;
995 tick = uint16(bytes+1, 20);
996 ao_flight_ground_accel = int16(bytes+1, 6);
997 ao_config.accel_plus_g = int16(bytes+1, 16);
998 ao_config.accel_minus_g = int16(bytes+1, 18);
1000 a = int16(bytes+1, 22);
1001 b = int16(bytes+1, 24);
1003 printf("unknown len %d\n", len);
1007 if (type != 'F' && !ao_flight_started)
1010 #if TELEMEGA || TELEMETRUM_V2
1016 ao_flight_ground_accel = a;
1017 if (ao_config.accel_plus_g == 0) {
1018 ao_config.accel_plus_g = a;
1019 ao_config.accel_minus_g = a + 530;
1021 if (ao_config.main_deploy == 0)
1022 ao_config.main_deploy = 250;
1023 ao_flight_started = 1;
1028 ao_data_static.tick = tick;
1029 ao_data_static.adc.accel = a;
1030 ao_data_static.adc.pres_real = b;
1031 ao_data_static.adc.pres = b;
1036 ao_data_static.tick = tick;
1037 ao_data_static.adc.temp = a;
1038 ao_data_static.adc.v_batt = b;
1052 #define COUNTS_PER_G 264.8
1059 static const struct option options[] = {
1060 { .name = "summary", .has_arg = 0, .val = 's' },
1061 { .name = "debug", .has_arg = 0, .val = 'd' },
1062 { .name = "info", .has_arg = 1, .val = 'i' },
1066 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1068 emulator_name = name;
1070 emulator_info = info;
1071 ao_summary = summary;
1078 main (int argc, char **argv)
1086 emulator_app="full";
1088 emulator_app="baro";
1090 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1096 ao_flight_debug = 1;
1105 run_flight_fixed("<stdin>", stdin, summary, info);
1107 for (i = optind; i < argc; i++) {
1108 FILE *f = fopen(argv[i], "r");
1113 run_flight_fixed(argv[i], f, summary, info);