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.
30 #define AO_DATA_RING 64
31 #define ao_data_ring_next(n) (((n) + 1) & (AO_DATA_RING - 1))
32 #define ao_data_ring_prev(n) (((n) - 1) & (AO_DATA_RING - 1))
34 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
35 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
36 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
39 #define AO_ADC_NUM_SENSE 6
45 int16_t sense[AO_ADC_NUM_SENSE];
54 * One set of samples read from the A/D converter
57 int16_t accel; /* accelerometer */
58 int16_t pres; /* pressure sensor */
59 int16_t pres_real; /* unclipped */
60 int16_t temp; /* temperature sensor */
61 int16_t v_batt; /* battery voltage */
62 int16_t sense_d; /* drogue continuity sense */
63 int16_t sense_m; /* main continuity sense */
68 #define HAS_ACCEL_REF 0
87 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
88 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
89 #define from_fix(x) ((x) >> 16)
92 * Above this height, the baro sensor doesn't work
94 #define AO_BARO_SATURATE 13000
95 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
98 * Above this speed, baro measurements are unreliable
100 #define AO_MAX_BARO_SPEED 200
102 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
104 extern enum ao_flight_state ao_flight_state;
109 volatile struct ao_data ao_data_ring[AO_DATA_RING];
110 volatile uint8_t ao_data_head;
114 #define ao_led_off(l)
115 #define ao_timer_set_adc_interval(i)
116 #define ao_wakeup(wchan) ao_dump_state()
117 #define ao_cmd_register(c)
118 #define ao_usb_disable()
119 #define ao_telemetry_set_interval(x)
120 #define ao_rdf_set(rdf)
121 #define ao_packet_slave_start()
122 #define ao_packet_slave_stop()
125 ao_igniter_drogue = 0,
129 struct ao_data ao_data_static;
138 static int32_t ao_k_height;
141 ao_ignite(enum ao_igniter igniter)
143 double time = (double) (ao_data_static.tick + tick_offset) / 100;
145 if (igniter == ao_igniter_drogue) {
147 drogue_height = ao_k_height >> 16;
150 main_height = ao_k_height >> 16;
158 #define ao_add_task(t,f,n) ((void) (t))
160 #define ao_log_start()
161 #define ao_log_stop()
163 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
164 #define AO_SEC_TO_TICKS(s) ((s) * 100)
166 #define AO_FLIGHT_TEST
174 double emulator_error_max = 4;
175 double emulator_height_error_max = 20; /* noise in the baro sensor */
181 ao_sleep(void *wchan);
183 const char const * const ao_state_names[] = {
184 "startup", "idle", "pad", "boost", "fast",
185 "coast", "drogue", "main", "landed", "invalid"
193 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
194 #define ao_xmemset(d,v,c) memset(d,v,c)
195 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
197 #define AO_NEED_ALTITUDE_TO_PRES 1
199 #include "ao_convert_pa.c"
200 #include <ao_ms5607.h>
201 struct ao_ms5607_prom ms5607_prom;
202 #include "ao_ms5607_convert.c"
204 #include "ao_convert.c"
208 uint16_t main_deploy;
209 int16_t accel_plus_g;
210 int16_t accel_minus_g;
211 uint8_t pad_orientation;
212 uint16_t apogee_lockout;
215 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
216 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
218 #define ao_config_get()
220 struct ao_config ao_config;
222 #define DATA_TO_XDATA(x) (x)
225 #define GRAVITY 9.80665
226 extern int16_t ao_ground_accel, ao_flight_accel;
227 extern int16_t ao_accel_2g;
229 typedef int16_t accel_t;
231 extern uint16_t ao_sample_tick;
233 extern alt_t ao_sample_height;
234 extern accel_t ao_sample_accel;
235 extern int32_t ao_accel_scale;
236 extern alt_t ao_ground_height;
237 extern alt_t ao_sample_alt;
239 int ao_sample_prev_tick;
242 #include "ao_kalman.c"
243 #include "ao_sample.c"
244 #include "ao_flight.c"
246 #define to_double(f) ((f) / 65536.0)
248 static int ao_records_read = 0;
249 static int ao_eof_read = 0;
250 static int ao_flight_ground_accel;
251 static int ao_flight_started = 0;
252 static int ao_test_max_height;
253 static double ao_test_max_height_time;
254 static int ao_test_main_height;
255 static double ao_test_main_height_time;
256 static double ao_test_landed_time;
257 static double ao_test_landed_height;
258 static double ao_test_landed_time;
259 static int landed_set;
260 static double landed_time;
261 static double landed_height;
264 static struct ao_mpu6000_sample ao_ground_mpu6000;
273 double landed_time_error;
275 if (!ao_test_main_height_time) {
276 ao_test_main_height_time = ao_test_max_height_time;
277 ao_test_main_height = ao_test_max_height;
279 drogue_error = fabs(ao_test_max_height_time - drogue_time);
280 main_error = fabs(ao_test_main_height_time - main_time);
281 landed_error = fabs(ao_test_landed_height - landed_height);
282 landed_time_error = ao_test_landed_time - landed_time;
283 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
285 emulator_app, emulator_name);
287 printf ("\t%s\n", emulator_info);
288 printf ("\tApogee error %g\n", drogue_error);
289 printf ("\tMain error %g\n", main_error);
290 printf ("\tLanded height error %g\n", landed_error);
291 printf ("\tLanded time error %g\n", landed_time_error);
292 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
293 ao_test_max_height, ao_test_max_height_time,
294 ao_test_main_height, ao_test_main_height_time,
295 ao_test_landed_height, ao_test_landed_time);
296 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
297 drogue_height, drogue_time, main_height, main_time,
298 landed_height, landed_time);
306 ao_mpu6000_accel(int16_t sensor)
308 return sensor / 32767.0 * MPU6000_ACCEL_FULLSCALE * GRAVITY;
312 ao_mpu6000_gyro(int16_t sensor)
314 return sensor / 32767.0 * MPU6000_GYRO_FULLSCALE;
323 ao_data_ring[ao_data_head] = ao_data_static;
324 ao_data_head = ao_data_ring_next(ao_data_head);
325 if (ao_flight_state != ao_flight_startup) {
327 double accel = ((ao_flight_ground_accel - ao_data_accel_cook(&ao_data_static)) * GRAVITY * 2.0) /
328 (ao_config.accel_minus_g - ao_config.accel_plus_g);
335 ao_ms5607_convert(&ao_data_static.ms5607_raw, &ao_data_static.ms5607_cooked);
336 height = ao_pa_to_altitude(ao_data_static.ms5607_cooked.pres) - ao_ground_height;
338 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
342 tick_offset = -ao_data_static.tick;
343 if ((prev_tick - ao_data_static.tick) > 0x400)
344 tick_offset += 65536;
345 prev_tick = ao_data_static.tick;
346 time = (double) (ao_data_static.tick + tick_offset) / 100;
348 if (ao_test_max_height < height) {
349 ao_test_max_height = height;
350 ao_test_max_height_time = time;
351 ao_test_landed_height = height;
352 ao_test_landed_time = time;
354 if (height > ao_config.main_deploy) {
355 ao_test_main_height_time = time;
356 ao_test_main_height = height;
359 if (ao_test_landed_height > height) {
360 ao_test_landed_height = height;
361 ao_test_landed_time = time;
364 if (ao_flight_state == ao_flight_landed && !landed_set) {
367 landed_height = height;
371 printf("%7.2f height %8.2f accel %8.3f "
373 "accel_x %8.3f accel_y %8.3f accel_z %8.3f gyro_x %8.3f gyro_y %8.3f gyro_z %8.3f "
375 "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",
380 ao_mpu6000_accel(ao_data_static.mpu6000.accel_x),
381 ao_mpu6000_accel(ao_data_static.mpu6000.accel_y),
382 ao_mpu6000_accel(ao_data_static.mpu6000.accel_z),
383 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_x - ao_ground_mpu6000.gyro_x),
384 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_y - ao_ground_mpu6000.gyro_y),
385 ao_mpu6000_gyro(ao_data_static.mpu6000.gyro_z - ao_ground_mpu6000.gyro_z),
387 ao_state_names[ao_flight_state],
388 ao_k_height / 65536.0,
389 ao_k_speed / 65536.0 / 16.0,
390 ao_k_accel / 65536.0 / 16.0,
396 // if (ao_flight_state == ao_flight_landed)
402 #define AO_MAX_CALLSIGN 8
403 #define AO_MAX_VERSION 8
404 #define AO_MAX_TELEMETRY 128
406 struct ao_telemetry_generic {
407 uint16_t serial; /* 0 */
408 uint16_t tick; /* 2 */
409 uint8_t type; /* 4 */
410 uint8_t payload[27]; /* 5 */
414 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
415 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
416 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
418 struct ao_telemetry_sensor {
419 uint16_t serial; /* 0 */
420 uint16_t tick; /* 2 */
421 uint8_t type; /* 4 */
423 uint8_t state; /* 5 flight state */
424 int16_t accel; /* 6 accelerometer (TM only) */
425 int16_t pres; /* 8 pressure sensor */
426 int16_t temp; /* 10 temperature sensor */
427 int16_t v_batt; /* 12 battery voltage */
428 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
429 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
431 int16_t acceleration; /* 18 m/s² * 16 */
432 int16_t speed; /* 20 m/s * 16 */
433 int16_t height; /* 22 m */
435 int16_t ground_pres; /* 24 average pres on pad */
436 int16_t ground_accel; /* 26 average accel on pad */
437 int16_t accel_plus_g; /* 28 accel calibration at +1g */
438 int16_t accel_minus_g; /* 30 accel calibration at -1g */
442 #define AO_TELEMETRY_CONFIGURATION 0x04
444 struct ao_telemetry_configuration {
445 uint16_t serial; /* 0 */
446 uint16_t tick; /* 2 */
447 uint8_t type; /* 4 */
449 uint8_t device; /* 5 device type */
450 uint16_t flight; /* 6 flight number */
451 uint8_t config_major; /* 8 Config major version */
452 uint8_t config_minor; /* 9 Config minor version */
453 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
454 uint16_t main_deploy; /* 12 Main deploy alt in meters */
455 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
456 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
457 char version[AO_MAX_VERSION]; /* 24 Software version */
461 #define AO_TELEMETRY_LOCATION 0x05
463 #define AO_GPS_MODE_NOT_VALID 'N'
464 #define AO_GPS_MODE_AUTONOMOUS 'A'
465 #define AO_GPS_MODE_DIFFERENTIAL 'D'
466 #define AO_GPS_MODE_ESTIMATED 'E'
467 #define AO_GPS_MODE_MANUAL 'M'
468 #define AO_GPS_MODE_SIMULATED 'S'
470 struct ao_telemetry_location {
471 uint16_t serial; /* 0 */
472 uint16_t tick; /* 2 */
473 uint8_t type; /* 4 */
475 uint8_t flags; /* 5 Number of sats and other flags */
476 int16_t altitude; /* 6 GPS reported altitude (m) */
477 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
478 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
479 uint8_t year; /* 16 (- 2000) */
480 uint8_t month; /* 17 (1-12) */
481 uint8_t day; /* 18 (1-31) */
482 uint8_t hour; /* 19 (0-23) */
483 uint8_t minute; /* 20 (0-59) */
484 uint8_t second; /* 21 (0-59) */
485 uint8_t pdop; /* 22 (m * 5) */
486 uint8_t hdop; /* 23 (m * 5) */
487 uint8_t vdop; /* 24 (m * 5) */
488 uint8_t mode; /* 25 */
489 uint16_t ground_speed; /* 26 cm/s */
490 int16_t climb_rate; /* 28 cm/s */
491 uint8_t course; /* 30 degrees / 2 */
492 uint8_t unused[1]; /* 31 */
496 #define AO_TELEMETRY_SATELLITE 0x06
498 struct ao_telemetry_satellite_info {
503 struct ao_telemetry_satellite {
504 uint16_t serial; /* 0 */
505 uint16_t tick; /* 2 */
506 uint8_t type; /* 4 */
507 uint8_t channels; /* 5 number of reported sats */
509 struct ao_telemetry_satellite_info sats[12]; /* 6 */
510 uint8_t unused[2]; /* 30 */
514 union ao_telemetry_all {
515 struct ao_telemetry_generic generic;
516 struct ao_telemetry_sensor sensor;
517 struct ao_telemetry_configuration configuration;
518 struct ao_telemetry_location location;
519 struct ao_telemetry_satellite satellite;
523 uint16(uint8_t *bytes, int off)
525 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
529 int16(uint8_t *bytes, int off)
531 return (int16_t) uint16(bytes, off);
535 uint32(uint8_t *bytes, int off)
537 return (uint32_t) bytes[off] | (((uint32_t) bytes[off+1]) << 8) |
538 (((uint32_t) bytes[off+2]) << 16) |
539 (((uint32_t) bytes[off+3]) << 24);
543 int32(uint8_t *bytes, int off)
545 return (int32_t) uint32(bytes, off);
548 static int log_format;
551 ao_sleep(void *wchan)
553 if (wchan == &ao_data_head) {
556 uint16_t a = 0, b = 0;
558 union ao_telemetry_all telem;
566 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
569 ao_data_static.mpu6000 = ao_ground_mpu6000;
571 ao_data_static.adc.accel = ao_flight_ground_accel;
577 if (!fgets(line, sizeof (line), emulator_in)) {
578 if (++ao_eof_read >= 1000) {
580 printf ("no more data, exiting simulation\n");
583 ao_data_static.tick += 10;
588 for (nword = 0; nword < 64; nword++) {
589 words[nword] = strtok_r(l, " \t\n", &saveptr);
591 if (words[nword] == NULL)
595 if (log_format == AO_LOG_FORMAT_MEGAMETRUM && nword == 30 && strlen(words[0]) == 1) {
597 struct ao_ms5607_value value;
600 tick = strtoul(words[1], NULL, 16);
601 // printf ("%c %04x", type, tick);
602 for (i = 2; i < nword; i++) {
603 bytes[i - 2] = strtoul(words[i], NULL, 16);
604 // printf(" %02x", bytes[i-2]);
609 ao_flight_ground_accel = int16(bytes, 2);
610 ao_flight_started = 1;
611 ao_ground_pres = int32(bytes, 4);
612 ao_ground_height = ao_pa_to_altitude(ao_ground_pres);
615 ao_data_static.tick = tick;
616 ao_data_static.ms5607_raw.pres = int32(bytes, 0);
617 ao_data_static.ms5607_raw.temp = int32(bytes, 4);
618 ao_ms5607_convert(&ao_data_static.ms5607_raw, &value);
619 ao_data_static.mpu6000.accel_x = int16(bytes, 8);
620 ao_data_static.mpu6000.accel_y = -int16(bytes, 10);
621 ao_data_static.mpu6000.accel_z = int16(bytes, 12);
622 ao_data_static.mpu6000.gyro_x = int16(bytes, 14);
623 ao_data_static.mpu6000.gyro_y = -int16(bytes, 16);
624 ao_data_static.mpu6000.gyro_z = int16(bytes, 18);
626 ao_data_static.mma655x = int16(bytes, 26);
628 if (ao_records_read == 0)
629 ao_ground_mpu6000 = ao_data_static.mpu6000;
630 else if (ao_records_read < 10) {
631 #define f(f) ao_ground_mpu6000.f = ao_ground_mpu6000.f + ((ao_data_static.mpu6000.f - ao_ground_mpu6000.f) >> 2)
644 } else if (nword == 3 && strcmp(words[0], "ms5607") == 0) {
645 if (strcmp(words[1], "reserved:") == 0)
646 ms5607_prom.reserved = strtoul(words[2], NULL, 10);
647 else if (strcmp(words[1], "sens:") == 0)
648 ms5607_prom.sens = strtoul(words[2], NULL, 10);
649 else if (strcmp(words[1], "off:") == 0)
650 ms5607_prom.off = strtoul(words[2], NULL, 10);
651 else if (strcmp(words[1], "tcs:") == 0)
652 ms5607_prom.tcs = strtoul(words[2], NULL, 10);
653 else if (strcmp(words[1], "tco:") == 0)
654 ms5607_prom.tco = strtoul(words[2], NULL, 10);
655 else if (strcmp(words[1], "tref:") == 0)
656 ms5607_prom.tref = strtoul(words[2], NULL, 10);
657 else if (strcmp(words[1], "tempsens:") == 0)
658 ms5607_prom.tempsens = strtoul(words[2], NULL, 10);
659 else if (strcmp(words[1], "crc:") == 0)
660 ms5607_prom.crc = strtoul(words[2], NULL, 10);
664 if (nword == 4 && log_format != AO_LOG_FORMAT_MEGAMETRUM) {
666 tick = strtoul(words[1], NULL, 16);
667 a = strtoul(words[2], NULL, 16);
668 b = strtoul(words[3], NULL, 16);
673 else if (nword == 2 && strcmp(words[0], "log-format") == 0) {
674 log_format = strtoul(words[1], NULL, 10);
675 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
676 ao_config.accel_plus_g = atoi(words[3]);
677 ao_config.accel_minus_g = atoi(words[5]);
678 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
679 ao_config.main_deploy = atoi(words[2]);
680 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
681 strcmp(words[1], "lockout:") == 0) {
682 ao_config.apogee_lockout = atoi(words[2]);
683 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
684 tick = atoi(words[10]);
685 if (!ao_flight_started) {
688 ao_flight_started = 1;
694 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
695 tick = strtol(words[1], NULL, 16);
697 b = strtol(words[2], NULL, 10);
699 if (!ao_flight_started) {
700 ao_flight_ground_accel = 16384 - 328;
701 ao_config.accel_plus_g = 16384 - 328;
702 ao_config.accel_minus_g = 16384 + 328;
703 ao_flight_started = 1;
705 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
706 __xdata char *hex = words[1];
712 if (len > sizeof (bytes) * 2) {
713 len = sizeof (bytes)*2;
716 for (i = 0; i < len; i += 2) {
720 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
723 if (bytes[0] != len - 2) {
724 printf ("bad length %d != %d\n", bytes[0], len - 2);
728 for (i = 1; i < len-1; i++)
730 if (sum != bytes[len-1]) {
731 printf ("bad checksum\n");
734 if ((bytes[len-2] & 0x80) == 0) {
738 ao_xmemcpy(&telem, bytes + 1, 32);
739 tick = telem.generic.tick;
740 switch (telem.generic.type) {
741 case AO_TELEMETRY_SENSOR_TELEMETRUM:
742 case AO_TELEMETRY_SENSOR_TELEMINI:
743 case AO_TELEMETRY_SENSOR_TELENANO:
744 if (!ao_flight_started) {
745 ao_flight_ground_accel = telem.sensor.ground_accel;
746 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
747 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
748 ao_flight_started = 1;
751 a = telem.sensor.accel;
752 b = telem.sensor.pres;
755 } else if (len == 99) {
756 ao_flight_started = 1;
757 tick = uint16(bytes+1, 21);
758 ao_flight_ground_accel = int16(bytes+1, 7);
759 ao_config.accel_plus_g = int16(bytes+1, 17);
760 ao_config.accel_minus_g = int16(bytes+1, 19);
762 a = int16(bytes+1, 23);
763 b = int16(bytes+1, 25);
764 } else if (len == 98) {
765 ao_flight_started = 1;
766 tick = uint16(bytes+1, 20);
767 ao_flight_ground_accel = int16(bytes+1, 6);
768 ao_config.accel_plus_g = int16(bytes+1, 16);
769 ao_config.accel_minus_g = int16(bytes+1, 18);
771 a = int16(bytes+1, 22);
772 b = int16(bytes+1, 24);
774 printf("unknown len %d\n", len);
778 if (type != 'F' && !ao_flight_started)
785 ao_flight_ground_accel = a;
786 if (ao_config.accel_plus_g == 0) {
787 ao_config.accel_plus_g = a;
788 ao_config.accel_minus_g = a + 530;
790 if (ao_config.main_deploy == 0)
791 ao_config.main_deploy = 250;
792 ao_flight_started = 1;
797 ao_data_static.tick = tick;
798 ao_data_static.adc.accel = a;
799 ao_data_static.adc.pres_real = b;
800 if (b < AO_MIN_BARO_VALUE)
801 b = AO_MIN_BARO_VALUE;
802 ao_data_static.adc.pres = b;
807 ao_data_static.tick = tick;
808 ao_data_static.adc.temp = a;
809 ao_data_static.adc.v_batt = b;
823 #define COUNTS_PER_G 264.8
830 static const struct option options[] = {
831 { .name = "summary", .has_arg = 0, .val = 's' },
832 { .name = "debug", .has_arg = 0, .val = 'd' },
833 { .name = "info", .has_arg = 1, .val = 'i' },
837 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
839 emulator_name = name;
841 emulator_info = info;
842 ao_summary = summary;
848 main (int argc, char **argv)
860 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
875 run_flight_fixed("<stdin>", stdin, summary, info);
877 for (i = optind; i < argc; i++) {
878 FILE *f = fopen(argv[i], "r");
883 run_flight_fixed(argv[i], f, summary, info);