]> git.gag.com Git - fw/altos/blob - src/kernel/ao_fake_flight.c
ao-tools/ao-cal-freq: --verbose option has no parameter
[fw/altos] / src / kernel / ao_fake_flight.c
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_fake_flight.h>
21 #if HAS_MS5607 || HAS_MS5611
22 #include <ao_ms5607.h>
23 #endif
24
25 uint8_t                 ao_fake_flight_active;
26
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;
32
33 void
34 ao_fake_flight_poll(void)
35 {
36         if (ao_fake_has_next && (ao_tick_count - ao_fake_next.tick) >= 0) {
37                 ao_fake_cur = ao_fake_next;
38                 ao_fake_has_next = 0;
39                 ao_wakeup((void *) &ao_fake_has_next);
40                 ao_fake_has_cur = 1;
41         }
42         if (!ao_fake_has_cur)
43                 return;
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);
48 }
49
50 static uint8_t
51 ao_fake_data_read(void)
52 {
53         uint8_t i;
54         uint8_t *d = (void *) &ao_fake_next;
55
56         if (getchar() == 0)
57                 return FALSE;
58         for (i = 0; i < sizeof (struct ao_data); i++)
59                 *d++ = getchar();
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;
64         } else
65                 ao_fake_next.tick += ao_fake_tick_offset;
66         ao_fake_has_next = 1;
67         return TRUE;
68 }
69
70 static void
71 ao_fake_calib_get(struct ao_fake_calib *calib)
72 {
73 #if HAS_ACCEL
74         calib->accel_plus_g = ao_config.accel_plus_g;
75         calib->accel_minus_g = ao_config.accel_minus_g;
76 #endif
77 #if HAS_GYRO
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;
81 #endif
82 #if HAS_MS5607 || HAS_MS5611
83         calib->ms5607_prom = ao_ms5607_prom;
84 #endif
85 }
86
87 static void
88 ao_fake_calib_set(struct ao_fake_calib *calib)
89 {
90 #if HAS_ACCEL
91         ao_config.accel_plus_g = calib->accel_plus_g;
92         ao_config.accel_minus_g = calib->accel_minus_g;
93 #endif
94 #if HAS_GYRO
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;
98 #endif
99 #if HAS_MS5607 || HAS_MS5611
100         ao_ms5607_prom = calib->ms5607_prom;
101 #endif
102 }
103
104 static uint8_t
105 ao_fake_calib_read(void)
106 {
107         struct ao_fake_calib    ao_calib;
108         uint8_t                 *d = (void *) &ao_calib;
109         uint16_t                i;
110
111         /* Read calibration data */
112         for (i = 0; i < sizeof (struct ao_fake_calib); i++)
113                 *d++ = getchar();
114         if (ao_calib.major != AO_FAKE_CALIB_MAJOR
115 #if AO_FAKE_CALIB_MINOR != 0
116             || ao_calib.minor < AO_FAKE_CALIB_MINOR
117 #endif
118                 ) {
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);
121                 return FALSE;
122         }
123         ao_fake_calib_set(&ao_calib);
124         return TRUE;
125 }
126
127 static void
128 ao_fake_flight(void)
129 {
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;
134         int                     i;
135
136         ao_cmd_hex();
137         if (ao_cmd_status != ao_cmd_success)
138                 return;
139         calib_size = ao_cmd_lex_i;
140         ao_cmd_hex();
141         if (ao_cmd_status != ao_cmd_success)
142                 return;
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;
148                 return;
149         }
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;
154                 return;
155         }
156         ao_fake_calib_get(&save_calib);
157         if (!ao_fake_calib_read())
158                 return;
159
160         ao_fake_has_next = 0;
161         ao_fake_has_cur = 0;
162         ao_fake_flight_active = 1;
163         ao_sample_init();
164 #if PACKET_HAS_SLAVE
165         ao_packet_slave_stop();
166 #endif
167 #if AO_LED_RED
168         /* Turn on the LED to indicate startup */
169         ao_led_on(AO_LED_RED);
170 #endif
171         ao_flight_state = ao_flight_startup;
172         for (;;) {
173                 if (my_state != ao_flight_state) {
174                         printf("state %d\n", ao_flight_state);
175                         my_state = ao_flight_state;
176                         flush();
177                 }
178                 if (my_pyro_fired != ao_pyro_fired) {
179                         int     pyro;
180
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);
185                         }
186                         my_pyro_fired = ao_pyro_fired;
187                 }
188                 while (ao_fake_has_next)
189                         ao_sleep((void *) &ao_fake_has_next);
190                 if (!ao_fake_data_read())
191                         break;
192         }
193
194         /* Wait 20 seconds to see if we enter landed state */
195         for (i = 0; i < 200; i++)
196         {
197                 if (ao_flight_state == ao_flight_landed)
198                         break;
199                 ao_delay(AO_MS_TO_TICKS(100));
200         }
201 #if AO_LED_RED
202         /* Turn on the LED to indicate startup */
203         ao_led_on(AO_LED_RED);
204 #endif
205         ao_fake_flight_active = 0;
206         ao_flight_state = ao_flight_startup;
207         ao_sample_init();
208         ao_fake_calib_set(&save_calib);
209 }
210
211 static const struct ao_cmds ao_fake_flight_cmds[] = {
212         { ao_fake_flight,       "F <calib-size> <data-size>\0Start fake flight" },
213         { 0, NULL }
214 };
215
216 void
217 ao_fake_flight_init(void)
218 {
219         ao_cmd_register(&ao_fake_flight_cmds[0]);
220 }