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.
31 #define AO_DATA_RING 64
32 #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1))
33 #define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1))
35 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
36 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
37 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
40 #define AO_ADC_NUM_SENSE 6
46 int16_t sense[AO_ADC_NUM_SENSE];
55 * One set of samples read from the A/D converter
58 int16_t accel; /* accelerometer */
59 int16_t pres; /* pressure sensor */
60 int16_t pres_real; /* unclipped */
61 int16_t temp; /* temperature sensor */
62 int16_t v_batt; /* battery voltage */
63 int16_t sense_d; /* drogue continuity sense */
64 int16_t sense_m; /* main continuity sense */
69 #define HAS_ACCEL_REF 0
88 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
89 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
90 #define from_fix(x) ((x) >> 16)
93 * Above this height, the baro sensor doesn't work
95 #define AO_BARO_SATURATE 13000
96 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
99 * Above this speed, baro measurements are unreliable
101 #define AO_MAX_BARO_SPEED 200
103 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
105 extern enum ao_flight_state ao_flight_state;
110 volatile struct ao_data ao_data_ring[AO_DATA_RING];
111 volatile uint8_t ao_data_head;
115 #define ao_led_off(l)
116 #define ao_timer_set_adc_interval(i)
117 #define ao_wakeup(wchan) ao_dump_state()
118 #define ao_cmd_register(c)
119 #define ao_usb_disable()
120 #define ao_telemetry_set_interval(x)
121 #define ao_rdf_set(rdf)
122 #define ao_packet_slave_start()
123 #define ao_packet_slave_stop()
126 ao_igniter_drogue = 0,
130 struct ao_data ao_data_static;
139 static int32_t ao_k_height;
144 return ao_data_static.tick;
148 ao_delay(int16_t interval)
154 ao_ignite(enum ao_igniter igniter)
156 double time = (double) (ao_data_static.tick + tick_offset) / 100;
158 if (igniter == ao_igniter_drogue) {
160 drogue_height = ao_k_height >> 16;
163 main_height = ao_k_height >> 16;
171 #define ao_add_task(t,f,n) ((void) (t))
173 #define ao_log_start()
174 #define ao_log_stop()
176 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
177 #define AO_SEC_TO_TICKS(s) ((s) * 100)
179 #define AO_FLIGHT_TEST
187 double emulator_error_max = 4;
188 double emulator_height_error_max = 20; /* noise in the baro sensor */
194 ao_sleep(void *wchan);
196 const char const * const ao_state_names[] = {
197 "startup", "idle", "pad", "boost", "fast",
198 "coast", "drogue", "main", "landed", "invalid"
206 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
207 #define ao_xmemset(d,v,c) memset(d,v,c)
208 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
210 #define AO_NEED_ALTITUDE_TO_PRES 1
212 #include "ao_convert_pa.c"
213 #include <ao_ms5607.h>
214 struct ao_ms5607_prom ms5607_prom;
215 #include "ao_ms5607_convert.c"
216 #define AO_PYRO_NUM 4
219 #include "ao_convert.c"
223 uint16_t main_deploy;
224 int16_t accel_plus_g;
225 int16_t accel_minus_g;
226 uint8_t pad_orientation;
227 uint16_t apogee_lockout;
229 struct ao_pyro pyro[AO_PYRO_NUM]; /* minor version 12 */
233 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
234 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
236 #define ao_config_get()
238 struct ao_config ao_config;
240 #define DATA_TO_XDATA(x) (x)
243 #define GRAVITY 9.80665
244 extern int16_t ao_ground_accel, ao_flight_accel;
245 extern int16_t ao_accel_2g;
247 typedef int16_t accel_t;
249 extern uint16_t ao_sample_tick;
251 extern alt_t ao_sample_height;
252 extern accel_t ao_sample_accel;
253 extern int32_t ao_accel_scale;
254 extern alt_t ao_ground_height;
255 extern alt_t ao_sample_alt;
257 double ao_sample_qangle;
259 int ao_sample_prev_tick;
263 #include "ao_kalman.c"
265 #include "ao_sample.c"
266 #include "ao_flight.c"
268 #define AO_PYRO_NUM 4
276 ao_pyro_pin_set(uint8_t pin, uint8_t value)
278 printf ("set pyro %d %d\n", pin, value);
284 #define to_double(f) ((f) / 65536.0)
286 static int ao_records_read = 0;
287 static int ao_eof_read = 0;
288 static int ao_flight_ground_accel;
289 static int ao_flight_started = 0;
290 static int ao_test_max_height;
291 static double ao_test_max_height_time;
292 static int ao_test_main_height;
293 static double ao_test_main_height_time;
294 static double ao_test_landed_time;
295 static double ao_test_landed_height;
296 static double ao_test_landed_time;
297 static int landed_set;
298 static double landed_time;
299 static double landed_height;
302 static struct ao_mpu6000_sample ao_ground_mpu6000;
311 double landed_time_error;
313 if (!ao_test_main_height_time) {
314 ao_test_main_height_time = ao_test_max_height_time;
315 ao_test_main_height = ao_test_max_height;
317 drogue_error = fabs(ao_test_max_height_time - drogue_time);
318 main_error = fabs(ao_test_main_height_time - main_time);
319 landed_error = fabs(ao_test_landed_height - landed_height);
320 landed_time_error = ao_test_landed_time - landed_time;
321 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
323 emulator_app, emulator_name);
325 printf ("\t%s\n", emulator_info);
326 printf ("\tApogee error %g\n", drogue_error);
327 printf ("\tMain error %g\n", main_error);
328 printf ("\tLanded height error %g\n", landed_error);
329 printf ("\tLanded time error %g\n", landed_time_error);
330 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
331 ao_test_max_height, ao_test_max_height_time,
332 ao_test_main_height, ao_test_main_height_time,
333 ao_test_landed_height, ao_test_landed_time);
334 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
335 drogue_height, drogue_time, main_height, main_time,
336 landed_height, landed_time);
344 ao_mpu6000_accel(int16_t sensor)
346 return sensor / 32767.0 * MPU6000_ACCEL_FULLSCALE * GRAVITY;
350 ao_mpu6000_gyro(int32_t sensor)
352 return sensor / 32767.0 * MPU6000_GYRO_FULLSCALE;
361 ao_data_ring[ao_data_head] = ao_data_static;
362 ao_data_head = ao_data_ring_next(ao_data_head);
363 if (ao_flight_state != ao_flight_startup) {
365 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
366 (ao_config.accel_minus_g - ao_config.accel_plus_g);
373 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
374 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
376 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
380 tick_offset = -ao_data_static.tick;
381 if ((prev_tick - ao_data_static.tick) > 0x400)
382 tick_offset += 65536;
383 prev_tick = ao_data_static.tick;
384 time = (double) (ao_data_static.tick + tick_offset) / 100;
386 if (ao_test_max_height < height) {
387 ao_test_max_height = height;
388 ao_test_max_height_time = time;
389 ao_test_landed_height = height;
390 ao_test_landed_time = time;
392 if (height > ao_config.main_deploy) {
393 ao_test_main_height_time = time;
394 ao_test_main_height = height;
397 if (ao_test_landed_height > height) {
398 ao_test_landed_height = height;
399 ao_test_landed_time = time;
402 if (ao_flight_state == ao_flight_landed && !landed_set) {
405 landed_height = height;
409 printf("%7.2f height %8.2f accel %8.3f "
411 "roll %8.3f angle %8.3f qangle %8.3f "
412 "accel_x %8.3f accel_y %8.3f accel_z %8.3f gyro_x %8.3f gyro_y %8.3f gyro_z %8.3f "
414 "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",
419 ao_mpu6000_gyro(ao_sample_roll_angle) / 100.0,
420 ao_mpu6000_gyro(ao_sample_angle) / 100.0,
422 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
423 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
424 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
425 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
426 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
427 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
429 ao_state_names[ao_flight_state],
430 ao_k_height / 65536.0,
431 ao_k_speed / 65536.0 / 16.0,
432 ao_k_accel / 65536.0 / 16.0,
438 // if (ao_flight_state == ao_flight_landed)
444 #define AO_MAX_CALLSIGN 8
445 #define AO_MAX_VERSION 8
446 #define AO_MAX_TELEMETRY 128
448 struct ao_telemetry_generic {
449 uint16_t serial; /* 0 */
450 uint16_t tick; /* 2 */
451 uint8_t type; /* 4 */
452 uint8_t payload[27]; /* 5 */
456 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
457 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
458 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
460 struct ao_telemetry_sensor {
461 uint16_t serial; /* 0 */
462 uint16_t tick; /* 2 */
463 uint8_t type; /* 4 */
465 uint8_t state; /* 5 flight state */
466 int16_t accel; /* 6 accelerometer (TM only) */
467 int16_t pres; /* 8 pressure sensor */
468 int16_t temp; /* 10 temperature sensor */
469 int16_t v_batt; /* 12 battery voltage */
470 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
471 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
473 int16_t acceleration; /* 18 m/s² * 16 */
474 int16_t speed; /* 20 m/s * 16 */
475 int16_t height; /* 22 m */
477 int16_t ground_pres; /* 24 average pres on pad */
478 int16_t ground_accel; /* 26 average accel on pad */
479 int16_t accel_plus_g; /* 28 accel calibration at +1g */
480 int16_t accel_minus_g; /* 30 accel calibration at -1g */
484 #define AO_TELEMETRY_CONFIGURATION 0x04
486 struct ao_telemetry_configuration {
487 uint16_t serial; /* 0 */
488 uint16_t tick; /* 2 */
489 uint8_t type; /* 4 */
491 uint8_t device; /* 5 device type */
492 uint16_t flight; /* 6 flight number */
493 uint8_t config_major; /* 8 Config major version */
494 uint8_t config_minor; /* 9 Config minor version */
495 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
496 uint16_t main_deploy; /* 12 Main deploy alt in meters */
497 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
498 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
499 char version[AO_MAX_VERSION]; /* 24 Software version */
503 #define AO_TELEMETRY_LOCATION 0x05
505 #define AO_GPS_MODE_NOT_VALID 'N'
506 #define AO_GPS_MODE_AUTONOMOUS 'A'
507 #define AO_GPS_MODE_DIFFERENTIAL 'D'
508 #define AO_GPS_MODE_ESTIMATED 'E'
509 #define AO_GPS_MODE_MANUAL 'M'
510 #define AO_GPS_MODE_SIMULATED 'S'
512 struct ao_telemetry_location {
513 uint16_t serial; /* 0 */
514 uint16_t tick; /* 2 */
515 uint8_t type; /* 4 */
517 uint8_t flags; /* 5 Number of sats and other flags */
518 int16_t altitude; /* 6 GPS reported altitude (m) */
519 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
520 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
521 uint8_t year; /* 16 (- 2000) */
522 uint8_t month; /* 17 (1-12) */
523 uint8_t day; /* 18 (1-31) */
524 uint8_t hour; /* 19 (0-23) */
525 uint8_t minute; /* 20 (0-59) */
526 uint8_t second; /* 21 (0-59) */
527 uint8_t pdop; /* 22 (m * 5) */
528 uint8_t hdop; /* 23 (m * 5) */
529 uint8_t vdop; /* 24 (m * 5) */
530 uint8_t mode; /* 25 */
531 uint16_t ground_speed; /* 26 cm/s */
532 int16_t climb_rate; /* 28 cm/s */
533 uint8_t course; /* 30 degrees / 2 */
534 uint8_t unused[1]; /* 31 */
538 #define AO_TELEMETRY_SATELLITE 0x06
540 struct ao_telemetry_satellite_info {
545 struct ao_telemetry_satellite {
546 uint16_t serial; /* 0 */
547 uint16_t tick; /* 2 */
548 uint8_t type; /* 4 */
549 uint8_t channels; /* 5 number of reported sats */
551 struct ao_telemetry_satellite_info sats[12]; /* 6 */
552 uint8_t unused[2]; /* 30 */
556 union ao_telemetry_all {
557 struct ao_telemetry_generic generic;
558 struct ao_telemetry_sensor sensor;
559 struct ao_telemetry_configuration configuration;
560 struct ao_telemetry_location location;
561 struct ao_telemetry_satellite satellite;
565 uint16(uint8_t *bytes, int off)
567 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
571 int16(uint8_t *bytes, int off)
573 return (int16_t) uint16(bytes, off);
577 uint32(uint8_t *bytes, int off)
579 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
580 (((uint32_t) bytes[off+2]) << 16) |
581 (((uint32_t) bytes[off+3]) << 24);
585 int32(uint8_t *bytes, int off)
587 return (int32_t) uint32(bytes, off);
590 static int log_format;
595 ao_vec_norm(double x, double y, double z)
597 return x*x + y*y + z*z;
601 ao_vec_normalize(double *x, double *y, double *z)
603 double scale = 1/sqrt(ao_vec_norm(*x, *y, *z));
611 double q0, q1, q2, q3;
615 ao_quat_mul(struct ao_quat *r, struct ao_quat *a, struct ao_quat *b)
617 r->q0 = a->q0 * b->q0 - a->q1 * b->q1 - a->q2 * b->q2 - a->q3 * b->q3;
618 r->q1 = a->q0 * b->q1 + a->q1 * b->q0 + a->q2 * b->q3 - a->q3 * b->q2;
619 r->q2 = a->q0 * b->q2 - a->q1 * b->q3 + a->q2 * b->q0 + a->q3 * b->q1;
620 r->q3 = a->q0 * b->q3 + a->q1 * b->q2 - a->q2 * b->q1 + a->q3 * b->q0;
625 ao_quat_scale(struct ao_quat *r, struct ao_quat *a, double s)
635 ao_quat_conj(struct ao_quat *r, struct ao_quat *a)
644 ao_quat_rot(struct ao_quat *r, struct ao_quat *a, struct ao_quat *q)
648 ao_quat_mul(&t, q, a);
650 ao_quat_mul(r, &t, &c);
654 ao_quat_from_angle(struct ao_quat *r,
659 double angle = sqrt (x_rad * x_rad + y_rad * y_rad + z_rad * z_rad);
660 double s = sin(angle/2);
661 double c = cos(angle/2);
664 r->q1 = x_rad * s / angle;
665 r->q2 = y_rad * s / angle;
666 r->q3 = z_rad * s / angle;
670 ao_quat_from_vector(struct ao_quat *r, double x, double y, double z)
672 ao_vec_normalize(&x, &y, &z);
673 double x_rad = atan2(z, y);
674 double y_rad = atan2(x, z);
675 double z_rad = atan2(y, x);
677 ao_quat_from_angle(r, x_rad, y_rad, z_rad);
681 ao_quat_norm(struct ao_quat *a)
683 return (a->q0 * a->q0 +
690 ao_quat_normalize(struct ao_quat *a)
692 double norm = ao_quat_norm(a);
695 double m = 1/sqrt(norm);
704 static struct ao_quat ao_up, ao_current;
705 static struct ao_quat ao_orient;
706 static int ao_orient_tick;
709 set_orientation(double x, double y, double z, int tick)
713 printf ("set_orientation %g %g %g\n", x, y, z);
714 ao_quat_from_vector(&ao_orient, x, y, z);
715 ao_up.q1 = ao_up.q2 = 0;
716 ao_up.q0 = ao_up.q3 = sqrt(2)/2;
717 ao_orient_tick = tick;
724 printf ("orient (%g) %g %g %g up (%g) %g %g %g\n",
734 ao_quat_rot(&t, &ao_up, &ao_orient);
735 printf ("pad orient (%g) %g %g %g\n",
744 update_orientation (double rate_x, double rate_y, double rate_z, int tick)
746 struct ao_quat q_dot;
748 double dt = (tick - ao_orient_tick) / 100.0;
750 ao_orient_tick = tick;
752 // lambda = 1 - ao_quat_norm(&ao_orient);
755 q_dot.q0 = -0.5 * (ao_orient.q1 * rate_x + ao_orient.q2 * rate_y + ao_orient.q3 * rate_z) + lambda * ao_orient.q0;
756 q_dot.q1 = 0.5 * (ao_orient.q0 * rate_x + ao_orient.q2 * rate_z - ao_orient.q3 * rate_y) + lambda * ao_orient.q1;
757 q_dot.q2 = 0.5 * (ao_orient.q0 * rate_y + ao_orient.q3 * rate_x - ao_orient.q1 * rate_z) + lambda * ao_orient.q2;
758 q_dot.q3 = 0.5 * (ao_orient.q0 * rate_z + ao_orient.q1 * rate_y - ao_orient.q2 * rate_x) + lambda * ao_orient.q3;
761 printf ("update_orientation %g %g %g (%g s)\n", rate_x, rate_y, rate_z, dt);
762 printf ("q_dot (%g) %g %g %g\n",
769 ao_orient.q0 += q_dot.q0 * dt;
770 ao_orient.q1 += q_dot.q1 * dt;
771 ao_orient.q2 += q_dot.q2 * dt;
772 ao_orient.q3 += q_dot.q3 * dt;
774 ao_quat_normalize(&ao_orient);
776 ao_quat_rot(&ao_current, &ao_up, &ao_orient);
778 ao_sample_qangle = 180 / M_PI * acos(ao_current.q3 * sqrt(2));
780 printf ("orient (%g) %g %g %g current (%g) %g %g %g\n",
794 ao_sleep(void *wchan)
796 if (wchan == &ao_data_head) {
799 uint16_t a = 0, b = 0;
801 union ao_telemetry_all telem;
809 if (ao_flight_state >= ao_flight_boost && ao_flight_state < ao_flight_landed)
813 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
816 ao_data_static.mpu6000 = ao_ground_mpu6000;
818 ao_data_static.adc.accel = ao_flight_ground_accel;
824 if (!fgets(line, sizeof (line), emulator_in)) {
825 if (++ao_eof_read >= 1000) {
827 printf ("no more data, exiting simulation\n");
830 ao_data_static.tick += 10;
835 for (nword = 0; nword < 64; nword++) {
836 words[nword] = strtok_r(l, " \t\n", &saveptr);
838 if (words[nword] == NULL)
842 if (log_format == AO_LOG_FORMAT_TELEMEGA && nword == 30 && strlen(words[0]) == 1) {
844 struct ao_ms5607_value value;
847 tick = strtoul(words[1], NULL, 16);
848 // printf ("%c %04x", type, tick);
849 for (i = 2; i < nword; i++) {
850 bytes[i - 2] = strtoul(words[i], NULL, 16);
851 // printf(" %02x", bytes[i-2]);
856 ao_flight_ground_accel = int16(bytes, 2);
857 ao_flight_started = 1;
858 ao_ground_pres = int32(bytes, 4);
859 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
862 ao_data_static.tick = tick;
863 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
864 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
865 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
866 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
867 ao_data_static.mpu6000.accel_y = -int16(bytes, 10);
868 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
869 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
870 ao_data_static.mpu6000.gyro_y = -int16(bytes, 16);
871 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
873 ao_data_static.mma655x = int16(bytes, 26);
875 if (ao_records_read == 0)
876 ao_ground_mpu6000 = ao_data_static.mpu6000;
877 else if (ao_records_read < 10) {
878 #define f(f) ao_ground_mpu6000.f = ao_ground_mpu6000.f + ((ao_data_static.mpu6000.f - ao_ground_mpu6000.f) >> 2)
886 double accel_x = ao_mpu6000_accel(ao_ground_mpu6000.accel_x);
887 double accel_y = ao_mpu6000_accel(ao_ground_mpu6000.accel_y);
888 double accel_z = ao_mpu6000_accel(ao_ground_mpu6000.accel_z);
890 /* X and Y are in the ground plane, arbitraryily picked as MPU X and Z axes
891 * Z is normal to the ground, the MPU y axis
893 set_orientation(accel_x, accel_z, accel_y, tick);
895 double rate_x = ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x);
896 double rate_y = ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y);
897 double rate_z = ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z);
899 update_orientation(rate_x * M_PI / 180, rate_z * M_PI / 180, rate_y * M_PI / 180, tick);
906 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
907 if (strcmp(words[1], "reserved:") == 0)
908 ms5607_prom.reserved = strtoul(words[2], NULL, 10);
909 else if (strcmp(words[1], "sens:") == 0)
910 ms5607_prom.sens = strtoul(words[2], NULL, 10);
911 else if (strcmp(words[1], "off:") == 0)
912 ms5607_prom.off = strtoul(words[2], NULL, 10);
913 else if (strcmp(words[1], "tcs:") == 0)
914 ms5607_prom.tcs = strtoul(words[2], NULL, 10);
915 else if (strcmp(words[1], "tco:") == 0)
916 ms5607_prom.tco = strtoul(words[2], NULL, 10);
917 else if (strcmp(words[1], "tref:") == 0)
918 ms5607_prom.tref = strtoul(words[2], NULL, 10);
919 else if (strcmp(words[1], "tempsens:") == 0)
920 ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
921 else if (strcmp(words[1], "crc:") == 0)
922 ms5607_prom.crc = strtoul(words[2], NULL, 10);
924 } else if (nword >= 3 && strcmp(words[0], "Pyro") == 0) {
925 int p = strtoul(words[1], NULL, 10);
927 struct ao_pyro *pyro = &ao_config.pyro[p];
929 for (i = 2; i < nword; i++) {
930 for (j = 0; j < NUM_PYRO_VALUES; j++)
931 if (!strcmp (words[2], ao_pyro_values[j].name))
933 if (j == NUM_PYRO_VALUES)
935 pyro->flags |= ao_pyro_values[j].flag;
936 if (ao_pyro_values[j].offset != NO_VALUE && i + 1 < nword) {
937 int16_t val = strtoul(words[++i], NULL, 10);
938 *((int16_t *) ((char *) pyro + ao_pyro_values[j].offset)) = val;
943 if (nword == 4 && log_format != AO_LOG_FORMAT_TELEMEGA) {
945 tick = strtoul(words[1], NULL, 16);
946 a = strtoul(words[2], NULL, 16);
947 b = strtoul(words[3], NULL, 16);
952 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
953 log_format = strtoul(words[1], NULL, 10);
954 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
955 ao_config.accel_plus_g = atoi(words[3]);
956 ao_config.accel_minus_g = atoi(words[5]);
957 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
958 ao_config.main_deploy = atoi(words[2]);
959 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
960 strcmp(words[1], "lockout:") == 0) {
961 ao_config.apogee_lockout = atoi(words[2]);
962 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
963 tick = atoi(words[10]);
964 if (!ao_flight_started) {
967 ao_flight_started = 1;
973 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
974 tick = strtol(words[1], NULL, 16);
976 b = strtol(words[2], NULL, 10);
978 if (!ao_flight_started) {
979 ao_flight_ground_accel = 16384 - 328;
980 ao_config.accel_plus_g = 16384 - 328;
981 ao_config.accel_minus_g = 16384 + 328;
982 ao_flight_started = 1;
984 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
985 __xdata char *hex = words[1];
991 if (len > sizeof (bytes) * 2) {
992 len = sizeof (bytes)*2;
995 for (i = 0; i < len; i += 2) {
999 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
1002 if (bytes[0] != len - 2) {
1003 printf ("bad length %d != %d\n", bytes[0], len - 2);
1007 for (i = 1; i < len-1; i++)
1009 if (sum != bytes[len-1]) {
1010 printf ("bad checksum\n");
1013 if ((bytes[len-2] & 0x80) == 0) {
1017 ao_xmemcpy(&telem, bytes + 1, 32);
1018 tick = telem.generic.tick;
1019 switch (telem.generic.type) {
1020 case AO_TELEMETRY_SENSOR_TELEMETRUM:
1021 case AO_TELEMETRY_SENSOR_TELEMINI:
1022 case AO_TELEMETRY_SENSOR_TELENANO:
1023 if (!ao_flight_started) {
1024 ao_flight_ground_accel = telem.sensor.ground_accel;
1025 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
1026 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
1027 ao_flight_started = 1;
1030 a = telem.sensor.accel;
1031 b = telem.sensor.pres;
1034 } else if (len == 99) {
1035 ao_flight_started = 1;
1036 tick = uint16(bytes+1, 21);
1037 ao_flight_ground_accel = int16(bytes+1, 7);
1038 ao_config.accel_plus_g = int16(bytes+1, 17);
1039 ao_config.accel_minus_g = int16(bytes+1, 19);
1041 a = int16(bytes+1, 23);
1042 b = int16(bytes+1, 25);
1043 } else if (len == 98) {
1044 ao_flight_started = 1;
1045 tick = uint16(bytes+1, 20);
1046 ao_flight_ground_accel = int16(bytes+1, 6);
1047 ao_config.accel_plus_g = int16(bytes+1, 16);
1048 ao_config.accel_minus_g = int16(bytes+1, 18);
1050 a = int16(bytes+1, 22);
1051 b = int16(bytes+1, 24);
1053 printf("unknown len %d\n", len);
1057 if (type != 'F' && !ao_flight_started)
1066 ao_flight_ground_accel = a;
1067 if (ao_config.accel_plus_g == 0) {
1068 ao_config.accel_plus_g = a;
1069 ao_config.accel_minus_g = a + 530;
1071 if (ao_config.main_deploy == 0)
1072 ao_config.main_deploy = 250;
1073 ao_flight_started = 1;
1078 ao_data_static.tick = tick;
1079 ao_data_static.adc.accel = a;
1080 ao_data_static.adc.pres_real = b;
1081 if (b < AO_MIN_BARO_VALUE)
1082 b = AO_MIN_BARO_VALUE;
1083 ao_data_static.adc.pres = b;
1088 ao_data_static.tick = tick;
1089 ao_data_static.adc.temp = a;
1090 ao_data_static.adc.v_batt = b;
1104 #define COUNTS_PER_G 264.8
1111 static const struct option options[] = {
1112 { .name = "summary", .has_arg = 0, .val = 's' },
1113 { .name = "debug", .has_arg = 0, .val = 'd' },
1114 { .name = "info", .has_arg = 1, .val = 'i' },
1118 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
1120 emulator_name = name;
1122 emulator_info = info;
1123 ao_summary = summary;
1129 main (int argc, char **argv)
1137 emulator_app="full";
1139 emulator_app="baro";
1141 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
1147 ao_flight_debug = 1;
1156 run_flight_fixed("<stdin>", stdin, summary, info);
1158 for (i = optind; i < argc; i++) {
1159 FILE *f = fopen(argv[i], "r");
1164 run_flight_fixed(argv[i], f, summary, info);