altos: Add gyro-based orientation tracking
[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 #ifndef AO_FLIGHT_TEST
19 #include <ao.h>
20 #include <ao_sample.h>
21 #include <ao_flight.h>
22 #endif
23 #include <ao_pyro.h>
24
25 #if IS_COMPANION
26 #include <ao_companion.h>
27 #define ao_accel ao_companion_command.accel
28 #define ao_speed ao_companion_command.speed
29 #define ao_height ao_companion_command.height
30 #define ao_flight_state ao_companion_command.flight_state
31 #define ao_motor_number ao_companion_command.motor_number
32 #endif
33
34 #define ao_lowbit(x)    ((x) & (-x))
35
36 #ifndef AO_FLIGHT_TEST
37 enum ao_igniter_status
38 ao_pyro_status(uint8_t p)
39 {
40         __xdata struct ao_data packet;
41         __pdata int16_t value;
42
43         ao_arch_critical(
44                 ao_data_get(&packet);
45                 );
46
47         value = (AO_IGNITER_CLOSED>>1);
48         value = AO_SENSE_PYRO(&packet, p);
49         if (value < AO_IGNITER_OPEN)
50                 return ao_igniter_open;
51         else if (value > AO_IGNITER_CLOSED)
52                 return ao_igniter_ready;
53         else
54                 return ao_igniter_unknown;
55 }
56
57 void
58 ao_pyro_print_status(void)
59 {
60         uint8_t p;
61
62         for(p = 0; p < AO_PYRO_NUM; p++) {
63                 enum ao_igniter_status status = ao_pyro_status(p);
64                 printf("Igniter: %d Status: %s\n",
65                        p, ao_igniter_status_names[status]);
66         }
67 }
68 #endif
69
70 uint16_t        ao_pyro_fired;
71
72 /*
73  * Given a pyro structure, figure out
74  * if the current flight state satisfies all
75  * of the requirements
76  */
77 static uint8_t
78 ao_pyro_ready(struct ao_pyro *pyro)
79 {
80         enum ao_pyro_flag flag, flags;
81
82         flags = pyro->flags;
83         while (flags != ao_pyro_none) {
84                 flag = ao_lowbit(flags);
85                 flags &= ~flag;
86                 switch (flag) {
87
88                 case ao_pyro_accel_less:
89                         if (ao_accel <= pyro->accel_less)
90                                 continue;
91                         break;
92                 case ao_pyro_accel_greater:
93                         if (ao_accel >= pyro->accel_greater)
94                                 continue;
95                         break;
96
97
98                 case ao_pyro_speed_less:
99                         if (ao_speed <= pyro->speed_less)
100                                 continue;
101                         break;
102                 case ao_pyro_speed_greater:
103                         if (ao_speed >= pyro->speed_greater)
104                                 continue;
105                         break;
106
107                 case ao_pyro_height_less:
108                         if (ao_height <= pyro->height_less)
109                                 continue;
110                         break;
111                 case ao_pyro_height_greater:
112                         if (ao_height >= pyro->height_greater)
113                                 continue;
114                         break;
115
116 #if HAS_GYRO
117                 case ao_pyro_orient_less:
118                         if (ao_orient <= pyro->orient_less)
119                                 continue;
120                         break;
121                 case ao_pyro_orient_greater:
122                         if (ao_orient >= pyro->orient_greater)
123                                 continue;
124                         break;
125 #endif
126
127                 case ao_pyro_time_less:
128                         if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
129                                 continue;
130                         break;
131                 case ao_pyro_time_greater:
132                         if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
133                                 continue;
134                         break;
135
136                 case ao_pyro_ascending:
137                         if (ao_speed > 0)
138                                 continue;
139                         break;
140                 case ao_pyro_descending:
141                         if (ao_speed < 0)
142                                 continue;
143                         break;
144
145                 case ao_pyro_after_motor:
146                         if (ao_motor_number == pyro->motor)
147                                 continue;
148                         break;
149
150                 case ao_pyro_delay:
151                         /* handled separately */
152                         continue;
153
154                 case ao_pyro_state_less:
155                         if (ao_flight_state < pyro->state_less)
156                                 continue;
157                         break;
158                 case ao_pyro_state_greater_or_equal:
159                         if (ao_flight_state >= pyro->state_greater_or_equal)
160                                 continue;
161                         break;
162
163                 default:
164                         continue;
165                 }
166                 return FALSE;
167         }
168         return TRUE;
169 }
170
171 #ifndef AO_FLIGHT_TEST
172 static void
173 ao_pyro_pin_set(uint8_t p, uint8_t v)
174 {
175         switch (p) {
176 #if AO_PYRO_NUM > 0
177         case 0: ao_gpio_set(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, v); break;
178 #endif
179 #if AO_PYRO_NUM > 1
180         case 1: ao_gpio_set(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, v); break;
181 #endif
182 #if AO_PYRO_NUM > 2
183         case 2: ao_gpio_set(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, v); break;
184 #endif
185 #if AO_PYRO_NUM > 3
186         case 3: ao_gpio_set(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, v); break;
187 #endif
188 #if AO_PYRO_NUM > 4
189         case 4: ao_gpio_set(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, v); break;
190 #endif
191 #if AO_PYRO_NUM > 5
192         case 5: ao_gpio_set(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, v); break;
193 #endif
194 #if AO_PYRO_NUM > 6
195         case 6: ao_gpio_set(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, v); break;
196 #endif
197 #if AO_PYRO_NUM > 7
198         case 7: ao_gpio_set(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, v); break;
199 #endif
200         default: break;
201         }
202 }
203 #endif
204
205 uint8_t ao_pyro_wakeup;
206
207 static void
208 ao_pyro_pins_fire(uint16_t fire)
209 {
210         uint8_t p;
211
212         for (p = 0; p < AO_PYRO_NUM; p++) {
213                 if (fire & (1 << p))
214                         ao_pyro_pin_set(p, 1);
215         }
216         ao_delay(AO_MS_TO_TICKS(50));
217         for (p = 0; p < AO_PYRO_NUM; p++) {
218                 if (fire & (1 << p)) {
219                         ao_pyro_pin_set(p, 0);
220                         ao_config.pyro[p].fired = 1;
221                         ao_pyro_fired |= (1 << p);
222                 }
223         }
224         ao_delay(AO_MS_TO_TICKS(50));
225 }
226
227 static uint8_t
228 ao_pyro_check(void)
229 {
230         struct ao_pyro  *pyro;
231         uint8_t         p, any_waiting;
232         uint16_t        fire = 0;
233         
234         any_waiting = 0;
235         for (p = 0; p < AO_PYRO_NUM; p++) {
236                 pyro = &ao_config.pyro[p];
237
238                 /* Ignore igniters which have already fired
239                  */
240                 if (pyro->fired)
241                         continue;
242
243                 /* Ignore disabled igniters
244                  */
245                 if (!pyro->flags)
246                         continue;
247
248                 any_waiting = 1;
249                 /* Check pyro state to see if it should fire
250                  */
251                 if (!pyro->delay_done) {
252                         if (!ao_pyro_ready(pyro))
253                                 continue;
254
255                         /* If there's a delay set, then remember when
256                          * it expires
257                          */
258                         if (pyro->flags & ao_pyro_delay) {
259                                 pyro->delay_done = ao_time() + pyro->delay;
260                                 if (!pyro->delay_done)
261                                         pyro->delay_done = 1;
262                         }
263                 }
264
265                 /* Check to see if we're just waiting for
266                  * the delay to expire
267                  */
268                 if (pyro->delay_done) {
269                         if ((int16_t) (ao_time() - pyro->delay_done) < 0)
270                                 continue;
271                 }
272
273                 fire |= (1 << p);
274         }
275
276         if (fire)
277                 ao_pyro_pins_fire(fire);
278
279         return any_waiting;
280 }
281
282 #define NO_VALUE        0xff
283
284 #define AO_PYRO_NAME_LEN        3
285
286 #if !DISABLE_HELP
287 #define ENABLE_HELP 1
288 #endif
289
290 #if ENABLE_HELP
291 #define HELP(s) (s)
292 #else
293 #define HELP(s)
294 #endif
295
296 const struct {
297         char                    name[AO_PYRO_NAME_LEN];
298         enum ao_pyro_flag       flag;
299         uint8_t                 offset;
300 #if ENABLE_HELP
301         char                    *help;
302 #endif
303 } ao_pyro_values[] = {
304         { "a<", ao_pyro_accel_less,     offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
305         { "a>", ao_pyro_accel_greater,  offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
306
307         { "s<", ao_pyro_speed_less,     offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
308         { "s>", ao_pyro_speed_greater,  offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
309
310         { "h<", ao_pyro_height_less,    offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
311         { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
312
313 #if HAS_GYRO
314         { "o<", ao_pyro_orient_less,    offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
315         { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)")  },
316 #endif
317
318         { "t<", ao_pyro_time_less,      offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
319         { "t>", ao_pyro_time_greater,   offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)")  },
320
321         { "f<", ao_pyro_state_less,     offsetof(struct ao_pyro, state_less), HELP("state less") },
322         { "f>=",ao_pyro_state_greater_or_equal, offsetof(struct ao_pyro, state_greater_or_equal), HELP("state greater or equal")  },
323
324         { "A", ao_pyro_ascending,       NO_VALUE, HELP("ascending") },
325         { "D", ao_pyro_descending,      NO_VALUE, HELP("descending") },
326
327         { "m", ao_pyro_after_motor,     offsetof(struct ao_pyro, motor), HELP("after motor") },
328
329         { "d", ao_pyro_delay,           offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
330         { "", ao_pyro_none,             NO_VALUE, HELP(NULL) },
331 };
332
333 #define NUM_PYRO_VALUES (sizeof ao_pyro_values / sizeof ao_pyro_values[0])
334
335 #ifndef AO_FLIGHT_TEST
336 static void
337 ao_pyro(void)
338 {
339         uint8_t         any_waiting;
340
341         ao_config_get();
342         while (ao_flight_state < ao_flight_boost)
343                 ao_sleep(&ao_flight_state);
344
345         for (;;) {
346                 ao_alarm(AO_MS_TO_TICKS(100));
347                 ao_sleep(&ao_pyro_wakeup);
348                 ao_clear_alarm();
349                 if (ao_flight_state >= ao_flight_landed)
350                         break;
351                 any_waiting = ao_pyro_check();
352                 if (!any_waiting)
353                         break;
354         }
355         ao_exit();
356 }
357
358 __xdata struct ao_task ao_pyro_task;
359
360
361 static void
362 ao_pyro_print_name(uint8_t v)
363 {
364         const char *s = ao_pyro_values[v].name;
365         printf ("%s%s", s, "   " + strlen(s));
366 }
367
368 #if ENABLE_HELP
369 static void
370 ao_pyro_help(void)
371 {
372         uint8_t v;
373         for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
374                 ao_pyro_print_name(v);
375                 if (ao_pyro_values[v].offset != NO_VALUE)
376                         printf ("<n> ");
377                 else
378                         printf ("    ");
379                 printf ("%s\n", ao_pyro_values[v].help);
380         }
381 }
382 #endif
383
384 void
385 ao_pyro_show(void)
386 {
387         uint8_t         p;
388         uint8_t         v;
389         struct ao_pyro  *pyro;
390
391         printf ("Pyro-count: %d\n", AO_PYRO_NUM);
392         for (p = 0; p < AO_PYRO_NUM; p++) {
393                 printf ("Pyro %2d: ", p);
394                 pyro = &ao_config.pyro[p];
395                 if (!pyro->flags) {
396                         printf ("<disabled>\n");
397                         continue;
398                 }
399                 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
400                         if (!(pyro->flags & ao_pyro_values[v].flag))
401                                 continue;
402                         ao_pyro_print_name(v);
403                         if (ao_pyro_values[v].offset != NO_VALUE) {
404                                 int16_t value;
405
406                                 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
407                                 printf ("%6d ", value);
408                         } else {
409                                 printf ("       ");
410                         }
411                 }
412                 printf ("\n");
413         }
414 }
415
416 void
417 ao_pyro_set(void)
418 {
419         uint8_t p;
420         struct ao_pyro pyro_tmp;
421         char    name[AO_PYRO_NAME_LEN];
422         uint8_t c;
423         uint8_t v;
424
425         ao_cmd_white();
426
427 #if ENABLE_HELP
428         switch (ao_cmd_lex_c) {
429         case '?':
430                 ao_pyro_help();
431                 return;
432         }
433 #endif
434
435         ao_cmd_decimal();
436         if (ao_cmd_status != ao_cmd_success)
437                 return;
438         p = ao_cmd_lex_i;
439         if (p < 0 || AO_PYRO_NUM <= p) {
440                 printf ("invalid pyro channel %d\n", p);
441                 return;
442         }
443         pyro_tmp.flags = 0;
444         for (;;) {
445                 ao_cmd_white();
446                 if (ao_cmd_lex_c == '\n')
447                         break;
448
449                 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
450                         if (ao_cmd_is_white())
451                                 break;
452                         name[c] = ao_cmd_lex_c;
453                         ao_cmd_lex();
454                 }
455                 name[c] = '\0';
456                 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
457                         if (!strcmp (ao_pyro_values[v].name, name))
458                                 break;
459                 }
460                 if (ao_pyro_values[v].flag == ao_pyro_none) {
461                         printf ("invalid pyro field %s\n", name);
462                         ao_cmd_status = ao_cmd_syntax_error;
463                         return;
464                 }
465                 pyro_tmp.flags |= ao_pyro_values[v].flag;
466                 if (ao_pyro_values[v].offset != NO_VALUE) {
467                         ao_cmd_decimal();
468                         if (ao_cmd_status != ao_cmd_success)
469                                 return;
470                         *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
471                 }
472         }
473         _ao_config_edit_start();
474         ao_config.pyro[p] = pyro_tmp;
475         _ao_config_edit_finish();
476 }
477
478 void
479 ao_pyro_manual(uint8_t p)
480 {
481         printf ("ao_pyro_manual %d\n", p);
482         if (p >= AO_PYRO_NUM) {
483                 ao_cmd_status = ao_cmd_syntax_error;
484                 return;
485         }
486         ao_pyro_pins_fire(1 << p);
487 }
488
489 void
490 ao_pyro_init(void)
491 {
492 #if AO_PYRO_NUM > 0
493         ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
494 #endif
495 #if AO_PYRO_NUM > 1
496         ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
497 #endif
498 #if AO_PYRO_NUM > 2
499         ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
500 #endif
501 #if AO_PYRO_NUM > 3
502         ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
503 #endif
504 #if AO_PYRO_NUM > 4
505         ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
506 #endif
507 #if AO_PYRO_NUM > 5
508         ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
509 #endif
510 #if AO_PYRO_NUM > 6
511         ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
512 #endif
513 #if AO_PYRO_NUM > 7
514         ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
515 #endif
516         ao_add_task(&ao_pyro_task, ao_pyro, "pyro");
517 }
518 #endif