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 #ifndef AO_FLIGHT_TEST
37 enum ao_igniter_status
38 ao_pyro_status(uint8_t p)
40 __xdata struct ao_data packet;
41 __pdata int16_t value;
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;
54 return ao_igniter_unknown;
58 ao_pyro_print_status(void)
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]);
70 uint16_t ao_pyro_fired;
77 #define DBG(...) do { printf("\t%d: ", (int) (pyro - ao_config.pyro)); printf(__VA_ARGS__); } while (0)
83 * Given a pyro structure, figure out
84 * if the current flight state satisfies all
88 ao_pyro_ready(struct ao_pyro *pyro)
90 enum ao_pyro_flag flag, flags;
93 while (flags != ao_pyro_none) {
94 flag = ao_lowbit(flags);
98 case ao_pyro_accel_less:
99 if (ao_accel <= pyro->accel_less)
101 DBG("accel %d > %d\n", ao_accel, pyro->accel_less);
103 case ao_pyro_accel_greater:
104 if (ao_accel >= pyro->accel_greater)
106 DBG("accel %d < %d\n", ao_accel, pyro->accel_greater);
108 case ao_pyro_speed_less:
109 if (ao_speed <= pyro->speed_less)
111 DBG("speed %d > %d\n", ao_speed, pyro->speed_less);
113 case ao_pyro_speed_greater:
114 if (ao_speed >= pyro->speed_greater)
116 DBG("speed %d < %d\n", ao_speed, pyro->speed_greater);
118 case ao_pyro_height_less:
119 if (ao_height <= pyro->height_less)
121 DBG("height %d > %d\n", ao_height, pyro->height_less);
123 case ao_pyro_height_greater:
124 if (ao_height >= pyro->height_greater)
126 DBG("height %d < %d\n", ao_height, pyro->height_greater);
130 case ao_pyro_orient_less:
131 if (ao_sample_orient <= pyro->orient_less)
133 DBG("orient %d > %d\n", ao_sample_orient, pyro->orient_less);
135 case ao_pyro_orient_greater:
136 if (ao_sample_orient >= pyro->orient_greater)
138 DBG("orient %d < %d\n", ao_sample_orient, pyro->orient_greater);
142 case ao_pyro_time_less:
143 if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
145 DBG("time %d > %d\n", (int16_t)(ao_time() - ao_boost_tick), pyro->time_less);
147 case ao_pyro_time_greater:
148 if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
150 DBG("time %d < %d\n", (int16_t)(ao_time() - ao_boost_tick), pyro->time_greater);
153 case ao_pyro_ascending:
156 DBG("not ascending speed %d\n", ao_speed);
158 case ao_pyro_descending:
161 DBG("not descending speed %d\n", ao_speed);
164 case ao_pyro_after_motor:
165 if (ao_motor_number == pyro->motor)
167 DBG("motor %d != %d\n", ao_motor_number, pyro->motor);
171 /* handled separately */
174 case ao_pyro_state_less:
175 if (ao_flight_state < pyro->state_less)
177 DBG("state %d >= %d\n", ao_flight_state, pyro->state_less);
179 case ao_pyro_state_greater_or_equal:
180 if (ao_flight_state >= pyro->state_greater_or_equal)
182 DBG("state %d >= %d\n", ao_flight_state, pyro->state_less);
193 #ifndef AO_FLIGHT_TEST
195 ao_pyro_pin_set(uint8_t p, uint8_t v)
199 case 0: ao_gpio_set(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, v); break;
202 case 1: ao_gpio_set(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, v); break;
205 case 2: ao_gpio_set(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, v); break;
208 case 3: ao_gpio_set(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, v); break;
211 case 4: ao_gpio_set(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, v); break;
214 case 5: ao_gpio_set(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, v); break;
217 case 6: ao_gpio_set(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, v); break;
220 case 7: ao_gpio_set(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, v); break;
227 uint8_t ao_pyro_wakeup;
230 ao_pyro_pins_fire(uint16_t fire)
234 for (p = 0; p < AO_PYRO_NUM; p++) {
236 ao_pyro_pin_set(p, 1);
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);
246 ao_delay(AO_MS_TO_TICKS(50));
252 struct ao_pyro *pyro;
253 uint8_t p, any_waiting;
257 for (p = 0; p < AO_PYRO_NUM; p++) {
258 pyro = &ao_config.pyro[p];
260 /* Ignore igniters which have already fired
265 /* Ignore disabled igniters
271 /* Check pyro state to see if it should fire
273 if (!pyro->delay_done) {
274 if (!ao_pyro_ready(pyro))
277 /* If there's a delay set, then remember when
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;
287 /* Check to see if we're just waiting for
288 * the delay to expire
290 if (pyro->delay_done) {
292 /* Check to make sure the required conditions
293 * remain valid. If not, inhibit the channel
294 * by setting the fired bit
296 if (!ao_pyro_ready(pyro)) {
301 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
309 ao_pyro_pins_fire(fire);
314 #define NO_VALUE 0xff
316 #define AO_PYRO_NAME_LEN 4
319 #define ENABLE_HELP 1
329 char name[AO_PYRO_NAME_LEN];
330 enum ao_pyro_flag flag;
335 } ao_pyro_values[] = {
336 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
337 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
339 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
340 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
342 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
343 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
346 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
347 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)") },
350 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
351 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)") },
353 { "f<", ao_pyro_state_less, offsetof(struct ao_pyro, state_less), HELP("state less") },
354 { "f>=",ao_pyro_state_greater_or_equal, offsetof(struct ao_pyro, state_greater_or_equal), HELP("state greater or equal") },
356 { "A", ao_pyro_ascending, NO_VALUE, HELP("ascending") },
357 { "D", ao_pyro_descending, NO_VALUE, HELP("descending") },
359 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), HELP("after motor") },
361 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
362 { "", ao_pyro_none, NO_VALUE, HELP(NULL) },
365 #define NUM_PYRO_VALUES (sizeof ao_pyro_values / sizeof ao_pyro_values[0])
367 #ifndef AO_FLIGHT_TEST
374 while (ao_flight_state < ao_flight_boost)
375 ao_sleep(&ao_flight_state);
378 ao_alarm(AO_MS_TO_TICKS(100));
379 ao_sleep(&ao_pyro_wakeup);
381 if (ao_flight_state >= ao_flight_landed)
383 any_waiting = ao_pyro_check();
390 __xdata struct ao_task ao_pyro_task;
394 ao_pyro_print_name(uint8_t v)
396 const char *s = ao_pyro_values[v].name;
397 printf ("%s%s", s, " " + strlen(s));
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)
411 printf ("%s\n", ao_pyro_values[v].help);
421 struct ao_pyro *pyro;
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];
428 printf ("<disabled>\n");
431 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
432 if (!(pyro->flags & ao_pyro_values[v].flag))
434 ao_pyro_print_name(v);
435 if (ao_pyro_values[v].offset != NO_VALUE) {
438 if (ao_pyro_values[v].flag & AO_PYRO_8_BIT_VALUE)
439 value = *((uint8_t *) ((char *) pyro + ao_pyro_values[v].offset));
441 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
442 printf ("%6d ", value);
455 struct ao_pyro pyro_tmp;
456 char name[AO_PYRO_NAME_LEN];
463 switch (ao_cmd_lex_c) {
471 if (ao_cmd_status != ao_cmd_success)
474 if (AO_PYRO_NUM <= p) {
475 printf ("invalid pyro channel %d\n", p);
478 memset(&pyro_tmp, '\0', sizeof (pyro_tmp));
481 if (ao_cmd_lex_c == '\n')
484 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
485 if (ao_cmd_is_white())
487 name[c] = ao_cmd_lex_c;
491 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
492 if (!strcmp (ao_pyro_values[v].name, name))
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;
500 pyro_tmp.flags |= ao_pyro_values[v].flag;
501 if (ao_pyro_values[v].offset != NO_VALUE) {
502 uint8_t negative = 0;
504 if (ao_cmd_lex_c == '-') {
509 if (ao_cmd_status != ao_cmd_success)
511 if (ao_pyro_values[v].flag & AO_PYRO_8_BIT_VALUE) {
513 ao_cmd_status = ao_cmd_syntax_error;
516 *((uint8_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
519 ao_cmd_lex_i = -ao_cmd_lex_i;
520 *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
524 _ao_config_edit_start();
525 ao_config.pyro[p] = pyro_tmp;
526 _ao_config_edit_finish();
530 ao_pyro_manual(uint8_t p)
532 printf ("ao_pyro_manual %d\n", p);
533 if (p >= AO_PYRO_NUM) {
534 ao_cmd_status = ao_cmd_syntax_error;
537 ao_pyro_pins_fire(1 << p);
544 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
547 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
550 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
553 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
556 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
559 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
562 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
565 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
567 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");