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 AO_ADC_RING 64
29 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
30 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
32 #define AO_M_TO_HEIGHT(m) ((int16_t) (m))
33 #define AO_MS_TO_SPEED(ms) ((int16_t) ((ms) * 16))
34 #define AO_MSS_TO_ACCEL(mss) ((int16_t) ((mss) * 16))
37 * One set of samples read from the A/D converter
40 uint16_t tick; /* tick when the sample was read */
41 int16_t accel; /* accelerometer */
42 int16_t pres; /* pressure sensor */
43 int16_t temp; /* temperature sensor */
44 int16_t v_batt; /* battery voltage */
45 int16_t sense_d; /* drogue continuity sense */
46 int16_t sense_m; /* main continuity sense */
55 enum ao_flight_state {
56 ao_flight_startup = 0,
68 struct ao_adc ao_adc_ring[AO_ADC_RING];
74 #define ao_timer_set_adc_interval(i)
75 #define ao_wakeup(wchan) ao_dump_state()
76 #define ao_cmd_register(c)
77 #define ao_usb_disable()
78 #define ao_telemetry_set_interval(x)
79 #define ao_rdf_set(rdf)
80 #define ao_packet_slave_start()
81 #define ao_packet_slave_stop()
84 ao_igniter_drogue = 0,
88 struct ao_adc ao_adc_static;
91 ao_ignite(enum ao_igniter igniter)
93 printf ("ignite %s at %7.2f\n", igniter == ao_igniter_drogue ? "drogue" : "main",
94 (double) ao_adc_static.tick / 100.0);
101 #define ao_add_task(t,f,n)
103 #define ao_log_start()
104 #define ao_log_stop()
106 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
107 #define AO_SEC_TO_TICKS(s) ((s) * 100)
109 #define AO_FLIGHT_TEST
117 ao_sleep(void *wchan);
119 const char const * const ao_state_names[] = {
120 "startup", "idle", "pad", "boost", "fast",
121 "coast", "drogue", "main", "landed", "invalid"
129 #include "ao_convert.c"
132 uint16_t main_deploy;
133 int16_t accel_plus_g;
134 int16_t accel_minus_g;
137 #define ao_config_get()
139 struct ao_config ao_config;
141 #define DATA_TO_XDATA(x) (x)
149 #define HAS_ACCEL_REF 0
152 #include "ao_flight.c"
154 #define to_double(f) ((f) / 65536.0)
156 #define GRAVITY 9.80665
157 extern int16_t ao_ground_accel, ao_raw_accel;
158 extern int16_t ao_accel_2g;
160 int32_t drogue_height;
165 static int ao_records_read = 0;
166 static int ao_eof_read = 0;
167 static int ao_flight_ground_accel;
168 static int ao_flight_started = 0;
173 ao_adc_ring[ao_adc_head] = ao_adc_static;
174 ao_adc_head = ao_adc_ring_next(ao_adc_head);
177 if (ao_flight_state == ao_flight_startup)
180 double height = ao_pres_to_altitude(ao_raw_pres) - ao_ground_height;
181 double accel = ((ao_flight_ground_accel - ao_adc_static.accel) * GRAVITY * 2.0) /
182 (ao_config.accel_minus_g - ao_config.accel_plus_g);
185 tick_offset = ao_adc_static.tick;
186 if (!drogue_height && ao_flight_state >= ao_flight_drogue)
187 drogue_height = ao_k_height;
188 if (!main_height && ao_flight_state >= ao_flight_main)
189 main_height = ao_k_height;
190 if ((prev_tick - ao_adc_static.tick) > 0)
191 tick_offset += 65536;
192 prev_tick = ao_adc_static.tick;
193 printf("%7.2f height %g accel %g state %s k_height %g k_speed %g k_accel %g drogue %g main %g error %d\n",
194 (double) (ao_adc_static.tick + tick_offset) / 100,
197 ao_state_names[ao_flight_state],
198 ao_k_height / 65536.0,
199 ao_k_speed / 65536.0 / 16.0,
200 ao_k_accel / 65536.0 / 16.0,
201 drogue_height / 65536.0,
202 main_height / 65536.0,
208 ao_sleep(void *wchan)
210 if (wchan == &ao_adc_head) {
222 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
224 ao_adc_static.accel = ao_flight_ground_accel;
229 if (!fgets(line, sizeof (line), emulator_in)) {
230 if (++ao_eof_read >= 1000) {
231 printf ("no more data, exiting simulation\n");
234 ao_adc_static.tick += 10;
239 for (nword = 0; nword < 64; nword++) {
240 words[nword] = strtok_r(l, " \t\n", &saveptr);
242 if (words[nword] == NULL)
247 tick = strtoul(words[1], NULL, 16);
248 a = strtoul(words[2], NULL, 16);
249 b = strtoul(words[3], NULL, 16);
250 } else if (nword >= 6 && strcmp(words[0], "Accel")) {
251 ao_config.accel_plus_g = atoi(words[3]);
252 ao_config.accel_minus_g = atoi(words[5]);
253 } else if (nword >= 4 && strcmp(words[0], "Main")) {
254 ao_config.main_deploy = atoi(words[2]);
255 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
256 tick = atoi(words[10]);
257 if (!ao_flight_started) {
260 ao_flight_started = 1;
267 if (type != 'F' && !ao_flight_started)
272 ao_flight_ground_accel = a;
273 if (ao_config.accel_plus_g == 0) {
274 ao_config.accel_plus_g = a;
275 ao_config.accel_minus_g = a + 530;
277 if (ao_config.main_deploy == 0)
278 ao_config.main_deploy = 250;
279 ao_flight_started = 1;
284 ao_adc_static.tick = tick;
285 ao_adc_static.accel = a;
286 ao_adc_static.pres = b;
291 ao_adc_static.tick = tick;
292 ao_adc_static.temp = a;
293 ao_adc_static.v_batt = b;
306 #define COUNTS_PER_G 264.8
311 if (ao_flight_state == ao_flight_startup)
315 if (ao_flight_state == ao_flight_landed)
319 static const struct option options[] = {
320 { .name = "summary", .has_arg = 0, .val = 's' },
324 void run_flight_fixed(char *name, FILE *f, int summary)
327 ao_summary = summary;
333 main (int argc, char **argv)
339 while ((c = getopt_long(argc, argv, "s", options, NULL)) != -1) {
348 run_flight_fixed("<stdin>", stdin, summary);
350 for (i = optind; i < argc; i++) {
351 FILE *f = fopen(argv[i], "r");
356 run_flight_fixed(argv[i], f, summary);