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.
22 #define AO_ADC_RING 64
23 #define ao_adc_ring_next(n) (((n) + 1) & (AO_ADC_RING - 1))
24 #define ao_adc_ring_prev(n) (((n) - 1) & (AO_ADC_RING - 1))
27 * One set of samples read from the A/D converter
30 uint16_t tick; /* tick when the sample was read */
31 int16_t accel; /* accelerometer */
32 int16_t pres; /* pressure sensor */
33 int16_t temp; /* temperature sensor */
34 int16_t v_batt; /* battery voltage */
35 int16_t sense_d; /* drogue continuity sense */
36 int16_t sense_m; /* main continuity sense */
45 enum ao_flight_state {
46 ao_flight_startup = 0,
48 ao_flight_launchpad = 2,
58 struct ao_adc ao_adc_ring[AO_ADC_RING];
63 #define ao_timer_set_adc_interval(i)
64 #define ao_wakeup(wchan) ao_dump_state()
65 #define ao_cmd_register(c)
68 ao_igniter_drogue = 0,
73 ao_ignite(enum ao_igniter igniter)
75 printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
82 #define ao_add_task(t,f,n)
84 #define ao_log_start()
87 #define AO_MS_TO_TICKS(ms) ((ms) / 10)
88 #define AO_SEC_TO_TICKS(s) ((s) * 100)
90 #define AO_FLIGHT_TEST
92 struct ao_adc ao_adc_static;
100 ao_sleep(void *wchan);
102 const char const * const ao_state_names[] = {
103 "startup", "idle", "pad", "boost", "coast",
104 "apogee", "drogue", "main", "landed", "invalid"
114 static int16_t altitude_table[2048] = {
115 #include "altitude.h"
119 ao_pres_to_altitude(int16_t pres) __reentrant
122 if (pres < 0) pres = 0;
123 if (pres > 2047) pres = 2047;
124 return altitude_table[pres];
127 #include "ao_flight.c"
132 ao_adc_ring[ao_adc_head] = ao_adc_static;
133 ao_adc_head = ao_adc_ring_next(ao_adc_head);
134 if (ao_flight_state != ao_flight_startup) {
135 printf("time %g accel %d pres %d\n",
136 (double) ao_adc_static.tick / 100,
142 static int ao_records_read = 0;
145 ao_sleep(void *wchan)
148 if (wchan == &ao_adc_ring) {
155 if (ao_records_read > 20 && ao_flight_state == ao_flight_startup)
161 ret = fscanf(emulator_in, "%c %hx %hx %hx\n", &type, &tick, &a, &b);
163 printf ("no more data, exiting simulation\n");
175 ao_adc_static.tick = tick;
176 ao_adc_static.accel = a;
177 ao_adc_static.pres = b;
182 ao_adc_static.tick = tick;
183 ao_adc_static.temp = a;
184 ao_adc_static.v_batt = b;
197 #define COUNTS_PER_G 264.8
202 if (ao_flight_state == ao_flight_startup)
204 printf ("\t%s accel %g vel %g alt %d\n",
205 ao_state_names[ao_flight_state],
206 (ao_flight_accel - ao_ground_accel) / COUNTS_PER_G * GRAVITY,
207 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
208 altitude_table[ao_flight_pres >> 4]);
209 if (ao_flight_state == ao_flight_landed)
214 main (int argc, char **argv)
216 emulator_in = fopen (argv[1], "r");