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_SENSE_PYRO(&packet, p);
49 return ao_igniter_check(value, AO_SENSE_PBATT(&packet));
53 ao_pyro_print_status(void)
57 for(p = 0; p < AO_PYRO_NUM; p++) {
58 enum ao_igniter_status status = ao_pyro_status(p);
59 printf("Igniter: %d Status: %s\n",
60 p, ao_igniter_status_names[status]);
65 uint16_t ao_pyro_fired;
66 uint16_t ao_pyro_inhibited;
74 #define DBG(...) do { if (pyro_dbg) { printf("\t%d: ", (int) (pyro - ao_config.pyro)); printf(__VA_ARGS__); } } while (0)
80 ao_sample_max_orient(void)
83 angle_t max = ao_sample_orients[0];
85 for (i = 1; i < AO_NUM_ORIENT; i++) {
86 angle_t a = ao_sample_orients[i];
93 * Given a pyro structure, figure out
94 * if the current flight state satisfies all
98 ao_pyro_ready(const struct ao_pyro *pyro)
100 enum ao_pyro_flag flag, flags;
106 while (flags != ao_pyro_none) {
107 flag = ao_lowbit(flags);
111 case ao_pyro_accel_less:
112 if (ao_accel <= pyro->accel_less)
114 DBG("accel %d > %d\n", ao_accel, pyro->accel_less);
116 case ao_pyro_accel_greater:
117 if (ao_accel >= pyro->accel_greater)
119 DBG("accel %d < %d\n", ao_accel, pyro->accel_greater);
121 case ao_pyro_speed_less:
122 if (ao_speed <= pyro->speed_less)
124 DBG("speed %d > %d\n", ao_speed, pyro->speed_less);
126 case ao_pyro_speed_greater:
127 if (ao_speed >= pyro->speed_greater)
129 DBG("speed %d < %d\n", ao_speed, pyro->speed_greater);
131 case ao_pyro_height_less:
132 if (ao_height <= pyro->height_less)
134 DBG("height %d > %d\n", ao_height, pyro->height_less);
136 case ao_pyro_height_greater:
137 if (ao_height >= pyro->height_greater)
139 DBG("height %d < %d\n", ao_height, pyro->height_greater);
143 case ao_pyro_orient_less:
144 max_orient = ao_sample_max_orient();
145 if (max_orient <= pyro->orient_less)
147 DBG("orient %d > %d\n", max_orient, pyro->orient_less);
149 case ao_pyro_orient_greater:
150 max_orient = ao_sample_max_orient();
151 if (max_orient >= pyro->orient_greater)
153 DBG("orient %d < %d\n", max_orient, pyro->orient_greater);
157 case ao_pyro_time_less:
158 if ((AO_TICK_SIGNED) (ao_time() - ao_launch_tick) <= pyro->time_less)
160 DBG("time %d > %d\n", (AO_TICK_SIGNED)(ao_time() - ao_launch_tick), pyro->time_less);
162 case ao_pyro_time_greater:
163 if ((AO_TICK_SIGNED) (ao_time() - ao_launch_tick) >= pyro->time_greater)
165 DBG("time %d < %d\n", (AO_TICK_SIGNED)(ao_time() - ao_launch_tick), pyro->time_greater);
168 case ao_pyro_ascending:
171 DBG("not ascending speed %d\n", ao_speed);
173 case ao_pyro_descending:
176 DBG("not descending speed %d\n", ao_speed);
179 case ao_pyro_after_motor:
180 if (ao_motor_number >= pyro->motor)
182 DBG("motor %d != %d\n", ao_motor_number, pyro->motor);
186 /* handled separately */
189 case ao_pyro_state_less:
190 if (ao_flight_state < pyro->state_less)
192 DBG("state %d >= %d\n", ao_flight_state, pyro->state_less);
194 case ao_pyro_state_greater_or_equal:
195 if (ao_flight_state >= pyro->state_greater_or_equal)
197 DBG("state %d < %d\n", ao_flight_state, pyro->state_greater_or_equal);
208 #ifndef AO_FLIGHT_TEST
210 ao_pyro_pin_set(uint8_t p, uint8_t v)
214 case 0: ao_gpio_set(AO_PYRO_PORT_0, AO_PYRO_PIN_0, v); break;
217 case 1: ao_gpio_set(AO_PYRO_PORT_1, AO_PYRO_PIN_1, v); break;
220 case 2: ao_gpio_set(AO_PYRO_PORT_2, AO_PYRO_PIN_2, v); break;
223 case 3: ao_gpio_set(AO_PYRO_PORT_3, AO_PYRO_PIN_3, v); break;
226 case 4: ao_gpio_set(AO_PYRO_PORT_4, AO_PYRO_PIN_4, v); break;
229 case 5: ao_gpio_set(AO_PYRO_PORT_5, AO_PYRO_PIN_5, v); break;
232 case 6: ao_gpio_set(AO_PYRO_PORT_6, AO_PYRO_PIN_6, v); break;
235 case 7: ao_gpio_set(AO_PYRO_PORT_7, AO_PYRO_PIN_7, v); break;
242 uint8_t ao_pyro_wakeup;
245 ao_pyro_pins_fire(uint16_t fire)
249 for (p = 0; p < AO_PYRO_NUM; p++) {
251 ao_pyro_pin_set(p, 1);
253 ao_delay(ao_config.pyro_time);
254 for (p = 0; p < AO_PYRO_NUM; p++) {
256 ao_pyro_pin_set(p, 0);
258 ao_delay(AO_MS_TO_TICKS(50));
261 static AO_TICK_TYPE pyro_delay_done[AO_PYRO_NUM];
266 const struct ao_pyro *pyro;
267 uint8_t p, any_waiting;
271 for (p = 0; p < AO_PYRO_NUM; p++) {
272 pyro = &ao_config.pyro[p];
274 /* Ignore igniters which have already fired or inhibited
276 if ((ao_pyro_fired|ao_pyro_inhibited) & (1 << p))
279 /* Ignore disabled igniters
285 /* Check pyro state to see if it should fire
287 if (!pyro_delay_done[p]) {
288 if (!ao_pyro_ready(pyro))
291 /* If there's a delay set, then remember when
294 if (pyro->flags & ao_pyro_delay) {
295 AO_TICK_TYPE delay_done;
296 AO_TICK_SIGNED delay = pyro->delay;
299 delay_done = ao_time() + (AO_TICK_TYPE) delay;
302 * delay_done is the time at which the
303 * delay ends, but it is also used as
304 * an indication that a delay is
305 * active -- non-zero values indicate
306 * an active delay. This code ensures
307 * the value is non-zero, which might
308 * introduce an additional tick
313 pyro_delay_done[p] = delay_done;
317 /* Check to see if we're just waiting for
318 * the delay to expire
320 if (pyro_delay_done[p] != 0) {
322 /* Check to make sure the required conditions
323 * remain valid. If not, inhibit the channel
324 * by setting the inhibited bit
326 if (!ao_pyro_ready(pyro)) {
327 ao_pyro_inhibited |= (uint16_t) (1 << p);
331 if ((AO_TICK_SIGNED) (ao_time() - pyro_delay_done[p]) < 0)
335 fire |= (uint16_t) (1U << p);
339 ao_pyro_fired |= fire;
340 ao_pyro_pins_fire(fire);
346 #define NO_VALUE 0xff
348 #define AO_PYRO_NAME_LEN 4
351 #define ENABLE_HELP 1
361 char name[AO_PYRO_NAME_LEN];
362 enum ao_pyro_flag flag;
367 } ao_pyro_values[] = {
368 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
369 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
371 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
372 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
374 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
375 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
378 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
379 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)") },
382 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
383 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)") },
385 { "f<", ao_pyro_state_less, offsetof(struct ao_pyro, state_less), HELP("state less") },
386 { "f>=",ao_pyro_state_greater_or_equal, offsetof(struct ao_pyro, state_greater_or_equal), HELP("state greater or equal") },
388 { "A", ao_pyro_ascending, NO_VALUE, HELP("ascending") },
389 { "D", ao_pyro_descending, NO_VALUE, HELP("descending") },
391 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), HELP("after motor") },
393 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
394 { "", ao_pyro_none, NO_VALUE, HELP(NULL) },
397 #define NUM_PYRO_VALUES (sizeof ao_pyro_values / sizeof ao_pyro_values[0])
399 #ifndef AO_FLIGHT_TEST
406 while (ao_flight_state < ao_flight_boost)
407 ao_sleep(&ao_flight_state);
410 ao_sleep_for(&ao_pyro_wakeup, AO_MS_TO_TICKS(100));
411 if (ao_flight_state >= ao_flight_landed)
413 any_waiting = ao_pyro_check();
420 struct ao_task ao_pyro_task;
424 ao_pyro_print_name(uint8_t v)
426 const char *s = ao_pyro_values[v].name;
427 printf ("%s%s", s, " " + strlen(s));
435 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
436 ao_pyro_print_name(v);
437 if (ao_pyro_values[v].offset != NO_VALUE)
441 printf ("%s\n", ao_pyro_values[v].help);
447 ao_pyro_get(void *base, uint8_t offset, uint8_t size)
452 value = *((uint8_t *) ((char *) base + offset));
456 value = *((int16_t *) (void *) ((char *) base + offset));
459 value = *((int32_t *) (void *) ((char *) base + offset));
466 ao_pyro_put(void *base, uint8_t offset, uint8_t size, int32_t value)
472 *((uint8_t *) ((char *) base + offset)) = (uint8_t) value;
476 *((int16_t *) (void *) ((char *) base + offset)) = (int16_t) value;
479 *((int32_t *) (void *) ((char *) base + offset)) = value;
486 ao_pyro_size(enum ao_pyro_flag flag)
488 if (flag & AO_PYRO_8_BIT_VALUE)
490 if (flag & AO_PYRO_32_BIT_VALUE)
500 struct ao_pyro *pyro;
502 printf ("Pyro-count: %d\n", AO_PYRO_NUM);
503 for (p = 0; p < AO_PYRO_NUM; p++) {
504 printf ("Pyro %2d: ", p);
505 pyro = &ao_config.pyro[p];
507 printf ("<disabled>\n");
510 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
511 if (!(pyro->flags & ao_pyro_values[v].flag))
513 ao_pyro_print_name(v);
514 if (ao_pyro_values[v].offset != NO_VALUE) {
516 (long) ao_pyro_get(pyro,
517 ao_pyro_values[v].offset,
518 ao_pyro_size(ao_pyro_values[v].flag)));
531 struct ao_pyro pyro_tmp;
532 char name[AO_PYRO_NAME_LEN];
539 switch (ao_cmd_lex_c) {
546 p = ao_cmd_decimal();
547 if (ao_cmd_status != ao_cmd_success)
549 if (AO_PYRO_NUM <= p) {
550 printf ("invalid pyro channel %lu\n", p);
553 memset(&pyro_tmp, '\0', sizeof (pyro_tmp));
556 if (ao_cmd_lex_c == '\n')
559 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
560 if (ao_cmd_is_white() || ao_cmd_lex_c == '\n')
562 name[c] = ao_cmd_lex_c;
566 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
567 if (!strcmp (ao_pyro_values[v].name, name))
570 if (ao_pyro_values[v].flag == ao_pyro_none) {
571 printf ("invalid pyro field %s\n", name);
572 ao_cmd_status = ao_cmd_syntax_error;
575 pyro_tmp.flags |= ao_pyro_values[v].flag;
576 if (ao_pyro_values[v].offset != NO_VALUE) {
579 if (ao_cmd_lex_c == '-') {
583 r *= (int32_t) ao_cmd_decimal();
584 if (ao_cmd_status != ao_cmd_success)
586 if (!ao_pyro_put(&pyro_tmp, ao_pyro_values[v].offset,
587 ao_pyro_size(ao_pyro_values[v].flag), r))
589 ao_cmd_status = ao_cmd_syntax_error;
594 _ao_config_edit_start();
595 ao_config.pyro[p] = pyro_tmp;
596 _ao_config_edit_finish();
600 ao_pyro_manual(uint8_t p)
602 printf ("ao_pyro_manual %d\n", p);
603 if (p >= AO_PYRO_NUM) {
604 ao_cmd_status = ao_cmd_syntax_error;
607 ao_pyro_pins_fire(1 << p);
610 struct ao_pyro_old_values {
611 enum ao_pyro_flag flag;
616 static const struct ao_pyro_old_values ao_pyro_1_24_values[] = {
617 { .flag = ao_pyro_accel_less, .offset = offsetof(struct ao_pyro_1_24, accel_less), 16 },
618 { .flag = ao_pyro_accel_greater, .offset = offsetof(struct ao_pyro_1_24, accel_greater), 16 },
619 { .flag = ao_pyro_speed_less, .offset = offsetof(struct ao_pyro_1_24, speed_less), 16 },
620 { .flag = ao_pyro_speed_greater, .offset = offsetof(struct ao_pyro_1_24, speed_greater), 16 },
621 { .flag = ao_pyro_height_less, .offset = offsetof(struct ao_pyro_1_24, height_less), 16 },
622 { .flag = ao_pyro_height_greater, .offset = offsetof(struct ao_pyro_1_24, height_greater), 16 },
623 { .flag = ao_pyro_orient_less, .offset = offsetof(struct ao_pyro_1_24, orient_less), 16 },
624 { .flag = ao_pyro_orient_greater, .offset = offsetof(struct ao_pyro_1_24, orient_greater), 16 },
625 { .flag = ao_pyro_time_less, .offset = offsetof(struct ao_pyro_1_24, time_less), 16 },
626 { .flag = ao_pyro_time_greater, .offset = offsetof(struct ao_pyro_1_24, time_greater), 16 },
627 { .flag = ao_pyro_delay, .offset = offsetof(struct ao_pyro_1_24, delay), 16 },
628 { .flag = ao_pyro_state_less, .offset = offsetof(struct ao_pyro_1_24, state_less), 8 },
629 { .flag = ao_pyro_state_greater_or_equal, .offset = offsetof(struct ao_pyro_1_24, state_greater_or_equal), 8 },
630 { .flag = ao_pyro_after_motor, .offset = offsetof(struct ao_pyro_1_24, motor), 16 },
633 #define NUM_PYRO_1_24_VALUES (sizeof ao_pyro_1_24_values / sizeof ao_pyro_1_24_values[0])
636 ao_pyro_get_1_24(void *base, enum ao_pyro_flag flag)
640 for (v = 0; v < NUM_PYRO_1_24_VALUES; v++) {
641 if (ao_pyro_1_24_values[v].flag == flag)
642 return ao_pyro_get(base, ao_pyro_1_24_values[v].offset, ao_pyro_1_24_values[v].size);
648 ao_pyro_update_version(void)
650 if (ao_config.minor <= 24)
653 /* First, move all of the config bits that follow the pyro data */
655 struct ao_config_1_24 *ao_config_1_24 = (void *) &ao_config;
656 struct ao_pyro *pyro_1_25 = &ao_config.pyro[0];
657 struct ao_pyro_1_24 *pyro_1_24 = &(ao_config_1_24->pyro)[0];
660 char *pyro_base_1_25 = (void *) pyro_1_25;
661 char *pyro_base_1_24 = (void *) pyro_1_24;
662 char *after_pyro_1_25 = pyro_base_1_25 + AO_PYRO_NUM * sizeof (struct ao_pyro);
663 char *after_pyro_1_24 = pyro_base_1_24 + AO_PYRO_NUM * sizeof (struct ao_pyro_1_24);
665 char *config_end_1_25 = (void *) (&ao_config + 1);
666 size_t to_move = (size_t) (config_end_1_25 - after_pyro_1_25);
668 memmove(after_pyro_1_25, after_pyro_1_24, to_move);
670 /* Now, adjust all of the pyro entries */
674 /* New struct is larger than the old, so start at the
675 * last one and work towards the first
682 memset(&tmp, '\0', sizeof(tmp));
683 tmp.flags = pyro_1_24[p].flags;
685 for (v = 0; v < NUM_PYRO_VALUES; v++)
687 if (ao_pyro_values[v].offset != NO_VALUE) {
688 value = ao_pyro_get_1_24(&pyro_1_24[p], ao_pyro_values[v].flag);
689 ao_pyro_put(&tmp, ao_pyro_values[v].offset,
690 ao_pyro_size(ao_pyro_values[v].flag), value);
693 memcpy(&pyro_1_25[p], &tmp, sizeof(tmp));
702 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, 0);
705 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, 0);
708 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, 0);
711 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, 0);
714 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, 0);
717 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, 0);
720 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, 0);
723 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, 0);
725 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");