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))
37 * Given a pyro structure, figure out
38 * if the current flight state satisfies all
42 ao_pyro_ready(struct ao_pyro *pyro)
44 enum ao_pyro_flag flag, flags;
47 while (flags != ao_pyro_none) {
48 flag = ao_lowbit(flags);
52 case ao_pyro_accel_less:
53 if (ao_accel <= pyro->accel_less)
56 case ao_pyro_accel_greater:
57 if (ao_accel >= pyro->accel_greater)
62 case ao_pyro_speed_less:
63 if (ao_speed <= pyro->speed_less)
66 case ao_pyro_speed_greater:
67 if (ao_speed >= pyro->speed_greater)
71 case ao_pyro_height_less:
72 if (ao_height <= pyro->height_less)
75 case ao_pyro_height_greater:
76 if (ao_height >= pyro->height_greater)
81 case ao_pyro_orient_less:
82 if (ao_orient <= pyro->orient_less)
85 case ao_pyro_orient_greater:
86 if (ao_orient >= pyro->orient_greater)
91 case ao_pyro_time_less:
92 if ((int16_t) (ao_time() - ao_boost_tick) <= pyro->time_less)
95 case ao_pyro_time_greater:
96 if ((int16_t) (ao_time() - ao_boost_tick) >= pyro->time_greater)
100 case ao_pyro_ascending:
104 case ao_pyro_descending:
109 case ao_pyro_after_motor:
110 if (ao_motor_number == pyro->motor)
115 /* handled separately */
118 case ao_pyro_state_less:
119 if (ao_flight_state < pyro->state_less)
122 case ao_pyro_state_greater_or_equal:
123 if (ao_flight_state >= pyro->state_greater_or_equal)
135 #ifndef AO_FLIGHT_TEST
136 #define ao_pyro_fire_port(port, bit, pin) do { \
137 ao_gpio_set(port, bit, pin, 1); \
138 ao_delay(AO_MS_TO_TICKS(50)); \
139 ao_gpio_set(port, bit, pin, 0); \
144 ao_pyro_fire(uint8_t p)
148 case 0: ao_pyro_fire_port(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0); break;
151 case 1: ao_pyro_fire_port(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1); break;
154 case 2: ao_pyro_fire_port(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2); break;
157 case 3: ao_pyro_fire_port(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3); break;
160 case 4: ao_pyro_fire_port(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4); break;
163 case 5: ao_pyro_fire_port(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5); break;
166 case 6: ao_pyro_fire_port(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6); break;
169 case 7: ao_pyro_fire_port(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7); break;
173 ao_delay(AO_MS_TO_TICKS(50));
176 uint8_t ao_pyro_wakeup;
181 struct ao_pyro *pyro;
182 uint8_t p, any_waiting;
185 for (p = 0; p < AO_PYRO_NUM; p++) {
186 pyro = &ao_config.pyro[p];
188 /* Ignore igniters which have already fired
193 /* Ignore disabled igniters
199 /* Check pyro state to see if it should fire
201 if (!pyro->delay_done) {
202 if (!ao_pyro_ready(pyro))
205 /* If there's a delay set, then remember when
208 if (pyro->flags & ao_pyro_delay) {
209 pyro->delay_done = ao_time() + pyro->delay;
210 if (!pyro->delay_done)
211 pyro->delay_done = 1;
215 /* Check to see if we're just waiting for
216 * the delay to expire
218 if (pyro->delay_done) {
219 if ((int16_t) (ao_time() - pyro->delay_done) < 0)
225 ao_pyro_fired |= (1 << p);
230 #define NO_VALUE 0xff
232 #define AO_PYRO_NAME_LEN 3
235 #define ENABLE_HELP 1
245 char name[AO_PYRO_NAME_LEN];
246 enum ao_pyro_flag flag;
251 } ao_pyro_values[] = {
252 { "a<", ao_pyro_accel_less, offsetof(struct ao_pyro, accel_less), HELP("accel less (m/ss * 16)") },
253 { "a>", ao_pyro_accel_greater, offsetof(struct ao_pyro, accel_greater), HELP("accel greater (m/ss * 16)") },
255 { "s<", ao_pyro_speed_less, offsetof(struct ao_pyro, speed_less), HELP("speed less (m/s * 16)") },
256 { "s>", ao_pyro_speed_greater, offsetof(struct ao_pyro, speed_greater), HELP("speed greater (m/s * 16)") },
258 { "h<", ao_pyro_height_less, offsetof(struct ao_pyro, height_less), HELP("height less (m)") },
259 { "h>", ao_pyro_height_greater, offsetof(struct ao_pyro, height_greater), HELP("height greater (m)") },
262 { "o<", ao_pyro_orient_less, offsetof(struct ao_pyro, orient_less), HELP("orient less (deg)") },
263 { "o>", ao_pyro_orient_greater, offsetof(struct ao_pyro, orient_greater), HELP("orient greater (deg)") },
266 { "t<", ao_pyro_time_less, offsetof(struct ao_pyro, time_less), HELP("time less (s * 100)") },
267 { "t>", ao_pyro_time_greater, offsetof(struct ao_pyro, time_greater), HELP("time greater (s * 100)") },
269 { "f<", ao_pyro_state_less, offsetof(struct ao_pyro, state_less), HELP("state less") },
270 { "f>=",ao_pyro_state_greater_or_equal, offsetof(struct ao_pyro, state_greater_or_equal), HELP("state greater or equal") },
272 { "A", ao_pyro_ascending, NO_VALUE, HELP("ascending") },
273 { "D", ao_pyro_descending, NO_VALUE, HELP("descending") },
275 { "m", ao_pyro_after_motor, offsetof(struct ao_pyro, motor), HELP("after motor") },
277 { "d", ao_pyro_delay, offsetof(struct ao_pyro, delay), HELP("delay before firing (s * 100)") },
278 { "", ao_pyro_none, NO_VALUE, HELP(NULL) },
281 #define NUM_PYRO_VALUES (sizeof ao_pyro_values / sizeof ao_pyro_values[0])
283 #ifndef AO_FLIGHT_TEST
290 while (ao_flight_state < ao_flight_boost)
291 ao_sleep(&ao_flight_state);
294 ao_alarm(AO_MS_TO_TICKS(100));
295 ao_sleep(&ao_pyro_wakeup);
297 if (ao_flight_state >= ao_flight_landed)
299 any_waiting = ao_pyro_check();
306 __xdata struct ao_task ao_pyro_task;
310 ao_pyro_print_name(uint8_t v)
312 const char *s = ao_pyro_values[v].name;
313 printf ("%s%s", s, " " + strlen(s));
321 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
322 ao_pyro_print_name(v);
323 if (ao_pyro_values[v].offset != NO_VALUE)
327 printf ("%s\n", ao_pyro_values[v].help);
337 struct ao_pyro *pyro;
339 printf ("Pyro-count: %d\n", AO_PYRO_NUM);
340 for (p = 0; p < AO_PYRO_NUM; p++) {
341 printf ("Pyro %2d: ", p);
342 pyro = &ao_config.pyro[p];
344 printf ("<disabled>\n");
347 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
348 if (!(pyro->flags & ao_pyro_values[v].flag))
350 ao_pyro_print_name(v);
351 if (ao_pyro_values[v].offset != NO_VALUE) {
354 value = *((int16_t *) ((char *) pyro + ao_pyro_values[v].offset));
355 printf ("%6d ", value);
368 struct ao_pyro pyro_tmp;
369 char name[AO_PYRO_NAME_LEN];
376 switch (ao_cmd_lex_c) {
384 if (ao_cmd_status != ao_cmd_success)
387 if (p < 0 || AO_PYRO_NUM <= p) {
388 printf ("invalid pyro channel %d\n", p);
394 if (ao_cmd_lex_c == '\n')
397 for (c = 0; c < AO_PYRO_NAME_LEN - 1; c++) {
398 if (ao_cmd_is_white())
400 name[c] = ao_cmd_lex_c;
404 for (v = 0; ao_pyro_values[v].flag != ao_pyro_none; v++) {
405 if (!strcmp (ao_pyro_values[v].name, name))
408 if (ao_pyro_values[v].flag == ao_pyro_none) {
409 printf ("invalid pyro field %s\n", name);
410 ao_cmd_status = ao_cmd_syntax_error;
413 pyro_tmp.flags |= ao_pyro_values[v].flag;
414 if (ao_pyro_values[v].offset != NO_VALUE) {
416 if (ao_cmd_status != ao_cmd_success)
418 *((int16_t *) ((char *) &pyro_tmp + ao_pyro_values[v].offset)) = ao_cmd_lex_i;
421 _ao_config_edit_start();
422 ao_config.pyro[p] = pyro_tmp;
423 _ao_config_edit_finish();
430 if (!ao_match_word("DoIt"))
434 if (ao_cmd_lex_i < 0 || AO_PYRO_NUM <= ao_cmd_lex_i)
436 ao_pyro_fire(ao_cmd_lex_i);
440 const struct ao_cmds ao_pyro_cmds[] = {
441 { ao_pyro_manual, "P DoIt <n>\0Fire igniter" },
449 ao_enable_output(AO_PYRO_PORT_0, AO_PYRO_PIN_0, AO_PYRO_0, 0);
452 ao_enable_output(AO_PYRO_PORT_1, AO_PYRO_PIN_1, AO_PYRO_1, 0);
455 ao_enable_output(AO_PYRO_PORT_2, AO_PYRO_PIN_2, AO_PYRO_2, 0);
458 ao_enable_output(AO_PYRO_PORT_3, AO_PYRO_PIN_3, AO_PYRO_3, 0);
461 ao_enable_output(AO_PYRO_PORT_4, AO_PYRO_PIN_4, AO_PYRO_4, 0);
464 ao_enable_output(AO_PYRO_PORT_5, AO_PYRO_PIN_5, AO_PYRO_5, 0);
467 ao_enable_output(AO_PYRO_PORT_6, AO_PYRO_PIN_6, AO_PYRO_6, 0);
470 ao_enable_output(AO_PYRO_PORT_7, AO_PYRO_PIN_7, AO_PYRO_7, 0);
472 ao_cmd_register(&ao_pyro_cmds[0]);
473 ao_add_task(&ao_pyro_task, ao_pyro, "pyro");