2e332b12837e3729dae82bb1490b9ae0d4d0ecb9
[fw/altos] / src / ao_flight_nano.c
1 /*
2  * Copyright © 2011 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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 /* Main flight thread. */
21
22 __pdata enum ao_flight_state    ao_flight_state;        /* current flight state */
23 __pdata uint16_t                ao_launch_tick;         /* time of launch detect */
24
25 /*
26  * track min/max data over a long interval to detect
27  * resting
28  */
29 __pdata uint16_t                ao_interval_end;
30 __pdata int16_t                 ao_interval_min_height;
31 __pdata int16_t                 ao_interval_max_height;
32
33 __pdata uint8_t                 ao_flight_force_idle;
34
35 /* Landing is detected by getting constant readings from both pressure and accelerometer
36  * for a fairly long time (AO_INTERVAL_TICKS)
37  */
38 #define AO_INTERVAL_TICKS       AO_SEC_TO_TICKS(5)
39
40 static void
41 ao_flight_nano(void)
42 {
43         ao_sample_init();
44         ao_flight_state = ao_flight_startup;
45
46         for (;;) {
47                 /*
48                  * Process ADC samples, just looping
49                  * until the sensors are calibrated.
50                  */
51                 if (!ao_sample())
52                         continue;
53
54                 switch (ao_flight_state) {
55                 case ao_flight_startup:
56                         if (ao_flight_force_idle) {
57                                 /* Set idle mode */
58                                 ao_flight_state = ao_flight_idle;
59                         } else {
60                                 ao_flight_state = ao_flight_pad;
61                                 /* Disable packet mode in pad state */
62                                 ao_packet_slave_stop();
63
64                                 /* Turn on telemetry system */
65                                 ao_rdf_set(1);
66                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_PAD);
67                         }
68                         /* signal successful initialization by turning off the LED */
69                         ao_led_off(AO_LED_RED);
70
71                         /* wakeup threads due to state change */
72                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
73                         break;
74                 case ao_flight_pad:
75                         if (ao_height> AO_M_TO_HEIGHT(20)) {
76                                 ao_flight_state = ao_flight_drogue;
77                                 ao_launch_tick = ao_sample_tick;
78
79                                 /* start logging data */
80                                 ao_log_start();
81
82                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
83                         }
84                         break;
85                 case ao_flight_drogue:
86                         /* drogue/main to land:
87                          *
88                          * barometer: altitude stable
89                          */
90
91                         if (ao_height < ao_interval_min_height)
92                                 ao_interval_min_height = ao_height;
93                         if (ao_height > ao_interval_max_height)
94                                 ao_interval_max_height = ao_height;
95
96                         if ((int16_t) (ao_sample_tick - ao_interval_end) >= 0) {
97                                 if (ao_interval_max_height - ao_interval_min_height < AO_M_TO_HEIGHT(5))
98                                 {
99                                         ao_flight_state = ao_flight_landed;
100
101                                         /* turn off the ADC capture */
102                                         ao_timer_set_adc_interval(0);
103                                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
104                                 }
105                                 ao_interval_min_height = ao_interval_max_height = ao_height;
106                                 ao_interval_end = ao_sample_tick + AO_INTERVAL_TICKS;
107                         }
108                         break;
109                 }
110         }
111 }
112
113 static __xdata struct ao_task   flight_task;
114
115 void
116 ao_flight_nano_init(void)
117 {
118         ao_flight_state = ao_flight_startup;
119         ao_add_task(&flight_task, ao_flight_nano, "flight");
120 }