Merge remote branch 'origin/master'
[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
25 #define AO_ADC_RING     64
26 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
27 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
28
29 /*
30  * One set of samples read from the A/D converter
31  */
32 struct ao_adc {
33         uint16_t        tick;           /* tick when the sample was read */
34         int16_t         accel;          /* accelerometer */
35         int16_t         pres;           /* pressure sensor */
36         int16_t         temp;           /* temperature sensor */
37         int16_t         v_batt;         /* battery voltage */
38         int16_t         sense_d;        /* drogue continuity sense */
39         int16_t         sense_m;        /* main continuity sense */
40 };
41
42 #define __pdata
43 #define __data
44 #define __xdata
45 #define __code
46 #define __reentrant
47
48 enum ao_flight_state {
49         ao_flight_startup = 0,
50         ao_flight_idle = 1,
51         ao_flight_pad = 2,
52         ao_flight_boost = 3,
53         ao_flight_fast = 4,
54         ao_flight_coast = 5,
55         ao_flight_drogue = 6,
56         ao_flight_main = 7,
57         ao_flight_landed = 8,
58         ao_flight_invalid = 9
59 };
60
61 struct ao_adc ao_adc_ring[AO_ADC_RING];
62 uint8_t ao_adc_head;
63
64 #define ao_led_on(l)
65 #define ao_led_off(l)
66 #define ao_timer_set_adc_interval(i)
67 #define ao_wakeup(wchan) ao_dump_state()
68 #define ao_cmd_register(c)
69 #define ao_usb_disable()
70 #define ao_telemetry_set_interval(x)
71 #define ao_rdf_set(rdf)
72 #define ao_packet_slave_start()
73
74 enum ao_igniter {
75         ao_igniter_drogue = 0,
76         ao_igniter_main = 1
77 };
78
79 void
80 ao_ignite(enum ao_igniter igniter)
81 {
82         printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
83 }
84
85 struct ao_task {
86         int dummy;
87 };
88
89 #define ao_add_task(t,f,n)
90
91 #define ao_log_start()
92 #define ao_log_stop()
93
94 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
95 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
96
97 #define AO_FLIGHT_TEST
98
99 struct ao_adc ao_adc_static;
100
101 FILE *emulator_in;
102
103 void
104 ao_dump_state(void);
105
106 void
107 ao_sleep(void *wchan);
108
109 const char const * const ao_state_names[] = {
110         "startup", "idle", "pad", "boost", "fast",
111         "coast", "drogue", "main", "landed", "invalid"
112 };
113
114 struct ao_cmds {
115         char            cmd;
116         void            (*func)(void);
117         const char      *help;
118 };
119
120
121 static int16_t altitude_table[2048] = {
122 #include "altitude.h"
123 };
124
125 int16_t
126 ao_pres_to_altitude(int16_t pres) __reentrant
127 {
128         pres = pres >> 4;
129         if (pres < 0) pres = 0;
130         if (pres > 2047) pres = 2047;
131         return altitude_table[pres];
132 }
133
134 int16_t
135 ao_altitude_to_pres(int16_t alt) __reentrant
136 {
137         int16_t pres;
138
139         for (pres = 0; pres < 2047; pres++)
140                 if (altitude_table[pres] <= alt)
141                         break;
142         return pres << 4;
143 }
144
145 struct ao_config {
146         uint16_t        main_deploy;
147         int16_t         accel_zero_g;
148 };
149
150 #define ao_config_get()
151
152 struct ao_config ao_config = { 250, 16000 };
153
154 #include "ao_flight.c"
155
156 void
157 ao_insert(void)
158 {
159         ao_adc_ring[ao_adc_head] = ao_adc_static;
160         ao_adc_head = ao_adc_ring_next(ao_adc_head);
161         if (ao_flight_state != ao_flight_startup) {
162                 printf("time %g accel %d pres %d\n",
163                        (double) ao_adc_static.tick / 100,
164                        ao_adc_static.accel,
165                        ao_adc_static.pres);
166         }
167 }
168
169 static int      ao_records_read = 0;
170 static int      ao_eof_read = 0;
171 static int      ao_flight_ground_accel;
172 static int      ao_flight_started = 0;
173
174 void
175 ao_sleep(void *wchan)
176 {
177         ao_dump_state();
178         if (wchan == &ao_adc_ring) {
179                 char            type;
180                 uint16_t        tick;
181                 uint16_t        a, b;
182                 int             ret;
183                 char            line[1024];
184                 char            *saveptr;
185                 char            *l;
186                 char            *words[64];
187                 int             nword;
188
189                 for (;;) {
190                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
191                         {
192                                 ao_adc_static.accel = ao_flight_ground_accel;
193                                 ao_insert();
194                                 return;
195                         }
196
197                         if (!fgets(line, sizeof (line), emulator_in)) {
198                                 if (++ao_eof_read >= 1000) {
199                                         printf ("no more data, exiting simulation\n");
200                                         exit(0);
201                                 }
202                                 ao_adc_static.tick += 10;
203                                 ao_insert();
204                                 return;
205                         }
206                         l = line;
207                         for (nword = 0; nword < 64; nword++) {
208                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
209                                 l = NULL;
210                                 if (words[nword] == NULL)
211                                         break;
212                         }
213                         if (nword == 4) {
214                                 type = words[0][0];
215                                 tick = strtoul(words[1], NULL, 16);
216                                 a = strtoul(words[2], NULL, 16);
217                                 b = strtoul(words[2], NULL, 16);
218                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
219                                 tick = atoi(words[10]);
220                                 if (!ao_flight_started) {
221                                         type = 'F';
222                                         a = atoi(words[26]);
223                                         ao_flight_started = 1;
224                                 } else {
225                                         type = 'A';
226                                         a = atoi(words[12]);
227                                         b = atoi(words[14]);
228                                 }
229                         }
230                         if (type != 'F' && !ao_flight_started)
231                                 continue;
232
233                         switch (type) {
234                         case 'F':
235                                 ao_flight_ground_accel = a;
236                                 ao_flight_started = 1;
237                                 break;
238                         case 'S':
239                                 break;
240                         case 'A':
241                                 ao_adc_static.tick = tick;
242                                 ao_adc_static.accel = a;
243                                 ao_adc_static.pres = b;
244                                 ao_records_read++;
245                                 ao_insert();
246                                 return;
247                         case 'T':
248                                 ao_adc_static.tick = tick;
249                                 ao_adc_static.temp = a;
250                                 ao_adc_static.v_batt = b;
251                                 break;
252                         case 'D':
253                         case 'G':
254                         case 'N':
255                         case 'W':
256                         case 'H':
257                                 break;
258                         }
259                 }
260
261         }
262 }
263 #define COUNTS_PER_G 264.8
264
265 void
266 ao_dump_state(void)
267 {
268         if (ao_flight_state == ao_flight_startup)
269                 return;
270         printf ("\t\t\t\t\t%s accel %g vel %g alt %d main %d\n",
271                 ao_state_names[ao_flight_state],
272                 (ao_ground_accel - ao_flight_accel) / COUNTS_PER_G * GRAVITY,
273                 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
274                 ao_pres_to_altitude(ao_flight_pres) - ao_pres_to_altitude(ao_ground_pres),
275                 ao_pres_to_altitude(ao_main_pres) - ao_pres_to_altitude(ao_ground_pres));
276         if (ao_flight_state == ao_flight_landed)
277                 exit(0);
278 }
279
280 int
281 main (int argc, char **argv)
282 {
283         emulator_in = fopen (argv[1], "r");
284         if (!emulator_in) {
285                 perror(argv[1]);
286                 exit(1);
287         }
288         ao_flight_init();
289         ao_flight();
290 }