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