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
53 #define AO_CONFIG_MAX_SIZE 1024
56 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 ao_ms5607_prom;
313 #include "ao_ms5607_convert.c"
314 #define AO_PYRO_NUM 4
317 #include "ao_convert.c"
320 #include <ao_config.h>
321 #include <ao_fake_flight.h>
323 #define ao_config_get()
325 struct ao_config ao_config;
327 #define DATA_TO_XDATA(x) (x)
330 extern int16_t ao_ground_accel, ao_flight_accel;
331 extern int16_t ao_accel_2g;
333 typedef int16_t accel_t;
335 extern uint16_t ao_sample_tick;
337 extern alt_t ao_sample_height;
338 extern accel_t ao_sample_accel;
339 extern int32_t ao_accel_scale;
340 extern alt_t ao_ground_height;
341 extern alt_t ao_sample_alt;
343 double ao_sample_qangle;
345 int ao_sample_prev_tick;
349 #include "ao_kalman.c"
351 #include "ao_sample.c"
352 #include "ao_flight.c"
354 #define AO_PYRO_NUM 4
362 ao_pyro_pin_set(uint8_t pin, uint8_t value)
364 printf ("set pyro %d %d\n", pin, value);
370 #define to_double(f) ((f) / 65536.0)
372 static int ao_records_read = 0;
373 static int ao_eof_read = 0;
374 static int ao_flight_ground_accel;
375 static int ao_flight_started = 0;
376 static int ao_test_max_height;
377 static double ao_test_max_height_time;
378 static int ao_test_main_height;
379 static double ao_test_main_height_time;
380 static double ao_test_landed_time;
381 static double ao_test_landed_height;
382 static double ao_test_landed_time;
383 static int landed_set;
384 static double landed_time;
385 static double landed_height;
388 static struct ao_mpu6000_sample ao_ground_mpu6000;
397 double landed_time_error;
399 if (!ao_test_main_height_time) {
400 ao_test_main_height_time = ao_test_max_height_time;
401 ao_test_main_height = ao_test_max_height;
403 drogue_error = fabs(ao_test_max_height_time - drogue_time);
404 main_error = fabs(ao_test_main_height_time - main_time);
405 landed_error = fabs(ao_test_landed_height - landed_height);
406 landed_time_error = ao_test_landed_time - landed_time;
407 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
409 emulator_app, emulator_name);
411 printf ("\t%s\n", emulator_info);
412 printf ("\tApogee error %g\n", drogue_error);
413 printf ("\tMain error %g\n", main_error);
414 printf ("\tLanded height error %g\n", landed_error);
415 printf ("\tLanded time error %g\n", landed_time_error);
416 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
417 ao_test_max_height, ao_test_max_height_time,
418 ao_test_main_height, ao_test_main_height_time,
419 ao_test_landed_height, ao_test_landed_time);
420 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
421 drogue_height, drogue_time, main_height, main_time,
422 landed_height, landed_time);
435 azel (struct ao_azel *r, struct ao_quaternion *q)
439 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
440 v = sqrt (q->x*q->x + q->y*q->y);
441 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
450 ao_data_ring[ao_data_head] = ao_data_static;
451 ao_data_head = ao_data_ring_next(ao_data_head);
452 if (ao_flight_state != ao_flight_startup) {
454 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
455 (ao_config.accel_minus_g - ao_config.accel_plus_g);
462 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
463 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
465 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
470 tick_offset = -ao_data_static.tick;
471 if ((prev_tick - ao_data_static.tick) > 0x400)
472 tick_offset += 65536;
473 prev_tick = ao_data_static.tick;
474 time = (double) (ao_data_static.tick + tick_offset) / 100;
476 if (ao_test_max_height < height) {
477 ao_test_max_height = height;
478 ao_test_max_height_time = time;
479 ao_test_landed_height = height;
480 ao_test_landed_time = time;
482 if (height > ao_config.main_deploy) {
483 ao_test_main_height_time = time;
484 ao_test_main_height = height;
487 if (ao_test_landed_height > height) {
488 ao_test_landed_height = height;
489 ao_test_landed_time = time;
492 if (ao_flight_state == ao_flight_landed && !landed_set) {
495 landed_height = height;
500 static struct ao_quaternion ao_ground_mag;
501 static int ao_ground_mag_set;
503 if (!ao_ground_mag_set) {
504 ao_quaternion_init_vector (&ao_ground_mag,
505 ao_data_mag_across(&ao_data_static),
506 ao_data_mag_through(&ao_data_static),
507 ao_data_mag_along(&ao_data_static));
508 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
509 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
510 ao_ground_mag_set = 1;
513 struct ao_quaternion ao_mag, ao_mag_rot;
515 ao_quaternion_init_vector(&ao_mag,
516 ao_data_mag_across(&ao_data_static),
517 ao_data_mag_through(&ao_data_static),
518 ao_data_mag_along(&ao_data_static));
520 ao_quaternion_normalize(&ao_mag, &ao_mag);
521 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
526 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
528 struct ao_azel ground_azel, mag_azel, rot_azel;
530 azel(&ground_azel, &ao_ground_mag);
531 azel(&mag_azel, &ao_mag);
532 azel(&rot_azel, &ao_mag_rot);
534 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
538 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
539 struct ao_quaternion ao_out;
541 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
543 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
546 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
548 ao_state_names[ao_flight_state],
549 ao_k_height / 65536.0,
550 ao_sample_orient, out,
554 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d dist %12.2f gps_tilt %4d gps_sats %2d\n",
556 ao_state_names[ao_flight_state],
557 ao_k_height / 65536.0,
558 ao_sample_orient, out,
559 ao_distance_from_pad(),
560 (int) floor (ao_gps_angle() + 0.5),
561 (ao_gps_static.flags & 0xf) * 10);
564 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",
565 ao_state_names[ao_flight_state],
566 ground_azel.az, ground_azel.el,
567 mag_azel.az, mag_azel.el,
568 rot_azel.az, rot_azel.el,
569 ground_azel.el - rot_azel.el,
570 ground_azel.az - rot_azel.az,
586 printf("%7.2f height %8.2f accel %8.3f "
589 "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 "
591 "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",
598 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
599 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
600 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
601 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
602 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
603 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
604 ao_data_static.hmc5883.x,
605 ao_data_static.hmc5883.y,
606 ao_data_static.hmc5883.z,
609 ao_state_names[ao_flight_state],
610 ao_k_height / 65536.0,
611 ao_k_speed / 65536.0 / 16.0,
612 ao_k_accel / 65536.0 / 16.0,
619 // if (ao_flight_state == ao_flight_landed)
627 uint16(uint8_t *bytes, int off)
629 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
633 int16(uint8_t *bytes, int off)
635 return (int16_t) uint16(bytes, off);
639 uint32(uint8_t *bytes, int off)
641 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
642 (((uint32_t) bytes[off+2]) << 16) |
643 (((uint32_t) bytes[off+3]) << 24);
647 int32(uint8_t *bytes, int off)
649 return (int32_t) uint32(bytes, off);
652 static int log_format;
655 ao_sleep(void *wchan)
657 if (wchan == &ao_data_head) {
660 uint16_t a = 0, b = 0;
662 union ao_telemetry_all telem;
670 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
674 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
677 ao_data_static.mpu6000 = ao_ground_mpu6000;
679 ao_data_static.adc.accel = ao_flight_ground_accel;
685 if (!fgets(line, sizeof (line), emulator_in)) {
686 if (++ao_eof_read >= 1000) {
688 printf ("no more data, exiting simulation\n");
691 ao_data_static.tick += 10;
696 for (nword = 0; nword < 64; nword++) {
697 words[nword] = strtok_r(l, " \t\n", &saveptr);
699 if (words[nword] == NULL)
703 if (log_format == AO_LOG_FORMAT_TELEMEGA && nword == 30 && strlen(words[0]) == 1) {
705 struct ao_ms5607_value value;
708 tick = strtoul(words[1], NULL, 16);
709 // printf ("%c %04x", type, tick);
710 for (i = 2; i < nword; i++) {
711 bytes[i - 2] = strtoul(words[i], NULL, 16);
712 // printf(" %02x", bytes[i-2]);
717 ao_flight_ground_accel = int16(bytes, 2);
718 ao_flight_started = 1;
719 ao_ground_pres = int32(bytes, 4);
720 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
721 ao_ground_accel_along = int16(bytes, 8);
722 ao_ground_accel_across = int16(bytes, 10);
723 ao_ground_accel_through = int16(bytes, 12);
724 ao_ground_roll = int16(bytes, 14);
725 ao_ground_pitch = int16(bytes, 16);
726 ao_ground_yaw = int16(bytes, 18);
727 ao_ground_mpu6000.accel_x = ao_ground_accel_across;
728 ao_ground_mpu6000.accel_y = ao_ground_accel_along;
729 ao_ground_mpu6000.accel_z = ao_ground_accel_through;
730 ao_ground_mpu6000.gyro_x = ao_ground_pitch >> 9;
731 ao_ground_mpu6000.gyro_y = ao_ground_roll >> 9;
732 ao_ground_mpu6000.gyro_z = ao_ground_yaw >> 9;
735 ao_data_static.tick = tick;
736 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
737 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
738 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
739 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
740 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
741 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
742 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
743 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
744 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
745 ao_data_static.hmc5883.x = int16(bytes, 20);
746 ao_data_static.hmc5883.y = int16(bytes, 22);
747 ao_data_static.hmc5883.z = int16(bytes, 24);
749 ao_data_static.mma655x = int16(bytes, 26);
755 ao_gps_prev = ao_gps_static;
756 ao_gps_static.tick = tick;
757 ao_gps_static.latitude = int32(bytes, 0);
758 ao_gps_static.longitude = int32(bytes, 4);
759 ao_gps_static.altitude = int32(bytes, 8);
760 ao_gps_static.flags = bytes[13];
762 ao_gps_first = ao_gps_static;
767 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
768 if (strcmp(words[1], "reserved:") == 0)
769 ao_ms5607_prom.reserved = strtoul(words[2], NULL, 10);
770 else if (strcmp(words[1], "sens:") == 0)
771 ao_ms5607_prom.sens = strtoul(words[2], NULL, 10);
772 else if (strcmp(words[1], "off:") == 0)
773 ao_ms5607_prom.off = strtoul(words[2], NULL, 10);
774 else if (strcmp(words[1], "tcs:") == 0)
775 ao_ms5607_prom.tcs = strtoul(words[2], NULL, 10);
776 else if (strcmp(words[1], "tco:") == 0)
777 ao_ms5607_prom.tco = strtoul(words[2], NULL, 10);
778 else if (strcmp(words[1], "tref:") == 0)
779 ao_ms5607_prom.tref = strtoul(words[2], NULL, 10);
780 else if (strcmp(words[1], "tempsens:") == 0)
781 ao_ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
782 else if (strcmp(words[1], "crc:") == 0)
783 ao_ms5607_prom.crc = strtoul(words[2], NULL, 10);
785 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
786 int p = strtoul(words[1], NULL, 10);
788 struct ao_pyro *pyro = &ao_config.pyro[p];
790 for (i = 2; i < nword; i++) {
791 for (j = 0; j < NUM_PYRO_VALUES; j++)
792 if (!strcmp (words[i], ao_pyro_values[j].name))
794 if (j == NUM_PYRO_VALUES)
796 pyro->flags |= ao_pyro_values[j].flag;
797 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
798 int16_t val = strtoul(words[++i], NULL, 10);
799 printf("pyro %d condition %s value %d\n", p, words[i-1], val);
800 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
805 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
807 tick = strtoul(words[1], NULL, 16);
808 a = strtoul(words[2], NULL, 16);
809 b = strtoul(words[3], NULL, 16);
814 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
815 log_format = strtoul(words[1], NULL, 10);
816 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
817 ao_config.accel_plus_g = atoi(words[3]);
818 ao_config.accel_minus_g = atoi(words[5]);
820 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
821 ao_config.accel_zero_along = atoi(words[3]);
822 ao_config.accel_zero_across = atoi(words[5]);
823 ao_config.accel_zero_through = atoi(words[7]);
824 printf ("%d %d %d\n", ao_config.accel_zero_along, ao_config.accel_zero_across, ao_config.accel_zero_through);
826 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
827 ao_config.main_deploy = atoi(words[2]);
828 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
829 strcmp(words[1], "lockout:") == 0) {
830 ao_config.apogee_lockout = atoi(words[2]);
831 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
832 tick = atoi(words[10]);
833 if (!ao_flight_started) {
836 ao_flight_started = 1;
842 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
843 tick = strtol(words[1], NULL, 16);
845 b = strtol(words[2], NULL, 10);
847 if (!ao_flight_started) {
848 ao_flight_ground_accel = 16384 - 328;
849 ao_config.accel_plus_g = 16384 - 328;
850 ao_config.accel_minus_g = 16384 + 328;
851 ao_flight_started = 1;
853 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
854 __xdata char *hex = words[1];
860 if (len > sizeof (bytes) * 2) {
861 len = sizeof (bytes)*2;
864 for (i = 0; i < len; i += 2) {
868 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
871 if (bytes[0] != len - 2) {
872 printf ("bad length %d != %d\n", bytes[0], len - 2);
876 for (i = 1; i < len-1; i++)
878 if (sum != bytes[len-1]) {
879 printf ("bad checksum\n");
882 if ((bytes[len-2] & 0x80) == 0) {
886 ao_xmemcpy(&telem, bytes + 1, 32);
887 tick = telem.generic.tick;
888 switch (telem.generic.type) {
889 case AO_TELEMETRY_SENSOR_TELEMETRUM:
890 case AO_TELEMETRY_SENSOR_TELEMINI:
891 case AO_TELEMETRY_SENSOR_TELENANO:
892 if (!ao_flight_started) {
893 ao_flight_ground_accel = telem.sensor.ground_accel;
894 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
895 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
896 ao_flight_started = 1;
899 a = telem.sensor.accel;
900 b = telem.sensor.pres;
903 } else if (len == 99) {
904 ao_flight_started = 1;
905 tick = uint16(bytes+1, 21);
906 ao_flight_ground_accel = int16(bytes+1, 7);
907 ao_config.accel_plus_g = int16(bytes+1, 17);
908 ao_config.accel_minus_g = int16(bytes+1, 19);
910 a = int16(bytes+1, 23);
911 b = int16(bytes+1, 25);
912 } else if (len == 98) {
913 ao_flight_started = 1;
914 tick = uint16(bytes+1, 20);
915 ao_flight_ground_accel = int16(bytes+1, 6);
916 ao_config.accel_plus_g = int16(bytes+1, 16);
917 ao_config.accel_minus_g = int16(bytes+1, 18);
919 a = int16(bytes+1, 22);
920 b = int16(bytes+1, 24);
922 printf("unknown len %d\n", len);
926 if (type != 'F' && !ao_flight_started)
935 ao_flight_ground_accel = a;
936 if (ao_config.accel_plus_g == 0) {
937 ao_config.accel_plus_g = a;
938 ao_config.accel_minus_g = a + 530;
940 if (ao_config.main_deploy == 0)
941 ao_config.main_deploy = 250;
942 ao_flight_started = 1;
947 ao_data_static.tick = tick;
948 ao_data_static.adc.accel = a;
949 ao_data_static.adc.pres_real = b;
950 if (b < AO_MIN_BARO_VALUE)
951 b = AO_MIN_BARO_VALUE;
952 ao_data_static.adc.pres = b;
957 ao_data_static.tick = tick;
958 ao_data_static.adc.temp = a;
959 ao_data_static.adc.v_batt = b;
973 #define COUNTS_PER_G 264.8
980 static const struct option options[] = {
981 { .name = "summary", .has_arg = 0, .val = 's' },
982 { .name = "debug", .has_arg = 0, .val = 'd' },
983 { .name = "info", .has_arg = 1, .val = 'i' },
987 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
989 emulator_name = name;
991 emulator_info = info;
992 ao_summary = summary;
999 main (int argc, char **argv)
1007 emulator_app="full";
1009 emulator_app="baro";
1011 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1017 ao_flight_debug = 1;
1026 run_flight_fixed("<stdin>", stdin, summary, info);
1028 for (i = optind; i < argc; i++) {
1029 FILE *f = fopen(argv[i], "r");
1034 run_flight_fixed(argv[i], f, summary, info);