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