Update flight algorithm based on data collected from SN-1 Flight 1
[fw/altos] / 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 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #define AO_ADC_RING     64
23 #define ao_adc_ring_next(n)     (((n) + 1) & (AO_ADC_RING - 1))
24 #define ao_adc_ring_prev(n)     (((n) - 1) & (AO_ADC_RING - 1))
25
26 /*
27  * One set of samples read from the A/D converter
28  */
29 struct ao_adc {
30         uint16_t        tick;           /* tick when the sample was read */
31         int16_t         accel;          /* accelerometer */
32         int16_t         pres;           /* pressure sensor */
33         int16_t         temp;           /* temperature sensor */
34         int16_t         v_batt;         /* battery voltage */
35         int16_t         sense_d;        /* drogue continuity sense */
36         int16_t         sense_m;        /* main continuity sense */
37 };
38
39 #define __pdata
40 #define __data
41 #define __xdata
42
43 enum ao_flight_state {
44         ao_flight_startup = 0,
45         ao_flight_idle = 1,
46         ao_flight_launchpad = 2,
47         ao_flight_boost = 3,
48         ao_flight_coast = 4,
49         ao_flight_apogee = 5,
50         ao_flight_drogue = 6,
51         ao_flight_main = 7,
52         ao_flight_landed = 8,
53         ao_flight_invalid = 9
54 };
55
56 struct ao_adc ao_adc_ring[AO_ADC_RING];
57 uint8_t ao_adc_head;
58
59 #define ao_led_on(l)
60 #define ao_led_off(l)
61 #define ao_timer_set_adc_interval(i)
62 #define ao_wakeup(wchan) ao_dump_state()
63
64 enum ao_igniter {
65         ao_igniter_drogue = 0,
66         ao_igniter_main = 1
67 };
68
69 void
70 ao_ignite(enum ao_igniter igniter)
71 {
72         printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
73 }
74
75 struct ao_task {
76         int dummy;
77 };
78
79 #define ao_add_task(t,f,n)
80
81 #define ao_log_start()
82 #define ao_log_stop()
83
84 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
85 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
86
87 #define AO_FLIGHT_TEST
88
89 struct ao_adc ao_adc_static;
90
91 FILE *emulator_in;
92
93 void
94 ao_dump_state(void);
95
96 void
97 ao_sleep(void *wchan);
98
99 #include "ao_flight.c"
100
101 void
102 ao_insert(void)
103 {
104         ao_adc_ring[ao_adc_head] = ao_adc_static;
105         ao_adc_head = ao_adc_ring_next(ao_adc_head);
106         if (ao_flight_state != ao_flight_startup) {
107                 printf("time %g accel %d pres %d\n",
108                        (double) ao_adc_static.tick / 100,
109                        ao_adc_static.accel,
110                        ao_adc_static.pres);
111         }
112 }
113
114 static int      ao_records_read = 0;
115
116 void
117 ao_sleep(void *wchan)
118 {
119         ao_dump_state();
120         if (wchan == &ao_adc_ring) {
121                 char            type;
122                 uint16_t        tick;
123                 uint16_t        a, b;
124                 int             ret;
125
126                 for (;;) {
127                         if (ao_records_read > 20 && ao_flight_state == ao_flight_startup)
128                         {
129                                 ao_insert();
130                                 return;
131                         }
132
133                         ret = fscanf(emulator_in, "%c %hx %hx %hx\n", &type, &tick, &a, &b);
134                         if (ret == EOF) {
135                                 printf ("no more data, exiting simulation\n");
136                                 exit(0);
137                                 return;
138                         }
139                         if (ret != 4)
140                                 continue;
141                         switch (type) {
142                         case 'F':
143                                 break;
144                         case 'S':
145                                 break;
146                         case 'A':
147                                 ao_adc_static.tick = tick;
148                                 ao_adc_static.accel = a;
149                                 ao_adc_static.pres = b;
150                                 ao_records_read++;
151                                 ao_insert();
152                                 return;
153                         case 'T':
154                                 ao_adc_static.tick = tick;
155                                 ao_adc_static.temp = a;
156                                 ao_adc_static.v_batt = b;
157                                 break;
158                         case 'D':
159                         case 'G':
160                         case 'N':
161                         case 'W':
162                         case 'H':
163                                 break;
164                         }
165                 }
166
167         }
168 }
169
170 const char const * const ao_state_names[] = {
171         "startup", "idle", "pad", "boost", "coast",
172         "apogee", "drogue", "main", "landed", "invalid"
173 };
174
175 static int16_t altitude[2048] = {
176 #include "altitude.h"
177 };
178
179 #define COUNTS_PER_G 264.8
180
181 void
182 ao_dump_state(void)
183 {
184         if (ao_flight_state == ao_flight_startup)
185                 return;
186         printf ("\t%s accel %g vel %g alt %d\n",
187                 ao_state_names[ao_flight_state],
188                 (ao_flight_accel - ao_ground_accel) / COUNTS_PER_G * GRAVITY,
189                 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
190                 altitude[ao_flight_pres >> 4]);
191         if (ao_flight_state == ao_flight_landed)
192                 exit(0);
193 }
194
195 int
196 main (int argc, char **argv)
197 {
198         emulator_in = fopen (argv[1], "r");
199         if (!emulator_in) {
200                 perror(argv[1]);
201                 exit(1);
202         }
203         ao_flight_init();
204         ao_flight();
205 }