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