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) && !defined(EASYMINI)
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 0
77 #define AO_CONFIG_MAX_SIZE 1024
88 #define AO_ADC_NUM_SENSE 2
91 #define AO_CONFIG_MAX_SIZE 1024
102 * One set of samples read from the A/D converter
105 int16_t accel; /* accelerometer */
106 int16_t pres; /* pressure sensor */
107 int16_t pres_real; /* unclipped */
108 int16_t temp; /* temperature sensor */
109 int16_t v_batt; /* battery voltage */
110 int16_t sense_d; /* drogue continuity sense */
111 int16_t sense_m; /* main continuity sense */
116 #define HAS_ACCEL_REF 0
134 #include <ao_telemetry.h>
135 #include <ao_sample.h>
139 struct ao_telemetry_location ao_gps_first;
140 struct ao_telemetry_location ao_gps_prev;
141 struct ao_telemetry_location ao_gps_static;
143 struct ao_telemetry_satellite ao_gps_tracking;
145 static inline double sqr(double a) { return a * a; }
148 cc_great_circle (double start_lat, double start_lon,
149 double end_lat, double end_lon,
150 double *dist, double *bearing)
152 const double rad = M_PI / 180;
153 const double earth_radius = 6371.2 * 1000; /* in meters */
154 double lat1 = rad * start_lat;
155 double lon1 = rad * -start_lon;
156 double lat2 = rad * end_lat;
157 double lon2 = rad * -end_lon;
159 // double d_lat = lat2 - lat1;
160 double d_lon = lon2 - lon1;
162 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
163 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
164 sqr(cos(lat1) * sin(lat2) -
165 sin(lat1) * cos(lat2) * cos(d_lon)));
166 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
167 double d = atan2(vdn,vdd);
170 if (cos(lat1) < 1e-20) {
179 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
181 if (sin(lon2-lon1) > 0)
182 course = 2 * M_PI-course;
184 *dist = d * earth_radius;
185 *bearing = course * 180/M_PI;
189 ao_distance_from_pad(void)
191 double dist, bearing;
195 cc_great_circle(ao_gps_first.latitude / 1e7,
196 ao_gps_first.longitude / 1e7,
197 ao_gps_static.latitude / 1e7,
198 ao_gps_static.longitude / 1e7,
206 double dist, bearing;
210 if (ao_gps_count < 2)
213 cc_great_circle(ao_gps_prev.latitude / 1e7,
214 ao_gps_prev.longitude / 1e7,
215 ao_gps_static.latitude / 1e7,
216 ao_gps_static.longitude / 1e7,
218 height = AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_static) - AO_TELEMETRY_LOCATION_ALTITUDE(&ao_gps_prev);
220 angle = atan2(dist, height);
221 return angle * 180/M_PI;
225 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
226 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
227 #define from_fix(x) ((x) >> 16)
229 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
231 extern enum ao_flight_state ao_flight_state;
236 volatile struct ao_data ao_data_ring[AO_DATA_RING];
237 volatile uint8_t ao_data_head;
241 #define ao_led_off(l)
242 #define ao_timer_set_adc_interval(i)
243 #define ao_wakeup(wchan) ao_dump_state()
244 #define ao_cmd_register(c)
245 #define ao_usb_disable()
246 #define ao_telemetry_set_interval(x)
247 #define ao_rdf_set(rdf)
248 #define ao_packet_slave_start()
249 #define ao_packet_slave_stop()
253 ao_igniter_drogue = 0,
257 struct ao_data ao_data_static;
266 static ao_k_t ao_k_height;
271 return ao_data_static.tick;
275 ao_delay(int16_t interval)
281 ao_ignite(enum ao_igniter igniter)
283 double time = (double) (ao_data_static.tick + tick_offset) / 100;
285 if (igniter == ao_igniter_drogue) {
287 drogue_height = ao_k_height >> 16;
290 main_height = ao_k_height >> 16;
298 #define ao_add_task(t,f,n) ((void) (t))
300 #define ao_log_start()
301 #define ao_log_stop()
303 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
304 #define AO_SEC_TO_TICKS(s) ((s) * 100)
306 #define AO_FLIGHT_TEST
314 double emulator_error_max = 4;
315 double emulator_height_error_max = 20; /* noise in the baro sensor */
321 ao_sleep(void *wchan);
323 const char const * const ao_state_names[] = {
324 "startup", "idle", "pad", "boost", "fast",
325 "coast", "drogue", "main", "landed", "invalid"
333 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
334 #define ao_xmemset(d,v,c) memset(d,v,c)
335 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
337 #define AO_NEED_ALTITUDE_TO_PRES 1
338 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
339 #include "ao_convert_pa.c"
340 #include <ao_ms5607.h>
341 struct ao_ms5607_prom ao_ms5607_prom;
342 #include "ao_ms5607_convert.c"
343 #define AO_PYRO_NUM 4
346 #include "ao_convert.c"
349 #include <ao_config.h>
350 #include <ao_fake_flight.h>
352 #define ao_config_get()
354 struct ao_config ao_config;
356 #define DATA_TO_XDATA(x) (x)
359 extern int16_t ao_ground_accel, ao_flight_accel;
360 extern int16_t ao_accel_2g;
362 typedef int16_t accel_t;
364 extern uint16_t ao_sample_tick;
366 extern alt_t ao_sample_height;
367 extern accel_t ao_sample_accel;
368 extern int32_t ao_accel_scale;
369 extern alt_t ao_ground_height;
370 extern alt_t ao_sample_alt;
372 double ao_sample_qangle;
374 int ao_sample_prev_tick;
378 #include "ao_kalman.c"
380 #include "ao_sample.c"
381 #include "ao_flight.c"
383 #define AO_PYRO_NUM 4
393 ao_pyro_pin_set(uint8_t pin, uint8_t value)
395 printf ("set pyro %d %d\n", pin, value);
401 #define to_double(f) ((f) / 65536.0)
403 static int ao_records_read = 0;
404 static int ao_eof_read = 0;
405 static int ao_flight_ground_accel;
406 static int ao_flight_started = 0;
407 static int ao_test_max_height;
408 static double ao_test_max_height_time;
409 static int ao_test_main_height;
410 static double ao_test_main_height_time;
411 static double ao_test_landed_time;
412 static double ao_test_landed_height;
413 static double ao_test_landed_time;
414 static int landed_set;
415 static double landed_time;
416 static double landed_height;
419 static struct ao_mpu6000_sample ao_ground_mpu6000;
428 double landed_time_error;
430 if (!ao_test_main_height_time) {
431 ao_test_main_height_time = ao_test_max_height_time;
432 ao_test_main_height = ao_test_max_height;
434 drogue_error = fabs(ao_test_max_height_time - drogue_time);
435 main_error = fabs(ao_test_main_height_time - main_time);
436 landed_error = fabs(ao_test_landed_height - landed_height);
437 landed_time_error = ao_test_landed_time - landed_time;
438 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
440 emulator_app, emulator_name);
442 printf ("\t%s\n", emulator_info);
443 printf ("\tApogee error %g\n", drogue_error);
444 printf ("\tMain error %g\n", main_error);
445 printf ("\tLanded height error %g\n", landed_error);
446 printf ("\tLanded time error %g\n", landed_time_error);
447 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
448 ao_test_max_height, ao_test_max_height_time,
449 ao_test_main_height, ao_test_main_height_time,
450 ao_test_landed_height, ao_test_landed_time);
451 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
452 drogue_height, drogue_time, main_height, main_time,
453 landed_height, landed_time);
466 azel (struct ao_azel *r, struct ao_quaternion *q)
470 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
471 v = sqrt (q->x*q->x + q->y*q->y);
472 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
481 ao_data_ring[ao_data_head] = ao_data_static;
482 ao_data_head = ao_data_ring_next(ao_data_head);
483 if (ao_flight_state != ao_flight_startup) {
485 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
486 (ao_config.accel_minus_g - ao_config.accel_plus_g);
490 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
493 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
494 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
496 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
501 tick_offset = -ao_data_static.tick;
502 if ((prev_tick - ao_data_static.tick) > 0x400)
503 tick_offset += 65536;
504 prev_tick = ao_data_static.tick;
505 time = (double) (ao_data_static.tick + tick_offset) / 100;
507 if (ao_test_max_height < height) {
508 ao_test_max_height = height;
509 ao_test_max_height_time = time;
510 ao_test_landed_height = height;
511 ao_test_landed_time = time;
513 if (height > ao_config.main_deploy) {
514 ao_test_main_height_time = time;
515 ao_test_main_height = height;
518 if (ao_test_landed_height > height) {
519 ao_test_landed_height = height;
520 ao_test_landed_time = time;
523 if (ao_flight_state == ao_flight_landed && !landed_set) {
526 landed_height = height;
531 static struct ao_quaternion ao_ground_mag;
532 static int ao_ground_mag_set;
534 if (!ao_ground_mag_set) {
535 ao_quaternion_init_vector (&ao_ground_mag,
536 ao_data_mag_across(&ao_data_static),
537 ao_data_mag_through(&ao_data_static),
538 ao_data_mag_along(&ao_data_static));
539 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
540 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
541 ao_ground_mag_set = 1;
544 struct ao_quaternion ao_mag, ao_mag_rot;
546 ao_quaternion_init_vector(&ao_mag,
547 ao_data_mag_across(&ao_data_static),
548 ao_data_mag_through(&ao_data_static),
549 ao_data_mag_along(&ao_data_static));
551 ao_quaternion_normalize(&ao_mag, &ao_mag);
552 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
557 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
559 struct ao_azel ground_azel, mag_azel, rot_azel;
561 azel(&ground_azel, &ao_ground_mag);
562 azel(&mag_azel, &ao_mag);
563 azel(&rot_azel, &ao_mag_rot);
565 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
569 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
570 struct ao_quaternion ao_out;
572 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
575 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
577 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
579 ao_state_names[ao_flight_state],
580 ao_k_height / 65536.0,
581 ao_sample_orient, out,
586 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
588 ao_state_names[ao_flight_state],
589 ao_k_height / 65536.0,
590 ao_sample_orient, out,
591 ao_distance_from_pad(),
592 (int) floor (ao_gps_angle() + 0.5),
593 (ao_gps_static.flags & 0xf) * 10);
597 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",
598 ao_state_names[ao_flight_state],
599 ground_azel.az, ground_azel.el,
600 mag_azel.az, mag_azel.el,
601 rot_azel.az, rot_azel.el,
602 ground_azel.el - rot_azel.el,
603 ground_azel.az - rot_azel.az,
619 printf("%7.2f height %8.2f accel %8.3f "
622 "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 "
624 "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",
631 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
632 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
633 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
634 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
635 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
636 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
637 ao_data_static.hmc5883.x,
638 ao_data_static.hmc5883.y,
639 ao_data_static.hmc5883.z,
642 ao_state_names[ao_flight_state],
643 ao_k_height / 65536.0,
644 ao_k_speed / 65536.0 / 16.0,
645 ao_k_accel / 65536.0 / 16.0,
652 // if (ao_flight_state == ao_flight_landed)
660 uint16(uint8_t *bytes, int off)
662 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
666 int16(uint8_t *bytes, int off)
668 return (int16_t) uint16(bytes, off);
672 uint32(uint8_t *bytes, int off)
674 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
675 (((uint32_t) bytes[off+2]) << 16) |
676 (((uint32_t) bytes[off+3]) << 24);
680 int32(uint8_t *bytes, int off)
682 return (int32_t) uint32(bytes, off);
686 uint24(uint8_t *bytes, int off)
688 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
689 (((uint32_t) bytes[off+2]) << 16);
693 int24(uint8_t *bytes, int off)
695 return (int32_t) uint24(bytes, off);
698 static int log_format;
701 ao_sleep(void *wchan)
703 if (wchan == &ao_data_head) {
706 uint16_t a = 0, b = 0;
708 union ao_telemetry_all telem;
716 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
720 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
724 ao_data_static.mpu6000 = ao_ground_mpu6000;
727 ao_data_static.adc.accel = ao_flight_ground_accel;
734 if (!fgets(line, sizeof (line), emulator_in)) {
735 if (++ao_eof_read >= 1000) {
737 printf ("no more data, exiting simulation\n");
740 ao_data_static.tick += 10;
745 for (nword = 0; nword < 64; nword++) {
746 words[nword] = strtok_r(l, " \t\n", &saveptr);
748 if (words[nword] == NULL)
752 if ((log_format == AO_LOG_FORMAT_TELEMEGA_OLD || log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) {
754 struct ao_ms5607_value value;
757 tick = strtoul(words[1], NULL, 16);
758 // printf ("%c %04x", type, tick);
759 for (i = 2; i < nword; i++) {
760 bytes[i - 2] = strtoul(words[i], NULL, 16);
761 // printf(" %02x", bytes[i-2]);
766 ao_flight_ground_accel = int16(bytes, 2);
767 ao_flight_started = 1;
768 ao_ground_pres = int32(bytes, 4);
769 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
770 ao_ground_accel_along = int16(bytes, 8);
771 ao_ground_accel_across = int16(bytes, 10);
772 ao_ground_accel_through = int16(bytes, 12);
773 ao_ground_roll = int16(bytes, 14);
774 ao_ground_pitch = int16(bytes, 16);
775 ao_ground_yaw = int16(bytes, 18);
776 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
777 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
778 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
779 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
780 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
781 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
784 ao_data_static.tick = tick;
785 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
786 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
787 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
788 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
789 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
790 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
791 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
792 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
793 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
794 ao_data_static.hmc5883.x = int16(bytes, 20);
795 ao_data_static.hmc5883.y = int16(bytes, 22);
796 ao_data_static.hmc5883.z = int16(bytes, 24);
798 ao_data_static.mma655x = int16(bytes, 26);
799 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
800 ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
806 ao_gps_prev = ao_gps_static;
807 ao_gps_static.tick = tick;
808 ao_gps_static.latitude = int32(bytes, 0);
809 ao_gps_static.longitude = int32(bytes, 4);
811 int32_t altitude = int32(bytes, 8);
812 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
814 ao_gps_static.flags = bytes[13];
816 ao_gps_first = ao_gps_static;
821 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
822 if (strcmp(words[1], "reserved:") == 0)
823 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
824 else if (strcmp(words[1], "sens:") == 0)
825 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
826 else if (strcmp(words[1], "off:") == 0)
827 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
828 else if (strcmp(words[1], "tcs:") == 0)
829 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
830 else if (strcmp(words[1], "tco:") == 0)
831 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
832 else if (strcmp(words[1], "tref:") == 0)
833 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
834 else if (strcmp(words[1], "tempsens:") == 0)
835 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
836 else if (strcmp(words[1], "crc:") == 0)
837 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
839 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
840 int p = strtoul(words[1], NULL, 10);
842 struct ao_pyro *pyro = &ao_config.pyro[p];
844 for (i = 2; i < nword; i++) {
845 for (j = 0; j < NUM_PYRO_VALUES; j++)
846 if (!strcmp (words[i], ao_pyro_values[j].name))
848 if (j == NUM_PYRO_VALUES)
850 pyro->flags |= ao_pyro_values[j].flag;
851 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
852 int16_t val = strtoul(words[++i], NULL, 10);
853 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
854 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
860 if ((log_format == AO_LOG_FORMAT_EASYMINI1 || log_format == AO_LOG_FORMAT_EASYMINI2) && nword == 14 && strlen(words[0]) == 1) {
862 struct ao_ms5607_value value;
865 tick = strtoul(words[1], NULL, 16);
866 // printf ("%c %04x", type, tick);
867 for (i = 2; i < nword; i++) {
868 bytes[i - 2] = strtoul(words[i], NULL, 16);
869 // printf(" %02x", bytes[i-2]);
874 ao_flight_started = 1;
875 ao_ground_pres = uint32(bytes, 4);
876 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
878 printf("ground pres %d height %d\n", ao_ground_pres, ao_ground_height);
879 printf("sens %d off %d tcs %d tco %d tref %d tempsens %d crc %d\n",
885 ao_ms5607_prom.tempsens,
890 ao_data_static.tick = tick;
891 ao_data_static.ms5607_raw.pres = int24(bytes, 0);
892 ao_data_static.ms5607_raw.temp = int24(bytes, 3);
894 printf("raw pres %d temp %d\n",
895 ao_data_static.ms5607_raw.pres,
896 ao_data_static.ms5607_raw.temp);
898 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
899 // printf("pres %d height %d\n", value.pres, ao_pa_to_altitude(value.pres));
905 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
906 if (strcmp(words[1], "reserved:") == 0)
907 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
908 else if (strcmp(words[1], "sens:") == 0)
909 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
910 else if (strcmp(words[1], "off:") == 0)
911 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
912 else if (strcmp(words[1], "tcs:") == 0)
913 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
914 else if (strcmp(words[1], "tco:") == 0)
915 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
916 else if (strcmp(words[1], "tref:") == 0)
917 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
918 else if (strcmp(words[1], "tempsens:") == 0)
919 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
920 else if (strcmp(words[1], "crc:") == 0)
921 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
926 if (log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1) {
928 struct ao_ms5607_value value;
931 tick = strtoul(words[1], NULL, 16);
932 // printf ("%c %04x", type, tick);
933 for (i = 2; i < nword; i++) {
934 bytes[i - 2] = strtoul(words[i], NULL, 16);
935 // printf(" %02x", bytes[i-2]);
940 ao_flight_ground_accel = int16(bytes, 2);
941 ao_flight_started = 1;
942 ao_ground_pres = int32(bytes, 4);
943 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
946 ao_data_static.tick = tick;
947 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
948 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
949 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
950 ao_data_static.mma655x = int16(bytes, 8);
956 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
957 if (strcmp(words[1], "reserved:") == 0)
958 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
959 else if (strcmp(words[1], "sens:") == 0)
960 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
961 else if (strcmp(words[1], "off:") == 0)
962 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
963 else if (strcmp(words[1], "tcs:") == 0)
964 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
965 else if (strcmp(words[1], "tco:") == 0)
966 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
967 else if (strcmp(words[1], "tref:") == 0)
968 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
969 else if (strcmp(words[1], "tempsens:") == 0)
970 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
971 else if (strcmp(words[1], "crc:") == 0)
972 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
977 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
979 tick = strtoul(words[1], NULL, 16);
980 a = strtoul(words[2], NULL, 16);
981 b = strtoul(words[3], NULL, 16);
986 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
987 log_format = strtoul(words[1], NULL, 10);
988 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
989 ao_config.accel_plus_g = atoi(words[3]);
990 ao_config.accel_minus_g = atoi(words[5]);
992 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
993 ao_config.accel_zero_along = atoi(words[3]);
994 ao_config.accel_zero_across = atoi(words[5]);
995 ao_config.accel_zero_through = atoi(words[7]);
997 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
998 ao_config.main_deploy = atoi(words[2]);
999 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
1000 strcmp(words[1], "lockout:") == 0) {
1001 ao_config.apogee_lockout = atoi(words[2]);
1002 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
1003 strcmp(words[1], "orientation:") == 0) {
1004 ao_config.pad_orientation = atoi(words[2]);
1005 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
1006 tick = atoi(words[10]);
1007 if (!ao_flight_started) {
1009 a = atoi(words[26]);
1010 ao_flight_started = 1;
1013 a = atoi(words[12]);
1014 b = atoi(words[14]);
1016 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
1017 tick = strtol(words[1], NULL, 16);
1019 b = strtol(words[2], NULL, 10);
1021 if (!ao_flight_started) {
1022 ao_flight_ground_accel = 16384 - 328;
1023 ao_config.accel_plus_g = 16384 - 328;
1024 ao_config.accel_minus_g = 16384 + 328;
1025 ao_flight_started = 1;
1027 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
1028 __xdata char *hex = words[1];
1034 if (len > sizeof (bytes) * 2) {
1035 len = sizeof (bytes)*2;
1038 for (i = 0; i < len; i += 2) {
1042 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
1045 if (bytes[0] != len - 2) {
1046 printf ("bad length %d != %d\n", bytes[0], len - 2);
1050 for (i = 1; i < len-1; i++)
1052 if (sum != bytes[len-1]) {
1053 printf ("bad checksum\n");
1056 if ((bytes[len-2] & 0x80) == 0) {
1060 ao_xmemcpy(&telem, bytes + 1, 32);
1061 tick = telem.generic.tick;
1062 switch (telem.generic.type) {
1063 case AO_TELEMETRY_SENSOR_TELEMETRUM:
1064 case AO_TELEMETRY_SENSOR_TELEMINI:
1065 case AO_TELEMETRY_SENSOR_TELENANO:
1066 if (!ao_flight_started) {
1067 ao_flight_ground_accel = telem.sensor.ground_accel;
1068 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
1069 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
1070 ao_flight_started = 1;
1073 a = telem.sensor.accel;
1074 b = telem.sensor.pres;
1077 } else if (len == 99) {
1078 ao_flight_started = 1;
1079 tick = uint16(bytes+1, 21);
1080 ao_flight_ground_accel = int16(bytes+1, 7);
1081 ao_config.accel_plus_g = int16(bytes+1, 17);
1082 ao_config.accel_minus_g = int16(bytes+1, 19);
1084 a = int16(bytes+1, 23);
1085 b = int16(bytes+1, 25);
1086 } else if (len == 98) {
1087 ao_flight_started = 1;
1088 tick = uint16(bytes+1, 20);
1089 ao_flight_ground_accel = int16(bytes+1, 6);
1090 ao_config.accel_plus_g = int16(bytes+1, 16);
1091 ao_config.accel_minus_g = int16(bytes+1, 18);
1093 a = int16(bytes+1, 22);
1094 b = int16(bytes+1, 24);
1096 printf("unknown len %d\n", len);
1100 if (type != 'F' && !ao_flight_started)
1103 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
1109 ao_flight_ground_accel = a;
1110 if (ao_config.accel_plus_g == 0) {
1111 ao_config.accel_plus_g = a;
1112 ao_config.accel_minus_g = a + 530;
1114 if (ao_config.main_deploy == 0)
1115 ao_config.main_deploy = 250;
1116 ao_flight_started = 1;
1121 ao_data_static.tick = tick;
1122 ao_data_static.adc.accel = a;
1123 ao_data_static.adc.pres_real = b;
1124 ao_data_static.adc.pres = b;
1129 ao_data_static.tick = tick;
1130 ao_data_static.adc.temp = a;
1131 ao_data_static.adc.v_batt = b;
1145 #define COUNTS_PER_G 264.8
1152 static const struct option options[] = {
1153 { .name = "summary", .has_arg = 0, .val = 's' },
1154 { .name = "debug", .has_arg = 0, .val = 'd' },
1155 { .name = "info", .has_arg = 1, .val = 'i' },
1159 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1161 emulator_name = name;
1163 emulator_info = info;
1164 ao_summary = summary;
1171 main (int argc, char **argv)
1179 emulator_app="full";
1181 emulator_app="baro";
1183 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1189 ao_flight_debug = 1;
1198 run_flight_fixed("<stdin>", stdin, summary, info);
1200 for (i = optind; i < argc; i++) {
1201 FILE *f = fopen(argv[i], "r");
1206 run_flight_fixed(argv[i], f, summary, info);