altos: Switch telemetrum over to kalman filter
[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
26 #define AO_HERTZ        100
27
28 #define AO_ADC_RING     64
29 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
30 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
31
32 #define AO_M_TO_HEIGHT(m)       ((int16_t) (m))
33 #define AO_MS_TO_SPEED(ms)      ((int16_t) ((ms) * 16))
34 #define AO_MSS_TO_ACCEL(mss)    ((int16_t) ((mss) * 16))
35
36 /*
37  * One set of samples read from the A/D converter
38  */
39 struct ao_adc {
40         uint16_t        tick;           /* tick when the sample was read */
41         int16_t         accel;          /* accelerometer */
42         int16_t         pres;           /* pressure sensor */
43         int16_t         temp;           /* temperature sensor */
44         int16_t         v_batt;         /* battery voltage */
45         int16_t         sense_d;        /* drogue continuity sense */
46         int16_t         sense_m;        /* main continuity sense */
47 };
48
49 #define __pdata
50 #define __data
51 #define __xdata
52 #define __code
53 #define __reentrant
54
55 enum ao_flight_state {
56         ao_flight_startup = 0,
57         ao_flight_idle = 1,
58         ao_flight_pad = 2,
59         ao_flight_boost = 3,
60         ao_flight_fast = 4,
61         ao_flight_coast = 5,
62         ao_flight_drogue = 6,
63         ao_flight_main = 7,
64         ao_flight_landed = 8,
65         ao_flight_invalid = 9
66 };
67
68 struct ao_adc ao_adc_ring[AO_ADC_RING];
69 uint8_t ao_adc_head;
70 int     ao_summary = 0;
71
72 #define ao_led_on(l)
73 #define ao_led_off(l)
74 #define ao_timer_set_adc_interval(i)
75 #define ao_wakeup(wchan) ao_dump_state()
76 #define ao_cmd_register(c)
77 #define ao_usb_disable()
78 #define ao_telemetry_set_interval(x)
79 #define ao_rdf_set(rdf)
80 #define ao_packet_slave_start()
81 #define ao_packet_slave_stop()
82
83 enum ao_igniter {
84         ao_igniter_drogue = 0,
85         ao_igniter_main = 1
86 };
87
88 struct ao_adc ao_adc_static;
89
90 void
91 ao_ignite(enum ao_igniter igniter)
92 {
93         printf ("ignite %s at %7.2f\n", igniter == ao_igniter_drogue ? "drogue" : "main",
94                 (double) ao_adc_static.tick / 100.0);
95 }
96
97 struct ao_task {
98         int dummy;
99 };
100
101 #define ao_add_task(t,f,n)
102
103 #define ao_log_start()
104 #define ao_log_stop()
105
106 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
107 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
108
109 #define AO_FLIGHT_TEST
110
111 FILE *emulator_in;
112
113 void
114 ao_dump_state(void);
115
116 void
117 ao_sleep(void *wchan);
118
119 const char const * const ao_state_names[] = {
120         "startup", "idle", "pad", "boost", "fast",
121         "coast", "drogue", "main", "landed", "invalid"
122 };
123
124 struct ao_cmds {
125         void            (*func)(void);
126         const char      *help;
127 };
128
129 #include "ao_convert.c"
130
131 struct ao_config {
132         uint16_t        main_deploy;
133         int16_t         accel_plus_g;
134         int16_t         accel_minus_g;
135 };
136
137 #define ao_config_get()
138
139 struct ao_config ao_config;
140
141 #define DATA_TO_XDATA(x) (x)
142
143 #define HAS_FLIGHT 1
144 #define HAS_ADC 1
145 #define HAS_USB 1
146 #define HAS_GPS 1
147 #ifndef HAS_ACCEL
148 #define HAS_ACCEL 1
149 #define HAS_ACCEL_REF 0
150 #endif
151
152 #include "ao_flight.c"
153
154 #define to_double(f)    ((f) / 65536.0)
155
156 #define GRAVITY 9.80665
157 extern int16_t ao_ground_accel, ao_raw_accel;
158 extern int16_t ao_accel_2g;
159
160 int32_t drogue_height;
161 int32_t main_height;
162
163 int             tick_offset;
164 uint16_t        prev_tick;
165 static int      ao_records_read = 0;
166 static int      ao_eof_read = 0;
167 static int      ao_flight_ground_accel;
168 static int      ao_flight_started = 0;
169
170 void
171 ao_insert(void)
172 {
173         ao_adc_ring[ao_adc_head] = ao_adc_static;
174         ao_adc_head = ao_adc_ring_next(ao_adc_head);
175         if (ao_summary)
176                 return;
177         if (ao_flight_state == ao_flight_startup)
178                 return;
179         {
180                 double  height = ao_pres_to_altitude(ao_raw_pres) - ao_ground_height;
181                 double  accel = ((ao_flight_ground_accel - ao_adc_static.accel) * GRAVITY * 2.0) /
182                         (ao_config.accel_minus_g - ao_config.accel_plus_g);
183
184                 if (!tick_offset)
185                         tick_offset = ao_adc_static.tick;
186                 if (!drogue_height && ao_flight_state >= ao_flight_drogue)
187                         drogue_height = ao_k_height;
188                 if (!main_height && ao_flight_state >= ao_flight_main)
189                         main_height = ao_k_height;
190                 if ((prev_tick - ao_adc_static.tick) > 0)
191                         tick_offset += 65536;
192                 prev_tick = ao_adc_static.tick;
193                 printf("%7.2f height %g accel %g state %s k_height %g k_speed %g k_accel %g drogue %g main %g error %d\n",
194                        (double) (ao_adc_static.tick + tick_offset) / 100,
195                        height,
196                        accel,
197                        ao_state_names[ao_flight_state],
198                        ao_k_height / 65536.0,
199                        ao_k_speed / 65536.0 / 16.0,
200                        ao_k_accel / 65536.0 / 16.0,
201                        drogue_height / 65536.0,
202                        main_height / 65536.0,
203                        ao_error_h_sq_avg);
204         }
205 }
206
207 void
208 ao_sleep(void *wchan)
209 {
210         if (wchan == &ao_adc_head) {
211                 char            type;
212                 uint16_t        tick;
213                 uint16_t        a, b;
214                 int             ret;
215                 char            line[1024];
216                 char            *saveptr;
217                 char            *l;
218                 char            *words[64];
219                 int             nword;
220
221                 for (;;) {
222                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
223                         {
224                                 ao_adc_static.accel = ao_flight_ground_accel;
225                                 ao_insert();
226                                 return;
227                         }
228
229                         if (!fgets(line, sizeof (line), emulator_in)) {
230                                 if (++ao_eof_read >= 1000) {
231                                         printf ("no more data, exiting simulation\n");
232                                         exit(0);
233                                 }
234                                 ao_adc_static.tick += 10;
235                                 ao_insert();
236                                 return;
237                         }
238                         l = line;
239                         for (nword = 0; nword < 64; nword++) {
240                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
241                                 l = NULL;
242                                 if (words[nword] == NULL)
243                                         break;
244                         }
245                         if (nword == 4) {
246                                 type = words[0][0];
247                                 tick = strtoul(words[1], NULL, 16);
248                                 a = strtoul(words[2], NULL, 16);
249                                 b = strtoul(words[3], NULL, 16);
250                         } else if (nword >= 6 && strcmp(words[0], "Accel")) {
251                                 ao_config.accel_plus_g = atoi(words[3]);
252                                 ao_config.accel_minus_g = atoi(words[5]);
253                         } else if (nword >= 4 && strcmp(words[0], "Main")) {
254                                 ao_config.main_deploy = atoi(words[2]);
255                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
256                                 tick = atoi(words[10]);
257                                 if (!ao_flight_started) {
258                                         type = 'F';
259                                         a = atoi(words[26]);
260                                         ao_flight_started = 1;
261                                 } else {
262                                         type = 'A';
263                                         a = atoi(words[12]);
264                                         b = atoi(words[14]);
265                                 }
266                         }
267                         if (type != 'F' && !ao_flight_started)
268                                 continue;
269
270                         switch (type) {
271                         case 'F':
272                                 ao_flight_ground_accel = a;
273                                 if (ao_config.accel_plus_g == 0) {
274                                         ao_config.accel_plus_g = a;
275                                         ao_config.accel_minus_g = a + 530;
276                                 }
277                                 if (ao_config.main_deploy == 0)
278                                         ao_config.main_deploy = 250;
279                                 ao_flight_started = 1;
280                                 break;
281                         case 'S':
282                                 break;
283                         case 'A':
284                                 ao_adc_static.tick = tick;
285                                 ao_adc_static.accel = a;
286                                 ao_adc_static.pres = b;
287                                 ao_records_read++;
288                                 ao_insert();
289                                 return;
290                         case 'T':
291                                 ao_adc_static.tick = tick;
292                                 ao_adc_static.temp = a;
293                                 ao_adc_static.v_batt = b;
294                                 break;
295                         case 'D':
296                         case 'G':
297                         case 'N':
298                         case 'W':
299                         case 'H':
300                                 break;
301                         }
302                 }
303
304         }
305 }
306 #define COUNTS_PER_G 264.8
307
308 void
309 ao_dump_state(void)
310 {
311         if (ao_flight_state == ao_flight_startup)
312                 return;
313         if (ao_summary)
314                 return;
315         if (ao_flight_state == ao_flight_landed)
316                 exit(0);
317 }
318
319 static const struct option options[] = {
320         { .name = "summary", .has_arg = 0, .val = 's' },
321         { 0, 0, 0, 0},
322 };
323
324 void run_flight_fixed(char *name, FILE *f, int summary)
325 {
326         emulator_in = f;
327         ao_summary = summary;
328         ao_flight_init();
329         ao_flight();
330 }
331
332 int
333 main (int argc, char **argv)
334 {
335         int     summary = 0;
336         int     c;
337         int     i;
338
339         while ((c = getopt_long(argc, argv, "s", options, NULL)) != -1) {
340                 switch (c) {
341                 case 's':
342                         summary = 1;
343                         break;
344                 }
345         }
346
347         if (optind == argc)
348                 run_flight_fixed("<stdin>", stdin, summary);
349         else
350                 for (i = optind; i < argc; i++) {
351                         FILE    *f = fopen(argv[i], "r");
352                         if (!f) {
353                                 perror(argv[i]);
354                                 continue;
355                         }
356                         run_flight_fixed(argv[i], f, summary);
357                         fclose(f);
358                 }
359 }