Add monitor task to flight software
[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 #define __code
43 #define __reentrant
44
45 enum ao_flight_state {
46         ao_flight_startup = 0,
47         ao_flight_idle = 1,
48         ao_flight_launchpad = 2,
49         ao_flight_boost = 3,
50         ao_flight_coast = 4,
51         ao_flight_apogee = 5,
52         ao_flight_drogue = 6,
53         ao_flight_main = 7,
54         ao_flight_landed = 8,
55         ao_flight_invalid = 9
56 };
57
58 struct ao_adc ao_adc_ring[AO_ADC_RING];
59 uint8_t ao_adc_head;
60
61 #define ao_led_on(l)
62 #define ao_led_off(l)
63 #define ao_timer_set_adc_interval(i)
64 #define ao_wakeup(wchan) ao_dump_state()
65 #define ao_cmd_register(c)
66
67 enum ao_igniter {
68         ao_igniter_drogue = 0,
69         ao_igniter_main = 1
70 };
71
72 void
73 ao_ignite(enum ao_igniter igniter)
74 {
75         printf ("ignite %s\n", igniter == ao_igniter_drogue ? "drogue" : "main");
76 }
77
78 struct ao_task {
79         int dummy;
80 };
81
82 #define ao_add_task(t,f,n)
83
84 #define ao_log_start()
85 #define ao_log_stop()
86
87 #define AO_MS_TO_TICKS(ms)      ((ms) / 10)
88 #define AO_SEC_TO_TICKS(s)      ((s) * 100)
89
90 #define AO_FLIGHT_TEST
91
92 struct ao_adc ao_adc_static;
93
94 FILE *emulator_in;
95
96 void
97 ao_dump_state(void);
98
99 void
100 ao_sleep(void *wchan);
101
102 const char const * const ao_state_names[] = {
103         "startup", "idle", "pad", "boost", "coast",
104         "apogee", "drogue", "main", "landed", "invalid"
105 };
106
107 struct ao_cmds {
108         uint8_t         cmd;
109         void            (*func)(void);
110         const char      *help;
111 };
112
113
114 static int16_t altitude_table[2048] = {
115 #include "altitude.h"
116 };
117
118 int16_t
119 ao_pres_to_altitude(int16_t pres) __reentrant
120 {
121         pres = pres >> 4;
122         if (pres < 0) pres = 0;
123         if (pres > 2047) pres = 2047;
124         return altitude_table[pres];
125 }
126
127 #include "ao_flight.c"
128
129 void
130 ao_insert(void)
131 {
132         ao_adc_ring[ao_adc_head] = ao_adc_static;
133         ao_adc_head = ao_adc_ring_next(ao_adc_head);
134         if (ao_flight_state != ao_flight_startup) {
135                 printf("time %g accel %d pres %d\n",
136                        (double) ao_adc_static.tick / 100,
137                        ao_adc_static.accel,
138                        ao_adc_static.pres);
139         }
140 }
141
142 static int      ao_records_read = 0;
143
144 void
145 ao_sleep(void *wchan)
146 {
147         ao_dump_state();
148         if (wchan == &ao_adc_ring) {
149                 char            type;
150                 uint16_t        tick;
151                 uint16_t        a, b;
152                 int             ret;
153
154                 for (;;) {
155                         if (ao_records_read > 20 && ao_flight_state == ao_flight_startup)
156                         {
157                                 ao_insert();
158                                 return;
159                         }
160
161                         ret = fscanf(emulator_in, "%c %hx %hx %hx\n", &type, &tick, &a, &b);
162                         if (ret == EOF) {
163                                 printf ("no more data, exiting simulation\n");
164                                 exit(0);
165                                 return;
166                         }
167                         if (ret != 4)
168                                 continue;
169                         switch (type) {
170                         case 'F':
171                                 break;
172                         case 'S':
173                                 break;
174                         case 'A':
175                                 ao_adc_static.tick = tick;
176                                 ao_adc_static.accel = a;
177                                 ao_adc_static.pres = b;
178                                 ao_records_read++;
179                                 ao_insert();
180                                 return;
181                         case 'T':
182                                 ao_adc_static.tick = tick;
183                                 ao_adc_static.temp = a;
184                                 ao_adc_static.v_batt = b;
185                                 break;
186                         case 'D':
187                         case 'G':
188                         case 'N':
189                         case 'W':
190                         case 'H':
191                                 break;
192                         }
193                 }
194
195         }
196 }
197 #define COUNTS_PER_G 264.8
198
199 void
200 ao_dump_state(void)
201 {
202         if (ao_flight_state == ao_flight_startup)
203                 return;
204         printf ("\t%s accel %g vel %g alt %d\n",
205                 ao_state_names[ao_flight_state],
206                 (ao_flight_accel - ao_ground_accel) / COUNTS_PER_G * GRAVITY,
207                 (double) ao_flight_vel / 100 / COUNTS_PER_G * GRAVITY,
208                 altitude_table[ao_flight_pres >> 4]);
209         if (ao_flight_state == ao_flight_landed)
210                 exit(0);
211 }
212
213 int
214 main (int argc, char **argv)
215 {
216         emulator_in = fopen (argv[1], "r");
217         if (!emulator_in) {
218                 perror(argv[1]);
219                 exit(1);
220         }
221         ao_flight_init();
222         ao_flight();
223 }