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