2 * Copyright © 2014 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.
20 #include <ao_fake_flight.h>
21 #if HAS_MS5607 || HAS_MS5611
22 #include <ao_ms5607.h>
25 uint8_t ao_fake_flight_active;
27 static uint8_t ao_fake_has_cur;
28 static volatile uint8_t ao_fake_has_next;
29 static uint8_t ao_fake_has_offset;
30 static uint16_t ao_fake_tick_offset;
31 static struct ao_data ao_fake_cur, ao_fake_next;
34 ao_fake_flight_poll(void)
36 if (ao_fake_has_next && (ao_tick_count - ao_fake_next.tick) >= 0) {
37 ao_fake_cur = ao_fake_next;
39 ao_wakeup((void *) &ao_fake_has_next);
44 ao_data_ring[ao_data_head] = ao_fake_cur;
45 ao_data_ring[ao_data_head].tick = ao_tick_count;
46 ao_data_head = ao_data_ring_next(ao_data_head);
47 ao_wakeup((void *) &ao_data_head);
51 ao_fake_data_read(void)
54 uint8_t *d = (void *) &ao_fake_next;
58 for (i = 0; i < sizeof (struct ao_data); i++)
60 if (!ao_fake_has_offset) {
61 ao_fake_tick_offset = (ao_tick_count + 1000) - ao_fake_next.tick;
62 ao_fake_next.tick = ao_tick_count;
63 ao_fake_has_offset = 1;
65 ao_fake_next.tick += ao_fake_tick_offset;
71 ao_fake_calib_get(struct ao_fake_calib *calib)
74 calib->accel_plus_g = ao_config.accel_plus_g;
75 calib->accel_minus_g = ao_config.accel_minus_g;
78 calib->accel_zero_along = ao_config.accel_zero_along;
79 calib->accel_zero_across = ao_config.accel_zero_across;
80 calib->accel_zero_through = ao_config.accel_zero_through;
82 #if HAS_MS5607 || HAS_MS5611
83 calib->ms5607_prom = ao_ms5607_prom;
88 ao_fake_calib_set(struct ao_fake_calib *calib)
91 ao_config.accel_plus_g = calib->accel_plus_g;
92 ao_config.accel_minus_g = calib->accel_minus_g;
95 ao_config.accel_zero_along = calib->accel_zero_along;
96 ao_config.accel_zero_across = calib->accel_zero_across;
97 ao_config.accel_zero_through = calib->accel_zero_through;
99 #if HAS_MS5607 || HAS_MS5611
100 ao_ms5607_prom = calib->ms5607_prom;
105 ao_fake_calib_read(void)
107 struct ao_fake_calib ao_calib;
108 uint8_t *d = (void *) &ao_calib;
111 /* Read calibration data */
112 for (i = 0; i < sizeof (struct ao_fake_calib); i++)
114 if (ao_calib.major != AO_FAKE_CALIB_MAJOR
115 #if AO_FAKE_CALIB_MINOR != 0
116 || ao_calib.minor < AO_FAKE_CALIB_MINOR
119 printf ("Calibration data major version mismatch %d.%d <= %d.%d\n",
120 ao_calib.major, ao_calib.minor, AO_FAKE_CALIB_MAJOR, AO_FAKE_CALIB_MINOR);
123 ao_fake_calib_set(&ao_calib);
130 int16_t calib_size, data_size;
131 struct ao_fake_calib save_calib;
132 uint16_t my_pyro_fired = 0;
133 enum ao_flight_state my_state = ao_flight_invalid;
137 if (ao_cmd_status != ao_cmd_success)
139 calib_size = ao_cmd_lex_i;
141 if (ao_cmd_status != ao_cmd_success)
143 data_size = ao_cmd_lex_i;
144 if ((unsigned) calib_size != sizeof (struct ao_fake_calib)) {
145 printf ("calib size %d larger than actual size %d\n",
146 calib_size, sizeof (struct ao_fake_calib));
147 ao_cmd_status = ao_cmd_syntax_error;
150 if (data_size != sizeof (struct ao_data)) {
151 printf ("data size %d doesn't match actual size %d\n",
152 data_size, sizeof (struct ao_data));
153 ao_cmd_status = ao_cmd_syntax_error;
156 ao_fake_calib_get(&save_calib);
157 if (!ao_fake_calib_read())
160 ao_fake_has_next = 0;
162 ao_fake_flight_active = 1;
165 ao_packet_slave_stop();
168 /* Turn on the LED to indicate startup */
169 ao_led_on(AO_LED_RED);
171 ao_flight_state = ao_flight_startup;
173 if (my_state != ao_flight_state) {
174 printf("state %d\n", ao_flight_state);
175 my_state = ao_flight_state;
178 if (my_pyro_fired != ao_pyro_fired) {
181 for (pyro = 0; pyro < AO_PYRO_NUM; pyro++) {
182 uint16_t bit = (1 << pyro);
183 if (!(my_pyro_fired & bit) && (ao_pyro_fired & bit))
184 printf ("fire %d\n", pyro);
186 my_pyro_fired = ao_pyro_fired;
188 while (ao_fake_has_next)
189 ao_sleep((void *) &ao_fake_has_next);
190 if (!ao_fake_data_read())
194 /* Wait 20 seconds to see if we enter landed state */
195 for (i = 0; i < 200; i++)
197 if (ao_flight_state == ao_flight_landed)
199 ao_delay(AO_MS_TO_TICKS(100));
202 /* Turn on the LED to indicate startup */
203 ao_led_on(AO_LED_RED);
205 ao_fake_flight_active = 0;
206 ao_flight_state = ao_flight_startup;
208 ao_fake_calib_set(&save_calib);
211 static const struct ao_cmds ao_fake_flight_cmds[] = {
212 { ao_fake_flight, "F <calib-size> <data-size>\0Start fake flight" },
217 ao_fake_flight_init(void)
219 ao_cmd_register(&ao_fake_flight_cmds[0]);