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