altos: Split up flight code into separate flight/sample/kalman bits
[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 #include <getopt.h>
25 #include <math.h>
26
27 #define AO_HERTZ        100
28
29 #define AO_ADC_RING     64
30 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
31 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
32
33 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
34 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
35 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
36
37 /*
38  * One set of samples read from the A/D converter
39  */
40 struct ao_adc {
41         uint16_t        tick;           /* tick when the sample was read */
42         int16_t         accel;          /* accelerometer */
43         int16_t         pres;           /* pressure sensor */
44         int16_t         temp;           /* temperature sensor */
45         int16_t         v_batt;         /* battery voltage */
46         int16_t         sense_d;        /* drogue continuity sense */
47         int16_t         sense_m;        /* main continuity sense */
48 };
49
50 #define __pdata
51 #define __data
52 #define __xdata
53 #define __code
54 #define __reentrant
55
56 #define to_fix16(x) ((int16_t) ((x) * 65536.0 + 0.5))
57 #define to_fix32(x) ((int32_t) ((x) * 65536.0 + 0.5))
58 #define from_fix(x)     ((x) >> 16)
59
60 /*
61  * Above this height, the baro sensor doesn't work
62  */
63 #define AO_MAX_BARO_HEIGHT      12000
64
65 /*
66  * Above this speed, baro measurements are unreliable
67  */
68 #define AO_MAX_BARO_SPEED       200
69
70 #define ACCEL_NOSE_UP   (ao_accel_2g >> 2)
71
72 enum ao_flight_state {
73         ao_flight_startup = 0,
74         ao_flight_idle = 1,
75         ao_flight_pad = 2,
76         ao_flight_boost = 3,
77         ao_flight_fast = 4,
78         ao_flight_coast = 5,
79         ao_flight_drogue = 6,
80         ao_flight_main = 7,
81         ao_flight_landed = 8,
82         ao_flight_invalid = 9
83 };
84
85 extern enum ao_flight_state ao_flight_state;
86
87 #define FALSE 0
88 #define TRUE 1
89
90 struct ao_adc ao_adc_ring[AO_ADC_RING];
91 uint8_t ao_adc_head;
92 int     ao_summary = 0;
93
94 #define ao_led_on(l)
95 #define ao_led_off(l)
96 #define ao_timer_set_adc_interval(i)
97 #define ao_wakeup(wchan) ao_dump_state()
98 #define ao_cmd_register(c)
99 #define ao_usb_disable()
100 #define ao_telemetry_set_interval(x)
101 #define ao_rdf_set(rdf)
102 #define ao_packet_slave_start()
103 #define ao_packet_slave_stop()
104
105 enum ao_igniter {
106         ao_igniter_drogue = 0,
107         ao_igniter_main = 1
108 };
109
110 struct ao_adc ao_adc_static;
111
112 int     drogue_height;
113 double  drogue_time;
114 int     main_height;
115 double  main_time;
116
117 int     tick_offset;
118
119 static int32_t  ao_k_height;
120
121 void
122 ao_ignite(enum ao_igniter igniter)
123 {
124         double time = (double) (ao_adc_static.tick + tick_offset) / 100;
125
126         if (igniter == ao_igniter_drogue) {
127                 drogue_time = time;
128                 drogue_height = ao_k_height >> 16;
129         } else {
130                 main_time = time;
131                 main_height = ao_k_height >> 16;
132         }
133 }
134
135 struct ao_task {
136         int dummy;
137 };
138
139 #define ao_add_task(t,f,n)
140
141 #define ao_log_start()
142 #define ao_log_stop()
143
144 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
145 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
146
147 #define AO_FLIGHT_TEST
148
149 int     ao_flight_debug;
150
151 FILE *emulator_in;
152 char *emulator_app;
153 char *emulator_name;
154 double emulator_error_max = 4;
155
156 void
157 ao_dump_state(void);
158
159 void
160 ao_sleep(void *wchan);
161
162 const char const * const ao_state_names[] = {
163         "startup", "idle", "pad", "boost", "fast",
164         "coast", "drogue", "main", "landed", "invalid"
165 };
166
167 struct ao_cmds {
168         void            (*func)(void);
169         const char      *help;
170 };
171
172 #include "ao_convert.c"
173
174 struct ao_config {
175         uint16_t        main_deploy;
176         int16_t         accel_plus_g;
177         int16_t         accel_minus_g;
178 };
179
180 #define ao_config_get()
181
182 struct ao_config ao_config;
183
184 #define DATA_TO_XDATA(x) (x)
185
186 #define HAS_FLIGHT 1
187 #define HAS_ADC 1
188 #define HAS_USB 1
189 #define HAS_GPS 1
190 #ifndef HAS_ACCEL
191 #define HAS_ACCEL 1
192 #define HAS_ACCEL_REF 0
193 #endif
194
195 #define GRAVITY 9.80665
196 extern int16_t ao_ground_accel, ao_flight_accel;
197 extern int16_t ao_accel_2g;
198
199 extern uint16_t ao_sample_tick;
200
201 extern int16_t  ao_sample_height;
202 extern int16_t  ao_sample_accel;
203 extern int32_t  ao_accel_scale;
204
205 int ao_sample_prev_tick;
206 uint16_t        prev_tick;
207
208 #include "ao_kalman.c"
209 #include "ao_sample.c"
210 #include "ao_flight.c"
211
212 #define to_double(f)    ((f) / 65536.0)
213
214 static int      ao_records_read = 0;
215 static int      ao_eof_read = 0;
216 static int      ao_flight_ground_accel;
217 static int      ao_flight_started = 0;
218 static int      ao_test_max_height;
219 static double   ao_test_max_height_time;
220 static int      ao_test_main_height;
221 static double   ao_test_main_height_time;
222
223 void
224 ao_test_exit(void)
225 {
226         double  drogue_error;
227         double  main_error;
228
229         if (!ao_test_main_height_time) {
230                 ao_test_main_height_time = ao_test_max_height_time;
231                 ao_test_main_height = ao_test_max_height;
232         }
233         drogue_error = fabs(ao_test_max_height_time - drogue_time);
234         main_error = fabs(ao_test_main_height_time - main_time);
235         if (drogue_error > emulator_error_max || main_error > emulator_error_max) {
236                 printf ("%s %s\n",
237                         emulator_app, emulator_name);
238                 printf ("\tApogee error %g\n", drogue_error);
239                 printf ("\tMain error %g\n", main_error);
240                 printf ("\tActual: apogee: %d at %7.2f main: %d at %7.2f\n",
241                         ao_test_max_height, ao_test_max_height_time,
242                         ao_test_main_height, ao_test_main_height_time);
243                 printf ("\tComputed: apogee: %d at %7.2f main: %d at %7.2f\n",
244                         drogue_height, drogue_time, main_height, main_time);
245                 exit (1);
246         }
247         exit(0);
248 }
249
250 void
251 ao_insert(void)
252 {
253         double  time;
254
255         ao_adc_ring[ao_adc_head] = ao_adc_static;
256         ao_adc_head = ao_adc_ring_next(ao_adc_head);
257         if (ao_flight_state != ao_flight_startup) {
258                 double  height = ao_pres_to_altitude(ao_sample_pres) - ao_ground_height;
259                 double  accel = ((ao_flight_ground_accel - ao_adc_static.accel) * GRAVITY * 2.0) /
260                         (ao_config.accel_minus_g - ao_config.accel_plus_g);
261
262                 if (!tick_offset)
263                         tick_offset = -ao_adc_static.tick;
264                 if ((prev_tick - ao_adc_static.tick) > 0x400)
265                         tick_offset += 65536;
266                 prev_tick = ao_adc_static.tick;
267                 time = (double) (ao_adc_static.tick + tick_offset) / 100;
268
269                 if (ao_test_max_height < height) {
270                         ao_test_max_height = height;
271                         ao_test_max_height_time = time;
272                 }
273                 if (height > ao_config.main_deploy) {
274                         ao_test_main_height_time = time;
275                         ao_test_main_height = height;
276                 }
277
278                 if (!ao_summary) {
279                         printf("%7.2f height %g accel %g state %s k_height %g k_speed %g k_accel %g drogue %d main %d error %d\n",
280                                time,
281                                height,
282                                accel,
283                                ao_state_names[ao_flight_state],
284                                ao_k_height / 65536.0,
285                                ao_k_speed / 65536.0 / 16.0,
286                                ao_k_accel / 65536.0 / 16.0,
287                                drogue_height,
288                                main_height,
289                                ao_error_h_sq_avg);
290                         if (ao_flight_state == ao_flight_landed)
291                                 ao_test_exit();
292                 }
293         }
294 }
295
296 void
297 ao_sleep(void *wchan)
298 {
299         if (wchan == &ao_adc_head) {
300                 char            type;
301                 uint16_t        tick;
302                 uint16_t        a, b;
303                 int             ret;
304                 char            line[1024];
305                 char            *saveptr;
306                 char            *l;
307                 char            *words[64];
308                 int             nword;
309
310                 for (;;) {
311                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
312                         {
313                                 ao_adc_static.accel = ao_flight_ground_accel;
314                                 ao_insert();
315                                 return;
316                         }
317
318                         if (!fgets(line, sizeof (line), emulator_in)) {
319                                 if (++ao_eof_read >= 1000) {
320                                         if (!ao_summary)
321                                                 printf ("no more data, exiting simulation\n");
322                                         ao_test_exit();
323                                 }
324                                 ao_adc_static.tick += 10;
325                                 ao_insert();
326                                 return;
327                         }
328                         l = line;
329                         for (nword = 0; nword < 64; nword++) {
330                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
331                                 l = NULL;
332                                 if (words[nword] == NULL)
333                                         break;
334                         }
335                         if (nword == 4) {
336                                 type = words[0][0];
337                                 tick = strtoul(words[1], NULL, 16);
338                                 a = strtoul(words[2], NULL, 16);
339                                 b = strtoul(words[3], NULL, 16);
340                         } else if (nword >= 6 && strcmp(words[0], "Accel") == 0) {
341                                 ao_config.accel_plus_g = atoi(words[3]);
342                                 ao_config.accel_minus_g = atoi(words[5]);
343                         } else if (nword >= 4 && strcmp(words[0], "Main") == 0) {
344                                 ao_config.main_deploy = atoi(words[2]);
345                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
346                                 tick = atoi(words[10]);
347                                 if (!ao_flight_started) {
348                                         type = 'F';
349                                         a = atoi(words[26]);
350                                         ao_flight_started = 1;
351                                 } else {
352                                         type = 'A';
353                                         a = atoi(words[12]);
354                                         b = atoi(words[14]);
355                                 }
356                         }
357                         if (type != 'F' && !ao_flight_started)
358                                 continue;
359
360                         switch (type) {
361                         case 'F':
362                                 ao_flight_ground_accel = a;
363                                 if (ao_config.accel_plus_g == 0) {
364                                         ao_config.accel_plus_g = a;
365                                         ao_config.accel_minus_g = a + 530;
366                                 }
367                                 if (ao_config.main_deploy == 0)
368                                         ao_config.main_deploy = 250;
369                                 ao_flight_started = 1;
370                                 break;
371                         case 'S':
372                                 break;
373                         case 'A':
374                                 ao_adc_static.tick = tick;
375                                 ao_adc_static.accel = a;
376                                 ao_adc_static.pres = b;
377                                 ao_records_read++;
378                                 ao_insert();
379                                 return;
380                         case 'T':
381                                 ao_adc_static.tick = tick;
382                                 ao_adc_static.temp = a;
383                                 ao_adc_static.v_batt = b;
384                                 break;
385                         case 'D':
386                         case 'G':
387                         case 'N':
388                         case 'W':
389                         case 'H':
390                                 break;
391                         }
392                 }
393
394         }
395 }
396 #define COUNTS_PER_G 264.8
397
398 void
399 ao_dump_state(void)
400 {
401 }
402
403 static const struct option options[] = {
404         { .name = "summary", .has_arg = 0, .val = 's' },
405         { .name = "debug", .has_arg = 0, .val = 'd' },
406         { 0, 0, 0, 0},
407 };
408
409 void run_flight_fixed(char *name, FILE *f, int summary)
410 {
411         emulator_name = name;
412         emulator_in = f;
413         ao_summary = summary;
414         ao_flight_init();
415         ao_flight();
416 }
417
418 int
419 main (int argc, char **argv)
420 {
421         int     summary = 0;
422         int     c;
423         int     i;
424
425 #if HAS_ACCEL
426         emulator_app="full";
427 #else
428         emulator_app="baro";
429 #endif
430         while ((c = getopt_long(argc, argv, "sd", options, NULL)) != -1) {
431                 switch (c) {
432                 case 's':
433                         summary = 1;
434                         break;
435                 case 'd':
436                         ao_flight_debug = 1;
437                         break;
438                 }
439         }
440
441         if (optind == argc)
442                 run_flight_fixed("<stdin>", stdin, summary);
443         else
444                 for (i = optind; i < argc; i++) {
445                         FILE    *f = fopen(argv[i], "r");
446                         if (!f) {
447                                 perror(argv[i]);
448                                 continue;
449                         }
450                         run_flight_fixed(argv[i], f, summary);
451                         fclose(f);
452                 }
453 }