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
96 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
97 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
98 #define from_fix(x) ((x) >> 16)
101 * Above this height, the baro sensor doesn't work
103 #define AO_BARO_SATURATE 13000
104 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
107 * Above this speed, baro measurements are unreliable
109 #define AO_MAX_BARO_SPEED 200
111 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
113 extern enum ao_flight_state ao_flight_state;
118 volatile struct ao_data ao_data_ring[AO_DATA_RING];
119 volatile uint8_t ao_data_head;
123 #define ao_led_off(l)
124 #define ao_timer_set_adc_interval(i)
125 #define ao_wakeup(wchan) ao_dump_state()
126 #define ao_cmd_register(c)
127 #define ao_usb_disable()
128 #define ao_telemetry_set_interval(x)
129 #define ao_rdf_set(rdf)
130 #define ao_packet_slave_start()
131 #define ao_packet_slave_stop()
135 ao_igniter_drogue = 0,
139 struct ao_data ao_data_static;
148 static int32_t ao_k_height;
153 return ao_data_static.tick;
157 ao_delay(int16_t interval)
163 ao_ignite(enum ao_igniter igniter)
165 double time = (double) (ao_data_static.tick + tick_offset) / 100;
167 if (igniter == ao_igniter_drogue) {
169 drogue_height = ao_k_height >> 16;
172 main_height = ao_k_height >> 16;
180 #define ao_add_task(t,f,n) ((void) (t))
182 #define ao_log_start()
183 #define ao_log_stop()
185 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
186 #define AO_SEC_TO_TICKS(s) ((s) * 100)
188 #define AO_FLIGHT_TEST
196 double emulator_error_max = 4;
197 double emulator_height_error_max = 20; /* noise in the baro sensor */
203 ao_sleep(void *wchan);
205 const char const * const ao_state_names[] = {
206 "startup", "idle", "pad", "boost", "fast",
207 "coast", "drogue", "main", "landed", "invalid"
215 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
216 #define ao_xmemset(d,v,c) memset(d,v,c)
217 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
219 #define AO_NEED_ALTITUDE_TO_PRES 1
221 #include "ao_convert_pa.c"
222 #include <ao_ms5607.h>
223 struct ao_ms5607_prom ms5607_prom;
224 #include "ao_ms5607_convert.c"
225 #define AO_PYRO_NUM 4
228 #include "ao_convert.c"
232 uint16_t main_deploy;
233 int16_t accel_plus_g;
234 int16_t accel_minus_g;
235 uint8_t pad_orientation;
236 uint16_t apogee_lockout;
238 struct ao_pyro pyro[AO_PYRO_NUM]; /* minor version 12 */
239 int16_t accel_zero_along;
240 int16_t accel_zero_across;
241 int16_t accel_zero_through;
245 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
246 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
248 #define ao_config_get()
250 struct ao_config ao_config;
252 #define DATA_TO_XDATA(x) (x)
255 extern int16_t ao_ground_accel, ao_flight_accel;
256 extern int16_t ao_accel_2g;
258 typedef int16_t accel_t;
260 extern uint16_t ao_sample_tick;
262 extern alt_t ao_sample_height;
263 extern accel_t ao_sample_accel;
264 extern int32_t ao_accel_scale;
265 extern alt_t ao_ground_height;
266 extern alt_t ao_sample_alt;
268 double ao_sample_qangle;
270 int ao_sample_prev_tick;
274 #include "ao_kalman.c"
276 #include "ao_sample.c"
277 #include "ao_flight.c"
279 #define AO_PYRO_NUM 4
287 ao_pyro_pin_set(uint8_t pin, uint8_t value)
289 printf ("set pyro %d %d\n", pin, value);
295 #define to_double(f) ((f) / 65536.0)
297 static int ao_records_read = 0;
298 static int ao_eof_read = 0;
299 static int ao_flight_ground_accel;
300 static int ao_flight_started = 0;
301 static int ao_test_max_height;
302 static double ao_test_max_height_time;
303 static int ao_test_main_height;
304 static double ao_test_main_height_time;
305 static double ao_test_landed_time;
306 static double ao_test_landed_height;
307 static double ao_test_landed_time;
308 static int landed_set;
309 static double landed_time;
310 static double landed_height;
313 static struct ao_mpu6000_sample ao_ground_mpu6000;
322 double landed_time_error;
324 if (!ao_test_main_height_time) {
325 ao_test_main_height_time = ao_test_max_height_time;
326 ao_test_main_height = ao_test_max_height;
328 drogue_error = fabs(ao_test_max_height_time - drogue_time);
329 main_error = fabs(ao_test_main_height_time - main_time);
330 landed_error = fabs(ao_test_landed_height - landed_height);
331 landed_time_error = ao_test_landed_time - landed_time;
332 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
334 emulator_app, emulator_name);
336 printf ("\t%s\n", emulator_info);
337 printf ("\tApogee error %g\n", drogue_error);
338 printf ("\tMain error %g\n", main_error);
339 printf ("\tLanded height error %g\n", landed_error);
340 printf ("\tLanded time error %g\n", landed_time_error);
341 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
342 ao_test_max_height, ao_test_max_height_time,
343 ao_test_main_height, ao_test_main_height_time,
344 ao_test_landed_height, ao_test_landed_time);
345 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
346 drogue_height, drogue_time, main_height, main_time,
347 landed_height, landed_time);
360 azel (struct ao_azel *r, struct ao_quaternion *q)
364 r->az = floor (atan2(q->y, q->x) * 180/M_PI + 0.5);
365 v = sqrt (q->x*q->x + q->y*q->y);
366 r->el = floor (atan2(q->z, v) * 180/M_PI + 0.5);
375 ao_data_ring[ao_data_head] = ao_data_static;
376 ao_data_head = ao_data_ring_next(ao_data_head);
377 if (ao_flight_state != ao_flight_startup) {
379 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
380 (ao_config.accel_minus_g - ao_config.accel_plus_g);
387 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
388 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
390 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
394 tick_offset = -ao_data_static.tick;
395 if ((prev_tick - ao_data_static.tick) > 0x400)
396 tick_offset += 65536;
397 prev_tick = ao_data_static.tick;
398 time = (double) (ao_data_static.tick + tick_offset) / 100;
400 if (ao_test_max_height < height) {
401 ao_test_max_height = height;
402 ao_test_max_height_time = time;
403 ao_test_landed_height = height;
404 ao_test_landed_time = time;
406 if (height > ao_config.main_deploy) {
407 ao_test_main_height_time = time;
408 ao_test_main_height = height;
411 if (ao_test_landed_height > height) {
412 ao_test_landed_height = height;
413 ao_test_landed_time = time;
416 if (ao_flight_state == ao_flight_landed && !landed_set) {
419 landed_height = height;
424 static struct ao_quaternion ao_ground_mag;
425 static int ao_ground_mag_set;
427 if (!ao_ground_mag_set) {
428 ao_quaternion_init_vector (&ao_ground_mag,
429 ao_data_mag_across(&ao_data_static),
430 ao_data_mag_through(&ao_data_static),
431 ao_data_mag_along(&ao_data_static));
432 ao_quaternion_normalize(&ao_ground_mag, &ao_ground_mag);
433 ao_quaternion_rotate(&ao_ground_mag, &ao_ground_mag, &ao_rotation);
434 ao_ground_mag_set = 1;
437 struct ao_quaternion ao_mag, ao_mag_rot;
439 ao_quaternion_init_vector(&ao_mag,
440 ao_data_mag_across(&ao_data_static),
441 ao_data_mag_through(&ao_data_static),
442 ao_data_mag_along(&ao_data_static));
444 ao_quaternion_normalize(&ao_mag, &ao_mag);
445 ao_quaternion_rotate(&ao_mag_rot, &ao_mag, &ao_rotation);
450 ao_dot = ao_quaternion_dot(&ao_mag_rot, &ao_ground_mag);
452 struct ao_azel ground_azel, mag_azel, rot_azel;
454 azel(&ground_azel, &ao_ground_mag);
455 azel(&mag_azel, &ao_mag);
456 azel(&rot_azel, &ao_mag_rot);
458 ao_mag_angle = floor (acos(ao_dot) * 180 / M_PI + 0.5);
460 static struct ao_quaternion ao_x = { .r = 0, .x = 1, .y = 0, .z = 0 };
461 struct ao_quaternion ao_out;
463 ao_quaternion_rotate(&ao_out, &ao_x, &ao_rotation);
465 int out = floor (atan2(ao_out.y, ao_out.x) * 180 / M_PI);
467 printf ("%7.2f state %-8.8s height %8.4f tilt %4d rot %4d mag_tilt %4d mag_rot %4d\n",
469 ao_state_names[ao_flight_state],
470 ao_k_height / 65536.0,
471 ao_sample_orient, out,
477 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",
478 ao_state_names[ao_flight_state],
479 ground_azel.az, ground_azel.el,
480 mag_azel.az, mag_azel.el,
481 rot_azel.az, rot_azel.el,
482 ground_azel.el - rot_azel.el,
483 ground_azel.az - rot_azel.az,
499 printf("%7.2f height %8.2f accel %8.3f "
502 "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 "
504 "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",
511 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
512 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
513 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
514 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
515 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
516 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
517 ao_data_static.hmc5883.x,
518 ao_data_static.hmc5883.y,
519 ao_data_static.hmc5883.z,
522 ao_state_names[ao_flight_state],
523 ao_k_height / 65536.0,
524 ao_k_speed / 65536.0 / 16.0,
525 ao_k_accel / 65536.0 / 16.0,
532 // if (ao_flight_state == ao_flight_landed)
538 #define AO_MAX_CALLSIGN 8
539 #define AO_MAX_VERSION 8
540 #define AO_MAX_TELEMETRY 128
542 struct ao_telemetry_generic {
543 uint16_t serial; /* 0 */
544 uint16_t tick; /* 2 */
545 uint8_t type; /* 4 */
546 uint8_t payload[27]; /* 5 */
550 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
551 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
552 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
554 struct ao_telemetry_sensor {
555 uint16_t serial; /* 0 */
556 uint16_t tick; /* 2 */
557 uint8_t type; /* 4 */
559 uint8_t state; /* 5 flight state */
560 int16_t accel; /* 6 accelerometer (TM only) */
561 int16_t pres; /* 8 pressure sensor */
562 int16_t temp; /* 10 temperature sensor */
563 int16_t v_batt; /* 12 battery voltage */
564 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
565 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
567 int16_t acceleration; /* 18 m/s² * 16 */
568 int16_t speed; /* 20 m/s * 16 */
569 int16_t height; /* 22 m */
571 int16_t ground_pres; /* 24 average pres on pad */
572 int16_t ground_accel; /* 26 average accel on pad */
573 int16_t accel_plus_g; /* 28 accel calibration at +1g */
574 int16_t accel_minus_g; /* 30 accel calibration at -1g */
578 #define AO_TELEMETRY_CONFIGURATION 0x04
580 struct ao_telemetry_configuration {
581 uint16_t serial; /* 0 */
582 uint16_t tick; /* 2 */
583 uint8_t type; /* 4 */
585 uint8_t device; /* 5 device type */
586 uint16_t flight; /* 6 flight number */
587 uint8_t config_major; /* 8 Config major version */
588 uint8_t config_minor; /* 9 Config minor version */
589 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
590 uint16_t main_deploy; /* 12 Main deploy alt in meters */
591 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
592 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
593 char version[AO_MAX_VERSION]; /* 24 Software version */
597 #define AO_TELEMETRY_LOCATION 0x05
599 #define AO_GPS_MODE_NOT_VALID 'N'
600 #define AO_GPS_MODE_AUTONOMOUS 'A'
601 #define AO_GPS_MODE_DIFFERENTIAL 'D'
602 #define AO_GPS_MODE_ESTIMATED 'E'
603 #define AO_GPS_MODE_MANUAL 'M'
604 #define AO_GPS_MODE_SIMULATED 'S'
606 struct ao_telemetry_location {
607 uint16_t serial; /* 0 */
608 uint16_t tick; /* 2 */
609 uint8_t type; /* 4 */
611 uint8_t flags; /* 5 Number of sats and other flags */
612 int16_t altitude; /* 6 GPS reported altitude (m) */
613 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
614 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
615 uint8_t year; /* 16 (- 2000) */
616 uint8_t month; /* 17 (1-12) */
617 uint8_t day; /* 18 (1-31) */
618 uint8_t hour; /* 19 (0-23) */
619 uint8_t minute; /* 20 (0-59) */
620 uint8_t second; /* 21 (0-59) */
621 uint8_t pdop; /* 22 (m * 5) */
622 uint8_t hdop; /* 23 (m * 5) */
623 uint8_t vdop; /* 24 (m * 5) */
624 uint8_t mode; /* 25 */
625 uint16_t ground_speed; /* 26 cm/s */
626 int16_t climb_rate; /* 28 cm/s */
627 uint8_t course; /* 30 degrees / 2 */
628 uint8_t unused[1]; /* 31 */
632 #define AO_TELEMETRY_SATELLITE 0x06
634 struct ao_telemetry_satellite_info {
639 struct ao_telemetry_satellite {
640 uint16_t serial; /* 0 */
641 uint16_t tick; /* 2 */
642 uint8_t type; /* 4 */
643 uint8_t channels; /* 5 number of reported sats */
645 struct ao_telemetry_satellite_info sats[12]; /* 6 */
646 uint8_t unused[2]; /* 30 */
650 union ao_telemetry_all {
651 struct ao_telemetry_generic generic;
652 struct ao_telemetry_sensor sensor;
653 struct ao_telemetry_configuration configuration;
654 struct ao_telemetry_location location;
655 struct ao_telemetry_satellite satellite;
659 uint16(uint8_t *bytes, int off)
661 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
665 int16(uint8_t *bytes, int off)
667 return (int16_t) uint16(bytes, off);
671 uint32(uint8_t *bytes, int off)
673 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
674 (((uint32_t) bytes[off+2]) << 16) |
675 (((uint32_t) bytes[off+3]) << 24);
679 int32(uint8_t *bytes, int off)
681 return (int32_t) uint32(bytes, off);
684 static int log_format;
687 ao_sleep(void *wchan)
689 if (wchan == &ao_data_head) {
692 uint16_t a = 0, b = 0;
694 union ao_telemetry_all telem;
702 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
706 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
709 ao_data_static.mpu6000 = ao_ground_mpu6000;
711 ao_data_static.adc.accel = ao_flight_ground_accel;
717 if (!fgets(line, sizeof (line), emulator_in)) {
718 if (++ao_eof_read >= 1000) {
720 printf ("no more data, exiting simulation\n");
723 ao_data_static.tick += 10;
728 for (nword = 0; nword < 64; nword++) {
729 words[nword] = strtok_r(l, " \t\n", &saveptr);
731 if (words[nword] == NULL)
735 if (log_format == AO_LOG_FORMAT_TELEMEGA && nword == 30 && strlen(words[0]) == 1) {
737 struct ao_ms5607_value value;
740 tick = strtoul(words[1], NULL, 16);
741 // printf ("%c %04x", type, tick);
742 for (i = 2; i < nword; i++) {
743 bytes[i - 2] = strtoul(words[i], NULL, 16);
744 // printf(" %02x", bytes[i-2]);
749 ao_flight_ground_accel = int16(bytes, 2);
750 ao_flight_started = 1;
751 ao_ground_pres = int32(bytes, 4);
752 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
755 ao_data_static.tick = tick;
756 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
757 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
758 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
759 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
760 ao_data_static.mpu6000.accel_y = int16(bytes, 10);
761 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
762 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
763 ao_data_static.mpu6000.gyro_y = int16(bytes, 16);
764 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
765 ao_data_static.hmc5883.x = int16(bytes, 20);
766 ao_data_static.hmc5883.y = int16(bytes, 22);
767 ao_data_static.hmc5883.z = int16(bytes, 24);
769 ao_data_static.mma655x = int16(bytes, 26);
776 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
777 if (strcmp(words[1], "reserved:") == 0)
778 ms5607_prom.reserved = strtoul(words[2], NULL, 10);
779 else if (strcmp(words[1], "sens:") == 0)
780 ms5607_prom.sens = strtoul(words[2], NULL, 10);
781 else if (strcmp(words[1], "off:") == 0)
782 ms5607_prom.off = strtoul(words[2], NULL, 10);
783 else if (strcmp(words[1], "tcs:") == 0)
784 ms5607_prom.tcs = strtoul(words[2], NULL, 10);
785 else if (strcmp(words[1], "tco:") == 0)
786 ms5607_prom.tco = strtoul(words[2], NULL, 10);
787 else if (strcmp(words[1], "tref:") == 0)
788 ms5607_prom.tref = strtoul(words[2], NULL, 10);
789 else if (strcmp(words[1], "tempsens:") == 0)
790 ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
791 else if (strcmp(words[1], "crc:") == 0)
792 ms5607_prom.crc = strtoul(words[2], NULL, 10);
794 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
795 int p = strtoul(words[1], NULL, 10);
797 struct ao_pyro *pyro = &ao_config.pyro[p];
799 for (i = 2; i < nword; i++) {
800 for (j = 0; j < NUM_PYRO_VALUES; j++)
801 if (!strcmp (words[2], ao_pyro_values[j].name))
803 if (j == NUM_PYRO_VALUES)
805 pyro->flags |= ao_pyro_values[j].flag;
806 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
807 int16_t val = strtoul(words[++i], NULL, 10);
808 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
813 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
815 tick = strtoul(words[1], NULL, 16);
816 a = strtoul(words[2], NULL, 16);
817 b = strtoul(words[3], NULL, 16);
822 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
823 log_format = strtoul(words[1], NULL, 10);
824 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
825 ao_config.accel_plus_g = atoi(words[3]);
826 ao_config.accel_minus_g = atoi(words[5]);
828 } else if (nword >= 8 && strcmp(words[0], "IMU") == 0) {
829 ao_config.accel_zero_along = atoi(words[3]);
830 ao_config.accel_zero_across = atoi(words[5]);
831 ao_config.accel_zero_through = atoi(words[7]);
832 printf ("%d %d %d\n", ao_config.accel_zero_along, ao_config.accel_zero_across, ao_config.accel_zero_through);
834 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
835 ao_config.main_deploy = atoi(words[2]);
836 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
837 strcmp(words[1], "lockout:") == 0) {
838 ao_config.apogee_lockout = atoi(words[2]);
839 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
840 tick = atoi(words[10]);
841 if (!ao_flight_started) {
844 ao_flight_started = 1;
850 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
851 tick = strtol(words[1], NULL, 16);
853 b = strtol(words[2], NULL, 10);
855 if (!ao_flight_started) {
856 ao_flight_ground_accel = 16384 - 328;
857 ao_config.accel_plus_g = 16384 - 328;
858 ao_config.accel_minus_g = 16384 + 328;
859 ao_flight_started = 1;
861 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
862 __xdata char *hex = words[1];
868 if (len > sizeof (bytes) * 2) {
869 len = sizeof (bytes)*2;
872 for (i = 0; i < len; i += 2) {
876 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
879 if (bytes[0] != len - 2) {
880 printf ("bad length %d != %d\n", bytes[0], len - 2);
884 for (i = 1; i < len-1; i++)
886 if (sum != bytes[len-1]) {
887 printf ("bad checksum\n");
890 if ((bytes[len-2] & 0x80) == 0) {
894 ao_xmemcpy(&telem, bytes + 1, 32);
895 tick = telem.generic.tick;
896 switch (telem.generic.type) {
897 case AO_TELEMETRY_SENSOR_TELEMETRUM:
898 case AO_TELEMETRY_SENSOR_TELEMINI:
899 case AO_TELEMETRY_SENSOR_TELENANO:
900 if (!ao_flight_started) {
901 ao_flight_ground_accel = telem.sensor.ground_accel;
902 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
903 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
904 ao_flight_started = 1;
907 a = telem.sensor.accel;
908 b = telem.sensor.pres;
911 } else if (len == 99) {
912 ao_flight_started = 1;
913 tick = uint16(bytes+1, 21);
914 ao_flight_ground_accel = int16(bytes+1, 7);
915 ao_config.accel_plus_g = int16(bytes+1, 17);
916 ao_config.accel_minus_g = int16(bytes+1, 19);
918 a = int16(bytes+1, 23);
919 b = int16(bytes+1, 25);
920 } else if (len == 98) {
921 ao_flight_started = 1;
922 tick = uint16(bytes+1, 20);
923 ao_flight_ground_accel = int16(bytes+1, 6);
924 ao_config.accel_plus_g = int16(bytes+1, 16);
925 ao_config.accel_minus_g = int16(bytes+1, 18);
927 a = int16(bytes+1, 22);
928 b = int16(bytes+1, 24);
930 printf("unknown len %d\n", len);
934 if (type != 'F' && !ao_flight_started)
943 ao_flight_ground_accel = a;
944 if (ao_config.accel_plus_g == 0) {
945 ao_config.accel_plus_g = a;
946 ao_config.accel_minus_g = a + 530;
948 if (ao_config.main_deploy == 0)
949 ao_config.main_deploy = 250;
950 ao_flight_started = 1;
955 ao_data_static.tick = tick;
956 ao_data_static.adc.accel = a;
957 ao_data_static.adc.pres_real = b;
958 if (b < AO_MIN_BARO_VALUE)
959 b = AO_MIN_BARO_VALUE;
960 ao_data_static.adc.pres = b;
965 ao_data_static.tick = tick;
966 ao_data_static.adc.temp = a;
967 ao_data_static.adc.v_batt = b;
981 #define COUNTS_PER_G 264.8
988 static const struct option options[] = {
989 { .name = "summary", .has_arg = 0, .val = 's' },
990 { .name = "debug", .has_arg = 0, .val = 'd' },
991 { .name = "info", .has_arg = 1, .val = 'i' },
995 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
997 emulator_name = name;
999 emulator_info = info;
1000 ao_summary = summary;
1006 main (int argc, char **argv)
1014 emulator_app="full";
1016 emulator_app="baro";
1018 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1024 ao_flight_debug = 1;
1033 run_flight_fixed("<stdin>", stdin, summary, info);
1035 for (i = optind; i < argc; i++) {
1036 FILE *f = fopen(argv[i], "r");
1041 run_flight_fixed(argv[i], f, summary, info);