943413e699a9224f1d05f850c249dae74508fe59
[fw/altos] / src / core / ao_pyro.c
1 /*
2  * Copyright © 2012 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 <ao.h>
19 #include <ao_pyro.h>
20 #include <ao_sample.h>
21 #include <ao_flight.h>
22
23 #define ao_lowbit(x)    ((x) & (-x))
24
25 /*
26  * Given a pyro structure, figure out
27  * if the current flight state satisfies all
28  * of the requirements
29  */
30 static uint8_t
31 ao_pyro_ready(struct ao_pyro *pyro)
32 {
33         enum ao_pyro_flag flag, flags;
34
35         flags = pyro->flags;
36         while (flags != ao_pyro_none) {
37                 flag = ao_lowbit(flags);
38                 flags &= ~flag;
39                 switch (flag) {
40
41                 case ao_pyro_accel_less:
42                         if (ao_accel <= pyro->accel_less)
43                                 continue;
44                         break;
45                 case ao_pyro_accel_greater:
46                         if (ao_accel >= pyro->accel_greater)
47                                 continue;
48                         break;
49
50
51                 case ao_pyro_speed_less:
52                         if (ao_speed <= pyro->speed_less)
53                                 continue;
54                         break;
55                 case ao_pyro_speed_greater:
56                         if (ao_speed >= pyro->speed_greater)
57                                 continue;
58                         break;
59
60                 case ao_pyro_height_less:
61                         if (ao_height <= pyro->height_less)
62                                 continue;
63                         break;
64                 case ao_pyro_height_greater:
65                         if (ao_height >= pyro->height_greater)
66                                 continue;
67                         break;
68
69                 case ao_pyro_orient_less:
70 //                      if (ao_orient <= pyro->orient_less)
71                                 continue;
72                         break;
73                 case ao_pyro_orient_greater:
74 //                      if (ao_orient >= pyro->orient_greater)
75                                 continue;
76                         break;
77
78                 case ao_pyro_time_less:
79                         if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
80                                 continue;
81                         break;
82                 case ao_pyro_time_greater:
83                         if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
84                                 continue;
85                         break;
86
87                 case ao_pyro_ascending:
88                         if (ao_speed > 0)
89                                 continue;
90                         break;
91                 case ao_pyro_descending:
92                         if (ao_speed < 0)
93                                 continue;
94                         break;
95
96                 case ao_pyro_after_motor:
97                         if (ao_motor_number == pyro->motor)
98                                 continue;
99                         break;
100
101                 case ao_pyro_delay:
102                         /* handled separately */
103                         continue;
104                         
105                 case ao_pyro_none:
106                         break;
107                 }
108                 return FALSE;
109         }
110         return TRUE;
111 }
112
113 #define ao_pyro_fire_port(port, bit, pin) do {  \
114                 ao_gpio_set(port, bit, pin, 1); \
115                 ao_delay(AO_MS_TO_TICKS(50));   \
116                 ao_gpio_set(port, bit, pin, 0); \
117         } while (0)
118         
119
120 static void
121 ao_pyro_fire(uint8_t p)
122 {
123         switch (p) {
124 #if AO_PYRO_NUM > 0
125         case 0: ao_pyro_fire_port(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0); break;
126 #endif
127 #if AO_PYRO_NUM > 1
128         case 1: ao_pyro_fire_port(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1); break;
129 #endif
130 #if AO_PYRO_NUM > 2
131         case 2: ao_pyro_fire_port(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2); break;
132 #endif
133 #if AO_PYRO_NUM > 3
134         case 3: ao_pyro_fire_port(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3); break;
135 #endif
136 #if AO_PYRO_NUM > 4
137         case 4: ao_pyro_fire_port(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4); break;
138 #endif
139 #if AO_PYRO_NUM > 5
140         case 5: ao_pyro_fire_port(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5); break;
141 #endif
142 #if AO_PYRO_NUM > 6
143         case 6: ao_pyro_fire_port(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6); break;
144 #endif
145 #if AO_PYRO_NUM > 7
146         case 7: ao_pyro_fire_port(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7); break;
147 #endif
148         default: break;
149         }
150         ao_delay(AO_MS_TO_TICKS(50));
151 }
152
153 static void
154 ao_pyro(void)
155 {
156         uint8_t         p;
157         struct ao_pyro  *pyro;
158
159         ao_config_get();
160         while (ao_flight_state < ao_flight_boost)
161                 ao_sleep(&ao_flight_state);
162
163         for (;;) {
164                 ao_delay(AO_MS_TO_TICKS(100));
165                 for (p = 0; p < AO_PYRO_NUM; p++) {
166                         pyro = &ao_config.pyro[p];
167
168                         /* Ignore igniters which have already fired
169                          */
170                         if (pyro->fired)
171                                 continue;
172
173                         /* Ignore disabled igniters
174                          */
175                         if (!pyro->flags)
176                                 continue;
177
178                         /* Check pyro state to see if it shoule fire
179                          */
180                         if (!pyro->delay_done) {
181                                 if (!ao_pyro_ready(pyro))
182                                         continue;
183
184                                 /* If there's a delay set, then remember when
185                                  * it expires
186                                  */
187                                 if (pyro->flags & ao_pyro_delay)
188                                         pyro->delay_done = ao_time() + pyro->delay;
189                         }
190
191                         /* Check to see if we're just waiting for
192                          * the delay to expire
193                          */
194                         if (pyro->delay_done) {
195                                 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
196                                         continue;
197                         }
198
199                         ao_pyro_fire(p);
200                 }
201         }
202 }
203
204 __xdata struct ao_task ao_pyro_task;
205
206 #define NO_VALUE        0xff
207
208 #define AO_PYRO_NAME_LEN        3
209
210 const struct {
211         char                    name[AO_PYRO_NAME_LEN];
212         enum ao_pyro_flag       flag;
213         uint8_t                 offset;
214         char                    *help;
215 } ao_pyro_values[] = {
216         { "a<", ao_pyro_accel_less,     offsetof(struct ao_pyro, accel_less), "accel less (m/ss * 16)" },
217         { "a>", ao_pyro_accel_greater,  offsetof(struct ao_pyro, accel_greater), "accel greater (m/ss * 16)" },
218
219         { "s<", ao_pyro_speed_less,     offsetof(struct ao_pyro, speed_less), "speed less (m/s * 16)" },
220         { "s>", ao_pyro_speed_greater,  offsetof(struct ao_pyro, speed_greater), "speed greater (m/s * 16)" },
221
222         { "h<", ao_pyro_height_less,    offsetof(struct ao_pyro, height_less), "height less (m)" },
223         { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), "height greater (m)" },
224
225         { "o<", ao_pyro_orient_less,    offsetof(struct ao_pyro, orient_less), "orient less (deg)" },
226         { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), "orient greater (deg)"  },
227
228         { "t<", ao_pyro_time_less,      offsetof(struct ao_pyro, time_less), "time less (s * 100)" },
229         { "t>", ao_pyro_time_greater,   offsetof(struct ao_pyro, time_greater), "time greater (s * 100)"  },
230
231         { "A", ao_pyro_ascending,       NO_VALUE, "ascending" },
232         { "D", ao_pyro_descending,      NO_VALUE, "descending" },
233         
234         { "m", ao_pyro_after_motor,     offsetof(struct ao_pyro, motor), "after motor" },
235
236         { "d", ao_pyro_delay,           offsetof(struct ao_pyro, delay), "delay before firing (s * 100)" },
237         { "", ao_pyro_none,             NO_VALUE, NULL },
238 };
239
240 static void
241 ao_pyro_print_name(uint8_t v)
242 {
243         const char *s = ao_pyro_values[v].name;
244         printf ("%s%s", s, "   " + strlen(s));
245 }
246
247 static void
248 ao_pyro_help(void)
249 {
250         uint8_t v;
251         for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
252                 ao_pyro_print_name(v);
253                 if (ao_pyro_values[v].offset != NO_VALUE)
254                         printf ("<n> ");
255                 else
256                         printf ("    ");
257                 printf ("%s\n", ao_pyro_values[v].help);
258         }
259 }
260               
261 void
262 ao_pyro_show(void)
263 {
264         uint8_t         p;
265         uint8_t         v;
266         struct ao_pyro  *pyro;
267
268         for (p = 0; p < AO_PYRO_NUM; p++) {
269                 printf ("Pyro %2d: ", p);
270                 pyro = &ao_config.pyro[p];
271                 if (!pyro->flags) {
272                         printf ("<disabled>\n");
273                         continue;
274                 }
275                 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
276                         if (!(pyro->flags & ao_pyro_values[v].flag))
277                                 continue;
278                         ao_pyro_print_name(v);
279                         if (ao_pyro_values[v].offset != NO_VALUE) {
280                                 int16_t value;
281
282                                 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
283                                 printf ("%6d ", value);
284                         } else {
285                                 printf ("       ");
286                         }
287                 }
288                 printf ("\n");
289         }
290 }
291
292 void
293 ao_pyro_set(void)
294 {
295         uint8_t p;
296         struct ao_pyro pyro_tmp;
297         char    name[AO_PYRO_NAME_LEN];
298         uint8_t c;
299         uint8_t v;
300
301         ao_cmd_white();
302         switch (ao_cmd_lex_c) {
303         case '?':
304                 ao_pyro_help();
305                 return;
306         }
307
308         ao_cmd_decimal();
309         if (ao_cmd_status != ao_cmd_success)
310                 return;
311         p = ao_cmd_lex_i;
312         if (p < 0 || AO_PYRO_NUM <= p) {
313                 printf ("invalid pyro channel %d\n", p);
314                 return;
315         }
316         pyro_tmp.flags = 0;
317         for (;;) {
318                 ao_cmd_white();
319                 if (ao_cmd_lex_c == '\n')
320                         break;
321                 
322                 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
323                         if (ao_cmd_is_white())
324                                 break;
325                         name[c] = ao_cmd_lex_c;
326                         ao_cmd_lex();
327                 }
328                 name[c] = '\0';
329                 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
330                         if (!strcmp (ao_pyro_values[v].name, name))
331                                 break;
332                 }
333                 if (ao_pyro_values[v].flag == ao_pyro_none) {
334                         printf ("invalid pyro field %s\n", name);
335                         ao_cmd_status = ao_cmd_syntax_error;
336                         return;
337                 }
338                 pyro_tmp.flags |= ao_pyro_values[v].flag;
339                 if (ao_pyro_values[v].offset != NO_VALUE) {
340                         ao_cmd_decimal();
341                         if (ao_cmd_status != ao_cmd_success)
342                                 return;
343                         *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
344                 }
345         }
346         _ao_config_edit_start();
347         ao_config.pyro[p] = pyro_tmp;
348         _ao_config_edit_finish();
349 }
350
351 void
352 ao_pyro_init(void)
353 {
354 #if AO_PYRO_NUM > 0
355         ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
356 #endif
357 #if AO_PYRO_NUM > 1
358         ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
359 #endif
360 #if AO_PYRO_NUM > 2
361         ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
362 #endif
363 #if AO_PYRO_NUM > 3
364         ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
365 #endif
366 #if AO_PYRO_NUM > 4
367         ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
368 #endif
369 #if AO_PYRO_NUM > 5
370         ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
371 #endif
372 #if AO_PYRO_NUM > 6
373         ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
374 #endif
375 #if AO_PYRO_NUM > 7
376         ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
377 #endif
378         ao_add_task(&ao_pyro_task, ao_pyro, "pyro");
379 }