Switch from GPLv2 to GPLv2+
[fw/altos] / src / test / ao_micropeak_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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define _GNU_SOURCE
20
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <math.h>
27
28 FILE *emulator_in;
29 char *emulator_app;
30 char *emulator_name;
31 char *emulator_info;
32 uint8_t ao_flight_debug;
33
34 #define AO_FLIGHT_TEST
35
36 typedef int32_t alt_t;
37 typedef int32_t pres_t;
38
39 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
40
41 #define AO_LED_REPORT 0
42
43 static void ao_led_on(uint8_t led) {
44 }
45
46 static void ao_led_off(uint8_t led) {
47 }
48
49 static void ao_delay_until(uint16_t target) {
50 }
51
52 static uint16_t ao_time(void) {
53         return 0;
54 }
55
56 #include "ao_microflight.c"
57 #include "ao_microkalman.c"
58 #include "ao_convert_pa.c"
59
60 uint16_t        now;
61 uint8_t         running;
62
63 void ao_log_micro_data() {
64         running = 1;
65 }
66
67 void
68 ao_micro_report(void)
69 {
70         if (running) {
71                 alt_t   ground = ao_pa_to_altitude(pa_ground);
72                 printf ("%6.3f %10d %10d %10d %10d %10d\n", now / 100.0,
73                         ao_pa_to_altitude(pa) - ground,
74                         ao_pa_to_altitude(ao_pa) - ground,
75                         ao_pa_to_altitude(pa_min) - ground,
76                         ao_pa_speed, ao_pa_accel);
77         }
78 }
79
80 void
81 ao_micro_finish(void)
82 {
83         ao_micro_report();
84 }
85
86 void
87 ao_pa_get(void)
88 {
89         char    line[4096];
90         char    *toks[128];
91         char    *saveptr;
92         int     t, ntok;
93         static int      time_id;
94         static int      pa_id;
95         double          time;
96         double          pressure;
97         static double   last_time;
98         static double   last_pressure;
99         static int      been_here;
100         static int      start_samples;
101         static int      is_mp;
102         static int      use_saved;
103
104         if (been_here && start_samples < 100) {
105                 start_samples++;
106                 return;
107         }
108         ao_micro_report();
109         if (use_saved) {
110                 pa = last_pressure;
111                 now = last_time;
112                 use_saved = 0;
113 //              printf ("use saved %d %d\n", now, pa);
114                 return;
115         }
116         for (;;) {
117                 if (!fgets(line, sizeof (line), emulator_in))
118                         exit(0);
119                 for (t = 0; t < 128; t++) {
120                         toks[t] = strtok_r(t ? NULL : line, ", ", &saveptr);
121                         if (!toks[t])
122                                 break;
123                 }
124                 ntok = t;
125                 if (toks[0][0] == '#') {
126                         if (strcmp(toks[0],"#version") == 0) {
127                                 for (t = 1; t < ntok; t++) {
128                                         if (!strcmp(toks[t], "time"))
129                                                 time_id = t;
130                                         if (!strcmp(toks[t],"pressure"))
131                                                 pa_id = t;
132                                 }
133                         }
134                         continue;
135                 } else if (!strcmp(toks[0], "Time")) {
136                         time_id = 0;
137                         pa_id = 1;
138                         is_mp = 1;
139                         continue;
140                 }
141                 time = strtod(toks[time_id],NULL);
142                 pressure = strtod(toks[pa_id],NULL);
143                 time *= 100;
144                 if (been_here && time - last_time < 0.096 * 100)
145                         continue;
146                 if (is_mp && been_here) {
147                         double  avg_pressure = (pressure + last_pressure) / 2.0;
148                         double  avg_time = (time + last_time) / 2.0;
149
150                         now = avg_time;
151                         pa = avg_pressure;
152 //                      printf ("new %d %d\n", now, pa);
153                         use_saved = 1;
154                 } else {
155                         now = floor (time + 0.5);
156                         pa = pressure;
157                 }
158                 last_pressure = pressure;
159                 last_time = time;
160                 been_here = 1;
161                 break;
162         }
163 }
164
165 void
166 ao_dump_state(void)
167 {
168 }
169
170 static const struct option options[] = {
171         { .name = "summary", .has_arg = 0, .val = 's' },
172         { .name = "debug", .has_arg = 0, .val = 'd' },
173         { .name = "info", .has_arg = 1, .val = 'i' },
174         { 0, 0, 0, 0},
175 };
176
177 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
178 {
179         emulator_name = name;
180         emulator_in = f;
181         emulator_info = info;
182         ao_microflight();
183         ao_micro_finish();
184 }
185
186 int
187 main (int argc, char **argv)
188 {
189         int     summary = 0;
190         int     c;
191         int     i;
192         char    *info = NULL;
193
194         emulator_app="baro";
195         while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
196                 switch (c) {
197                 case 's':
198                         summary = 1;
199                         break;
200                 case 'd':
201                         ao_flight_debug = 1;
202                         break;
203                 case 'i':
204                         info = optarg;
205                         break;
206                 }
207         }
208
209         if (optind == argc)
210                 run_flight_fixed("<stdin>", stdin, summary, info);
211         else
212                 for (i = optind; i < argc; i++) {
213                         FILE    *f = fopen(argv[i], "r");
214                         if (!f) {
215                                 perror(argv[i]);
216                                 continue;
217                         }
218                         run_flight_fixed(argv[i], f, summary, info);
219                         fclose(f);
220                 }
221         exit(0);
222 }