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