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