altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / src / kernel / ao_ignite.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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #include <ao_data.h>
21 #if AO_PYRO_NUM
22 #include <ao_pyro.h>
23 #endif
24
25 #if HAS_IGNITE
26 struct ao_ignition ao_ignition[2];
27
28 #ifndef AO_SENSE_DROGUE
29 #define AO_SENSE_DROGUE(p)      ((p)->adc.sense_d)
30 #define AO_SENSE_MAIN(p)        ((p)->adc.sense_m)
31 #endif
32
33 enum ao_igniter_status
34 ao_igniter_status(enum ao_igniter igniter)
35 {
36         struct ao_data packet;
37         int16_t value;
38         uint8_t request, firing, fired;
39
40         ao_arch_critical(
41                 ao_data_get(&packet);
42                 request = ao_ignition[igniter].request;
43                 fired = ao_ignition[igniter].fired;
44                 firing = ao_ignition[igniter].firing;
45                 );
46         if (firing || (request && !fired))
47                 return ao_igniter_active;
48
49         value = (AO_IGNITER_CLOSED>>1);
50         switch (igniter) {
51         case ao_igniter_drogue:
52                 value = AO_SENSE_DROGUE(&packet);
53                 break;
54         case ao_igniter_main:
55                 value = AO_SENSE_MAIN(&packet);
56                 break;
57         }
58         if (value < AO_IGNITER_OPEN)
59                 return ao_igniter_open;
60         else if (value > AO_IGNITER_CLOSED)
61                 return ao_igniter_ready;
62         else
63                 return ao_igniter_unknown;
64 }
65
66 #define AO_IGNITER_SET_DROGUE(v)        ao_gpio_set(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, v)
67 #define AO_IGNITER_SET_MAIN(v)          ao_gpio_set(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, v)
68
69 #ifndef AO_IGNITER_FIRE_TIME
70 #define AO_IGNITER_FIRE_TIME            AO_MS_TO_TICKS(50)
71 #endif
72
73 #ifndef AO_IGNITER_CHARGE_TIME
74 #define AO_IGNITER_CHARGE_TIME          AO_MS_TO_TICKS(2000)
75 #endif
76
77 static void
78 ao_igniter_fire(enum ao_igniter igniter, bool wait)
79 {
80         if (!ao_ignition[igniter].fired) {
81                 ao_ignition[igniter].firing = 1;
82                 ao_ignition[igniter].fired = 1;
83                 switch (igniter) {
84                 case ao_igniter_drogue:
85                         if (wait && ao_config.apogee_delay &&
86                             ao_config.ignite_mode != AO_IGNITE_MODE_MAIN)
87                                 ao_delay(AO_SEC_TO_TICKS(ao_config.apogee_delay));
88                         AO_IGNITER_SET_DROGUE(1);
89                         ao_delay(AO_IGNITER_FIRE_TIME);
90                         AO_IGNITER_SET_DROGUE(0);
91                         break;
92                 case ao_igniter_main:
93                         AO_IGNITER_SET_MAIN(1);
94                         ao_delay(AO_IGNITER_FIRE_TIME);
95                         AO_IGNITER_SET_MAIN(0);
96                         break;
97                 }
98                 ao_ignition[igniter].firing = 0;
99                 if (wait)
100                         ao_delay(AO_IGNITER_CHARGE_TIME);
101         }
102 }
103
104 static void
105 ao_igniter(void)
106 {
107         ao_config_get();
108         for (;;) {
109                 /* Wait for flight state change */
110                 ao_sleep(&ao_flight_state);
111
112                 /* Fire any igniters that are supposed to be triggered
113                  * in this new state
114                  */
115                 switch(ao_config.ignite_mode) {
116                 case AO_IGNITE_MODE_DUAL:
117                         if (ao_flight_drogue <= ao_flight_state && ao_flight_state < ao_flight_landed)
118                                 ao_igniter_fire(ao_igniter_drogue, true);
119                         if (ao_flight_main <= ao_flight_state && ao_flight_state < ao_flight_landed)
120                                 ao_igniter_fire(ao_igniter_main, true);
121                         break;
122                 case AO_IGNITE_MODE_APOGEE:
123                         if (ao_flight_drogue <= ao_flight_state && ao_flight_state < ao_flight_landed) {
124                                 ao_igniter_fire(ao_igniter_drogue, true);
125                                 ao_igniter_fire(ao_igniter_main, true);
126                         }
127                         break;
128                 case AO_IGNITE_MODE_MAIN:
129                         if (ao_flight_main <= ao_flight_state && ao_flight_state < ao_flight_landed) {
130                                 ao_igniter_fire(ao_igniter_drogue, true);
131                                 ao_igniter_fire(ao_igniter_main, true);
132                         }
133                         break;
134                 case AO_IGNITE_MODE_BOOSTER:
135                         if (ao_flight_fast <= ao_flight_state && ao_flight_state < ao_flight_landed)
136                                 ao_igniter_fire(ao_igniter_main, true);
137                         if (ao_flight_drogue <= ao_flight_state && ao_flight_state < ao_flight_landed)
138                                 ao_igniter_fire(ao_igniter_drogue, true);
139                         break;
140                 }
141         }
142 }
143
144 #endif
145
146 static void
147 ao_ignite_manual(void)
148 {
149         ao_cmd_white();
150         if (!ao_match_word("DoIt"))
151                 return;
152         ao_cmd_white();
153 #if HAS_IGNITE
154         if (ao_cmd_lex_c == 'm' && ao_match_word("main")) {
155                 ao_ignition[ao_igniter_main].fired = 0;
156                 ao_igniter_fire(ao_igniter_main, false);
157                 return;
158         }
159         if (ao_cmd_lex_c == 'd' && ao_match_word("drogue")) {
160                 ao_ignition[ao_igniter_drogue].fired = 0;
161                 ao_igniter_fire(ao_igniter_drogue, false);
162                 return;
163         }
164 #endif
165 #if AO_PYRO_NUM
166         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9') {
167                 ao_pyro_manual(ao_cmd_lex_c - '0');
168                 return;
169         }
170 #endif
171         ao_cmd_status = ao_cmd_syntax_error;
172 }
173
174 const char * const ao_igniter_status_names[] = {
175         "unknown", "ready", "active", "open"
176 };
177
178 #if HAS_IGNITE
179 static void
180 ao_ignite_print_status(enum ao_igniter igniter, const char *name) 
181 {
182         enum ao_igniter_status status = ao_igniter_status(igniter);
183         printf("Igniter: %6s Status: %s\n",
184                name,
185                ao_igniter_status_names[status]);
186 }
187 #endif
188
189 static void
190 ao_ignite_test(void)
191 {
192 #if HAS_IGNITE
193         ao_ignite_print_status(ao_igniter_drogue, "drogue");
194         ao_ignite_print_status(ao_igniter_main, "main");
195 #endif
196 #if AO_PYRO_NUM
197         ao_pyro_print_status();
198 #endif
199 }
200
201 const struct ao_cmds ao_ignite_cmds[] = {
202         { ao_ignite_manual,     "i <key> {main|drogue}\0Fire igniter. <key> is doit with D&I" },
203         { ao_ignite_test,       "t\0Test igniter" },
204         { 0,    NULL },
205 };
206
207 #if HAS_IGNITE
208 struct ao_task ao_igniter_task;
209
210 void
211 ao_ignite_set_pins(void)
212 {
213         ao_enable_output(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, 0);
214         ao_enable_output(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, 0);
215 }
216 #endif
217
218 void
219 ao_igniter_init(void)
220 {
221 #if HAS_IGNITE
222         ao_ignite_set_pins();
223         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
224 #endif
225 #if HAS_IGNITE || AO_PYRO_NUM
226         ao_cmd_register(&ao_ignite_cmds[0]);
227 #endif
228 }