a635803f41b226b2bc988a65b229aaab60520883
[fw/altos] / src / 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 #define _GNU_SOURCE
19
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #define AO_HERTZ        100
26
27 #define AO_ADC_RING     64
28 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
29 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
30
31 /*
32  * One set of samples read from the A/D converter
33  */
34 struct ao_adc {
35         uint16_t        tick;           /* tick when the sample was read */
36         int16_t         accel;          /* accelerometer */
37         int16_t         pres;           /* pressure sensor */
38         int16_t         temp;           /* temperature sensor */
39         int16_t         v_batt;         /* battery voltage */
40         int16_t         sense_d;        /* drogue continuity sense */
41         int16_t         sense_m;        /* main continuity sense */
42 };
43
44 #define __pdata
45 #define __data
46 #define __xdata
47 #define __code
48 #define __reentrant
49
50 enum ao_flight_state {
51         ao_flight_startup = 0,
52         ao_flight_idle = 1,
53         ao_flight_pad = 2,
54         ao_flight_boost = 3,
55         ao_flight_fast = 4,
56         ao_flight_coast = 5,
57         ao_flight_drogue = 6,
58         ao_flight_main = 7,
59         ao_flight_landed = 8,
60         ao_flight_invalid = 9
61 };
62
63 struct ao_adc ao_adc_ring[AO_ADC_RING];
64 uint8_t ao_adc_head;
65
66 #define ao_led_on(l)
67 #define ao_led_off(l)
68 #define ao_timer_set_adc_interval(i)
69 #define ao_wakeup(wchan) ao_dump_state()
70 #define ao_cmd_register(c)
71 #define ao_usb_disable()
72 #define ao_telemetry_set_interval(x)
73 #define ao_rdf_set(rdf)
74 #define ao_packet_slave_start()
75 #define ao_packet_slave_stop()
76
77 enum ao_igniter {
78         ao_igniter_drogue = 0,
79         ao_igniter_main = 1
80 };
81
82 void
83 ao_ignite(enum ao_igniter igniter)
84 {
85         printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
86 }
87
88 struct ao_task {
89         int dummy;
90 };
91
92 #define ao_add_task(t,f,n)
93
94 #define ao_log_start()
95 #define ao_log_stop()
96
97 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
98 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
99
100 #define AO_FLIGHT_TEST
101
102 struct ao_adc ao_adc_static;
103
104 FILE *emulator_in;
105
106 void
107 ao_dump_state(void);
108
109 void
110 ao_sleep(void *wchan);
111
112 const char const * const ao_state_names[] = {
113         "startup", "idle", "pad", "boost", "fast",
114         "coast", "drogue", "main", "landed", "invalid"
115 };
116
117 struct ao_cmds {
118         void            (*func)(void);
119         const char      *help;
120 };
121
122 #include "ao_convert.c"
123
124 struct ao_config {
125         uint16_t        main_deploy;
126         int16_t         accel_plus_g;
127         int16_t         accel_minus_g;
128 };
129
130 #define ao_config_get()
131
132 struct ao_config ao_config;
133
134 #define DATA_TO_XDATA(x) (x)
135
136 #define HAS_FLIGHT 1
137 #define HAS_ADC 1
138 #define HAS_USB 1
139 #define HAS_GPS 1
140 #ifndef HAS_ACCEL
141 #define HAS_ACCEL 1
142 #define HAS_ACCEL_REF 0
143 #endif
144
145 #include "ao_flight.c"
146
147 void
148 ao_insert(void)
149 {
150         ao_adc_ring[ao_adc_head] = ao_adc_static;
151         ao_adc_head = ao_adc_ring_next(ao_adc_head);
152         if (ao_flight_state != ao_flight_startup) {
153                 printf("time %g accel %d pres %d\n",
154                        (double) ao_adc_static.tick / 100,
155                        ao_adc_static.accel,
156                        ao_adc_static.pres);
157         }
158 }
159
160 static int      ao_records_read = 0;
161 static int      ao_eof_read = 0;
162 static int      ao_flight_ground_accel;
163 static int      ao_flight_started = 0;
164
165 void
166 ao_sleep(void *wchan)
167 {
168         ao_dump_state();
169         if (wchan == &ao_adc_head) {
170                 char            type;
171                 uint16_t        tick;
172                 uint16_t        a, b;
173                 int             ret;
174                 char            line[1024];
175                 char            *saveptr;
176                 char            *l;
177                 char            *words[64];
178                 int             nword;
179
180                 for (;;) {
181                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
182                         {
183                                 ao_adc_static.accel = ao_flight_ground_accel;
184                                 ao_insert();
185                                 return;
186                         }
187
188                         if (!fgets(line, sizeof (line), emulator_in)) {
189                                 if (++ao_eof_read >= 1000) {
190                                         printf ("no more data, exiting simulation\n");
191                                         exit(0);
192                                 }
193                                 ao_adc_static.tick += 10;
194                                 ao_insert();
195                                 return;
196                         }
197                         l = line;
198                         for (nword = 0; nword < 64; nword++) {
199                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
200                                 l = NULL;
201                                 if (words[nword] == NULL)
202                                         break;
203                         }
204                         if (nword == 4) {
205                                 type = words[0][0];
206                                 tick = strtoul(words[1], NULL, 16);
207                                 a = strtoul(words[2], NULL, 16);
208                                 b = strtoul(words[3], NULL, 16);
209                         } else if (nword >= 6 && strcmp(words[0], "Accel")) {
210                                 ao_config.accel_plus_g = atoi(words[3]);
211                                 ao_config.accel_minus_g = atoi(words[5]);
212                         } else if (nword >= 4 && strcmp(words[0], "Main")) {
213                                 ao_config.main_deploy = atoi(words[2]);
214                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
215                                 tick = atoi(words[10]);
216                                 if (!ao_flight_started) {
217                                         type = 'F';
218                                         a = atoi(words[26]);
219                                         ao_flight_started = 1;
220                                 } else {
221                                         type = 'A';
222                                         a = atoi(words[12]);
223                                         b = atoi(words[14]);
224                                 }
225                         }
226                         if (type != 'F' && !ao_flight_started)
227                                 continue;
228
229                         switch (type) {
230                         case 'F':
231                                 ao_flight_ground_accel = a;
232                                 if (ao_config.accel_plus_g == 0) {
233                                         ao_config.accel_plus_g = a;
234                                         ao_config.accel_minus_g = a + 530;
235                                 }
236                                 if (ao_config.main_deploy == 0)
237                                         ao_config.main_deploy = 250;
238                                 ao_flight_started = 1;
239                                 break;
240                         case 'S':
241                                 break;
242                         case 'A':
243                                 ao_adc_static.tick = tick;
244                                 ao_adc_static.accel = a;
245                                 ao_adc_static.pres = b;
246                                 ao_records_read++;
247                                 ao_insert();
248                                 return;
249                         case 'T':
250                                 ao_adc_static.tick = tick;
251                                 ao_adc_static.temp = a;
252                                 ao_adc_static.v_batt = b;
253                                 break;
254                         case 'D':
255                         case 'G':
256                         case 'N':
257                         case 'W':
258                         case 'H':
259                                 break;
260                         }
261                 }
262
263         }
264 }
265 #define COUNTS_PER_G 264.8
266
267 void
268 ao_dump_state(void)
269 {
270         if (ao_flight_state == ao_flight_startup)
271                 return;
272 #if HAS_ACCEL
273         printf ("\t\t\t\t\t%s accel %g vel %g alt %d main %d\n",
274                 ao_state_names[ao_flight_state],
275                 (ao_ground_accel - ao_flight_accel) / COUNTS_PER_G * GRAVITY,
276                 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
277                 ao_pres_to_altitude(ao_flight_pres) - ao_pres_to_altitude(ao_ground_pres),
278                 ao_pres_to_altitude(ao_main_pres) - ao_pres_to_altitude(ao_ground_pres));
279 #else
280         printf ("\t\t\t\t\t%s alt %d main %d\n",
281                 ao_state_names[ao_flight_state],
282                 ao_pres_to_altitude(ao_flight_pres) - ao_pres_to_altitude(ao_ground_pres),
283                 ao_pres_to_altitude(ao_main_pres) - ao_pres_to_altitude(ao_ground_pres));
284 #endif
285         if (ao_flight_state == ao_flight_landed)
286                 exit(0);
287 }
288
289 int
290 main (int argc, char **argv)
291 {
292         emulator_in = fopen (argv[1], "r");
293         if (!emulator_in) {
294                 perror(argv[1]);
295                 exit(1);
296         }
297         ao_flight_init();
298         ao_flight();
299 }