altos: Remove old AO_SEND_ALL_BARO bits
[fw/altos] / src / kernel / ao_balloon.c
1 /*
2  * Copyright © 2009 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 #ifndef AO_FLIGHT_TEST
19 #include "ao.h"
20 #endif
21
22 #ifndef HAS_ACCEL
23 #error Please define HAS_ACCEL
24 #endif
25
26 #ifndef HAS_GPS
27 #error Please define HAS_GPS
28 #endif
29
30 #ifndef HAS_USB
31 #error Please define HAS_USB
32 #endif
33
34 #if HAS_SENSOR_ERRORS
35 /* Any sensor can set this to mark the flight computer as 'broken' */
36 __xdata uint8_t                 ao_sensor_errors;
37 #endif
38
39 __pdata uint16_t                ao_motor_number;        /* number of motors burned so far */
40
41 /* Main flight thread. */
42
43 __pdata enum ao_flight_state    ao_flight_state;        /* current flight state */
44
45 __pdata uint8_t                 ao_flight_force_idle;
46
47 void
48 ao_flight(void)
49 {
50         ao_sample_init();
51         ao_flight_state = ao_flight_startup;
52         for (;;) {
53
54                 /*
55                  * Process ADC samples, just looping
56                  * until the sensors are calibrated.
57                  */
58                 if (!ao_sample())
59                         continue;
60
61                 switch (ao_flight_state) {
62                 case ao_flight_startup:
63
64                         /* Check to see what mode we should go to.
65                          *  - Invalid mode if accel cal appears to be out
66                          *  - pad mode if we're upright,
67                          *  - idle mode otherwise
68                          */
69                         if (!ao_flight_force_idle)
70                         {
71                                 /* Set pad mode - we can fly! */
72                                 ao_flight_state = ao_flight_pad;
73 #if HAS_USB
74                                 /* Disable the USB controller in flight mode
75                                  * to save power
76                                  */
77                                 if (!ao_usb_running)
78                                         ao_usb_disable();
79 #endif
80
81                                 /* Disable packet mode in pad state */
82                                 ao_packet_slave_stop();
83
84                                 /* Turn on telemetry system */
85                                 ao_rdf_set(1);
86                                 ao_telemetry_set_interval(AO_TELEMETRY_INTERVAL_BALLOON);
87
88                                 /* signal successful initialization by turning off the LED */
89                                 ao_led_off(AO_LED_RED);
90                         } else {
91                                 /* Set idle mode */
92                                 ao_flight_state = ao_flight_idle;
93  
94                                 /* signal successful initialization by turning off the LED */
95                                 ao_led_off(AO_LED_RED);
96                         }
97                         /* wakeup threads due to state change */
98                         ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
99
100                         break;
101                 case ao_flight_pad:
102
103                         /* pad to coast:
104                          *
105                          * barometer: > 20m vertical motion
106                          */
107                         if (ao_height > AO_M_TO_HEIGHT(20))
108                         {
109                                 ao_flight_state = ao_flight_drogue;
110
111                                 /* start logging data */
112                                 ao_log_start();
113
114 #if HAS_GPS
115                                 /* Record current GPS position by waking up GPS log tasks */
116                                 ao_gps_new = AO_GPS_NEW_DATA | AO_GPS_NEW_TRACKING;
117                                 ao_wakeup(&ao_gps_new);
118 #endif
119
120                                 ao_wakeup(DATA_TO_XDATA(&ao_flight_state));
121                         }
122                         break;
123                 default:
124                         break;
125                 }
126         }
127 }
128
129 static __xdata struct ao_task   flight_task;
130
131 void
132 ao_flight_init(void)
133 {
134         ao_flight_state = ao_flight_startup;
135         ao_add_task(&flight_task, ao_flight, "flight");
136 }