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.
29 #define AO_ADC_RING 64
30 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
31 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
33 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
34 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
35 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
38 * One set of samples read from the A/D converter
41 uint16_t tick; /* tick when the sample was read */
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 */
57 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
58 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
59 #define from_fix(x) ((x) >> 16)
62 * Above this height, the baro sensor doesn't work
64 #define AO_MAX_BARO_HEIGHT 12000
65 #define AO_BARO_SATURATE 13000
66 #define AO_MIN_BARO_VALUE ao_altitude_to_pres(AO_BARO_SATURATE)
69 * Above this speed, baro measurements are unreliable
71 #define AO_MAX_BARO_SPEED 200
73 #define ACCEL_NOSE_UP (ao_accel_2g >> 2)
75 enum ao_flight_state {
76 ao_flight_startup = 0,
88 extern enum ao_flight_state ao_flight_state;
93 struct ao_adc ao_adc_ring[AO_ADC_RING];
99 #define ao_timer_set_adc_interval(i)
100 #define ao_wakeup(wchan) ao_dump_state()
101 #define ao_cmd_register(c)
102 #define ao_usb_disable()
103 #define ao_telemetry_set_interval(x)
104 #define ao_rdf_set(rdf)
105 #define ao_packet_slave_start()
106 #define ao_packet_slave_stop()
109 ao_igniter_drogue = 0,
113 struct ao_adc ao_adc_static;
122 static int32_t ao_k_height;
125 ao_ignite(enum ao_igniter igniter)
127 double time = (double) (ao_adc_static.tick + tick_offset) / 100;
129 if (igniter == ao_igniter_drogue) {
131 drogue_height = ao_k_height >> 16;
134 main_height = ao_k_height >> 16;
142 #define ao_add_task(t,f,n)
144 #define ao_log_start()
145 #define ao_log_stop()
147 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
148 #define AO_SEC_TO_TICKS(s) ((s) * 100)
150 #define AO_FLIGHT_TEST
157 double emulator_error_max = 4;
163 ao_sleep(void *wchan);
165 const char const * const ao_state_names[] = {
166 "startup", "idle", "pad", "boost", "fast",
167 "coast", "drogue", "main", "landed", "invalid"
175 #include "ao_convert.c"
178 uint16_t main_deploy;
179 int16_t accel_plus_g;
180 int16_t accel_minus_g;
183 #define ao_config_get()
185 struct ao_config ao_config;
187 #define DATA_TO_XDATA(x) (x)
195 #define HAS_ACCEL_REF 0
198 #define GRAVITY 9.80665
199 extern int16_t ao_ground_accel, ao_flight_accel;
200 extern int16_t ao_accel_2g;
202 extern uint16_t ao_sample_tick;
204 extern int16_t ao_sample_height;
205 extern int16_t ao_sample_accel;
206 extern int32_t ao_accel_scale;
207 extern int16_t ao_ground_height;
208 extern int16_t ao_sample_alt;
210 int ao_sample_prev_tick;
213 #include "ao_kalman.c"
214 #include "ao_sample.c"
215 #include "ao_flight.c"
217 #define to_double(f) ((f) / 65536.0)
219 static int ao_records_read = 0;
220 static int ao_eof_read = 0;
221 static int ao_flight_ground_accel;
222 static int ao_flight_started = 0;
223 static int ao_test_max_height;
224 static double ao_test_max_height_time;
225 static int ao_test_main_height;
226 static double ao_test_main_height_time;
234 if (!ao_test_main_height_time) {
235 ao_test_main_height_time = ao_test_max_height_time;
236 ao_test_main_height = ao_test_max_height;
238 drogue_error = fabs(ao_test_max_height_time - drogue_time);
239 main_error = fabs(ao_test_main_height_time - main_time);
240 if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
242 emulator_app, emulator_name);
243 printf ("\tApogee error %g\n", drogue_error);
244 printf ("\tMain error %g\n", main_error);
245 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f\n",
246 ao_test_max_height, ao_test_max_height_time,
247 ao_test_main_height, ao_test_main_height_time);
248 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f\n",
249 drogue_height, drogue_time, main_height, main_time);
260 ao_adc_ring[ao_adc_head] = ao_adc_static;
261 ao_adc_head = ao_adc_ring_next(ao_adc_head);
262 if (ao_flight_state != ao_flight_startup) {
263 double height = ao_pres_to_altitude(ao_adc_static.pres_real) - ao_ground_height;
264 double accel = ((ao_flight_ground_accel - ao_adc_static.accel) * GRAVITY * 2.0) /
265 (ao_config.accel_minus_g - ao_config.accel_plus_g);
268 tick_offset = -ao_adc_static.tick;
269 if ((prev_tick - ao_adc_static.tick) > 0x400)
270 tick_offset += 65536;
271 prev_tick = ao_adc_static.tick;
272 time = (double) (ao_adc_static.tick + tick_offset) / 100;
274 if (ao_test_max_height < height) {
275 ao_test_max_height = height;
276 ao_test_max_height_time = time;
278 if (height > ao_config.main_deploy) {
279 ao_test_main_height_time = time;
280 ao_test_main_height = height;
284 printf("%7.2f height %g accel %g state %s k_height %g k_speed %g k_accel %g drogue %d main %d error %d\n",
288 ao_state_names[ao_flight_state],
289 ao_k_height / 65536.0,
290 ao_k_speed / 65536.0 / 16.0,
291 ao_k_accel / 65536.0 / 16.0,
295 if (ao_flight_state == ao_flight_landed)
302 ao_sleep(void *wchan)
304 if (wchan == &ao_adc_head) {
316 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
318 ao_adc_static.accel = ao_flight_ground_accel;
323 if (!fgets(line, sizeof (line), emulator_in)) {
324 if (++ao_eof_read >= 1000) {
326 printf ("no more data, exiting simulation\n");
329 ao_adc_static.tick += 10;
334 for (nword = 0; nword < 64; nword++) {
335 words[nword] = strtok_r(l, " \t\n", &saveptr);
337 if (words[nword] == NULL)
342 tick = strtoul(words[1], NULL, 16);
343 a = strtoul(words[2], NULL, 16);
344 b = strtoul(words[3], NULL, 16);
345 } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
346 ao_config.accel_plus_g = atoi(words[3]);
347 ao_config.accel_minus_g = atoi(words[5]);
348 } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
349 ao_config.main_deploy = atoi(words[2]);
350 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
351 tick = atoi(words[10]);
352 if (!ao_flight_started) {
355 ao_flight_started = 1;
362 if (type != 'F' && !ao_flight_started)
367 ao_flight_ground_accel = a;
368 if (ao_config.accel_plus_g == 0) {
369 ao_config.accel_plus_g = a;
370 ao_config.accel_minus_g = a + 530;
372 if (ao_config.main_deploy == 0)
373 ao_config.main_deploy = 250;
374 ao_flight_started = 1;
379 ao_adc_static.tick = tick;
380 ao_adc_static.accel = a;
381 ao_adc_static.pres_real = b;
382 if (b < AO_MIN_BARO_VALUE)
383 b = AO_MIN_BARO_VALUE;
384 ao_adc_static.pres = b;
389 ao_adc_static.tick = tick;
390 ao_adc_static.temp = a;
391 ao_adc_static.v_batt = b;
404 #define COUNTS_PER_G 264.8
411 static const struct option options[] = {
412 { .name = "summary", .has_arg = 0, .val = 's' },
413 { .name = "debug", .has_arg = 0, .val = 'd' },
417 void run_flight_fixed(char *name, FILE *f, int summary)
419 emulator_name = name;
421 ao_summary = summary;
427 main (int argc, char **argv)
438 while ((c = getopt_long(argc, argv, "sd", options, NULL)) != -1) {
450 run_flight_fixed("<stdin>", stdin, summary);
452 for (i = optind; i < argc; i++) {
453 FILE *f = fopen(argv[i], "r");
458 run_flight_fixed(argv[i], f, summary);