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;
267 static double simple_speed;
272 return ao_data_static.tick;
276 ao_delay(int16_t interval)
282 ao_ignite(enum ao_igniter igniter)
284 double time = (double) (ao_data_static.tick + tick_offset) / 100;
286 if (igniter == ao_igniter_drogue) {
288 drogue_height = ao_k_height >> 16;
291 main_height = ao_k_height >> 16;
299 #define ao_add_task(t,f,n) ((void) (t))
301 #define ao_log_start()
302 #define ao_log_stop()
304 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
305 #define AO_SEC_TO_TICKS(s) ((s) * 100)
307 #define AO_FLIGHT_TEST
315 double emulator_error_max = 4;
316 double emulator_height_error_max = 20; /* noise in the baro sensor */
322 ao_sleep(void *wchan);
324 const char const * const ao_state_names[] = {
325 "startup", "idle", "pad", "boost", "fast",
326 "coast", "drogue", "main", "landed", "invalid"
334 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
335 #define ao_xmemset(d,v,c) memset(d,v,c)
336 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
338 #define AO_NEED_ALTITUDE_TO_PRES 1
339 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
340 #include "ao_convert_pa.c"
341 #include <ao_ms5607.h>
342 struct ao_ms5607_prom ao_ms5607_prom;
343 #include "ao_ms5607_convert.c"
344 #define AO_PYRO_NUM 4
347 #include "ao_convert.c"
350 #include <ao_config.h>
351 #include <ao_fake_flight.h>
353 #define ao_config_get()
355 struct ao_config ao_config;
357 #define DATA_TO_XDATA(x) (x)
360 extern int16_t ao_ground_accel, ao_flight_accel;
361 extern int16_t ao_accel_2g;
363 typedef int16_t accel_t;
365 extern uint16_t ao_sample_tick;
367 extern alt_t ao_sample_height;
368 extern accel_t ao_sample_accel;
369 extern int32_t ao_accel_scale;
370 extern alt_t ao_ground_height;
371 extern alt_t ao_sample_alt;
373 double ao_sample_qangle;
375 int ao_sample_prev_tick;
379 #include "ao_kalman.c"
381 #include "ao_sample.c"
382 #include "ao_flight.c"
384 #define AO_PYRO_NUM 4
394 ao_pyro_pin_set(uint8_t pin, uint8_t value)
396 printf ("set pyro %d %d\n", pin, value);
402 #define to_double(f) ((f) / 65536.0)
404 static int ao_records_read = 0;
405 static int ao_eof_read = 0;
406 static int ao_flight_ground_accel;
407 static int ao_flight_started = 0;
408 static int ao_test_max_height;
409 static double ao_test_max_height_time;
410 static int ao_test_main_height;
411 static double ao_test_main_height_time;
412 static double ao_test_landed_time;
413 static double ao_test_landed_height;
414 static double ao_test_landed_time;
415 static int landed_set;
416 static double landed_time;
417 static double landed_height;
420 static struct ao_mpu6000_sample ao_ground_mpu6000;
429 double landed_time_error;
431 if (!ao_test_main_height_time) {
432 ao_test_main_height_time = ao_test_max_height_time;
433 ao_test_main_height = ao_test_max_height;
435 drogue_error = fabs(ao_test_max_height_time - drogue_time);
436 main_error = fabs(ao_test_main_height_time - main_time);
437 landed_error = fabs(ao_test_landed_height - landed_height);
438 landed_time_error = ao_test_landed_time - landed_time;
439 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
441 emulator_app, emulator_name);
443 printf ("\t%s\n", emulator_info);
444 printf ("\tApogee error %g\n", drogue_error);
445 printf ("\tMain error %g\n", main_error);
446 printf ("\tLanded height error %g\n", landed_error);
447 printf ("\tLanded time error %g\n", landed_time_error);
448 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
449 ao_test_max_height, ao_test_max_height_time,
450 ao_test_main_height, ao_test_main_height_time,
451 ao_test_landed_height, ao_test_landed_time);
452 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
453 drogue_height, drogue_time, main_height, main_time,
454 landed_height, landed_time);
467 azel (struct ao_azel *r, struct ao_quaternion *q)
471 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
472 v = sqrt (q->x*q->x + q->y*q->y);
473 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
482 ao_data_ring[ao_data_head] = ao_data_static;
483 ao_data_head = ao_data_ring_next(ao_data_head);
484 if (ao_flight_state != ao_flight_startup) {
486 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
487 (ao_config.accel_minus_g - ao_config.accel_plus_g);
491 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
494 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
495 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
497 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
502 tick_offset = -ao_data_static.tick;
503 if ((prev_tick - ao_data_static.tick) > 0x400)
504 tick_offset += 65536;
505 simple_speed += accel * (ao_data_static.tick - prev_tick) / 100.0;
506 prev_tick = ao_data_static.tick;
507 time = (double) (ao_data_static.tick + tick_offset) / 100;
509 if (ao_test_max_height < height) {
510 ao_test_max_height = height;
511 ao_test_max_height_time = time;
512 ao_test_landed_height = height;
513 ao_test_landed_time = time;
515 if (height > ao_config.main_deploy) {
516 ao_test_main_height_time = time;
517 ao_test_main_height = height;
520 if (ao_test_landed_height > height) {
521 ao_test_landed_height = height;
522 ao_test_landed_time = time;
525 if (ao_flight_state == ao_flight_landed && !landed_set) {
528 landed_height = height;
533 static struct ao_quaternion ao_ground_mag;
534 static int ao_ground_mag_set;
536 if (!ao_ground_mag_set) {
537 ao_quaternion_init_vector (&ao_ground_mag,
538 ao_data_mag_across(&ao_data_static),
539 ao_data_mag_through(&ao_data_static),
540 ao_data_mag_along(&ao_data_static));
541 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
542 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
543 ao_ground_mag_set = 1;
546 struct ao_quaternion ao_mag, ao_mag_rot;
548 ao_quaternion_init_vector(&ao_mag,
549 ao_data_mag_across(&ao_data_static),
550 ao_data_mag_through(&ao_data_static),
551 ao_data_mag_along(&ao_data_static));
553 ao_quaternion_normalize(&ao_mag, &ao_mag);
554 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
559 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
561 struct ao_azel ground_azel, mag_azel, rot_azel;
563 azel(&ground_azel, &ao_ground_mag);
564 azel(&mag_azel, &ao_mag);
565 azel(&rot_azel, &ao_mag_rot);
567 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
571 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
572 struct ao_quaternion ao_out;
574 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
577 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
579 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
581 ao_state_names[ao_flight_state],
582 ao_k_height / 65536.0,
583 ao_sample_orient, out,
588 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
590 ao_state_names[ao_flight_state],
591 ao_k_height / 65536.0,
592 ao_sample_orient, out,
593 ao_distance_from_pad(),
594 (int) floor (ao_gps_angle() + 0.5),
595 (ao_gps_static.flags & 0xf) * 10);
599 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",
600 ao_state_names[ao_flight_state],
601 ground_azel.az, ground_azel.el,
602 mag_azel.az, mag_azel.el,
603 rot_azel.az, rot_azel.el,
604 ground_azel.el - rot_azel.el,
605 ground_azel.az - rot_azel.az,
621 printf("%7.2f height %8.2f accel %8.3f accel_speed %8.3f "
622 "state %-8.8s k_height %8.2f k_speed %8.3f k_accel %8.3f avg_height %5d drogue %4d main %4d error %5d"
625 "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 "
631 simple_speed > -100.0 ? simple_speed : -100.0,
632 ao_state_names[ao_flight_state],
633 ao_k_height / 65536.0,
634 ao_k_speed / 65536.0 / 16.0,
635 ao_k_accel / 65536.0 / 16.0,
643 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
644 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
645 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
646 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
647 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
648 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
649 ao_data_static.hmc5883.x,
650 ao_data_static.hmc5883.y,
651 ao_data_static.hmc5883.z,
657 // if (ao_flight_state == ao_flight_landed)
665 uint16(uint8_t *bytes, int off)
667 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
671 int16(uint8_t *bytes, int off)
673 return (int16_t) uint16(bytes, off);
677 uint32(uint8_t *bytes, int off)
679 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
680 (((uint32_t) bytes[off+2]) << 16) |
681 (((uint32_t) bytes[off+3]) << 24);
685 int32(uint8_t *bytes, int off)
687 return (int32_t) uint32(bytes, off);
691 uint24(uint8_t *bytes, int off)
693 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
694 (((uint32_t) bytes[off+2]) << 16);
698 int24(uint8_t *bytes, int off)
700 return (int32_t) uint24(bytes, off);
703 static int log_format;
706 ao_sleep(void *wchan)
708 if (wchan == &ao_data_head) {
711 uint16_t a = 0, b = 0;
713 union ao_telemetry_all telem;
721 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
725 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
729 ao_data_static.mpu6000 = ao_ground_mpu6000;
732 ao_data_static.adc.accel = ao_flight_ground_accel;
739 if (!fgets(line, sizeof (line), emulator_in)) {
740 if (++ao_eof_read >= 1000) {
742 printf ("no more data, exiting simulation\n");
745 ao_data_static.tick += 10;
750 for (nword = 0; nword < 64; nword++) {
751 words[nword] = strtok_r(l, " \t\n", &saveptr);
753 if (words[nword] == NULL)
757 if ((log_format == AO_LOG_FORMAT_TELEMEGA_OLD || log_format == AO_LOG_FORMAT_TELEMEGA) && nword == 30 && strlen(words[0]) == 1) {
759 struct ao_ms5607_value value;
762 tick = strtoul(words[1], NULL, 16);
763 // printf ("%c %04x", type, tick);
764 for (i = 2; i < nword; i++) {
765 bytes[i - 2] = strtoul(words[i], NULL, 16);
766 // printf(" %02x", bytes[i-2]);
771 ao_flight_ground_accel = int16(bytes, 2);
772 ao_flight_started = 1;
773 ao_ground_pres = int32(bytes, 4);
774 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
775 ao_ground_accel_along = int16(bytes, 8);
776 ao_ground_accel_across = int16(bytes, 10);
777 ao_ground_accel_through = int16(bytes, 12);
778 ao_ground_roll = int16(bytes, 14);
779 ao_ground_pitch = int16(bytes, 16);
780 ao_ground_yaw = int16(bytes, 18);
781 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
782 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
783 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
784 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
785 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
786 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
789 ao_data_static.tick = tick;
790 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
791 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
792 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
793 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
794 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
795 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
796 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
797 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
798 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
799 ao_data_static.hmc5883.x = int16(bytes, 20);
800 ao_data_static.hmc5883.y = int16(bytes, 22);
801 ao_data_static.hmc5883.z = int16(bytes, 24);
803 ao_data_static.mma655x = int16(bytes, 26);
804 if (ao_config.pad_orientation != AO_PAD_ORIENTATION_ANTENNA_UP)
805 ao_data_static.mma655x = ao_data_accel_invert(ao_data_static.mma655x);
811 ao_gps_prev = ao_gps_static;
812 ao_gps_static.tick = tick;
813 ao_gps_static.latitude = int32(bytes, 0);
814 ao_gps_static.longitude = int32(bytes, 4);
816 int32_t altitude = int32(bytes, 8);
817 AO_TELEMETRY_LOCATION_SET_ALTITUDE(&ao_gps_static, altitude);
819 ao_gps_static.flags = bytes[13];
821 ao_gps_first = ao_gps_static;
826 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
827 if (strcmp(words[1], "reserved:") == 0)
828 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
829 else if (strcmp(words[1], "sens:") == 0)
830 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
831 else if (strcmp(words[1], "off:") == 0)
832 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
833 else if (strcmp(words[1], "tcs:") == 0)
834 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
835 else if (strcmp(words[1], "tco:") == 0)
836 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
837 else if (strcmp(words[1], "tref:") == 0)
838 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
839 else if (strcmp(words[1], "tempsens:") == 0)
840 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
841 else if (strcmp(words[1], "crc:") == 0)
842 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
844 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
845 int p = strtoul(words[1], NULL, 10);
847 struct ao_pyro *pyro = &ao_config.pyro[p];
849 for (i = 2; i < nword; i++) {
850 for (j = 0; j < NUM_PYRO_VALUES; j++)
851 if (!strcmp (words[i], ao_pyro_values[j].name))
853 if (j == NUM_PYRO_VALUES)
855 pyro->flags |= ao_pyro_values[j].flag;
856 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
857 int16_t val = strtoul(words[++i], NULL, 10);
858 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
859 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
865 if ((log_format == AO_LOG_FORMAT_EASYMINI1 || log_format == AO_LOG_FORMAT_EASYMINI2) && nword == 14 && strlen(words[0]) == 1) {
867 struct ao_ms5607_value value;
870 tick = strtoul(words[1], NULL, 16);
871 // printf ("%c %04x", type, tick);
872 for (i = 2; i < nword; i++) {
873 bytes[i - 2] = strtoul(words[i], NULL, 16);
874 // printf(" %02x", bytes[i-2]);
879 ao_flight_started = 1;
880 ao_ground_pres = uint32(bytes, 4);
881 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
883 printf("ground pres %d height %d\n", ao_ground_pres, ao_ground_height);
884 printf("sens %d off %d tcs %d tco %d tref %d tempsens %d crc %d\n",
890 ao_ms5607_prom.tempsens,
895 ao_data_static.tick = tick;
896 ao_data_static.ms5607_raw.pres = int24(bytes, 0);
897 ao_data_static.ms5607_raw.temp = int24(bytes, 3);
899 printf("raw pres %d temp %d\n",
900 ao_data_static.ms5607_raw.pres,
901 ao_data_static.ms5607_raw.temp);
903 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
904 // printf("pres %d height %d\n", value.pres, ao_pa_to_altitude(value.pres));
910 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
911 if (strcmp(words[1], "reserved:") == 0)
912 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
913 else if (strcmp(words[1], "sens:") == 0)
914 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
915 else if (strcmp(words[1], "off:") == 0)
916 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
917 else if (strcmp(words[1], "tcs:") == 0)
918 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
919 else if (strcmp(words[1], "tco:") == 0)
920 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
921 else if (strcmp(words[1], "tref:") == 0)
922 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
923 else if (strcmp(words[1], "tempsens:") == 0)
924 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
925 else if (strcmp(words[1], "crc:") == 0)
926 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
931 if (log_format == AO_LOG_FORMAT_TELEMETRUM && nword == 14 && strlen(words[0]) == 1) {
933 struct ao_ms5607_value value;
936 tick = strtoul(words[1], NULL, 16);
937 // printf ("%c %04x", type, tick);
938 for (i = 2; i < nword; i++) {
939 bytes[i - 2] = strtoul(words[i], NULL, 16);
940 // printf(" %02x", bytes[i-2]);
945 ao_flight_ground_accel = int16(bytes, 2);
946 ao_flight_started = 1;
947 ao_ground_pres = int32(bytes, 4);
948 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
951 ao_data_static.tick = tick;
952 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
953 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
954 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
955 ao_data_static.mma655x = int16(bytes, 8);
961 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
962 if (strcmp(words[1], "reserved:") == 0)
963 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
964 else if (strcmp(words[1], "sens:") == 0)
965 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
966 else if (strcmp(words[1], "off:") == 0)
967 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
968 else if (strcmp(words[1], "tcs:") == 0)
969 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
970 else if (strcmp(words[1], "tco:") == 0)
971 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
972 else if (strcmp(words[1], "tref:") == 0)
973 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
974 else if (strcmp(words[1], "tempsens:") == 0)
975 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
976 else if (strcmp(words[1], "crc:") == 0)
977 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
982 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
984 tick = strtoul(words[1], NULL, 16);
985 a = strtoul(words[2], NULL, 16);
986 b = strtoul(words[3], NULL, 16);
991 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
992 log_format = strtoul(words[1], NULL, 10);
993 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
994 ao_config.accel_plus_g = atoi(words[3]);
995 ao_config.accel_minus_g = atoi(words[5]);
997 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
998 ao_config.accel_zero_along = atoi(words[3]);
999 ao_config.accel_zero_across = atoi(words[5]);
1000 ao_config.accel_zero_through = atoi(words[7]);
1002 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
1003 ao_config.main_deploy = atoi(words[2]);
1004 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
1005 strcmp(words[1], "lockout:") == 0) {
1006 ao_config.apogee_lockout = atoi(words[2]);
1007 } else if (nword >= 3 && strcmp(words[0], "Pad") == 0 &&
1008 strcmp(words[1], "orientation:") == 0) {
1009 ao_config.pad_orientation = atoi(words[2]);
1010 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
1011 tick = atoi(words[10]);
1012 if (!ao_flight_started) {
1014 a = atoi(words[26]);
1015 ao_flight_started = 1;
1018 a = atoi(words[12]);
1019 b = atoi(words[14]);
1021 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
1022 tick = strtol(words[1], NULL, 16);
1024 b = strtol(words[2], NULL, 10);
1026 if (!ao_flight_started) {
1027 ao_flight_ground_accel = 16384 - 328;
1028 ao_config.accel_plus_g = 16384 - 328;
1029 ao_config.accel_minus_g = 16384 + 328;
1030 ao_flight_started = 1;
1032 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
1033 __xdata char *hex = words[1];
1039 if (len > sizeof (bytes) * 2) {
1040 len = sizeof (bytes)*2;
1043 for (i = 0; i < len; i += 2) {
1047 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
1050 if (bytes[0] != len - 2) {
1051 printf ("bad length %d != %d\n", bytes[0], len - 2);
1055 for (i = 1; i < len-1; i++)
1057 if (sum != bytes[len-1]) {
1058 printf ("bad checksum\n");
1061 if ((bytes[len-2] & 0x80) == 0) {
1065 ao_xmemcpy(&telem, bytes + 1, 32);
1066 tick = telem.generic.tick;
1067 switch (telem.generic.type) {
1068 case AO_TELEMETRY_SENSOR_TELEMETRUM:
1069 case AO_TELEMETRY_SENSOR_TELEMINI:
1070 case AO_TELEMETRY_SENSOR_TELENANO:
1071 if (!ao_flight_started) {
1072 ao_flight_ground_accel = telem.sensor.ground_accel;
1073 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
1074 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
1075 ao_flight_started = 1;
1078 a = telem.sensor.accel;
1079 b = telem.sensor.pres;
1082 } else if (len == 99) {
1083 ao_flight_started = 1;
1084 tick = uint16(bytes+1, 21);
1085 ao_flight_ground_accel = int16(bytes+1, 7);
1086 ao_config.accel_plus_g = int16(bytes+1, 17);
1087 ao_config.accel_minus_g = int16(bytes+1, 19);
1089 a = int16(bytes+1, 23);
1090 b = int16(bytes+1, 25);
1091 } else if (len == 98) {
1092 ao_flight_started = 1;
1093 tick = uint16(bytes+1, 20);
1094 ao_flight_ground_accel = int16(bytes+1, 6);
1095 ao_config.accel_plus_g = int16(bytes+1, 16);
1096 ao_config.accel_minus_g = int16(bytes+1, 18);
1098 a = int16(bytes+1, 22);
1099 b = int16(bytes+1, 24);
1101 printf("unknown len %d\n", len);
1105 if (type != 'F' && !ao_flight_started)
1108 #if TELEMEGA || TELEMETRUM_V2 || EASYMINI
1114 ao_flight_ground_accel = a;
1115 if (ao_config.accel_plus_g == 0) {
1116 ao_config.accel_plus_g = a;
1117 ao_config.accel_minus_g = a + 530;
1119 if (ao_config.main_deploy == 0)
1120 ao_config.main_deploy = 250;
1121 ao_flight_started = 1;
1126 ao_data_static.tick = tick;
1127 ao_data_static.adc.accel = a;
1128 ao_data_static.adc.pres_real = b;
1129 ao_data_static.adc.pres = b;
1134 ao_data_static.tick = tick;
1135 ao_data_static.adc.temp = a;
1136 ao_data_static.adc.v_batt = b;
1150 #define COUNTS_PER_G 264.8
1157 static const struct option options[] = {
1158 { .name = "summary", .has_arg = 0, .val = 's' },
1159 { .name = "debug", .has_arg = 0, .val = 'd' },
1160 { .name = "info", .has_arg = 1, .val = 'i' },
1164 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1166 emulator_name = name;
1168 emulator_info = info;
1169 ao_summary = summary;
1176 main (int argc, char **argv)
1184 emulator_app="full";
1186 emulator_app="baro";
1188 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1194 ao_flight_debug = 1;
1203 run_flight_fixed("<stdin>", stdin, summary, info);
1205 for (i = optind; i < argc; i++) {
1206 FILE *f = fopen(argv[i], "r");
1211 run_flight_fixed(argv[i], f, summary, info);