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