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 * One set of samples read from the A/D converter
42 int16_t accel; /* accelerometer */
43 int16_t pres; /* pressure sensor */
44 int16_t pres_real; /* unclipped */
45 int16_t temp; /* temperature sensor */
46 int16_t v_batt; /* battery voltage */
47 int16_t sense_d; /* drogue continuity sense */
48 int16_t sense_m; /* main continuity sense */
59 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
60 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
61 #define from_fix(x) ((x) >> 16)
64 * Above this height, the baro sensor doesn't work
66 #define AO_MAX_BARO_HEIGHT 12000
67 #define AO_BARO_SATURATE 13000
68 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
71 * Above this speed, baro measurements are unreliable
73 #define AO_MAX_BARO_SPEED 200
75 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
77 enum ao_flight_state {
78 ao_flight_startup = 0,
90 extern enum ao_flight_state ao_flight_state;
95 volatile struct ao_data ao_data_ring[AO_DATA_RING];
96 volatile uint8_t ao_data_head;
100 #define ao_led_off(l)
101 #define ao_timer_set_adc_interval(i)
102 #define ao_wakeup(wchan) ao_dump_state()
103 #define ao_cmd_register(c)
104 #define ao_usb_disable()
105 #define ao_telemetry_set_interval(x)
106 #define ao_rdf_set(rdf)
107 #define ao_packet_slave_start()
108 #define ao_packet_slave_stop()
111 ao_igniter_drogue = 0,
115 struct ao_data ao_data_static;
124 static int32_t ao_k_height;
127 ao_ignite(enum ao_igniter igniter)
129 double time = (double) (ao_data_static.tick + tick_offset) / 100;
131 if (igniter == ao_igniter_drogue) {
133 drogue_height = ao_k_height >> 16;
136 main_height = ao_k_height >> 16;
144 #define ao_add_task(t,f,n) ((void) (t))
146 #define ao_log_start()
147 #define ao_log_stop()
149 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
150 #define AO_SEC_TO_TICKS(s) ((s) * 100)
152 #define AO_FLIGHT_TEST
160 double emulator_error_max = 4;
161 double emulator_height_error_max = 20; /* noise in the baro sensor */
167 ao_sleep(void *wchan);
169 const char const * const ao_state_names[] = {
170 "startup", "idle", "pad", "boost", "fast",
171 "coast", "drogue", "main", "landed", "invalid"
179 #define ao_xmemcpy(d,s,c) memcpy(d,s,c)
180 #define ao_xmemset(d,v,c) memset(d,v,c)
181 #define ao_xmemcmp(d,s,c) memcmp(d,s,c)
183 #define AO_NEED_ALTITUDE_TO_PRES 1
184 #include "ao_convert.c"
187 uint16_t main_deploy;
188 int16_t accel_plus_g;
189 int16_t accel_minus_g;
190 uint8_t pad_orientation;
191 uint16_t apogee_lockout;
194 #define AO_PAD_ORIENTATION_ANTENNA_UP 0
195 #define AO_PAD_ORIENTATION_ANTENNA_DOWN 1
197 #define ao_config_get()
199 struct ao_config ao_config;
201 #define DATA_TO_XDATA(x) (x)
210 #define HAS_ACCEL_REF 0
213 #define GRAVITY 9.80665
214 extern int16_t ao_ground_accel, ao_flight_accel;
215 extern int16_t ao_accel_2g;
217 extern uint16_t ao_sample_tick;
219 extern int16_t ao_sample_height;
220 extern int16_t ao_sample_accel;
221 extern int32_t ao_accel_scale;
222 extern int16_t ao_ground_height;
223 extern int16_t ao_sample_alt;
225 int ao_sample_prev_tick;
228 #include "ao_kalman.c"
229 #include "ao_sample.c"
230 #include "ao_flight.c"
232 #define to_double(f) ((f) / 65536.0)
234 static int ao_records_read = 0;
235 static int ao_eof_read = 0;
236 static int ao_flight_ground_accel;
237 static int ao_flight_started = 0;
238 static int ao_test_max_height;
239 static double ao_test_max_height_time;
240 static int ao_test_main_height;
241 static double ao_test_main_height_time;
242 static double ao_test_landed_time;
243 static double ao_test_landed_height;
244 static double ao_test_landed_time;
245 static int landed_set;
246 static double landed_time;
247 static double landed_height;
255 double landed_time_error;
257 if (!ao_test_main_height_time) {
258 ao_test_main_height_time = ao_test_max_height_time;
259 ao_test_main_height = ao_test_max_height;
261 drogue_error = fabs(ao_test_max_height_time - drogue_time);
262 main_error = fabs(ao_test_main_height_time - main_time);
263 landed_error = fabs(ao_test_landed_height - landed_height);
264 landed_time_error = ao_test_landed_time - landed_time;
265 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
267 emulator_app, emulator_name);
269 printf ("\t%s\n", emulator_info);
270 printf ("\tApogee error %g\n", drogue_error);
271 printf ("\tMain error %g\n", main_error);
272 printf ("\tLanded height error %g\n", landed_error);
273 printf ("\tLanded time error %g\n", landed_time_error);
274 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
275 ao_test_max_height, ao_test_max_height_time,
276 ao_test_main_height, ao_test_main_height_time,
277 ao_test_landed_height, ao_test_landed_time);
278 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f landed %7.2f at %7.2f\n",
279 drogue_height, drogue_time, main_height, main_time,
280 landed_height, landed_time);
291 ao_data_ring[ao_data_head] = ao_data_static;
292 ao_data_head = ao_data_ring_next(ao_data_head);
293 if (ao_flight_state != ao_flight_startup) {
294 double height = ao_pres_to_altitude(ao_data_static.adc.pres_real) - ao_ground_height;
295 double accel = ((ao_flight_ground_accel - ao_data_static.adc.accel) * GRAVITY * 2.0) /
296 (ao_config.accel_minus_g - ao_config.accel_plus_g);
299 tick_offset = -ao_data_static.tick;
300 if ((prev_tick - ao_data_static.tick) > 0x400)
301 tick_offset += 65536;
302 prev_tick = ao_data_static.tick;
303 time = (double) (ao_data_static.tick + tick_offset) / 100;
305 if (ao_test_max_height < height) {
306 ao_test_max_height = height;
307 ao_test_max_height_time = time;
308 ao_test_landed_height = height;
309 ao_test_landed_time = time;
311 if (height > ao_config.main_deploy) {
312 ao_test_main_height_time = time;
313 ao_test_main_height = height;
316 if (ao_test_landed_height > height) {
317 ao_test_landed_height = height;
318 ao_test_landed_time = time;
321 if (ao_flight_state == ao_flight_landed && !landed_set) {
324 landed_height = height;
328 printf("%7.2f height %8.2f accel %8.3f 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",
332 ao_state_names[ao_flight_state],
333 ao_k_height / 65536.0,
334 ao_k_speed / 65536.0 / 16.0,
335 ao_k_accel / 65536.0 / 16.0,
341 // if (ao_flight_state == ao_flight_landed)
347 #define AO_MAX_CALLSIGN 8
348 #define AO_MAX_VERSION 8
349 #define AO_MAX_TELEMETRY 128
351 struct ao_telemetry_generic {
352 uint16_t serial; /* 0 */
353 uint16_t tick; /* 2 */
354 uint8_t type; /* 4 */
355 uint8_t payload[27]; /* 5 */
359 #define AO_TELEMETRY_SENSOR_TELEMETRUM 0x01
360 #define AO_TELEMETRY_SENSOR_TELEMINI 0x02
361 #define AO_TELEMETRY_SENSOR_TELENANO 0x03
363 struct ao_telemetry_sensor {
364 uint16_t serial; /* 0 */
365 uint16_t tick; /* 2 */
366 uint8_t type; /* 4 */
368 uint8_t state; /* 5 flight state */
369 int16_t accel; /* 6 accelerometer (TM only) */
370 int16_t pres; /* 8 pressure sensor */
371 int16_t temp; /* 10 temperature sensor */
372 int16_t v_batt; /* 12 battery voltage */
373 int16_t sense_d; /* 14 drogue continuity sense (TM/Tm) */
374 int16_t sense_m; /* 16 main continuity sense (TM/Tm) */
376 int16_t acceleration; /* 18 m/s² * 16 */
377 int16_t speed; /* 20 m/s * 16 */
378 int16_t height; /* 22 m */
380 int16_t ground_pres; /* 24 average pres on pad */
381 int16_t ground_accel; /* 26 average accel on pad */
382 int16_t accel_plus_g; /* 28 accel calibration at +1g */
383 int16_t accel_minus_g; /* 30 accel calibration at -1g */
387 #define AO_TELEMETRY_CONFIGURATION 0x04
389 struct ao_telemetry_configuration {
390 uint16_t serial; /* 0 */
391 uint16_t tick; /* 2 */
392 uint8_t type; /* 4 */
394 uint8_t device; /* 5 device type */
395 uint16_t flight; /* 6 flight number */
396 uint8_t config_major; /* 8 Config major version */
397 uint8_t config_minor; /* 9 Config minor version */
398 uint16_t apogee_delay; /* 10 Apogee deploy delay in seconds */
399 uint16_t main_deploy; /* 12 Main deploy alt in meters */
400 uint16_t flight_log_max; /* 14 Maximum flight log size in kB */
401 char callsign[AO_MAX_CALLSIGN]; /* 16 Radio operator identity */
402 char version[AO_MAX_VERSION]; /* 24 Software version */
406 #define AO_TELEMETRY_LOCATION 0x05
408 #define AO_GPS_MODE_NOT_VALID 'N'
409 #define AO_GPS_MODE_AUTONOMOUS 'A'
410 #define AO_GPS_MODE_DIFFERENTIAL 'D'
411 #define AO_GPS_MODE_ESTIMATED 'E'
412 #define AO_GPS_MODE_MANUAL 'M'
413 #define AO_GPS_MODE_SIMULATED 'S'
415 struct ao_telemetry_location {
416 uint16_t serial; /* 0 */
417 uint16_t tick; /* 2 */
418 uint8_t type; /* 4 */
420 uint8_t flags; /* 5 Number of sats and other flags */
421 int16_t altitude; /* 6 GPS reported altitude (m) */
422 int32_t latitude; /* 8 latitude (degrees * 10⁷) */
423 int32_t longitude; /* 12 longitude (degrees * 10⁷) */
424 uint8_t year; /* 16 (- 2000) */
425 uint8_t month; /* 17 (1-12) */
426 uint8_t day; /* 18 (1-31) */
427 uint8_t hour; /* 19 (0-23) */
428 uint8_t minute; /* 20 (0-59) */
429 uint8_t second; /* 21 (0-59) */
430 uint8_t pdop; /* 22 (m * 5) */
431 uint8_t hdop; /* 23 (m * 5) */
432 uint8_t vdop; /* 24 (m * 5) */
433 uint8_t mode; /* 25 */
434 uint16_t ground_speed; /* 26 cm/s */
435 int16_t climb_rate; /* 28 cm/s */
436 uint8_t course; /* 30 degrees / 2 */
437 uint8_t unused[1]; /* 31 */
441 #define AO_TELEMETRY_SATELLITE 0x06
443 struct ao_telemetry_satellite_info {
448 struct ao_telemetry_satellite {
449 uint16_t serial; /* 0 */
450 uint16_t tick; /* 2 */
451 uint8_t type; /* 4 */
452 uint8_t channels; /* 5 number of reported sats */
454 struct ao_telemetry_satellite_info sats[12]; /* 6 */
455 uint8_t unused[2]; /* 30 */
459 union ao_telemetry_all {
460 struct ao_telemetry_generic generic;
461 struct ao_telemetry_sensor sensor;
462 struct ao_telemetry_configuration configuration;
463 struct ao_telemetry_location location;
464 struct ao_telemetry_satellite satellite;
468 uint16(uint8_t *bytes, int off)
471 return (uint16_t) bytes[off] | (((uint16_t) bytes[off+1]) << 8);
475 int16(uint8_t *bytes, int off)
477 return (int16_t) uint16(bytes, off);
481 ao_sleep(void *wchan)
483 if (wchan == &ao_data_head) {
486 uint16_t a = 0, b = 0;
488 union ao_telemetry_all telem;
496 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
498 ao_data_static.adc.accel = ao_flight_ground_accel;
503 if (!fgets(line, sizeof (line), emulator_in)) {
504 if (++ao_eof_read >= 1000) {
506 printf ("no more data, exiting simulation\n");
509 ao_data_static.tick += 10;
514 for (nword = 0; nword < 64; nword++) {
515 words[nword] = strtok_r(l, " \t\n", &saveptr);
517 if (words[nword] == NULL)
522 tick = strtoul(words[1], NULL, 16);
523 a = strtoul(words[2], NULL, 16);
524 b = strtoul(words[3], NULL, 16);
527 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
528 ao_config.accel_plus_g = atoi(words[3]);
529 ao_config.accel_minus_g = atoi(words[5]);
530 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
531 ao_config.main_deploy = atoi(words[2]);
532 } else if (nword >= 3 && strcmp(words[0], "Apogee") == 0 &&
533 strcmp(words[1], "lockout:") == 0) {
534 ao_config.apogee_lockout = atoi(words[2]);
535 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
536 tick = atoi(words[10]);
537 if (!ao_flight_started) {
540 ao_flight_started = 1;
546 } else if (nword == 3 && strcmp(words[0], "BARO") == 0) {
547 tick = strtol(words[1], NULL, 16);
549 b = strtol(words[2], NULL, 10);
551 if (!ao_flight_started) {
552 ao_flight_ground_accel = 16384 - 328;
553 ao_config.accel_plus_g = 16384 - 328;
554 ao_config.accel_minus_g = 16384 + 328;
555 ao_flight_started = 1;
557 } else if (nword == 2 && strcmp(words[0], "TELEM") == 0) {
558 __xdata char *hex = words[1];
564 if (len > sizeof (bytes) * 2) {
565 len = sizeof (bytes)*2;
568 for (i = 0; i < len; i += 2) {
572 bytes[i/2] = (uint8_t) strtol(elt, NULL, 16);
575 if (bytes[0] != len - 2) {
576 printf ("bad length %d != %d\n", bytes[0], len - 2);
580 for (i = 1; i < len-1; i++)
582 if (sum != bytes[len-1]) {
583 printf ("bad checksum\n");
586 if ((bytes[len-2] & 0x80) == 0) {
590 ao_xmemcpy(&telem, bytes + 1, 32);
591 tick = telem.generic.tick;
592 switch (telem.generic.type) {
593 case AO_TELEMETRY_SENSOR_TELEMETRUM:
594 case AO_TELEMETRY_SENSOR_TELEMINI:
595 case AO_TELEMETRY_SENSOR_TELENANO:
596 if (!ao_flight_started) {
597 ao_flight_ground_accel = telem.sensor.ground_accel;
598 ao_config.accel_plus_g = telem.sensor.accel_plus_g;
599 ao_config.accel_minus_g = telem.sensor.accel_minus_g;
600 ao_flight_started = 1;
603 a = telem.sensor.accel;
604 b = telem.sensor.pres;
607 } else if (len == 99) {
608 ao_flight_started = 1;
609 tick = uint16(bytes, 21);
610 ao_flight_ground_accel = int16(bytes, 7);
611 ao_config.accel_plus_g = int16(bytes, 17);
612 ao_config.accel_minus_g = int16(bytes, 19);
614 a = int16(bytes, 23);
615 b = int16(bytes, 25);
616 } else if (len == 98) {
617 ao_flight_started = 1;
618 tick = uint16(bytes, 20);
619 ao_flight_ground_accel = int16(bytes, 6);
620 ao_config.accel_plus_g = int16(bytes, 16);
621 ao_config.accel_minus_g = int16(bytes, 18);
623 a = int16(bytes, 22);
624 b = int16(bytes, 24);
626 printf("unknown len %d\n", len);
630 if (type != 'F' && !ao_flight_started)
635 ao_flight_ground_accel = a;
636 if (ao_config.accel_plus_g == 0) {
637 ao_config.accel_plus_g = a;
638 ao_config.accel_minus_g = a + 530;
640 if (ao_config.main_deploy == 0)
641 ao_config.main_deploy = 250;
642 ao_flight_started = 1;
647 ao_data_static.tick = tick;
648 ao_data_static.adc.accel = a;
649 ao_data_static.adc.pres_real = b;
650 if (b < AO_MIN_BARO_VALUE)
651 b = AO_MIN_BARO_VALUE;
652 ao_data_static.adc.pres = b;
657 ao_data_static.tick = tick;
658 ao_data_static.adc.temp = a;
659 ao_data_static.adc.v_batt = b;
672 #define COUNTS_PER_G 264.8
679 static const struct option options[] = {
680 { .name = "summary", .has_arg = 0, .val = 's' },
681 { .name = "debug", .has_arg = 0, .val = 'd' },
682 { .name = "info", .has_arg = 1, .val = 'i' },
686 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
688 emulator_name = name;
690 emulator_info = info;
691 ao_summary = summary;
697 main (int argc, char **argv)
709 while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
724 run_flight_fixed("<stdin>", stdin, summary, info);
726 for (i = optind; i < argc; i++) {
727 FILE *f = fopen(argv[i], "r");
732 run_flight_fixed(argv[i], f, summary, info);