fix missing newline at end of src/test/Makefile
[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; 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 FILE *emulator_in;
28 char *emulator_app;
29 char *emulator_name;
30 char *emulator_info;
31 uint8_t ao_flight_debug;
32
33 #define AO_FLIGHT_TEST
34
35 typedef int32_t alt_t;
36
37 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
38
39 #define AO_LED_REPORT 0
40
41 static void ao_led_on(uint8_t led) {
42 }
43
44 static void ao_led_off(uint8_t led) {
45 }
46
47 static void ao_delay_until(uint16_t target) {
48 }
49
50 static uint16_t ao_time(void) {
51         return 0;
52 }
53
54 #include "ao_microflight.c"
55 #include "ao_microkalman.c"
56 #include "ao_convert_pa.c"
57
58 uint16_t        now;
59 uint8_t         running;
60
61 void ao_log_micro_data() {
62         running = 1;
63 }
64
65 void
66 ao_micro_report(void)
67 {
68         if (running) {
69                 alt_t   ground = ao_pa_to_altitude(pa_ground);
70                 printf ("%6.3f %10d %10d %10d %10d %10d\n", now / 100.0,
71                         ao_pa_to_altitude(pa) - ground,
72                         ao_pa_to_altitude(ao_pa) - ground,
73                         ao_pa_to_altitude(pa_min) - ground,
74                         ao_pa_speed, ao_pa_accel);
75         }
76 }
77
78 void
79 ao_micro_finish(void)
80 {
81         ao_micro_report();
82 }
83
84 void
85 ao_pa_get(void)
86 {
87         char    line[4096];
88         char    *toks[128];
89         char    *saveptr;
90         int     t, ntok;
91         static int      time_id;
92         static int      pa_id;
93         double          time;
94         double          pressure;
95         static double   last_time;
96         static double   last_pressure;
97         static int      been_here;
98         static int      start_samples;
99         static int      is_mp;
100         static int      use_saved;
101
102         if (been_here && start_samples < 100) {
103                 start_samples++;
104                 return;
105         }
106         ao_micro_report();
107         if (use_saved) {
108                 pa = last_pressure;
109                 now = last_time;
110                 use_saved = 0;
111 //              printf ("use saved %d %d\n", now, pa);
112                 return;
113         }
114         for (;;) {
115                 if (!fgets(line, sizeof (line), emulator_in))
116                         exit(0);
117                 for (t = 0; t < 128; t++) {
118                         toks[t] = strtok_r(t ? NULL : line, ", ", &saveptr);
119                         if (!toks[t])
120                                 break;
121                 }
122                 ntok = t;
123                 if (toks[0][0] == '#') {
124                         if (strcmp(toks[0],"#version") == 0) {
125                                 for (t = 1; t < ntok; t++) {
126                                         if (!strcmp(toks[t], "time"))
127                                                 time_id = t;
128                                         if (!strcmp(toks[t],"pressure"))
129                                                 pa_id = t;
130                                 }
131                         }
132                         continue;
133                 } else if (!strcmp(toks[0], "Time")) {
134                         time_id = 0;
135                         pa_id = 1;
136                         is_mp = 1;
137                         continue;
138                 }
139                 time = strtod(toks[time_id],NULL);
140                 pressure = strtod(toks[pa_id],NULL);
141                 time *= 100;
142                 if (been_here && time - last_time < 0.096 * 100)
143                         continue;
144                 if (is_mp && been_here) {
145                         double  avg_pressure = (pressure + last_pressure) / 2.0;
146                         double  avg_time = (time + last_time) / 2.0;
147
148                         now = avg_time;
149                         pa = avg_pressure;
150 //                      printf ("new %d %d\n", now, pa);
151                         use_saved = 1;
152                 } else {
153                         now = floor (time + 0.5);
154                         pa = pressure;
155                 }
156                 last_pressure = pressure;
157                 last_time = time;
158                 been_here = 1;
159                 break;
160         }
161 }
162
163 void
164 ao_dump_state(void)
165 {
166 }
167
168 static const struct option options[] = {
169         { .name = "summary", .has_arg = 0, .val = 's' },
170         { .name = "debug", .has_arg = 0, .val = 'd' },
171         { .name = "info", .has_arg = 1, .val = 'i' },
172         { 0, 0, 0, 0},
173 };
174
175 void run_flight_fixed(char *name, FILE *f, int summary, char *info)
176 {
177         emulator_name = name;
178         emulator_in = f;
179         emulator_info = info;
180         ao_microflight();
181         ao_micro_finish();
182 }
183
184 int
185 main (int argc, char **argv)
186 {
187         int     summary = 0;
188         int     c;
189         int     i;
190         char    *info = NULL;
191
192         emulator_app="baro";
193         while ((c = getopt_long(argc, argv, "sdi:", options, NULL)) != -1) {
194                 switch (c) {
195                 case 's':
196                         summary = 1;
197                         break;
198                 case 'd':
199                         ao_flight_debug = 1;
200                         break;
201                 case 'i':
202                         info = optarg;
203                         break;
204                 }
205         }
206
207         if (optind == argc)
208                 run_flight_fixed("<stdin>", stdin, summary, info);
209         else
210                 for (i = optind; i < argc; i++) {
211                         FILE    *f = fopen(argv[i], "r");
212                         if (!f) {
213                                 perror(argv[i]);
214                                 continue;
215                         }
216                         run_flight_fixed(argv[i], f, summary, info);
217                         fclose(f);
218                 }
219         exit(0);
220 }