2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 #ifndef AO_FLIGHT_TEST
20 #include <ao_sample.h>
21 #include <ao_flight.h>
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
34 #define ao_lowbit(x) ((x) & (-x))
36 uint16_t ao_pyro_fired;
39 * Given a pyro structure, figure out
40 * if the current flight state satisfies all
44 ao_pyro_ready(struct ao_pyro *pyro)
46 enum ao_pyro_flag flag, flags;
49 while (flags != ao_pyro_none) {
50 flag = ao_lowbit(flags);
54 case ao_pyro_accel_less:
55 if (ao_accel <= pyro->accel_less)
58 case ao_pyro_accel_greater:
59 if (ao_accel >= pyro->accel_greater)
64 case ao_pyro_speed_less:
65 if (ao_speed <= pyro->speed_less)
68 case ao_pyro_speed_greater:
69 if (ao_speed >= pyro->speed_greater)
73 case ao_pyro_height_less:
74 if (ao_height <= pyro->height_less)
77 case ao_pyro_height_greater:
78 if (ao_height >= pyro->height_greater)
83 case ao_pyro_orient_less:
84 if (ao_orient <= pyro->orient_less)
87 case ao_pyro_orient_greater:
88 if (ao_orient >= pyro->orient_greater)
93 case ao_pyro_time_less:
94 if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
97 case ao_pyro_time_greater:
98 if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
102 case ao_pyro_ascending:
106 case ao_pyro_descending:
111 case ao_pyro_after_motor:
112 if (ao_motor_number == pyro->motor)
117 /* handled separately */
120 case ao_pyro_state_less:
121 if (ao_flight_state < pyro->state_less)
124 case ao_pyro_state_greater_or_equal:
125 if (ao_flight_state >= pyro->state_greater_or_equal)
137 #ifndef AO_FLIGHT_TEST
139 ao_pyro_pin_set(uint8_t p, uint8_t v)
143 case 0: ao_gpio_set(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, v); break;
146 case 1: ao_gpio_set(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, v); break;
149 case 2: ao_gpio_set(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, v); break;
152 case 3: ao_gpio_set(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, v); break;
155 case 4: ao_gpio_set(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, v); break;
158 case 5: ao_gpio_set(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, v); break;
161 case 6: ao_gpio_set(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, v); break;
164 case 7: ao_gpio_set(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, v); break;
171 uint8_t ao_pyro_wakeup;
176 struct ao_pyro *pyro;
177 uint8_t p, any_waiting;
181 for (p = 0; p < AO_PYRO_NUM; p++) {
182 pyro = &ao_config.pyro[p];
184 /* Ignore igniters which have already fired
189 /* Ignore disabled igniters
195 /* Check pyro state to see if it should fire
197 if (!pyro->delay_done) {
198 if (!ao_pyro_ready(pyro))
201 /* If there's a delay set, then remember when
204 if (pyro->flags & ao_pyro_delay) {
205 pyro->delay_done = ao_time() + pyro->delay;
206 if (!pyro->delay_done)
207 pyro->delay_done = 1;
211 /* Check to see if we're just waiting for
212 * the delay to expire
214 if (pyro->delay_done) {
215 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
223 for (p = 0; p < AO_PYRO_NUM; p++) {
225 ao_pyro_pin_set(p, 1);
227 ao_delay(AO_MS_TO_TICKS(50));
228 for (p = 0; p < AO_PYRO_NUM; p++) {
229 if (fire & (1 << p)) {
230 ao_pyro_pin_set(p, 0);
231 ao_config.pyro[p].fired = 1;
232 ao_pyro_fired |= (1 << p);
235 ao_delay(AO_MS_TO_TICKS(50));
241 #define NO_VALUE 0xff
243 #define AO_PYRO_NAME_LEN 3
246 #define ENABLE_HELP 1
256 char name[AO_PYRO_NAME_LEN];
257 enum ao_pyro_flag flag;
262 } ao_pyro_values[] = {
263 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
264 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
266 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
267 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
269 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
270 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
273 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
274 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)") },
277 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
278 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)") },
280 { "f<", ao_pyro_state_less, offsetof(struct ao_pyro, state_less), HELP("state less") },
281 { "f>=",ao_pyro_state_greater_or_equal, offsetof(struct ao_pyro, state_greater_or_equal), HELP("state greater or equal") },
283 { "A", ao_pyro_ascending, NO_VALUE, HELP("ascending") },
284 { "D", ao_pyro_descending, NO_VALUE, HELP("descending") },
286 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), HELP("after motor") },
288 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
289 { "", ao_pyro_none, NO_VALUE, HELP(NULL) },
292 #define NUM_PYRO_VALUES (sizeof ao_pyro_values / sizeof ao_pyro_values[0])
294 #ifndef AO_FLIGHT_TEST
301 while (ao_flight_state < ao_flight_boost)
302 ao_sleep(&ao_flight_state);
305 ao_alarm(AO_MS_TO_TICKS(100));
306 ao_sleep(&ao_pyro_wakeup);
308 if (ao_flight_state >= ao_flight_landed)
310 any_waiting = ao_pyro_check();
317 __xdata struct ao_task ao_pyro_task;
321 ao_pyro_print_name(uint8_t v)
323 const char *s = ao_pyro_values[v].name;
324 printf ("%s%s", s, " " + strlen(s));
332 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
333 ao_pyro_print_name(v);
334 if (ao_pyro_values[v].offset != NO_VALUE)
338 printf ("%s\n", ao_pyro_values[v].help);
348 struct ao_pyro *pyro;
350 printf ("Pyro-count: %d\n", AO_PYRO_NUM);
351 for (p = 0; p < AO_PYRO_NUM; p++) {
352 printf ("Pyro %2d: ", p);
353 pyro = &ao_config.pyro[p];
355 printf ("<disabled>\n");
358 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
359 if (!(pyro->flags & ao_pyro_values[v].flag))
361 ao_pyro_print_name(v);
362 if (ao_pyro_values[v].offset != NO_VALUE) {
365 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
366 printf ("%6d ", value);
379 struct ao_pyro pyro_tmp;
380 char name[AO_PYRO_NAME_LEN];
387 switch (ao_cmd_lex_c) {
395 if (ao_cmd_status != ao_cmd_success)
398 if (p < 0 || AO_PYRO_NUM <= p) {
399 printf ("invalid pyro channel %d\n", p);
405 if (ao_cmd_lex_c == '\n')
408 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
409 if (ao_cmd_is_white())
411 name[c] = ao_cmd_lex_c;
415 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
416 if (!strcmp (ao_pyro_values[v].name, name))
419 if (ao_pyro_values[v].flag == ao_pyro_none) {
420 printf ("invalid pyro field %s\n", name);
421 ao_cmd_status = ao_cmd_syntax_error;
424 pyro_tmp.flags |= ao_pyro_values[v].flag;
425 if (ao_pyro_values[v].offset != NO_VALUE) {
427 if (ao_cmd_status != ao_cmd_success)
429 *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
432 _ao_config_edit_start();
433 ao_config.pyro[p] = pyro_tmp;
434 _ao_config_edit_finish();
441 if (!ao_match_word("DoIt"))
445 if (ao_cmd_lex_i < 0 || AO_PYRO_NUM <= ao_cmd_lex_i)
447 ao_pyro_fire(ao_cmd_lex_i);
451 const struct ao_cmds ao_pyro_cmds[] = {
452 { ao_pyro_manual, "P DoIt <n>\0Fire igniter" },
460 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
463 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
466 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
469 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
472 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
475 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
478 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
481 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
483 ao_cmd_register(&ao_pyro_cmds[0]);
484 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");