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