2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 #define GRAVITY 9.80665
33 #define AO_DATA_RING 64
34 #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1))
35 #define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1))
37 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
38 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
39 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
41 #define AO_GPS_NEW_DATA 1
42 #define AO_GPS_NEW_TRACKING 2
47 #define AO_ADC_NUM_SENSE 6
54 int16_t sense[AO_ADC_NUM_SENSE];
63 * One set of samples read from the A/D converter
66 int16_t accel; /* accelerometer */
67 int16_t pres; /* pressure sensor */
68 int16_t pres_real; /* unclipped */
69 int16_t temp; /* temperature sensor */
70 int16_t v_batt; /* battery voltage */
71 int16_t sense_d; /* drogue continuity sense */
72 int16_t sense_m; /* main continuity sense */
77 #define HAS_ACCEL_REF 0
95 #include <ao_telemetry.h>
99 struct ao_telemetry_location ao_gps_first;
100 struct ao_telemetry_location ao_gps_prev;
101 struct ao_telemetry_location ao_gps_static;
103 struct ao_telemetry_satellite ao_gps_tracking;
105 static inline double sqr(double a) { return a * a; }
108 cc_great_circle (double start_lat, double start_lon,
109 double end_lat, double end_lon,
110 double *dist, double *bearing)
112 const double rad = M_PI / 180;
113 const double earth_radius = 6371.2 * 1000; /* in meters */
114 double lat1 = rad * start_lat;
115 double lon1 = rad * -start_lon;
116 double lat2 = rad * end_lat;
117 double lon2 = rad * -end_lon;
119 // double d_lat = lat2 - lat1;
120 double d_lon = lon2 - lon1;
122 /* From http://en.wikipedia.org/wiki/Great-circle_distance */
123 double vdn = sqrt(sqr(cos(lat2) * sin(d_lon)) +
124 sqr(cos(lat1) * sin(lat2) -
125 sin(lat1) * cos(lat2) * cos(d_lon)));
126 double vdd = sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d_lon);
127 double d = atan2(vdn,vdd);
130 if (cos(lat1) < 1e-20) {
139 course = acos((sin(lat2)-sin(lat1)*cos(d)) /
141 if (sin(lon2-lon1) > 0)
142 course = 2 * M_PI-course;
144 *dist = d * earth_radius;
145 *bearing = course * 180/M_PI;
149 ao_distance_from_pad(void)
151 double dist, bearing;
155 cc_great_circle(ao_gps_first.latitude / 1e7,
156 ao_gps_first.longitude / 1e7,
157 ao_gps_static.latitude / 1e7,
158 ao_gps_static.longitude / 1e7,
166 double dist, bearing;
170 if (ao_gps_count < 2)
173 cc_great_circle(ao_gps_prev.latitude / 1e7,
174 ao_gps_prev.longitude / 1e7,
175 ao_gps_static.latitude / 1e7,
176 ao_gps_static.longitude / 1e7,
178 height = ao_gps_static.altitude - ao_gps_prev.altitude;
180 angle = atan2(dist, height);
181 return angle * 180/M_PI;
185 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
186 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
187 #define from_fix(x) ((x) >> 16)
190 * Above this height, the baro sensor doesn't work
192 #define AO_BARO_SATURATE 13000
193 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
196 * Above this speed, baro measurements are unreliable
198 #define AO_MAX_BARO_SPEED 200
200 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
202 extern enum ao_flight_state ao_flight_state;
207 volatile struct ao_data ao_data_ring[AO_DATA_RING];
208 volatile uint8_t ao_data_head;
212 #define ao_led_off(l)
213 #define ao_timer_set_adc_interval(i)
214 #define ao_wakeup(wchan) ao_dump_state()
215 #define ao_cmd_register(c)
216 #define ao_usb_disable()
217 #define ao_telemetry_set_interval(x)
218 #define ao_rdf_set(rdf)
219 #define ao_packet_slave_start()
220 #define ao_packet_slave_stop()
224 ao_igniter_drogue = 0,
228 struct ao_data ao_data_static;
237 static int32_t ao_k_height;
242 return ao_data_static.tick;
246 ao_delay(int16_t interval)
252 ao_ignite(enum ao_igniter igniter)
254 double time = (double) (ao_data_static.tick + tick_offset) / 100;
256 if (igniter == ao_igniter_drogue) {
258 drogue_height = ao_k_height >> 16;
261 main_height = ao_k_height >> 16;
269 #define ao_add_task(t,f,n) ((void) (t))
271 #define ao_log_start()
272 #define ao_log_stop()
274 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
275 #define AO_SEC_TO_TICKS(s) ((s) * 100)
277 #define AO_FLIGHT_TEST
285 double emulator_error_max = 4;
286 double emulator_height_error_max = 20; /* noise in the baro sensor */
292 ao_sleep(void *wchan);
294 const char const * const ao_state_names[] = {
295 "startup", "idle", "pad", "boost", "fast",
296 "coast", "drogue", "main", "landed", "invalid"
304 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
305 #define ao_xmemset(d,v,c) memset(d,v,c)
306 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
308 #define AO_NEED_ALTITUDE_TO_PRES 1
310 #include "ao_convert_pa.c"
311 #include <ao_ms5607.h>
312 struct ao_ms5607_prom ms5607_prom;
313 #include "ao_ms5607_convert.c"
314 #define AO_PYRO_NUM 4
317 #include "ao_convert.c"
321 uint16_t main_deploy;
322 int16_t accel_plus_g;
323 int16_t accel_minus_g;
324 uint8_t pad_orientation;
325 uint16_t apogee_lockout;
327 struct ao_pyro pyro[AO_PYRO_NUM]; /* minor version 12 */
328 int16_t accel_zero_along;
329 int16_t accel_zero_across;
330 int16_t accel_zero_through;
334 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
335 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
337 #define ao_config_get()
339 struct ao_config ao_config;
341 #define DATA_TO_XDATA(x) (x)
344 extern int16_t ao_ground_accel, ao_flight_accel;
345 extern int16_t ao_accel_2g;
347 typedef int16_t accel_t;
349 extern uint16_t ao_sample_tick;
351 extern alt_t ao_sample_height;
352 extern accel_t ao_sample_accel;
353 extern int32_t ao_accel_scale;
354 extern alt_t ao_ground_height;
355 extern alt_t ao_sample_alt;
357 double ao_sample_qangle;
359 int ao_sample_prev_tick;
363 #include "ao_kalman.c"
365 #include "ao_sample.c"
366 #include "ao_flight.c"
368 #define AO_PYRO_NUM 4
376 ao_pyro_pin_set(uint8_t pin, uint8_t value)
378 printf ("set pyro %d %d\n", pin, value);
384 #define to_double(f) ((f) / 65536.0)
386 static int ao_records_read = 0;
387 static int ao_eof_read = 0;
388 static int ao_flight_ground_accel;
389 static int ao_flight_started = 0;
390 static int ao_test_max_height;
391 static double ao_test_max_height_time;
392 static int ao_test_main_height;
393 static double ao_test_main_height_time;
394 static double ao_test_landed_time;
395 static double ao_test_landed_height;
396 static double ao_test_landed_time;
397 static int landed_set;
398 static double landed_time;
399 static double landed_height;
402 static struct ao_mpu6000_sample ao_ground_mpu6000;
411 double landed_time_error;
413 if (!ao_test_main_height_time) {
414 ao_test_main_height_time = ao_test_max_height_time;
415 ao_test_main_height = ao_test_max_height;
417 drogue_error = fabs(ao_test_max_height_time - drogue_time);
418 main_error = fabs(ao_test_main_height_time - main_time);
419 landed_error = fabs(ao_test_landed_height - landed_height);
420 landed_time_error = ao_test_landed_time - landed_time;
421 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
423 emulator_app, emulator_name);
425 printf ("\t%s\n", emulator_info);
426 printf ("\tApogee error %g\n", drogue_error);
427 printf ("\tMain error %g\n", main_error);
428 printf ("\tLanded height error %g\n", landed_error);
429 printf ("\tLanded time error %g\n", landed_time_error);
430 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
431 ao_test_max_height, ao_test_max_height_time,
432 ao_test_main_height, ao_test_main_height_time,
433 ao_test_landed_height, ao_test_landed_time);
434 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
435 drogue_height, drogue_time, main_height, main_time,
436 landed_height, landed_time);
449 azel (struct ao_azel *r, struct ao_quaternion *q)
453 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
454 v = sqrt (q->x*q->x + q->y*q->y);
455 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
464 ao_data_ring[ao_data_head] = ao_data_static;
465 ao_data_head = ao_data_ring_next(ao_data_head);
466 if (ao_flight_state != ao_flight_startup) {
468 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
469 (ao_config.accel_minus_g - ao_config.accel_plus_g);
476 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
477 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
479 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
484 tick_offset = -ao_data_static.tick;
485 if ((prev_tick - ao_data_static.tick) > 0x400)
486 tick_offset += 65536;
487 prev_tick = ao_data_static.tick;
488 time = (double) (ao_data_static.tick + tick_offset) / 100;
490 if (ao_test_max_height < height) {
491 ao_test_max_height = height;
492 ao_test_max_height_time = time;
493 ao_test_landed_height = height;
494 ao_test_landed_time = time;
496 if (height > ao_config.main_deploy) {
497 ao_test_main_height_time = time;
498 ao_test_main_height = height;
501 if (ao_test_landed_height > height) {
502 ao_test_landed_height = height;
503 ao_test_landed_time = time;
506 if (ao_flight_state == ao_flight_landed && !landed_set) {
509 landed_height = height;
514 static struct ao_quaternion ao_ground_mag;
515 static int ao_ground_mag_set;
517 if (!ao_ground_mag_set) {
518 ao_quaternion_init_vector (&ao_ground_mag,
519 ao_data_mag_across(&ao_data_static),
520 ao_data_mag_through(&ao_data_static),
521 ao_data_mag_along(&ao_data_static));
522 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
523 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
524 ao_ground_mag_set = 1;
527 struct ao_quaternion ao_mag, ao_mag_rot;
529 ao_quaternion_init_vector(&ao_mag,
530 ao_data_mag_across(&ao_data_static),
531 ao_data_mag_through(&ao_data_static),
532 ao_data_mag_along(&ao_data_static));
534 ao_quaternion_normalize(&ao_mag, &ao_mag);
535 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
540 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
542 struct ao_azel ground_azel, mag_azel, rot_azel;
544 azel(&ground_azel, &ao_ground_mag);
545 azel(&mag_azel, &ao_mag);
546 azel(&rot_azel, &ao_mag_rot);
548 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
552 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
553 struct ao_quaternion ao_out;
555 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
557 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
560 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
562 ao_state_names[ao_flight_state],
563 ao_k_height / 65536.0,
564 ao_sample_orient, out,
568 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
570 ao_state_names[ao_flight_state],
571 ao_k_height / 65536.0,
572 ao_sample_orient, out,
573 ao_distance_from_pad(),
574 (int) floor (ao_gps_angle() + 0.5),
575 (ao_gps_static.flags & 0xf) * 10);
578 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",
579 ao_state_names[ao_flight_state],
580 ground_azel.az, ground_azel.el,
581 mag_azel.az, mag_azel.el,
582 rot_azel.az, rot_azel.el,
583 ground_azel.el - rot_azel.el,
584 ground_azel.az - rot_azel.az,
600 printf("%7.2f height %8.2f accel %8.3f "
603 "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 "
605 "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",
612 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
613 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
614 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
615 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
616 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
617 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
618 ao_data_static.hmc5883.x,
619 ao_data_static.hmc5883.y,
620 ao_data_static.hmc5883.z,
623 ao_state_names[ao_flight_state],
624 ao_k_height / 65536.0,
625 ao_k_speed / 65536.0 / 16.0,
626 ao_k_accel / 65536.0 / 16.0,
633 // if (ao_flight_state == ao_flight_landed)
641 uint16(uint8_t *bytes, int off)
643 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
647 int16(uint8_t *bytes, int off)
649 return (int16_t) uint16(bytes, off);
653 uint32(uint8_t *bytes, int off)
655 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
656 (((uint32_t) bytes[off+2]) << 16) |
657 (((uint32_t) bytes[off+3]) << 24);
661 int32(uint8_t *bytes, int off)
663 return (int32_t) uint32(bytes, off);
666 static int log_format;
669 ao_sleep(void *wchan)
671 if (wchan == &ao_data_head) {
674 uint16_t a = 0, b = 0;
676 union ao_telemetry_all telem;
684 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
688 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
691 ao_data_static.mpu6000 = ao_ground_mpu6000;
693 ao_data_static.adc.accel = ao_flight_ground_accel;
699 if (!fgets(line, sizeof (line), emulator_in)) {
700 if (++ao_eof_read >= 1000) {
702 printf ("no more data, exiting simulation\n");
705 ao_data_static.tick += 10;
710 for (nword = 0; nword < 64; nword++) {
711 words[nword] = strtok_r(l, " \t\n", &saveptr);
713 if (words[nword] == NULL)
717 if (log_format == AO_LOG_FORMAT_TELEMEGA && nword == 30 && strlen(words[0]) == 1) {
719 struct ao_ms5607_value value;
722 tick = strtoul(words[1], NULL, 16);
723 // printf ("%c %04x", type, tick);
724 for (i = 2; i < nword; i++) {
725 bytes[i - 2] = strtoul(words[i], NULL, 16);
726 // printf(" %02x", bytes[i-2]);
731 ao_flight_ground_accel = int16(bytes, 2);
732 ao_flight_started = 1;
733 ao_ground_pres = int32(bytes, 4);
734 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
735 ao_ground_accel_along = int16(bytes, 8);
736 ao_ground_accel_across = int16(bytes, 10);
737 ao_ground_accel_through = int16(bytes, 12);
738 ao_ground_roll = int16(bytes, 14);
739 ao_ground_pitch = int16(bytes, 16);
740 ao_ground_yaw = int16(bytes, 18);
741 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
742 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
743 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
744 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
745 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
746 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
749 ao_data_static.tick = tick;
750 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
751 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
752 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
753 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
754 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
755 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
756 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
757 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
758 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
759 ao_data_static.hmc5883.x = int16(bytes, 20);
760 ao_data_static.hmc5883.y = int16(bytes, 22);
761 ao_data_static.hmc5883.z = int16(bytes, 24);
763 ao_data_static.mma655x = int16(bytes, 26);
769 ao_gps_prev = ao_gps_static;
770 ao_gps_static.tick = tick;
771 ao_gps_static.latitude = int32(bytes, 0);
772 ao_gps_static.longitude = int32(bytes, 4);
773 ao_gps_static.altitude = int32(bytes, 8);
774 ao_gps_static.flags = bytes[13];
776 ao_gps_first = ao_gps_static;
781 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
782 if (strcmp(words[1], "reserved:") == 0)
783 ms5607_prom.reserved = strtoul(words[2], NULL, 10);
784 else if (strcmp(words[1], "sens:") == 0)
785 ms5607_prom.sens = strtoul(words[2], NULL, 10);
786 else if (strcmp(words[1], "off:") == 0)
787 ms5607_prom.off = strtoul(words[2], NULL, 10);
788 else if (strcmp(words[1], "tcs:") == 0)
789 ms5607_prom.tcs = strtoul(words[2], NULL, 10);
790 else if (strcmp(words[1], "tco:") == 0)
791 ms5607_prom.tco = strtoul(words[2], NULL, 10);
792 else if (strcmp(words[1], "tref:") == 0)
793 ms5607_prom.tref = strtoul(words[2], NULL, 10);
794 else if (strcmp(words[1], "tempsens:") == 0)
795 ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
796 else if (strcmp(words[1], "crc:") == 0)
797 ms5607_prom.crc = strtoul(words[2], NULL, 10);
799 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
800 int p = strtoul(words[1], NULL, 10);
802 struct ao_pyro *pyro = &ao_config.pyro[p];
804 for (i = 2; i < nword; i++) {
805 for (j = 0; j < NUM_PYRO_VALUES; j++)
806 if (!strcmp (words[i], ao_pyro_values[j].name))
808 if (j == NUM_PYRO_VALUES)
810 pyro->flags |= ao_pyro_values[j].flag;
811 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
812 int16_t val = strtoul(words[++i], NULL, 10);
813 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
818 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
820 tick = strtoul(words[1], NULL, 16);
821 a = strtoul(words[2], NULL, 16);
822 b = strtoul(words[3], NULL, 16);
827 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
828 log_format = strtoul(words[1], NULL, 10);
829 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
830 ao_config.accel_plus_g = atoi(words[3]);
831 ao_config.accel_minus_g = atoi(words[5]);
833 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
834 ao_config.accel_zero_along = atoi(words[3]);
835 ao_config.accel_zero_across = atoi(words[5]);
836 ao_config.accel_zero_through = atoi(words[7]);
837 printf ("%d %d %d\n", ao_config.accel_zero_along, ao_config.accel_zero_across, ao_config.accel_zero_through);
839 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
840 ao_config.main_deploy = atoi(words[2]);
841 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
842 strcmp(words[1], "lockout:") == 0) {
843 ao_config.apogee_lockout = atoi(words[2]);
844 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
845 tick = atoi(words[10]);
846 if (!ao_flight_started) {
849 ao_flight_started = 1;
855 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
856 tick = strtol(words[1], NULL, 16);
858 b = strtol(words[2], NULL, 10);
860 if (!ao_flight_started) {
861 ao_flight_ground_accel = 16384 - 328;
862 ao_config.accel_plus_g = 16384 - 328;
863 ao_config.accel_minus_g = 16384 + 328;
864 ao_flight_started = 1;
866 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
867 __xdata char *hex = words[1];
873 if (len > sizeof (bytes) * 2) {
874 len = sizeof (bytes)*2;
877 for (i = 0; i < len; i += 2) {
881 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
884 if (bytes[0] != len - 2) {
885 printf ("bad length %d != %d\n", bytes[0], len - 2);
889 for (i = 1; i < len-1; i++)
891 if (sum != bytes[len-1]) {
892 printf ("bad checksum\n");
895 if ((bytes[len-2] & 0x80) == 0) {
899 ao_xmemcpy(&telem, bytes + 1, 32);
900 tick = telem.generic.tick;
901 switch (telem.generic.type) {
902 case AO_TELEMETRY_SENSOR_TELEMETRUM:
903 case AO_TELEMETRY_SENSOR_TELEMINI:
904 case AO_TELEMETRY_SENSOR_TELENANO:
905 if (!ao_flight_started) {
906 ao_flight_ground_accel = telem.sensor.ground_accel;
907 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
908 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
909 ao_flight_started = 1;
912 a = telem.sensor.accel;
913 b = telem.sensor.pres;
916 } else if (len == 99) {
917 ao_flight_started = 1;
918 tick = uint16(bytes+1, 21);
919 ao_flight_ground_accel = int16(bytes+1, 7);
920 ao_config.accel_plus_g = int16(bytes+1, 17);
921 ao_config.accel_minus_g = int16(bytes+1, 19);
923 a = int16(bytes+1, 23);
924 b = int16(bytes+1, 25);
925 } else if (len == 98) {
926 ao_flight_started = 1;
927 tick = uint16(bytes+1, 20);
928 ao_flight_ground_accel = int16(bytes+1, 6);
929 ao_config.accel_plus_g = int16(bytes+1, 16);
930 ao_config.accel_minus_g = int16(bytes+1, 18);
932 a = int16(bytes+1, 22);
933 b = int16(bytes+1, 24);
935 printf("unknown len %d\n", len);
939 if (type != 'F' && !ao_flight_started)
948 ao_flight_ground_accel = a;
949 if (ao_config.accel_plus_g == 0) {
950 ao_config.accel_plus_g = a;
951 ao_config.accel_minus_g = a + 530;
953 if (ao_config.main_deploy == 0)
954 ao_config.main_deploy = 250;
955 ao_flight_started = 1;
960 ao_data_static.tick = tick;
961 ao_data_static.adc.accel = a;
962 ao_data_static.adc.pres_real = b;
963 if (b < AO_MIN_BARO_VALUE)
964 b = AO_MIN_BARO_VALUE;
965 ao_data_static.adc.pres = b;
970 ao_data_static.tick = tick;
971 ao_data_static.adc.temp = a;
972 ao_data_static.adc.v_batt = b;
986 #define COUNTS_PER_G 264.8
993 static const struct option options[] = {
994 { .name = "summary", .has_arg = 0, .val = 's' },
995 { .name = "debug", .has_arg = 0, .val = 'd' },
996 { .name = "info", .has_arg = 1, .val = 'i' },
1000 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1002 emulator_name = name;
1004 emulator_info = info;
1005 ao_summary = summary;
1011 main (int argc, char **argv)
1019 emulator_app="full";
1021 emulator_app="baro";
1023 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1029 ao_flight_debug = 1;
1038 run_flight_fixed("<stdin>", stdin, summary, info);
1040 for (i = optind; i < argc; i++) {
1041 FILE *f = fopen(argv[i], "r");
1046 run_flight_fixed(argv[i], f, summary, info);