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 #include <ao_pyro.h>
22
23 #if HAS_IGNITE
24 struct ao_ignition ao_ignition[2];
25
26 #ifndef AO_SENSE_DROGUE
27 #define AO_SENSE_DROGUE(p)      ((p)->adc.sense_d)
28 #define AO_SENSE_MAIN(p)        ((p)->adc.sense_m)
29 #endif
30
31 enum ao_igniter_status
32 ao_igniter_status(enum ao_igniter igniter)
33 {
34         struct ao_data packet;
35         int16_t value;
36         uint8_t request, firing, fired;
37
38         ao_arch_critical(
39                 ao_data_get(&packet);
40                 request = ao_ignition[igniter].request;
41                 fired = ao_ignition[igniter].fired;
42                 firing = ao_ignition[igniter].firing;
43                 );
44         if (firing || (request && !fired))
45                 return ao_igniter_active;
46
47         value = (AO_IGNITER_CLOSED>>1);
48         switch (igniter) {
49         case ao_igniter_drogue:
50                 value = AO_SENSE_DROGUE(&packet);
51                 break;
52         case ao_igniter_main:
53                 value = AO_SENSE_MAIN(&packet);
54                 break;
55         }
56         return ao_igniter_check(value, AO_SENSE_PBATT(&packet));
57 }
58
59 #define AO_IGNITER_SET_DROGUE(v)        ao_gpio_set(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, v)
60 #define AO_IGNITER_SET_MAIN(v)          ao_gpio_set(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, v)
61
62 #ifndef AO_IGNITER_FIRE_TIME
63 #define AO_IGNITER_FIRE_TIME            AO_MS_TO_TICKS(50)
64 #endif
65
66 #ifndef AO_IGNITER_CHARGE_TIME
67 #define AO_IGNITER_CHARGE_TIME          AO_MS_TO_TICKS(2000)
68 #endif
69
70 static void
71 ao_igniter_fire(enum ao_igniter igniter, bool wait)
72 {
73         if (!ao_ignition[igniter].fired) {
74                 ao_ignition[igniter].firing = 1;
75                 ao_ignition[igniter].fired = 1;
76                 switch (igniter) {
77                 case ao_igniter_drogue:
78                         if (wait && ao_config.apogee_delay &&
79                             ao_config.ignite_mode != AO_IGNITE_MODE_MAIN)
80                                 ao_delay(AO_SEC_TO_TICKS(ao_config.apogee_delay));
81                         AO_IGNITER_SET_DROGUE(1);
82                         ao_delay(AO_IGNITER_FIRE_TIME);
83                         AO_IGNITER_SET_DROGUE(0);
84                         break;
85                 case ao_igniter_main:
86                         AO_IGNITER_SET_MAIN(1);
87                         ao_delay(AO_IGNITER_FIRE_TIME);
88                         AO_IGNITER_SET_MAIN(0);
89                         break;
90                 }
91                 ao_ignition[igniter].firing = 0;
92                 if (wait)
93                         ao_delay(AO_IGNITER_CHARGE_TIME);
94         }
95 }
96
97 static void
98 ao_igniter(void)
99 {
100         ao_config_get();
101         for (;;) {
102                 /* Wait for flight state change */
103                 ao_sleep(&ao_flight_state);
104
105                 /* Fire any igniters that are supposed to be triggered
106                  * in this new state
107                  */
108                 switch(ao_config.ignite_mode) {
109                 case AO_IGNITE_MODE_DUAL:
110                         if (ao_flight_drogue <= ao_flight_state && ao_flight_state < ao_flight_landed)
111                                 ao_igniter_fire(ao_igniter_drogue, true);
112                         if (ao_flight_main <= ao_flight_state && ao_flight_state < ao_flight_landed)
113                                 ao_igniter_fire(ao_igniter_main, true);
114                         break;
115                 case AO_IGNITE_MODE_APOGEE:
116                         if (ao_flight_drogue <= ao_flight_state && ao_flight_state < ao_flight_landed) {
117                                 ao_igniter_fire(ao_igniter_drogue, true);
118                                 ao_igniter_fire(ao_igniter_main, true);
119                         }
120                         break;
121                 case AO_IGNITE_MODE_MAIN:
122                         if (ao_flight_main <= ao_flight_state && ao_flight_state < ao_flight_landed) {
123                                 ao_igniter_fire(ao_igniter_drogue, true);
124                                 ao_igniter_fire(ao_igniter_main, true);
125                         }
126                         break;
127                 case AO_IGNITE_MODE_BOOSTER:
128                         if (ao_flight_fast <= ao_flight_state && ao_flight_state < ao_flight_landed)
129                                 ao_igniter_fire(ao_igniter_main, true);
130                         if (ao_flight_drogue <= ao_flight_state && ao_flight_state < ao_flight_landed)
131                                 ao_igniter_fire(ao_igniter_drogue, true);
132                         break;
133                 }
134         }
135 }
136
137 #endif
138
139 static void
140 ao_ignite_manual(void)
141 {
142         ao_cmd_white();
143         if (!ao_match_word("DoIt"))
144                 return;
145         ao_cmd_white();
146 #if HAS_IGNITE
147         if (ao_cmd_lex_c == 'm' && ao_match_word("main")) {
148                 ao_ignition[ao_igniter_main].fired = 0;
149                 ao_igniter_fire(ao_igniter_main, false);
150                 return;
151         }
152         if (ao_cmd_lex_c == 'd' && ao_match_word("drogue")) {
153                 ao_ignition[ao_igniter_drogue].fired = 0;
154                 ao_igniter_fire(ao_igniter_drogue, false);
155                 return;
156         }
157 #endif
158 #if AO_PYRO_NUM
159         if ('0' <= ao_cmd_lex_c && ao_cmd_lex_c <= '9') {
160                 ao_pyro_manual(ao_cmd_lex_c - '0');
161                 return;
162         }
163 #endif
164         ao_cmd_status = ao_cmd_syntax_error;
165 }
166
167 const char * const ao_igniter_status_names[] = {
168         "unknown", "ready", "active", "open"
169 };
170
171 #if HAS_IGNITE
172 static void
173 ao_ignite_print_status(enum ao_igniter igniter, const char *name) 
174 {
175         enum ao_igniter_status status = ao_igniter_status(igniter);
176         printf("Igniter: %6s Status: %s\n",
177                name,
178                ao_igniter_status_names[status]);
179 }
180 #endif
181
182 static void
183 ao_ignite_test(void)
184 {
185 #if HAS_IGNITE
186         ao_ignite_print_status(ao_igniter_drogue, "drogue");
187         ao_ignite_print_status(ao_igniter_main, "main");
188 #endif
189 #if AO_PYRO_NUM
190         ao_pyro_print_status();
191 #endif
192 }
193
194 const struct ao_cmds ao_ignite_cmds[] = {
195         { ao_ignite_manual,     "i <key> {main|drogue}\0Fire igniter. <key> is doit with D&I" },
196         { ao_ignite_test,       "t\0Test igniter" },
197         { 0,    NULL },
198 };
199
200 #if HAS_IGNITE
201 struct ao_task ao_igniter_task;
202
203 void
204 ao_ignite_set_pins(void)
205 {
206         ao_enable_output(AO_IGNITER_DROGUE_PORT, AO_IGNITER_DROGUE_PIN, 0);
207         ao_enable_output(AO_IGNITER_MAIN_PORT, AO_IGNITER_MAIN_PIN, 0);
208 }
209 #endif
210
211 void
212 ao_igniter_init(void)
213 {
214 #if HAS_IGNITE
215         ao_ignite_set_pins();
216         ao_add_task(&ao_igniter_task, ao_igniter, "igniter");
217 #endif
218 #if HAS_IGNITE || AO_PYRO_NUM
219         ao_cmd_register(&ao_ignite_cmds[0]);
220 #endif
221 }