update changelogs for Debian build
[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
72 enum ao_igniter {
73         ao_igniter_drogue = 0,
74         ao_igniter_main = 1
75 };
76
77 void
78 ao_ignite(enum ao_igniter igniter)
79 {
80         printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
81 }
82
83 struct ao_task {
84         int dummy;
85 };
86
87 #define ao_add_task(t,f,n)
88
89 #define ao_log_start()
90 #define ao_log_stop()
91
92 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
93 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
94
95 #define AO_FLIGHT_TEST
96
97 struct ao_adc ao_adc_static;
98
99 FILE *emulator_in;
100
101 void
102 ao_dump_state(void);
103
104 void
105 ao_sleep(void *wchan);
106
107 const char const * const ao_state_names[] = {
108         "startup", "idle", "pad", "boost", "fast",
109         "coast", "drogue", "main", "landed", "invalid"
110 };
111
112 struct ao_cmds {
113         char            cmd;
114         void            (*func)(void);
115         const char      *help;
116 };
117
118
119 static int16_t altitude_table[2048] = {
120 #include "altitude.h"
121 };
122
123 int16_t
124 ao_pres_to_altitude(int16_t pres) __reentrant
125 {
126         pres = pres >> 4;
127         if (pres < 0) pres = 0;
128         if (pres > 2047) pres = 2047;
129         return altitude_table[pres];
130 }
131
132 int16_t
133 ao_altitude_to_pres(int16_t alt) __reentrant
134 {
135         int16_t pres;
136
137         for (pres = 0; pres < 2047; pres++)
138                 if (altitude_table[pres] <= alt)
139                         break;
140         return pres << 4;
141 }
142
143 struct ao_config {
144         uint16_t        main_deploy;
145         int16_t         accel_zero_g;
146 };
147
148 #define ao_config_get()
149
150 struct ao_config ao_config = { 250, 16000 };
151
152 #include "ao_flight.c"
153
154 void
155 ao_insert(void)
156 {
157         ao_adc_ring[ao_adc_head] = ao_adc_static;
158         ao_adc_head = ao_adc_ring_next(ao_adc_head);
159         if (ao_flight_state != ao_flight_startup) {
160                 printf("time %g accel %d pres %d\n",
161                        (double) ao_adc_static.tick / 100,
162                        ao_adc_static.accel,
163                        ao_adc_static.pres);
164         }
165 }
166
167 static int      ao_records_read = 0;
168 static int      ao_eof_read = 0;
169 static int      ao_flight_ground_accel;
170 static int      ao_flight_started = 0;
171
172 void
173 ao_sleep(void *wchan)
174 {
175         ao_dump_state();
176         if (wchan == &ao_adc_ring) {
177                 char            type;
178                 uint16_t        tick;
179                 uint16_t        a, b;
180                 int             ret;
181                 char            line[1024];
182                 char            *saveptr;
183                 char            *l;
184                 char            *words[64];
185                 int             nword;
186
187                 for (;;) {
188                         if (ao_records_read > 2 && ao_flight_state == ao_flight_startup)
189                         {
190                                 ao_adc_static.accel = ao_flight_ground_accel;
191                                 ao_insert();
192                                 return;
193                         }
194
195                         if (!fgets(line, sizeof (line), emulator_in)) {
196                                 if (++ao_eof_read >= 1000) {
197                                         printf ("no more data, exiting simulation\n");
198                                         exit(0);
199                                 }
200                                 ao_adc_static.tick += 10;
201                                 ao_insert();
202                                 return;
203                         }
204                         l = line;
205                         for (nword = 0; nword < 64; nword++) {
206                                 words[nword] = strtok_r(l, " \t\n", &saveptr);
207                                 l = NULL;
208                                 if (words[nword] == NULL)
209                                         break;
210                         }
211                         if (nword == 4) {
212                                 type = words[0][0];
213                                 tick = strtoul(words[1], NULL, 16);
214                                 a = strtoul(words[2], NULL, 16);
215                                 b = strtoul(words[2], NULL, 16);
216                         } else if (nword >= 36 && strcmp(words[0], "CALL") == 0) {
217                                 tick = atoi(words[10]);
218                                 if (!ao_flight_started) {
219                                         type = 'F';
220                                         a = atoi(words[26]);
221                                         ao_flight_started = 1;
222                                 } else {
223                                         type = 'A';
224                                         a = atoi(words[12]);
225                                         b = atoi(words[14]);
226                                 }
227                         }
228                         if (type != 'F' && !ao_flight_started)
229                                 continue;
230
231                         switch (type) {
232                         case 'F':
233                                 ao_flight_ground_accel = a;
234                                 ao_flight_started = 1;
235                                 break;
236                         case 'S':
237                                 break;
238                         case 'A':
239                                 ao_adc_static.tick = tick;
240                                 ao_adc_static.accel = a;
241                                 ao_adc_static.pres = b;
242                                 ao_records_read++;
243                                 ao_insert();
244                                 return;
245                         case 'T':
246                                 ao_adc_static.tick = tick;
247                                 ao_adc_static.temp = a;
248                                 ao_adc_static.v_batt = b;
249                                 break;
250                         case 'D':
251                         case 'G':
252                         case 'N':
253                         case 'W':
254                         case 'H':
255                                 break;
256                         }
257                 }
258
259         }
260 }
261 #define COUNTS_PER_G 264.8
262
263 void
264 ao_dump_state(void)
265 {
266         if (ao_flight_state == ao_flight_startup)
267                 return;
268         printf ("\t\t\t\t\t%s accel %g vel %g alt %d main %d\n",
269                 ao_state_names[ao_flight_state],
270                 (ao_ground_accel - ao_flight_accel) / COUNTS_PER_G * GRAVITY,
271                 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
272                 ao_pres_to_altitude(ao_flight_pres) - ao_pres_to_altitude(ao_ground_pres),
273                 ao_pres_to_altitude(ao_main_pres) - ao_pres_to_altitude(ao_ground_pres));
274         if (ao_flight_state == ao_flight_landed)
275                 exit(0);
276 }
277
278 int
279 main (int argc, char **argv)
280 {
281         emulator_in = fopen (argv[1], "r");
282         if (!emulator_in) {
283                 perror(argv[1]);
284                 exit(1);
285         }
286         ao_flight_init();
287         ao_flight();
288 }