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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
19 #ifndef AO_FLIGHT_TEST
21 #include <ao_sample.h>
22 #include <ao_flight.h>
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
35 #define ao_lowbit(x) ((x) & (-x))
37 #ifndef AO_FLIGHT_TEST
38 enum ao_igniter_status
39 ao_pyro_status(uint8_t p)
41 struct ao_data packet;
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;
55 return ao_igniter_unknown;
59 ao_pyro_print_status(void)
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]);
71 uint16_t ao_pyro_fired;
72 uint16_t ao_pyro_inhibited;
80 #define DBG(...) do { if (pyro_dbg) { printf("\t%d: ", (int) (pyro - ao_config.pyro)); printf(__VA_ARGS__); } } while (0)
86 ao_sample_max_orient(void)
89 angle_t max = ao_sample_orients[0];
91 for (i = 1; i < AO_NUM_ORIENT; i++) {
92 angle_t a = ao_sample_orients[i];
99 * Given a pyro structure, figure out
100 * if the current flight state satisfies all
101 * of the requirements
104 ao_pyro_ready(const struct ao_pyro *pyro)
106 enum ao_pyro_flag flag, flags;
112 while (flags != ao_pyro_none) {
113 flag = ao_lowbit(flags);
117 case ao_pyro_accel_less:
118 if (ao_accel <= pyro->accel_less)
120 DBG("accel %d > %d\n", ao_accel, pyro->accel_less);
122 case ao_pyro_accel_greater:
123 if (ao_accel >= pyro->accel_greater)
125 DBG("accel %d < %d\n", ao_accel, pyro->accel_greater);
127 case ao_pyro_speed_less:
128 if (ao_speed <= pyro->speed_less)
130 DBG("speed %d > %d\n", ao_speed, pyro->speed_less);
132 case ao_pyro_speed_greater:
133 if (ao_speed >= pyro->speed_greater)
135 DBG("speed %d < %d\n", ao_speed, pyro->speed_greater);
137 case ao_pyro_height_less:
138 if (ao_height <= pyro->height_less)
140 DBG("height %d > %d\n", ao_height, pyro->height_less);
142 case ao_pyro_height_greater:
143 if (ao_height >= pyro->height_greater)
145 DBG("height %d < %d\n", ao_height, pyro->height_greater);
149 case ao_pyro_orient_less:
150 max_orient = ao_sample_max_orient();
151 if (max_orient <= pyro->orient_less)
153 DBG("orient %d > %d\n", max_orient, pyro->orient_less);
155 case ao_pyro_orient_greater:
156 max_orient = ao_sample_max_orient();
157 if (max_orient >= pyro->orient_greater)
159 DBG("orient %d < %d\n", max_orient, pyro->orient_greater);
163 case ao_pyro_time_less:
164 if ((AO_TICK_SIGNED) (ao_time() - ao_launch_tick) <= pyro->time_less)
166 DBG("time %d > %d\n", (AO_TICK_SIGNED)(ao_time() - ao_launch_tick), pyro->time_less);
168 case ao_pyro_time_greater:
169 if ((AO_TICK_SIGNED) (ao_time() - ao_launch_tick) >= pyro->time_greater)
171 DBG("time %d < %d\n", (AO_TICK_SIGNED)(ao_time() - ao_launch_tick), pyro->time_greater);
174 case ao_pyro_ascending:
177 DBG("not ascending speed %d\n", ao_speed);
179 case ao_pyro_descending:
182 DBG("not descending speed %d\n", ao_speed);
185 case ao_pyro_after_motor:
186 if (ao_motor_number >= pyro->motor)
188 DBG("motor %d != %d\n", ao_motor_number, pyro->motor);
192 /* handled separately */
195 case ao_pyro_state_less:
196 if (ao_flight_state < pyro->state_less)
198 DBG("state %d >= %d\n", ao_flight_state, pyro->state_less);
200 case ao_pyro_state_greater_or_equal:
201 if (ao_flight_state >= pyro->state_greater_or_equal)
203 DBG("state %d < %d\n", ao_flight_state, pyro->state_greater_or_equal);
214 #ifndef AO_FLIGHT_TEST
216 ao_pyro_pin_set(uint8_t p, uint8_t v)
220 case 0: ao_gpio_set(AO_PYRO_PORT_0, AO_PYRO_PIN_0, v); break;
223 case 1: ao_gpio_set(AO_PYRO_PORT_1, AO_PYRO_PIN_1, v); break;
226 case 2: ao_gpio_set(AO_PYRO_PORT_2, AO_PYRO_PIN_2, v); break;
229 case 3: ao_gpio_set(AO_PYRO_PORT_3, AO_PYRO_PIN_3, v); break;
232 case 4: ao_gpio_set(AO_PYRO_PORT_4, AO_PYRO_PIN_4, v); break;
235 case 5: ao_gpio_set(AO_PYRO_PORT_5, AO_PYRO_PIN_5, v); break;
238 case 6: ao_gpio_set(AO_PYRO_PORT_6, AO_PYRO_PIN_6, v); break;
241 case 7: ao_gpio_set(AO_PYRO_PORT_7, AO_PYRO_PIN_7, v); break;
248 uint8_t ao_pyro_wakeup;
251 ao_pyro_pins_fire(uint16_t fire)
255 for (p = 0; p < AO_PYRO_NUM; p++) {
257 ao_pyro_pin_set(p, 1);
259 ao_delay(ao_config.pyro_time);
260 for (p = 0; p < AO_PYRO_NUM; p++) {
262 ao_pyro_pin_set(p, 0);
264 ao_delay(AO_MS_TO_TICKS(50));
267 static AO_TICK_TYPE pyro_delay_done[AO_PYRO_NUM];
272 const struct ao_pyro *pyro;
273 uint8_t p, any_waiting;
277 for (p = 0; p < AO_PYRO_NUM; p++) {
278 pyro = &ao_config.pyro[p];
280 /* Ignore igniters which have already fired or inhibited
282 if ((ao_pyro_fired|ao_pyro_inhibited) & (1 << p))
285 /* Ignore disabled igniters
291 /* Check pyro state to see if it should fire
293 if (!pyro_delay_done[p]) {
294 if (!ao_pyro_ready(pyro))
297 /* If there's a delay set, then remember when
300 if (pyro->flags & ao_pyro_delay) {
301 AO_TICK_TYPE delay_done;
302 AO_TICK_SIGNED delay = pyro->delay;
305 delay_done = ao_time() + (AO_TICK_TYPE) delay;
308 * delay_done is the time at which the
309 * delay ends, but it is also used as
310 * an indication that a delay is
311 * active -- non-zero values indicate
312 * an active delay. This code ensures
313 * the value is non-zero, which might
314 * introduce an additional tick
319 pyro_delay_done[p] = delay_done;
323 /* Check to see if we're just waiting for
324 * the delay to expire
326 if (pyro_delay_done[p] != 0) {
328 /* Check to make sure the required conditions
329 * remain valid. If not, inhibit the channel
330 * by setting the inhibited bit
332 if (!ao_pyro_ready(pyro)) {
333 ao_pyro_inhibited |= (uint16_t) (1 << p);
337 if ((AO_TICK_SIGNED) (ao_time() - pyro_delay_done[p]) < 0)
341 fire |= (uint16_t) (1U << p);
345 ao_pyro_fired |= fire;
346 ao_pyro_pins_fire(fire);
352 #define NO_VALUE 0xff
354 #define AO_PYRO_NAME_LEN 4
357 #define ENABLE_HELP 1
367 char name[AO_PYRO_NAME_LEN];
368 enum ao_pyro_flag flag;
373 } ao_pyro_values[] = {
374 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
375 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
377 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
378 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
380 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
381 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
384 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
385 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)") },
388 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
389 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)") },
391 { "f<", ao_pyro_state_less, offsetof(struct ao_pyro, state_less), HELP("state less") },
392 { "f>=",ao_pyro_state_greater_or_equal, offsetof(struct ao_pyro, state_greater_or_equal), HELP("state greater or equal") },
394 { "A", ao_pyro_ascending, NO_VALUE, HELP("ascending") },
395 { "D", ao_pyro_descending, NO_VALUE, HELP("descending") },
397 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), HELP("after motor") },
399 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
400 { "", ao_pyro_none, NO_VALUE, HELP(NULL) },
403 #define NUM_PYRO_VALUES (sizeof ao_pyro_values / sizeof ao_pyro_values[0])
405 #ifndef AO_FLIGHT_TEST
412 while (ao_flight_state < ao_flight_boost)
413 ao_sleep(&ao_flight_state);
416 ao_sleep_for(&ao_pyro_wakeup, AO_MS_TO_TICKS(100));
417 if (ao_flight_state >= ao_flight_landed)
419 any_waiting = ao_pyro_check();
426 struct ao_task ao_pyro_task;
430 ao_pyro_print_name(uint8_t v)
432 const char *s = ao_pyro_values[v].name;
433 printf ("%s%s", s, " " + strlen(s));
441 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
442 ao_pyro_print_name(v);
443 if (ao_pyro_values[v].offset != NO_VALUE)
447 printf ("%s\n", ao_pyro_values[v].help);
453 ao_pyro_get(void *base, uint8_t offset, uint8_t size)
458 value = *((uint8_t *) ((char *) base + offset));
462 value = *((int16_t *) (void *) ((char *) base + offset));
465 value = *((int32_t *) (void *) ((char *) base + offset));
472 ao_pyro_put(void *base, uint8_t offset, uint8_t size, int32_t value)
478 *((uint8_t *) ((char *) base + offset)) = (uint8_t) value;
482 *((int16_t *) (void *) ((char *) base + offset)) = (int16_t) value;
485 *((int32_t *) (void *) ((char *) base + offset)) = value;
492 ao_pyro_size(enum ao_pyro_flag flag)
494 if (flag & AO_PYRO_8_BIT_VALUE)
496 if (flag & AO_PYRO_32_BIT_VALUE)
506 struct ao_pyro *pyro;
508 printf ("Pyro-count: %d\n", AO_PYRO_NUM);
509 for (p = 0; p < AO_PYRO_NUM; p++) {
510 printf ("Pyro %2d: ", p);
511 pyro = &ao_config.pyro[p];
513 printf ("<disabled>\n");
516 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
517 if (!(pyro->flags & ao_pyro_values[v].flag))
519 ao_pyro_print_name(v);
520 if (ao_pyro_values[v].offset != NO_VALUE) {
522 (long) ao_pyro_get(pyro,
523 ao_pyro_values[v].offset,
524 ao_pyro_size(ao_pyro_values[v].flag)));
537 struct ao_pyro pyro_tmp;
538 char name[AO_PYRO_NAME_LEN];
545 switch (ao_cmd_lex_c) {
552 p = ao_cmd_decimal();
553 if (ao_cmd_status != ao_cmd_success)
555 if (AO_PYRO_NUM <= p) {
556 printf ("invalid pyro channel %lu\n", p);
559 memset(&pyro_tmp, '\0', sizeof (pyro_tmp));
562 if (ao_cmd_lex_c == '\n')
565 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
566 if (ao_cmd_is_white() || ao_cmd_lex_c == '\n')
568 name[c] = ao_cmd_lex_c;
572 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
573 if (!strcmp (ao_pyro_values[v].name, name))
576 if (ao_pyro_values[v].flag == ao_pyro_none) {
577 printf ("invalid pyro field %s\n", name);
578 ao_cmd_status = ao_cmd_syntax_error;
581 pyro_tmp.flags |= ao_pyro_values[v].flag;
582 if (ao_pyro_values[v].offset != NO_VALUE) {
585 if (ao_cmd_lex_c == '-') {
589 r *= (int32_t) ao_cmd_decimal();
590 if (ao_cmd_status != ao_cmd_success)
592 if (!ao_pyro_put(&pyro_tmp, ao_pyro_values[v].offset,
593 ao_pyro_size(ao_pyro_values[v].flag), r))
595 ao_cmd_status = ao_cmd_syntax_error;
600 _ao_config_edit_start();
601 ao_config.pyro[p] = pyro_tmp;
602 _ao_config_edit_finish();
606 ao_pyro_manual(uint8_t p)
608 printf ("ao_pyro_manual %d\n", p);
609 if (p >= AO_PYRO_NUM) {
610 ao_cmd_status = ao_cmd_syntax_error;
613 ao_pyro_pins_fire(1 << p);
616 struct ao_pyro_old_values {
617 enum ao_pyro_flag flag;
622 static const struct ao_pyro_old_values ao_pyro_1_24_values[] = {
623 { .flag = ao_pyro_accel_less, .offset = offsetof(struct ao_pyro_1_24, accel_less), 16 },
624 { .flag = ao_pyro_accel_greater, .offset = offsetof(struct ao_pyro_1_24, accel_greater), 16 },
625 { .flag = ao_pyro_speed_less, .offset = offsetof(struct ao_pyro_1_24, speed_less), 16 },
626 { .flag = ao_pyro_speed_greater, .offset = offsetof(struct ao_pyro_1_24, speed_greater), 16 },
627 { .flag = ao_pyro_height_less, .offset = offsetof(struct ao_pyro_1_24, height_less), 16 },
628 { .flag = ao_pyro_height_greater, .offset = offsetof(struct ao_pyro_1_24, height_greater), 16 },
629 { .flag = ao_pyro_orient_less, .offset = offsetof(struct ao_pyro_1_24, orient_less), 16 },
630 { .flag = ao_pyro_orient_greater, .offset = offsetof(struct ao_pyro_1_24, orient_greater), 16 },
631 { .flag = ao_pyro_time_less, .offset = offsetof(struct ao_pyro_1_24, time_less), 16 },
632 { .flag = ao_pyro_time_greater, .offset = offsetof(struct ao_pyro_1_24, time_greater), 16 },
633 { .flag = ao_pyro_delay, .offset = offsetof(struct ao_pyro_1_24, delay), 16 },
634 { .flag = ao_pyro_state_less, .offset = offsetof(struct ao_pyro_1_24, state_less), 8 },
635 { .flag = ao_pyro_state_greater_or_equal, .offset = offsetof(struct ao_pyro_1_24, state_greater_or_equal), 8 },
636 { .flag = ao_pyro_after_motor, .offset = offsetof(struct ao_pyro_1_24, motor), 16 },
639 #define NUM_PYRO_1_24_VALUES (sizeof ao_pyro_1_24_values / sizeof ao_pyro_1_24_values[0])
642 ao_pyro_get_1_24(void *base, enum ao_pyro_flag flag)
646 for (v = 0; v < NUM_PYRO_1_24_VALUES; v++) {
647 if (ao_pyro_1_24_values[v].flag == flag)
648 return ao_pyro_get(base, ao_pyro_1_24_values[v].offset, ao_pyro_1_24_values[v].size);
654 ao_pyro_update_version(void)
656 if (ao_config.minor <= 24)
659 /* First, move all of the config bits that follow the pyro data */
661 struct ao_config_1_24 *ao_config_1_24 = (void *) &ao_config;
662 struct ao_pyro *pyro_1_25 = &ao_config.pyro[0];
663 struct ao_pyro_1_24 *pyro_1_24 = &(ao_config_1_24->pyro)[0];
666 char *pyro_base_1_25 = (void *) pyro_1_25;
667 char *pyro_base_1_24 = (void *) pyro_1_24;
668 char *after_pyro_1_25 = pyro_base_1_25 + AO_PYRO_NUM * sizeof (struct ao_pyro);
669 char *after_pyro_1_24 = pyro_base_1_24 + AO_PYRO_NUM * sizeof (struct ao_pyro_1_24);
671 char *config_end_1_25 = (void *) (&ao_config + 1);
672 size_t to_move = (size_t) (config_end_1_25 - after_pyro_1_25);
674 memmove(after_pyro_1_25, after_pyro_1_24, to_move);
676 /* Now, adjust all of the pyro entries */
680 /* New struct is larger than the old, so start at the
681 * last one and work towards the first
688 memset(&tmp, '\0', sizeof(tmp));
689 tmp.flags = pyro_1_24[p].flags;
691 for (v = 0; v < NUM_PYRO_VALUES; v++)
693 value = ao_pyro_get_1_24(&pyro_1_24[p], ao_pyro_values[v].flag);
694 ao_pyro_put(&tmp, ao_pyro_values[v].offset,
695 ao_pyro_size(ao_pyro_values[v].flag), value);
697 memcpy(&pyro_1_25[p], &tmp, sizeof(tmp));
706 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, 0);
709 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, 0);
712 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, 0);
715 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, 0);
718 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, 0);
721 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, 0);
724 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, 0);
727 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, 0);
729 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");