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