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