USB spec limits bulk endpoints to 64 byte payload max.
[fw/altos] / ao_flight_test.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
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))
25
26 /*
27  * One set of samples read from the A/D converter
28  */
29 struct ao_adc {
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 */
37 };
38
39 #define __pdata
40 #define __data
41 #define __xdata
42 #define __code
43 #define __reentrant
44
45 enum ao_flight_state {
46         ao_flight_startup = 0,
47         ao_flight_idle = 1,
48         ao_flight_launchpad = 2,
49         ao_flight_boost = 3,
50         ao_flight_coast = 4,
51         ao_flight_apogee = 5,
52         ao_flight_drogue = 6,
53         ao_flight_main = 7,
54         ao_flight_landed = 8,
55         ao_flight_invalid = 9
56 };
57
58 struct ao_adc ao_adc_ring[AO_ADC_RING];
59 uint8_t ao_adc_head;
60
61 #define ao_led_on(l)
62 #define ao_led_off(l)
63 #define ao_timer_set_adc_interval(i)
64 #define ao_wakeup(wchan) ao_dump_state()
65 #define ao_cmd_register(c)
66 #define ao_usb_disable()
67 #define ao_telemetry_set_interval(x)
68 #define ao_rdf_set(rdf)
69
70 enum ao_igniter {
71         ao_igniter_drogue = 0,
72         ao_igniter_main = 1
73 };
74
75 void
76 ao_ignite(enum ao_igniter igniter)
77 {
78         printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
79 }
80
81 struct ao_task {
82         int dummy;
83 };
84
85 #define ao_add_task(t,f,n)
86
87 #define ao_log_start()
88 #define ao_log_stop()
89
90 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
91 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
92
93 #define AO_FLIGHT_TEST
94
95 struct ao_adc ao_adc_static;
96
97 FILE *emulator_in;
98
99 void
100 ao_dump_state(void);
101
102 void
103 ao_sleep(void *wchan);
104
105 const char const * const ao_state_names[] = {
106         "startup", "idle", "pad", "boost", "coast",
107         "apogee", "drogue", "main", "landed", "invalid"
108 };
109
110 struct ao_cmds {
111         char            cmd;
112         void            (*func)(void);
113         const char      *help;
114 };
115
116
117 static int16_t altitude_table[2048] = {
118 #include "altitude.h"
119 };
120
121 int16_t
122 ao_pres_to_altitude(int16_t pres) __reentrant
123 {
124         pres = pres >> 4;
125         if (pres < 0) pres = 0;
126         if (pres > 2047) pres = 2047;
127         return altitude_table[pres];
128 }
129
130 int16_t
131 ao_altitude_to_pres(int16_t alt) __reentrant
132 {
133         int16_t pres;
134
135         for (pres = 0; pres < 2047; pres++)
136                 if (altitude_table[pres] <= alt)
137                         break;
138         return pres << 4;
139 }
140
141 struct ao_config {
142         uint16_t        main_deploy;
143         int16_t         accel_zero_g;
144 };
145
146 #define ao_config_get()
147
148 struct ao_config ao_config = { 250, 16000 };
149
150 #include "ao_flight.c"
151
152 void
153 ao_insert(void)
154 {
155         ao_adc_ring[ao_adc_head] = ao_adc_static;
156         ao_adc_head = ao_adc_ring_next(ao_adc_head);
157         if (ao_flight_state != ao_flight_startup) {
158                 printf("time %g accel %d pres %d\n",
159                        (double) ao_adc_static.tick / 100,
160                        ao_adc_static.accel,
161                        ao_adc_static.pres);
162         }
163 }
164
165 static int      ao_records_read = 0;
166
167 void
168 ao_sleep(void *wchan)
169 {
170         ao_dump_state();
171         if (wchan == &ao_adc_ring) {
172                 char            type;
173                 uint16_t        tick;
174                 uint16_t        a, b;
175                 int             ret;
176
177                 for (;;) {
178                         if (ao_records_read > 20 && ao_flight_state == ao_flight_startup)
179                         {
180                                 ao_insert();
181                                 return;
182                         }
183
184                         ret = fscanf(emulator_in, "%c %hx %hx %hx\n", &type, &tick, &a, &b);
185                         if (ret == EOF) {
186                                 printf ("no more data, exiting simulation\n");
187                                 exit(0);
188                                 return;
189                         }
190                         if (ret != 4)
191                                 continue;
192                         switch (type) {
193                         case 'F':
194                                 break;
195                         case 'S':
196                                 break;
197                         case 'A':
198                                 ao_adc_static.tick = tick;
199                                 ao_adc_static.accel = a;
200                                 ao_adc_static.pres = b;
201                                 ao_records_read++;
202                                 ao_insert();
203                                 return;
204                         case 'T':
205                                 ao_adc_static.tick = tick;
206                                 ao_adc_static.temp = a;
207                                 ao_adc_static.v_batt = b;
208                                 break;
209                         case 'D':
210                         case 'G':
211                         case 'N':
212                         case 'W':
213                         case 'H':
214                                 break;
215                         }
216                 }
217
218         }
219 }
220 #define COUNTS_PER_G 264.8
221
222 void
223 ao_dump_state(void)
224 {
225         if (ao_flight_state == ao_flight_startup)
226                 return;
227         printf ("\t\t\t\t\t%s accel %g vel %g alt %d main %d\n",
228                 ao_state_names[ao_flight_state],
229                 (ao_flight_accel - ao_ground_accel) / COUNTS_PER_G * GRAVITY,
230                 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
231                 ao_pres_to_altitude(ao_flight_pres),
232                 ao_pres_to_altitude(ao_main_pres));
233         if (ao_flight_state == ao_flight_landed)
234                 exit(0);
235 }
236
237 int
238 main (int argc, char **argv)
239 {
240         emulator_in = fopen (argv[1], "r");
241         if (!emulator_in) {
242                 perror(argv[1]);
243                 exit(1);
244         }
245         ao_flight_init();
246         ao_flight();
247 }