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.
25 #define AO_ADC_RING 64
26 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
27 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
30 * One set of samples read from the A/D converter
33 uint16_t tick; /* tick when the sample was read */
34 int16_t accel; /* accelerometer */
35 int16_t pres; /* pressure sensor */
36 int16_t temp; /* temperature sensor */
37 int16_t v_batt; /* battery voltage */
38 int16_t sense_d; /* drogue continuity sense */
39 int16_t sense_m; /* main continuity sense */
48 enum ao_flight_state {
49 ao_flight_startup = 0,
61 struct ao_adc ao_adc_ring[AO_ADC_RING];
66 #define ao_timer_set_adc_interval(i)
67 #define ao_wakeup(wchan) ao_dump_state()
68 #define ao_cmd_register(c)
69 #define ao_usb_disable()
70 #define ao_telemetry_set_interval(x)
71 #define ao_rdf_set(rdf)
74 ao_igniter_drogue = 0,
79 ao_ignite(enum ao_igniter igniter)
81 printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
88 #define ao_add_task(t,f,n)
90 #define ao_log_start()
93 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
94 #define AO_SEC_TO_TICKS(s) ((s) * 100)
96 #define AO_FLIGHT_TEST
98 struct ao_adc ao_adc_static;
106 ao_sleep(void *wchan);
108 const char const * const ao_state_names[] = {
109 "startup", "idle", "pad", "boost", "fast",
110 "coast", "drogue", "main", "landed", "invalid"
120 static int16_t altitude_table[2048] = {
121 #include "altitude.h"
125 ao_pres_to_altitude(int16_t pres) __reentrant
128 if (pres < 0) pres = 0;
129 if (pres > 2047) pres = 2047;
130 return altitude_table[pres];
134 ao_altitude_to_pres(int16_t alt) __reentrant
138 for (pres = 0; pres < 2047; pres++)
139 if (altitude_table[pres] <= alt)
145 uint16_t main_deploy;
146 int16_t accel_zero_g;
149 #define ao_config_get()
151 struct ao_config ao_config = { 250, 16000 };
153 #include "ao_flight.c"
158 ao_adc_ring[ao_adc_head] = ao_adc_static;
159 ao_adc_head = ao_adc_ring_next(ao_adc_head);
160 if (ao_flight_state != ao_flight_startup) {
161 printf("time %g accel %d pres %d\n",
162 (double) ao_adc_static.tick / 100,
168 static int ao_records_read = 0;
169 static int ao_eof_read = 0;
170 static int ao_flight_ground_accel;
171 static int ao_flight_started = 0;
174 ao_sleep(void *wchan)
177 if (wchan == &ao_adc_ring) {
189 if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
191 ao_adc_static.accel = ao_flight_ground_accel;
196 if (!fgets(line, sizeof (line), emulator_in)) {
197 if (++ao_eof_read >= 1000) {
198 printf ("no more data, exiting simulation\n");
201 ao_adc_static.tick += 10;
206 for (nword = 0; nword < 64; nword++) {
207 words[nword] = strtok_r(l, " \t\n", &saveptr);
209 if (words[nword] == NULL)
214 tick = strtoul(words[1], NULL, 16);
215 a = strtoul(words[2], NULL, 16);
216 b = strtoul(words[2], NULL, 16);
217 } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
218 tick = atoi(words[10]);
219 if (!ao_flight_started) {
222 ao_flight_started = 1;
229 if (type != 'F' && !ao_flight_started)
234 ao_flight_ground_accel = a;
235 ao_flight_started = 1;
240 ao_adc_static.tick = tick;
241 ao_adc_static.accel = a;
242 ao_adc_static.pres = b;
247 ao_adc_static.tick = tick;
248 ao_adc_static.temp = a;
249 ao_adc_static.v_batt = b;
262 #define COUNTS_PER_G 264.8
267 if (ao_flight_state == ao_flight_startup)
269 printf ("\t\t\t\t\t%s accel %g vel %g alt %d main %d\n",
270 ao_state_names[ao_flight_state],
271 (ao_ground_accel - ao_flight_accel) / COUNTS_PER_G * GRAVITY,
272 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
273 ao_pres_to_altitude(ao_flight_pres) - ao_pres_to_altitude(ao_ground_pres),
274 ao_pres_to_altitude(ao_main_pres) - ao_pres_to_altitude(ao_ground_pres));
275 if (ao_flight_state == ao_flight_landed)
280 main (int argc, char **argv)
282 emulator_in = fopen (argv[1], "r");