ec8691e9ad13425f014a13473442284561e28b94
[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         calib_size = ao_cmd_hex();
137         if (ao_cmd_status != ao_cmd_success)
138                 return;
139         data_size = ao_cmd_hex();
140         if (ao_cmd_status != ao_cmd_success)
141                 return;
142         if ((unsigned) calib_size != sizeof (struct ao_fake_calib)) {
143                 printf ("calib size %d larger than actual size %d\n",
144                         calib_size, sizeof (struct ao_fake_calib));
145                 ao_cmd_status = ao_cmd_syntax_error;
146                 return;
147         }
148         if (data_size != sizeof (struct ao_data)) {
149                 printf ("data size %d doesn't match actual size %d\n",
150                         data_size, sizeof (struct ao_data));
151                 ao_cmd_status = ao_cmd_syntax_error;
152                 return;
153         }
154         ao_fake_calib_get(&save_calib);
155         if (!ao_fake_calib_read())
156                 return;
157
158         ao_fake_has_next = 0;
159         ao_fake_has_cur = 0;
160         ao_fake_flight_active = 1;
161         ao_sample_init();
162 #if PACKET_HAS_SLAVE
163         ao_packet_slave_stop();
164 #endif
165 #if AO_LED_RED
166         /* Turn on the LED to indicate startup */
167         ao_led_on(AO_LED_RED);
168 #endif
169         ao_flight_state = ao_flight_startup;
170         for (;;) {
171                 if (my_state != ao_flight_state) {
172                         printf("state %d\n", ao_flight_state);
173                         my_state = ao_flight_state;
174                         flush();
175                 }
176                 if (my_pyro_fired != ao_pyro_fired) {
177                         int     pyro;
178
179                         for (pyro = 0; pyro < AO_PYRO_NUM; pyro++) {
180                                 uint16_t        bit = (1 << pyro);
181                                 if (!(my_pyro_fired & bit) && (ao_pyro_fired & bit))
182                                         printf ("fire %d\n", pyro);
183                         }
184                         my_pyro_fired = ao_pyro_fired;
185                 }
186                 while (ao_fake_has_next)
187                         ao_sleep((void *) &ao_fake_has_next);
188                 if (!ao_fake_data_read())
189                         break;
190         }
191
192         /* Wait 20 seconds to see if we enter landed state */
193         for (i = 0; i < 200; i++)
194         {
195                 if (ao_flight_state == ao_flight_landed)
196                         break;
197                 ao_delay(AO_MS_TO_TICKS(100));
198         }
199 #if AO_LED_RED
200         /* Turn on the LED to indicate startup */
201         ao_led_on(AO_LED_RED);
202 #endif
203         ao_fake_flight_active = 0;
204         ao_flight_state = ao_flight_startup;
205         ao_sample_init();
206         ao_fake_calib_set(&save_calib);
207 }
208
209 static const struct ao_cmds ao_fake_flight_cmds[] = {
210         { ao_fake_flight,       "F <calib-size> <data-size>\0Start fake flight" },
211         { 0, NULL }
212 };
213
214 void
215 ao_fake_flight_init(void)
216 {
217         ao_cmd_register(&ao_fake_flight_cmds[0]);
218 }